Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose server version via LSP #318

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions commands/serve_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func (c *ServeCommand) Run(args []string) int {

logger.Printf("Starting terraform-ls %s", c.Version)

ctx = lsctx.WithLanguageServerVersion(ctx, c.Version)

srv := langserver.NewLangServer(ctx, handlers.NewSession)
srv.SetLogger(logger)

Expand Down
13 changes: 13 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
ctxRootDir = &contextKey{"root directory"}
ctxCommandPrefix = &contextKey{"command prefix"}
ctxDiags = &contextKey{"diagnostics"}
ctxLsVersion = &contextKey{"language server version"}
)

func missingContextErr(ctxKey *contextKey) *MissingContextErr {
Expand Down Expand Up @@ -236,3 +237,15 @@ func Diagnostics(ctx context.Context) (*diagnostics.Notifier, error) {

return diags, nil
}

func WithLanguageServerVersion(ctx context.Context, version string) context.Context {
return context.WithValue(ctx, ctxLsVersion, version)
}

func LanguageServerVersion(ctx context.Context) (string, bool) {
version, ok := ctx.Value(ctxLsVersion).(string)
if !ok {
return "", false
}
return version, true
}
2 changes: 1 addition & 1 deletion langserver/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func initializeResponse(t *testing.T, commandPrefix string) string {
}
},
"serverInfo": {
"name": ""
"name": "terraform-ls"
}
}
}`, string(jsonArray))
Expand Down
6 changes: 6 additions & 0 deletions langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (lh *logHandler) Initialize(ctx context.Context, params lsp.InitializeParam
},
}

serverCaps.ServerInfo.Name = "terraform-ls"
version, ok := lsctx.LanguageServerVersion(ctx)
if ok {
serverCaps.ServerInfo.Version = version
}

fh := ilsp.FileHandlerFromDirURI(params.RootURI)
if fh.URI() == "" || !fh.IsDir() {
return serverCaps, fmt.Errorf("Editing a single file is not yet supported." +
Expand Down
5 changes: 5 additions & 0 deletions langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
ctx = lsctx.WithRootModuleManager(ctx, svc.modMgr)
ctx = lsctx.WithRootModuleLoader(ctx, rmLoader)

version, ok := lsctx.LanguageServerVersion(svc.srvCtx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the cycle here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the server starts with its own empty context, for better or worse:
https://github.com/creachadair/jrpc2/blob/3fb8081ec1bde4adba1b3a57dff458328a8ea769/server.go#L304

You could argue both sides - it's good for isolation - you don't get to inherit things by accident, but also it can make it more verbose 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay!

if ok {
ctx = lsctx.WithLanguageServerVersion(ctx, version)
}

return handle(ctx, req, lh.Initialize)
},
"initialized": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
Expand Down