Skip to content

Commit

Permalink
context: Introduce LanguageId
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Sep 18, 2023
1 parent 3a6b7b7 commit 88c0331
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
ctxProgressToken = &contextKey{"progress token"}
ctxExperimentalFeatures = &contextKey{"experimental features"}
ctxRPCContext = &contextKey{"rpc context"}
ctxLanguageId = &contextKey{"language ID"}
)

func missingContextErr(ctxKey *contextKey) *MissingContextErr {
Expand Down Expand Up @@ -186,3 +187,15 @@ func WithRPCContext(ctx context.Context, rpcc RPCContextData) context.Context {
func RPCContext(ctx context.Context) RPCContextData {
return ctx.Value(ctxRPCContext).(RPCContextData)
}

func WithLanguageId(ctx context.Context, languageId string) context.Context {
return context.WithValue(ctx, ctxLanguageId, languageId)
}

func IsLanguageId(ctx context.Context, expectedLangId string) bool {
langId, ok := ctx.Value(ctxLanguageId).(string)
if !ok {
return false
}
return langId == expectedLangId
}
3 changes: 3 additions & 0 deletions internal/langserver/handlers/did_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package handlers
import (
"context"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
Expand All @@ -28,6 +29,8 @@ func (svc *service) TextDocumentDidChange(ctx context.Context, params lsp.DidCha
return err
}

ctx = lsctx.WithLanguageId(ctx, doc.LanguageID)

newVersion := int(p.TextDocument.Version)

// Versions don't have to be consecutive, but they must be increasing
Expand Down
3 changes: 3 additions & 0 deletions internal/langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/creachadair/jrpc2"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/state"
Expand Down Expand Up @@ -37,6 +38,8 @@ func (svc *service) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpenT
return err
}

ctx = lsctx.WithLanguageId(ctx, params.TextDocument.LanguageID)

mod, err := svc.modStore.ModuleByPath(dh.Dir.Path())
if err != nil {
if state.IsModuleNotFound(err) {
Expand Down

0 comments on commit 88c0331

Please sign in to comment.