Skip to content

Commit

Permalink
Enable Semantic Token Refresh
Browse files Browse the repository at this point in the history
Add ModuleChangeHooks to trigger semantic token refresh when specified parts of the schema are discovered after initialization.

Fixes #344
  • Loading branch information
jpogran committed Feb 8, 2022
1 parent b13641c commit 46b149d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/langserver/handlers/hooks_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package handlers
import (
"context"
"fmt"
"log"

"github.com/creachadair/jrpc2"
"github.com/hashicorp/terraform-ls/internal/langserver/diagnostics"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
"github.com/hashicorp/terraform-ls/internal/state"
Expand Down Expand Up @@ -169,3 +171,15 @@ func refreshCodeLens(ctx context.Context, clientRequester session.ClientCaller)
}
}
}

func refreshSemanticTokens(ctx context.Context, svrCtx context.Context, logger *log.Logger) state.ModuleChangeHook {
return func(_, newMod *state.Module) {
logger.Printf("Request semantic token refresh for %s", newMod.Path)

jrpcsvc := jrpc2.ServerFromContext(ctx)
_, err := jrpcsvc.Callback(svrCtx, "workspace/semanticTokens/refresh", nil)
if err != nil {
logger.Printf("Error refreshing %s: %s", newMod.Path, err)
}
}
}
1 change: 1 addition & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ func (svc *service) configureSessionDependencies(ctx context.Context, cfgOpts *s
svc.stateStore.Modules.ChangeHooks = state.ModuleChangeHooks{
updateDiagnostics(svc.sessCtx, svc.diagsNotifier),
sendModuleTelemetry(svc.sessCtx, svc.stateStore, svc.telemetry),
refreshSemanticTokens(ctx, svc.srvCtx, svc.logger),
}

cc, err := ilsp.ClientCapabilities(ctx)
Expand Down
13 changes: 13 additions & 0 deletions internal/state/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ func (s *ModuleStore) UpdateModManifest(path string, manifest *datadir.ModuleMan
return err
}

txn.Defer(func() {
s.logger.Printf("Queuing refresh for %s", path)
go s.ChangeHooks.notifyModuleChange(nil, mod)
})

txn.Commit()
return nil
}
Expand All @@ -475,6 +480,10 @@ func (s *ModuleStore) SetTerraformVersionState(path string, state op.OpState) er
return err
}

txn.Defer(func() {
go s.ChangeHooks.notifyModuleChange(nil, mod)
})

txn.Commit()
return nil
}
Expand Down Expand Up @@ -556,6 +565,10 @@ func (s *ModuleStore) UpdateTerraformVersion(modPath string, tfVer *version.Vers
return err
}

txn.Defer(func() {
go s.ChangeHooks.notifyModuleChange(nil, mod)
})

txn.Commit()
return nil
}
Expand Down

0 comments on commit 46b149d

Please sign in to comment.