Skip to content

Commit

Permalink
Add command to reindex all workspaces without restarting the language…
Browse files Browse the repository at this point in the history
… server
  • Loading branch information
kralicky committed Sep 10, 2023
1 parent ba27782 commit 142d1b9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
19 changes: 15 additions & 4 deletions editors/vscode/client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}),
)
Expand Down
4 changes: 4 additions & 0 deletions editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions pkg/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"path/filepath"
"runtime"
"sync"

"github.com/samber/lo"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 142d1b9

Please sign in to comment.