Skip to content

Commit

Permalink
Expose server version via LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 23, 2020
1 parent 954e33f commit f2a5e49
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
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)
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

0 comments on commit f2a5e49

Please sign in to comment.