Skip to content

Add support for workspace #1238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/ls/completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,7 @@ func assertIncludesItem(t *testing.T, actual *lsproto.CompletionList, expected *

func createLanguageService(ctx context.Context, fileName string, files map[string]any) (*ls.LanguageService, func()) {
projectService, _ := projecttestutil.Setup(files, nil)
projectService.OpenFile(fileName, files[fileName].(string), core.GetScriptKindFromFileName(fileName), "")
projectService.OpenFile(fileName, files[fileName].(string), core.GetScriptKindFromFileName(fileName))
project := projectService.Projects()[0]
return project.GetLanguageServiceForRequest(ctx)
}
Expand Down
37 changes: 35 additions & 2 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type Server struct {

initializeParams *lsproto.InitializeParams
positionEncoding lsproto.PositionEncodingKind
workspace *project.Workspace

watchEnabled bool
watcherID atomic.Uint32
Expand Down Expand Up @@ -471,6 +472,8 @@ func (s *Server) handleRequestOrNotification(ctx context.Context, req *lsproto.R
return s.handleDidClose(ctx, req)
case *lsproto.DidChangeWatchedFilesParams:
return s.handleDidChangeWatchedFiles(ctx, req)
case *lsproto.DidChangeWorkspaceFoldersParams:
return s.handleDidChangeWorkspaceFolders(ctx, req)
case *lsproto.DocumentDiagnosticParams:
return s.handleDocumentDiagnostic(ctx, req)
case *lsproto.HoverParams:
Expand Down Expand Up @@ -566,6 +569,14 @@ func (s *Server) handleInitialize(req *lsproto.RequestMessage) {
FirstTriggerCharacter: "{",
MoreTriggerCharacter: &[]string{"}", ";", "\n"},
},
Workspace: &lsproto.WorkspaceOptions{
WorkspaceFolders: &lsproto.WorkspaceFoldersServerCapabilities{
Supported: ptrTo(true),
ChangeNotifications: ptrTo(lsproto.StringOrBoolean{
Boolean: ptrTo(true),
}),
},
},
},
})
}
Expand All @@ -591,12 +602,20 @@ func (s *Server) handleInitialized(ctx context.Context, req *lsproto.RequestMess
s.projectService.SetCompilerOptionsForInferredProjects(s.compilerOptionsForInferredProjects)
}

if s.initializeParams.RootUri.Value != "" {
s.projectService.Workspace.SetRoot(ls.DocumentURIToFileName(s.initializeParams.RootUri.Value))
}
if s.initializeParams.WorkspaceFolders != nil {
for _, folder := range s.initializeParams.WorkspaceFolders.Value {
s.projectService.Workspace.AddFolder(ls.DocumentURIToFileName(lsproto.DocumentUri(folder.Uri)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why we need to handle this kind of thing? Typically this stuff is only ever used if we plan to explicitly handle multi-root workspaces, but I don't think we have a reason to do that, since we're great at just accepting whatever gets opened anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was used for creating better inferred projects - thats when projectRootPath was introduced
It is also used to not lookup tsconfig beyond the project root locations. I added this because with multi project thing the search locations increase and wanted to make sure i am not going beyond the point it needs to.

}
}
return nil
}

func (s *Server) handleDidOpen(ctx context.Context, req *lsproto.RequestMessage) error {
params := req.Params.(*lsproto.DidOpenTextDocumentParams)
s.projectService.OpenFile(ls.DocumentURIToFileName(params.TextDocument.Uri), params.TextDocument.Text, ls.LanguageKindToScriptKind(params.TextDocument.LanguageId), "")
s.projectService.OpenFile(ls.DocumentURIToFileName(params.TextDocument.Uri), params.TextDocument.Text, ls.LanguageKindToScriptKind(params.TextDocument.LanguageId))
return nil
}

Expand All @@ -622,6 +641,19 @@ func (s *Server) handleDidChangeWatchedFiles(ctx context.Context, req *lsproto.R
return s.projectService.OnWatchedFilesChanged(ctx, params.Changes)
}

func (s *Server) handleDidChangeWorkspaceFolders(ctx context.Context, req *lsproto.RequestMessage) error {
params := req.Params.(*lsproto.DidChangeWorkspaceFoldersParams)
if params.Event != nil {
for _, folder := range params.Event.Added {
s.workspace.AddFolder(ls.DocumentURIToFileName(lsproto.DocumentUri(folder.Uri)))
}
for _, folder := range params.Event.Removed {
s.workspace.RemoveFolder(ls.DocumentURIToFileName(lsproto.DocumentUri(folder.Uri)))
}
}
return nil
}

func (s *Server) handleDocumentDiagnostic(ctx context.Context, req *lsproto.RequestMessage) error {
params := req.Params.(*lsproto.DocumentDiagnosticParams)
project := s.projectService.EnsureDefaultProjectForURI(params.TextDocument.Uri)
Expand Down Expand Up @@ -827,7 +859,8 @@ func isBlockingMethod(method lsproto.Method) bool {
lsproto.MethodTextDocumentDidChange,
lsproto.MethodTextDocumentDidSave,
lsproto.MethodTextDocumentDidClose,
lsproto.MethodWorkspaceDidChangeWatchedFiles:
lsproto.MethodWorkspaceDidChangeWatchedFiles,
lsproto.MethodWorkspaceDidChangeWorkspaceFolders:
return true
}
return false
Expand Down
44 changes: 22 additions & 22 deletions internal/project/ata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAta(t *testing.T) {
TypesRegistry: []string{"config"},
},
})
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
assert.Equal(t, len(service.Projects()), 1)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
assert.Equal(t, p.Kind(), project.KindConfigured)
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestAta(t *testing.T) {
},
},
})
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
assert.Equal(t, len(service.Projects()), 1)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
assert.Equal(t, p.Kind(), project.KindConfigured)
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestAta(t *testing.T) {
},
},
})
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
assert.Equal(t, len(service.Projects()), 1)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
assert.Equal(t, p.Kind(), project.KindInferred)
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestAta(t *testing.T) {
TypesRegistry: []string{"jquery"},
},
})
service.OpenFile("/user/username/projects/project/jquery.js", files["/user/username/projects/project/jquery.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/jquery.js", files["/user/username/projects/project/jquery.js"].(string), core.ScriptKindJS)
assert.Equal(t, len(service.Projects()), 1)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/jquery.js")
assert.Equal(t, p.Kind(), project.KindConfigured)
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestAta(t *testing.T) {
TypesRegistry: []string{"node"},
},
})
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
assert.Equal(t, len(service.Projects()), 1)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
assert.Equal(t, p.Kind(), project.KindConfigured)
Expand Down Expand Up @@ -210,8 +210,8 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project1/app.js", files["/user/username/projects/project1/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project2/app.js", files["/user/username/projects/project2/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project1/app.js", files["/user/username/projects/project1/app.js"].(string), core.ScriptKindJS)
service.OpenFile("/user/username/projects/project2/app.js", files["/user/username/projects/project2/app.js"].(string), core.ScriptKindJS)
_, p1 := service.EnsureDefaultProjectForFile("/user/username/projects/project1/app.js")
_, p2 := service.EnsureDefaultProjectForFile("/user/username/projects/project2/app.js")
var installStatuses []project.TypingsInstallerStatus
Expand Down Expand Up @@ -271,8 +271,8 @@ func TestAta(t *testing.T) {
}
}

service.OpenFile("/user/username/projects/project1/app.js", files["/user/username/projects/project1/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project2/app.js", files["/user/username/projects/project2/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project1/app.js", files["/user/username/projects/project1/app.js"].(string), core.ScriptKindJS)
service.OpenFile("/user/username/projects/project2/app.js", files["/user/username/projects/project2/app.js"].(string), core.ScriptKindJS)
_, p1 := service.EnsureDefaultProjectForFile("/user/username/projects/project1/app.js")
_, p2 := service.EnsureDefaultProjectForFile("/user/username/projects/project2/app.js")
// Order is determinate since second install will run only after completing first one
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -352,7 +352,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand All @@ -463,7 +463,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand All @@ -522,7 +522,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -602,7 +602,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -651,7 +651,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -697,7 +697,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -745,7 +745,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down Expand Up @@ -791,7 +791,7 @@ func TestAta(t *testing.T) {
},
})

service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS, "")
service.OpenFile("/user/username/projects/project/app.js", files["/user/username/projects/project/app.js"].(string), core.ScriptKindJS)
_, p := service.EnsureDefaultProjectForFile("/user/username/projects/project/app.js")
// Order is determinate since second install will run only after completing first one
status := <-host.ServiceOptions.InstallStatus
Expand Down
Loading