feat(server): Show code action to add a language

This commit is contained in:
Myzel394 2024-10-20 15:36:04 +02:00
parent ec4deeb59f
commit 9378392927
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
3 changed files with 50 additions and 7 deletions

View File

@ -0,0 +1,43 @@
package roothandler
import (
"fmt"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func fetchAddLanguageActions(uri protocol.DocumentUri) ([]protocol.CodeAction, error) {
actions := make([]protocol.CodeAction, 0, len(AllSupportedLanguages))
kind := protocol.CodeActionKindQuickFix
isPreferred := true
for _, language := range AllSupportedLanguages {
actions = append(actions, protocol.CodeAction{
Title: fmt.Sprintf("Use %s for this file", language),
Kind: &kind,
IsPreferred: &isPreferred,
Edit: &protocol.WorkspaceEdit{
Changes: map[protocol.DocumentUri][]protocol.TextEdit{
uri: {
{
Range: protocol.Range{
Start: protocol.Position{
Line: 0,
Character: 0,
},
End: protocol.Position{
Line: 0,
Character: 0,
},
},
NewText: fmt.Sprintf("#?lsp.language=%s\n", language),
},
},
},
},
})
}
return actions, nil
}

View File

@ -83,9 +83,14 @@ var filenameToLanguageMap = map[string]SupportedLanguage{
"sshd_config": LanguageSSHDConfig, "sshd_config": LanguageSSHDConfig,
"sshdconfig": LanguageSSHDConfig, "sshdconfig": LanguageSSHDConfig,
"sshd": LanguageSSHDConfig, "sshd": LanguageSSHDConfig,
"sshd_conf": LanguageSSHDConfig,
"sshdconf": LanguageSSHDConfig,
"ssh_config": LanguageSSHConfig, "ssh_config": LanguageSSHConfig,
"sshconfig": LanguageSSHConfig, "sshconfig": LanguageSSHConfig,
"ssh": LanguageSSHConfig,
"ssh_conf": LanguageSSHConfig,
"sshconf": LanguageSSHConfig,
"fstab": LanguageFstab, "fstab": LanguageFstab,
@ -93,15 +98,10 @@ var filenameToLanguageMap = map[string]SupportedLanguage{
"aliases": LanguageAliases, "aliases": LanguageAliases,
"mailaliases": LanguageAliases, "mailaliases": LanguageAliases,
"wg": LanguageWireguard,
"wg.conf": LanguageWireguard,
"wg0.conf": LanguageWireguard,
"wg0": LanguageWireguard,
} }
var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`) var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`)
var wireguardPattern = regexp.MustCompile(`/wg\d+\.conf$`) var wireguardPattern = regexp.MustCompile(`wg(\d+)?(\.conf)?$`)
var undetectableError = common.ParseError{ var undetectableError = common.ParseError{
Line: 0, Line: 0,

View File

@ -20,7 +20,7 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa
undetectableError, undetectableError,
) )
return nil, nil return fetchAddLanguageActions(params.TextDocument.URI)
} }
switch *language { switch *language {