Skip to content

Commit 0ee1312

Browse files
committed
Use diagnostic refresh instead of publish on watch changes
1 parent 5785405 commit 0ee1312

File tree

5 files changed

+35
-48
lines changed

5 files changed

+35
-48
lines changed

internal/api/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ func (api *API) Client() project.Client {
124124
return nil
125125
}
126126

127+
// IsWatchEnabled implements ProjectHost.
128+
func (api *API) IsWatchEnabled() bool {
129+
return false
130+
}
131+
127132
func (api *API) HandleRequest(id int, method string, payload []byte) ([]byte, error) {
128133
params, err := unmarshalPayload(method, payload)
129134
if err != nil {

internal/lsp/server.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,14 @@ func (s *Server) UnwatchFiles(handle project.WatcherHandle) error {
151151
return fmt.Errorf("no file watcher exists with ID %s", handle)
152152
}
153153

154-
// PublishDiagnostics implements project.Client.
155-
func (s *Server) PublishDiagnostics(params *lsproto.PublishDiagnosticsParams) error {
156-
return s.sendNotification(lsproto.MethodTextDocumentPublishDiagnostics, params)
154+
// RefreshDiagnostics implements project.Client.
155+
func (s *Server) RefreshDiagnostics() error {
156+
if ptrIsTrue(s.initializeParams.Capabilities.Workspace.Diagnostics.RefreshSupport) {
157+
if err := s.sendRequest(lsproto.MethodWorkspaceDiagnosticRefresh, nil); err != nil {
158+
return fmt.Errorf("failed to refresh diagnostics: %w", err)
159+
}
160+
}
161+
return nil
157162
}
158163

159164
func (s *Server) Run() error {
@@ -551,3 +556,10 @@ func codeFence(lang string, code string) string {
551556
func ptrTo[T any](v T) *T {
552557
return &v
553558
}
559+
560+
func ptrIsTrue(v *bool) bool {
561+
if v == nil {
562+
return false
563+
}
564+
return *v
565+
}

internal/project/host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type WatcherHandle string
1010
type Client interface {
1111
WatchFiles(watchers []*lsproto.FileSystemWatcher) (WatcherHandle, error)
1212
UnwatchFiles(handle WatcherHandle) error
13-
PublishDiagnostics(params *lsproto.PublishDiagnosticsParams) error
13+
RefreshDiagnostics() error
1414
}
1515

1616
type ServiceHost interface {

internal/project/project.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type ProjectHost interface {
4545
Log(s string)
4646
PositionEncoding() lsproto.PositionEncodingKind
4747

48+
IsWatchEnabled() bool
4849
Client() Client
4950
}
5051

@@ -90,7 +91,7 @@ func NewConfiguredProject(configFileName string, configFilePath tspath.Path, hos
9091
project.configFilePath = configFilePath
9192
project.initialLoadPending = true
9293
client := host.Client()
93-
if client != nil {
94+
if host.IsWatchEnabled() && client != nil {
9495
project.rootFilesWatch = newWatchedFiles(client, lsproto.WatchKindChange|lsproto.WatchKindCreate|lsproto.WatchKindDelete, core.Identity)
9596
}
9697
return project
@@ -117,7 +118,7 @@ func NewProject(name string, kind Kind, currentDirectory string, host ProjectHos
117118
UseCaseSensitiveFileNames: host.FS().UseCaseSensitiveFileNames(),
118119
}
119120
client := host.Client()
120-
if client != nil {
121+
if host.IsWatchEnabled() && client != nil {
121122
project.failedLookupsWatch = newWatchedFiles(client, lsproto.WatchKindCreate, func(data map[tspath.Path]string) []string {
122123
return slices.Sorted(maps.Values(data))
123124
})
@@ -271,7 +272,7 @@ func (p *Project) getModuleResolutionWatchGlobs() (failedLookups map[tspath.Path
271272

272273
func (p *Project) updateWatchers() {
273274
client := p.host.Client()
274-
if client == nil {
275+
if !p.host.IsWatchEnabled() || client == nil {
275276
return
276277
}
277278

internal/project/service.go

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package project
22

33
import (
44
"fmt"
5-
"slices"
65
"strings"
76
"sync"
87

@@ -135,10 +134,12 @@ func (s *Service) PositionEncoding() lsproto.PositionEncodingKind {
135134

136135
// Client implements ProjectHost.
137136
func (s *Service) Client() Client {
138-
if s.options.WatchEnabled {
139-
return s.host.Client()
140-
}
141-
return nil
137+
return s.host.Client()
138+
}
139+
140+
// IsWatchEnabled implements ProjectHost.
141+
func (s *Service) IsWatchEnabled() bool {
142+
return s.options.WatchEnabled
142143
}
143144

144145
func (s *Service) Projects() []*Project {
@@ -261,13 +262,11 @@ func (s *Service) OnWatchedFilesChanged(changes []*lsproto.FileEvent) error {
261262
}
262263
}
263264

264-
for _, project := range s.configuredProjects {
265-
if project.updateIfDirty() {
266-
if err := s.publishDiagnosticsForOpenFiles(project); err != nil {
267-
return err
268-
}
269-
}
265+
client := s.host.Client()
266+
if client != nil {
267+
return client.RefreshDiagnostics()
270268
}
269+
271270
return nil
272271
}
273272

@@ -290,36 +289,6 @@ func (s *Service) onConfigFileChanged(project *Project, changeKind lsproto.FileC
290289
return nil
291290
}
292291

293-
func (s *Service) publishDiagnosticsForOpenFiles(project *Project) error {
294-
client := s.host.Client()
295-
if client == nil {
296-
return nil
297-
}
298-
299-
for path := range s.openFiles {
300-
info := s.GetScriptInfoByPath(path)
301-
if slices.Contains(info.containingProjects, project) {
302-
diagnostics := project.LanguageService().GetDocumentDiagnostics(info.fileName)
303-
lspDiagnostics := make([]*lsproto.Diagnostic, len(diagnostics))
304-
for i, diagnostic := range diagnostics {
305-
if diag, err := s.converters.ToLSPDiagnostic(diagnostic); err != nil {
306-
return fmt.Errorf("error converting diagnostic: %w", err)
307-
} else {
308-
lspDiagnostics[i] = diag
309-
}
310-
}
311-
312-
if err := client.PublishDiagnostics(&lsproto.PublishDiagnosticsParams{
313-
Uri: ls.FileNameToDocumentURI(info.fileName),
314-
Diagnostics: lspDiagnostics,
315-
}); err != nil {
316-
return fmt.Errorf("error publishing diagnostics: %w", err)
317-
}
318-
}
319-
}
320-
return nil
321-
}
322-
323292
func (s *Service) ensureProjectStructureUpToDate() {
324293
var hasChanges bool
325294
for _, project := range s.configuredProjects {

0 commit comments

Comments
 (0)