From 142d1b96b40e2e8424faf78b367a5fc7f7952d71 Mon Sep 17 00:00:00 2001 From: Joe Kralicky Date: Sun, 10 Sep 2023 17:36:55 -0400 Subject: [PATCH] Add command to reindex all workspaces without restarting the language server --- editors/vscode/client/src/extension.ts | 19 +++++++++++++++---- editors/vscode/package.json | 4 ++++ pkg/lsp/server.go | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/editors/vscode/client/src/extension.ts b/editors/vscode/client/src/extension.ts index 6ad81a6..c4ce680 100644 --- a/editors/vscode/client/src/extension.ts +++ b/editors/vscode/client/src/extension.ts @@ -22,17 +22,28 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand("protols.restart", async () => { - try { - await client.restart() - } catch (e) { - // if it's already stopped, restart will throw an error. + if (!client.isRunning()) { await client.start() + } else { + await client.restart() + } + }), + vscode.commands.registerCommand("protols.reindex-workspaces", async () => { + if (!client.isRunning()) { + return } + await client.sendRequest("protols/reindex-workspaces", {}) }), vscode.commands.registerCommand("protols.stop", async () => { + if (!client.isRunning()) { + return + } await client.stop() }), vscode.commands.registerTextEditorCommand("protols.ast", async (editor) => { + if (!client.isRunning()) { + return + } await astViewer.openDocument(editor) }), ) diff --git a/editors/vscode/package.json b/editors/vscode/package.json index b4ba153..213194f 100644 --- a/editors/vscode/package.json +++ b/editors/vscode/package.json @@ -40,6 +40,10 @@ "command": "protols.stop", "title": "Protols: Stop Language Server" }, + { + "command": "protols.reindex-workspaces", + "title": "Protols: Reindex Workspaces" + }, { "command": "protols.ast", "title": "Protols: Show Document AST", diff --git a/pkg/lsp/server.go b/pkg/lsp/server.go index 7f3b46a..7f7b094 100644 --- a/pkg/lsp/server.go +++ b/pkg/lsp/server.go @@ -6,6 +6,7 @@ import ( "fmt" "net/url" "path/filepath" + "runtime" "sync" "github.com/samber/lo" @@ -609,6 +610,22 @@ func (s *Server) NonstandardRequest(ctx context.Context, method string, params i return nil, err } return DumpAST(parseRes.AST(), parseRes), nil + case "protols/reindex-workspaces": + s.cachesMu.Lock() + allWorkspaces := []protocol.WorkspaceFolder{} + for _, c := range s.caches { + allWorkspaces = append(allWorkspaces, c.workspace) + } + s.lg.Info("reindexing workspaces") + clear(s.caches) + runtime.GC() + for _, folder := range allWorkspaces { + path := span.URIFromURI(folder.URI).Filename() + c := NewCache(folder, s.lg.Named("cache."+folder.Name)) + s.caches[path] = c + } + s.cachesMu.Unlock() + return nil, nil default: return nil, fmt.Errorf("%w: unknown nonstandard request %q", jsonrpc2.ErrMethodNotFound, method) }