diff --git a/commands/serve_command.go b/commands/serve_command.go index bd6efa78..b7c66b6a 100644 --- a/commands/serve_command.go +++ b/commands/serve_command.go @@ -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) diff --git a/internal/context/context.go b/internal/context/context.go index 551ad9db..7a69c048 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -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 { @@ -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 +} diff --git a/langserver/handlers/handlers_test.go b/langserver/handlers/handlers_test.go index e8b78605..e9089103 100644 --- a/langserver/handlers/handlers_test.go +++ b/langserver/handlers/handlers_test.go @@ -51,7 +51,7 @@ func initializeResponse(t *testing.T, commandPrefix string) string { } }, "serverInfo": { - "name": "" + "name": "terraform-ls" } } }`, string(jsonArray)) diff --git a/langserver/handlers/initialize.go b/langserver/handlers/initialize.go index 561a3230..b6ff4139 100644 --- a/langserver/handlers/initialize.go +++ b/langserver/handlers/initialize.go @@ -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." + diff --git a/langserver/handlers/service.go b/langserver/handlers/service.go index 3617612b..798924a3 100644 --- a/langserver/handlers/service.go +++ b/langserver/handlers/service.go @@ -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) {