integration.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package discovery
  2. import (
  3. "fmt"
  4. "github.com/sst/opencode/internal/config"
  5. "log/slog"
  6. )
  7. // IntegrateLSPServers discovers languages and LSP servers and integrates them into the application configuration
  8. func IntegrateLSPServers(workingDir string) error {
  9. // Get the current configuration
  10. cfg := config.Get()
  11. if cfg == nil {
  12. return fmt.Errorf("config not loaded")
  13. }
  14. // Check if this is the first run
  15. shouldInit, err := config.ShouldShowInitDialog()
  16. if err != nil {
  17. return fmt.Errorf("failed to check initialization status: %w", err)
  18. }
  19. // Always run language detection, but log differently for first run vs. subsequent runs
  20. if shouldInit || len(cfg.LSP) == 0 {
  21. slog.Info("Running initial LSP auto-discovery...")
  22. } else {
  23. slog.Debug("Running LSP auto-discovery to detect new languages...")
  24. }
  25. // Configure LSP servers
  26. servers, err := ConfigureLSPServers(workingDir)
  27. if err != nil {
  28. return fmt.Errorf("failed to configure LSP servers: %w", err)
  29. }
  30. // Update the configuration with discovered servers
  31. for langID, serverInfo := range servers {
  32. // Skip languages that already have a configured server
  33. if _, exists := cfg.LSP[langID]; exists {
  34. slog.Debug("LSP server already configured for language", "language", langID)
  35. continue
  36. }
  37. if serverInfo.Available {
  38. // Only add servers that were found
  39. cfg.LSP[langID] = config.LSPConfig{
  40. Disabled: false,
  41. Command: serverInfo.Path,
  42. Args: serverInfo.Args,
  43. }
  44. slog.Info("Added LSP server to configuration",
  45. "language", langID,
  46. "command", serverInfo.Command,
  47. "path", serverInfo.Path)
  48. } else {
  49. slog.Warn("LSP server not available",
  50. "language", langID,
  51. "command", serverInfo.Command,
  52. "installCmd", serverInfo.InstallCmd)
  53. }
  54. }
  55. return nil
  56. }