Skip to content

Commit ea783e0

Browse files
authored
refactor(lsp): apply workspace change as request (#32)
1 parent d551861 commit ea783e0

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

internal/lsp/lsproto/lsp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
type DocumentUri string // !!!
1717

1818
func (uri DocumentUri) FileName() string {
19+
if suffix, ok := strings.CutPrefix(string(uri), "deno:/asset/"); ok {
20+
return "asset:///" + suffix
21+
}
1922
if bundled.IsBundled(string(uri)) {
2023
return string(uri)
2124
}

internal/lsp/lsproto/lsp_generated.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/lsp/server.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,23 +1345,23 @@ func (o *DenoCrossProjectOrchestrator) GetProjectsLoadingProjectTree(ctx context
13451345
}
13461346

13471347
func (s *Server) handleDenoRequest(ctx context.Context, params *lsproto.DenoRequestParams, _ *lsproto.RequestMessage) (any, error) {
1348-
if params.WorkspaceChange != nil {
1349-
if params.WorkspaceChange.NewConfiguration != nil {
1348+
if params.ApplyWorkspaceChange != nil {
1349+
if params.ApplyWorkspaceChange.NewConfiguration != nil {
13501350
// Clear document cache on new configuration
13511351
s.deno.documentCacheMu.Lock()
13521352
s.deno.documentCache = make(map[lsproto.DocumentUri]*lsproto.DenoDocumentData)
13531353
s.deno.documentCacheMu.Unlock()
13541354

1355-
byCompilerOptionsKey := make(map[string]*DenoProgramEntry, len(params.WorkspaceChange.NewConfiguration.ByCompilerOptionsKey))
1356-
byNotebookUri := make(map[string]*DenoProgramEntry, len(params.WorkspaceChange.NewConfiguration.ByNotebookUri))
1357-
for key, projectConfig := range params.WorkspaceChange.NewConfiguration.ByCompilerOptionsKey {
1355+
byCompilerOptionsKey := make(map[string]*DenoProgramEntry, len(params.ApplyWorkspaceChange.NewConfiguration.ByCompilerOptionsKey))
1356+
byNotebookUri := make(map[string]*DenoProgramEntry, len(params.ApplyWorkspaceChange.NewConfiguration.ByNotebookUri))
1357+
for key, projectConfig := range params.ApplyWorkspaceChange.NewConfiguration.ByCompilerOptionsKey {
13581358
entry, err := s.createDenoProgramEntry(ctx, key, nil, projectConfig)
13591359
if err != nil {
13601360
return nil, fmt.Errorf("Failed to create program entry for key %s: %w", key, err)
13611361
}
13621362
byCompilerOptionsKey[key] = entry
13631363
}
1364-
for notebookUri, projectConfig := range params.WorkspaceChange.NewConfiguration.ByNotebookUri {
1364+
for notebookUri, projectConfig := range params.ApplyWorkspaceChange.NewConfiguration.ByNotebookUri {
13651365
entry, err := s.createDenoProgramEntry(ctx, projectConfig.CompilerOptionsKey, &notebookUri, projectConfig)
13661366
if err != nil {
13671367
return nil, fmt.Errorf("Failed to create program entry for notebook %s: %w", notebookUri, err)
@@ -1374,9 +1374,7 @@ func (s *Server) handleDenoRequest(ctx context.Context, params *lsproto.DenoRequ
13741374
}
13751375
} else {
13761376
// Handle file changes by updating affected programs
1377-
// TODO(nayeemrmn): ParsedOptions::FileNames should be updated here
1378-
// depending on change kinds and in some other cases.
1379-
for _, fileChange := range params.WorkspaceChange.FileChanges {
1377+
for _, fileChange := range params.ApplyWorkspaceChange.FileChanges {
13801378
// Clear document cache entry for changed file
13811379
s.deno.documentCacheMu.Lock()
13821380
normalizedUri := lsconv.FileNameToDocumentURI(tspath.NormalizePath(fileChange.Uri.FileName()))
@@ -1414,9 +1412,22 @@ func (s *Server) handleDenoRequest(ctx context.Context, params *lsproto.DenoRequ
14141412
}
14151413
}
14161414
}
1417-
}
1418-
if params.Request.LanguageServiceMethod != nil {
1419-
req := params.Request.LanguageServiceMethod
1415+
workspaceInfo := lsproto.DenoWorkspaceInfo{
1416+
CompilerOptionsKeysForAssets: make(map[lsproto.DocumentUri]string),
1417+
}
1418+
for compilerOptionsKey, pe := range s.deno.programEntries.byCompilerOptionsKey {
1419+
for _, file := range pe.program.SourceFiles() {
1420+
if strings.HasPrefix(file.FileName(), "asset:///") {
1421+
uri := lsconv.FileNameToDocumentURI(file.FileName())
1422+
if _, ok := workspaceInfo.CompilerOptionsKeysForAssets[uri]; !ok {
1423+
workspaceInfo.CompilerOptionsKeysForAssets[uri] = compilerOptionsKey
1424+
}
1425+
}
1426+
}
1427+
}
1428+
return workspaceInfo, nil
1429+
} else if params.LanguageServiceMethod != nil {
1430+
req := params.LanguageServiceMethod
14201431
pe, languageService, err := s.getDenoLanguageService(req.CompilerOptionsKey, req.NotebookUri)
14211432
if err != nil {
14221433
return nil, err
@@ -1531,8 +1542,8 @@ func (s *Server) handleDenoRequest(ctx context.Context, params *lsproto.DenoRequ
15311542
default:
15321543
return nil, fmt.Errorf("Unsupported method: %s", req.Name)
15331544
}
1534-
} else if params.Request.GetAmbientModules != nil {
1535-
req := params.Request.GetAmbientModules
1545+
} else if params.GetAmbientModules != nil {
1546+
req := params.GetAmbientModules
15361547
pe, _, err := s.getDenoLanguageService(req.CompilerOptionsKey, req.NotebookUri)
15371548
if err != nil {
15381549
return nil, err
@@ -1541,8 +1552,8 @@ func (s *Server) handleDenoRequest(ctx context.Context, params *lsproto.DenoRequ
15411552
defer done()
15421553
symbols := checker.GetAmbientModules(nil)
15431554
return core.Map(symbols, func(s *ast.Symbol) string { return s.Name }), nil
1544-
} else if params.Request.WorkspaceSymbol != nil {
1545-
req := params.Request.WorkspaceSymbol
1555+
} else if params.WorkspaceSymbol != nil {
1556+
req := params.WorkspaceSymbol
15461557
var programs []*compiler.Program
15471558
var firstHost *DenoLanguageServiceHost
15481559
for _, pe := range s.deno.programEntries.byCompilerOptionsKey {

0 commit comments

Comments
 (0)