summaryrefslogtreecommitdiffhomepage
path: root/internal/lsp/discovery/integration.go
blob: a2043b53e0d1e0fdb3e899b5115444f344c2bc71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package discovery

import (
	"fmt"

	"github.com/sst/opencode/internal/config"
	"log/slog"
)

// IntegrateLSPServers discovers languages and LSP servers and integrates them into the application configuration
func IntegrateLSPServers(workingDir string) error {
	// Get the current configuration
	cfg := config.Get()
	if cfg == nil {
		return fmt.Errorf("config not loaded")
	}

	// Check if this is the first run
	shouldInit, err := config.ShouldShowInitDialog()
	if err != nil {
		return fmt.Errorf("failed to check initialization status: %w", err)
	}

	// Always run language detection, but log differently for first run vs. subsequent runs
	if shouldInit || len(cfg.LSP) == 0 {
		slog.Info("Running initial LSP auto-discovery...")
	} else {
		slog.Debug("Running LSP auto-discovery to detect new languages...")
	}

	// Configure LSP servers
	servers, err := ConfigureLSPServers(workingDir)
	if err != nil {
		return fmt.Errorf("failed to configure LSP servers: %w", err)
	}

	// Update the configuration with discovered servers
	for langID, serverInfo := range servers {
		// Skip languages that already have a configured server
		if _, exists := cfg.LSP[langID]; exists {
			slog.Debug("LSP server already configured for language", "language", langID)
			continue
		}

		if serverInfo.Available {
			// Only add servers that were found
			cfg.LSP[langID] = config.LSPConfig{
				Disabled: false,
				Command:  serverInfo.Path,
				Args:     serverInfo.Args,
			}
			slog.Info("Added LSP server to configuration",
				"language", langID,
				"command", serverInfo.Command,
				"path", serverInfo.Path)
		} else {
			slog.Warn("LSP server not available",
				"language", langID,
				"command", serverInfo.Command,
				"installCmd", serverInfo.InstallCmd)
		}
	}

	return nil
}