Skip to content

Commit

Permalink
Merge pull request #7 from asmaloney/add-example
Browse files Browse the repository at this point in the history
Add example to README
  • Loading branch information
tliron authored Aug 20, 2021
2 parents b5dd14d + 60f2145 commit f4d3bef
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,69 @@ References
----------

* [go-lsp](https://github.com/sourcegraph/go-lsp) is another implementation with reduced coverage of the protocol

Minimal Example
--

```go
package main

import (
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
"github.com/tliron/glsp/server"
"github.com/tliron/kutil/logging"

// Must include a backend implementation. See kutil's logging/ for other options.
_ "github.com/tliron/kutil/logging/simple"
)

const lsName = "my language"

var version string = "0.0.1"
var handler protocol.Handler

func main() {
// Must configure logging before using server. Note that the import of a backend
// is required as well.
// See kutil's logging.Backend for Configure() param details.
logging.Configure(1, nil)

handler = protocol.Handler{
Initialize: initialize,
Initialized: initialized,
Shutdown: shutdown,
SetTrace: setTrace,
}

server := server.NewServer(&handler, lsName, false)

server.RunStdio()
}

func initialize(context *glsp.Context, params *protocol.InitializeParams) (interface{}, error) {
capabilities := handler.CreateServerCapabilities()

return protocol.InitializeResult{
Capabilities: capabilities,
ServerInfo: &protocol.InitializeResultServerInfo{
Name: lsName,
Version: &version,
},
}, nil
}

func initialized(context *glsp.Context, params *protocol.InitializedParams) error {
return nil
}

func shutdown(context *glsp.Context) error {
protocol.SetTraceValue(protocol.TraceValueOff)
return nil
}

func setTrace(context *glsp.Context, params *protocol.SetTraceParams) error {
protocol.SetTraceValue(params.Value)
return nil
}
```

0 comments on commit f4d3bef

Please sign in to comment.