|
| 1 | +package lsp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// initializeTypescriptLanguageServer initializes the TypeScript language server |
| 12 | +// with specific configurations and opens all TypeScript files in the workspace. |
| 13 | +func initializeTypescriptLanguageServer(ctx context.Context, client *Client, workspaceDir string) error { |
| 14 | + lspLogger.Info("Initializing TypeScript language server with workspace: %s", workspaceDir) |
| 15 | + |
| 16 | + // First, open all TypeScript files in the workspace |
| 17 | + if err := openAllTypeScriptFiles(ctx, client, workspaceDir); err != nil { |
| 18 | + return fmt.Errorf("failed to open TypeScript files: %w", err) |
| 19 | + } |
| 20 | + |
| 21 | + return nil |
| 22 | +} |
| 23 | + |
| 24 | +// openAllTypeScriptFiles finds and opens all TypeScript files in the workspace |
| 25 | +func openAllTypeScriptFiles(ctx context.Context, client *Client, workspaceDir string) error { |
| 26 | + lspLogger.Info("Opening all TypeScript files in workspace: %s", workspaceDir) |
| 27 | + |
| 28 | + // Track count of opened files for logging |
| 29 | + fileCount := 0 |
| 30 | + |
| 31 | + // Walk the workspace directory |
| 32 | + err := filepath.Walk(workspaceDir, func(path string, info os.FileInfo, err error) error { |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + // Skip directories |
| 38 | + if info.IsDir() { |
| 39 | + // Skip node_modules, .git, and other common directories to avoid processing too many files |
| 40 | + basename := filepath.Base(path) |
| 41 | + if basename == "node_modules" || basename == ".git" || strings.HasPrefix(basename, ".") { |
| 42 | + return filepath.SkipDir |
| 43 | + } |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + // Check if file is a TypeScript file |
| 48 | + if strings.HasSuffix(path, ".ts") || strings.HasSuffix(path, ".tsx") { |
| 49 | + if err := client.OpenFile(ctx, path); err != nil { |
| 50 | + lspLogger.Warn("Failed to open TypeScript file %s: %v", path, err) |
| 51 | + return nil // Continue with other files even if one fails |
| 52 | + } |
| 53 | + fileCount++ |
| 54 | + } |
| 55 | + |
| 56 | + return nil |
| 57 | + }) |
| 58 | + |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("error walking workspace directory: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + lspLogger.Info("Opened %d TypeScript files", fileCount) |
| 64 | + return nil |
| 65 | +} |
0 commit comments