on main help sign in
Merge pull request #29 from charmbracelet/perf-lsp-watcher
5def756Raphael Amorim
internal/lsp/watcher/watcher.go+25−9
@@ -257,7 +257,10 @@ func (w *WorkspaceWatcher) openHighPriorityFiles(ctx context.Context, serverName
257 }
257 }
258 }
258 }
259
259
260+ // Collect all files to open first
261+ var filesToOpen []string
262+
260− // For each pattern, find and open matching files
263+ // For each pattern, find matching files
261 for _, pattern := range patterns {
264 for _, pattern := range patterns {
262 // Use doublestar.Glob to find files matching the pattern (supports ** patterns)
265 // Use doublestar.Glob to find files matching the pattern (supports ** patterns)
263 matches, err := doublestar.Glob(os.DirFS(w.workspacePath), pattern)
266 matches, err := doublestar.Glob(os.DirFS(w.workspacePath), pattern)
@@ -278,7 +281,23 @@ func (w *WorkspaceWatcher) openHighPriorityFiles(ctx context.Context, serverName
278 continue
281 continue
279 }
282 }
280
283
281− // Open the file
284+ filesToOpen = append(filesToOpen, fullPath)
285+
286+ // Limit the number of files per pattern
287+ if len(filesToOpen) >= 5 && (serverName != "java" && serverName != "jdtls") {
288+ break
289+ }
290+ }
291+ }
292+
293+ // Open files in batches to reduce overhead
294+ batchSize := 3
295+ for i := 0; i < len(filesToOpen); i += batchSize {
296+ end := min(i+batchSize, len(filesToOpen))
297+
298+ // Open batch of files
299+ for j := i; j < end; j++ {
300+ fullPath := filesToOpen[j]
282 if err := w.client.OpenFile(ctx, fullPath); err != nil {
301 if err := w.client.OpenFile(ctx, fullPath); err != nil {
283 if cnf.DebugLSP {
302 if cnf.DebugLSP {
284 logging.Debug("Error opening high-priority file", "path", fullPath, "error", err)
303 logging.Debug("Error opening high-priority file", "path", fullPath, "error", err)
@@ -289,14 +308,11 @@ func (w *WorkspaceWatcher) openHighPriorityFiles(ctx context.Context, serverName
289 logging.Debug("Opened high-priority file", "path", fullPath)
308 logging.Debug("Opened high-priority file", "path", fullPath)
290 }
309 }
291 }
310 }
311+ }
292
312
293− // Add a small delay to prevent overwhelming the server
313+ // Only add delay between batches, not individual files
314+ if end < len(filesToOpen) {
294− time.Sleep(20 * time.Millisecond)
315+ time.Sleep(50 * time.Millisecond)
295−
296− // Limit the number of files opened per pattern
297− if filesOpened >= 5 && (serverName != "java" && serverName != "jdtls") {
298− break
299− }
300 }
316 }
301 }
317 }
302
318