This is an example server component for the https://github.com/mhewett/sourcegraph-coding-examples extension.
It is architected to be a general language server but currently only works on examples generated by godoc installed on the local server.
- Install Go on your local machine (the Go package includes
godoc). - Install Node.js on your local machine
- This server is currently built using Node v8.11.3
npm run build- Runs
npm installandtscto compile the code
- Runs
npm run serverornode src/index.js- Runs
tscto compile the code and then starts the server - Defaults to port 8844
- Use
-port nnnnto change the port:npm run server -- -port 9999node src/index.js -port 9999
- Use
-debugto print debug statements
- Runs
General form: http://localhost:8844/<language>/<symbol>?package=<package>
parameters
- language the language, such as Go, Java, or Python (required)
- symbol the class, type, function, or method, such as
MultiReaderorConcurrentHashMap(required)- For a Java method, use the method name as the symbol and include the Class in the package, e.g.
java.lang.Math
- For a Java method, use the method name as the symbol and include the Class in the package, e.g.
- package the package the symbol is in (required except for base language symbols such as
switch) - format the desired return format (optional) defaults to
json, other options arehtmlandtext
Note: The current implementation is a proof of concept and only supports Go.
http://localhost:8844/go/MultiReader?package=io
http://localhost:8844/go/ResponseWriter?package=net/http
http://localhost:8844/java/ConcurrentHashMap?package=java.util.concurrent
You can optionally specify a language version with languageVersion.
This will refer to the language or library version, as appropriate for the language and symbol requested.
http://localhost:8844/go/io/MultiReader?languageVersion=1
http://localhost:8844/go/io/MultiReader?languageVersion=1.09
http://localhost:8844/java/java/util/concurrent/ConcurrentHashMap?languageVersion=7
http://localhost:8844/java/java/util/concurrent/ConcurrentHashMap?languageVersion=latest
http://localhost:8844/java/switch?languageVersion=latest
http://localhost:8844/lisp/signum?languageVersion=cltl2
If multiple versions of the desired language are not available, the languageVersion option is ignored.
If the server can determine that the class or function is not available in the specified version
(e.g. ConcurrentHashMap in Java prior to v1.5),
the server will respond with a 404 error.
http://localhost:8844/go/CopyN?package=io
{
"example": "Example:\n r := strings.NewReader(\"some io.Reader stream to be read\")\n\n if _, err := io.CopyN(os.Stdout, r, 5); err != nil {\n log.Fatal(err)\n }\n\n // Output:\n // some\n\n\n",
"doc": "func CopyN(dst Writer, src Reader, n int64) (written int64, err error)\n CopyN copies n bytes (or until an error) from src to dst. It returns the\n number of bytes copied and the earliest error encountered while copying.\n On return, written == n if and only if err == nil.\n\n If dst implements the ReaderFrom interface, the copy is implemented\n using it.\n\n "
}
http://localhost:8844/go/CopyFoo?package=io
{
"error": "io.CopyFoo not found"
}
- 200 successful response
- 404 unsupported language or unknown class, function, or method. Returns an object with an 'error' field whose value is an error message (string).
- 500 The expected example server is not available, for example
godocfor Go examples. Returns an object with an 'error' field containing an explanation.
Normally, the output is in JSON format and has the fields example and doc.
Use the format=text or format=html options to get raw text or HTML output, respectively.
In the non-JSON formats the server returns only the example text.
The Content-Type header is set appropriately for the response format.
http://localhost:8844/go/MultiReader?package=io&format=html
http://localhost:8844/go/MultiReader?package=io&format=json (default)
http://localhost:8844/go/MultiReader?package=io&format=text