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 4, 2022
1 parent b13641c commit f7f3e7b
Show file tree
Hide file tree
Showing 3 changed files with 31 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
16 changes: 16 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,12 @@ func (s *ModuleStore) SetTerraformVersionState(path string, state op.OpState) er
return err
}

txn.Defer(func() {

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

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

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

txn.Commit()
return nil
}
Expand Down

0 comments on commit f7f3e7b

Please sign in to comment.