feat(lsp): load defaults by either name or command (#1109)

Carlos Alexandro Becker created

* fix(lsp): load defaults by name or command name

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix(lsp): simplify

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Change summary

crush.json                |  4 +---
internal/config/config.go |  2 +-
internal/config/load.go   | 13 +++++++++++--
3 files changed, 13 insertions(+), 6 deletions(-)

Detailed changes

crush.json 🔗

@@ -1,8 +1,6 @@
 {
   "$schema": "https://charm.land/crush.json",
   "lsp": {
-    "Go": {
-      "command": "gopls"
-    }
+    "gopls": {}
   }
 }

internal/config/config.go 🔗

@@ -118,7 +118,7 @@ type MCPConfig struct {
 
 type LSPConfig struct {
 	Disabled    bool              `json:"disabled,omitempty" jsonschema:"description=Whether this LSP server is disabled,default=false"`
-	Command     string            `json:"command" jsonschema:"required,description=Command to execute for the LSP server,example=gopls"`
+	Command     string            `json:"command,omitempty" jsonschema:"required,description=Command to execute for the LSP server,example=gopls"`
 	Args        []string          `json:"args,omitempty" jsonschema:"description=Arguments to pass to the LSP server command"`
 	Env         map[string]string `json:"env,omitempty" jsonschema:"description=Environment variables to set to the LSP server command"`
 	FileTypes   []string          `json:"filetypes,omitempty" jsonschema:"description=File types this LSP server handles,example=go,example=mod,example=rs,example=c,example=js,example=ts"`

internal/config/load.go 🔗

@@ -351,10 +351,13 @@ func (c *Config) applyLSPDefaults() {
 
 	// Apply defaults to each LSP configuration
 	for name, cfg := range c.LSP {
-		// Try to get defaults from powernap based on command name
+		// Try to get defaults from powernap based on name or command name.
 		base, ok := configManager.GetServer(name)
 		if !ok {
-			continue
+			base, ok = configManager.GetServer(cfg.Command)
+			if !ok {
+				continue
+			}
 		}
 		if cfg.Options == nil {
 			cfg.Options = base.Settings
@@ -368,6 +371,12 @@ func (c *Config) applyLSPDefaults() {
 		if len(cfg.RootMarkers) == 0 {
 			cfg.RootMarkers = base.RootMarkers
 		}
+		if len(cfg.Args) == 0 {
+			cfg.Args = base.Args
+		}
+		if len(cfg.Env) == 0 {
+			cfg.Env = base.Environment
+		}
 		// Update the config in the map
 		c.LSP[name] = cfg
 	}