From 9378392927d747f72cafb821e09bd4593867c2c7 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:36:04 +0200 Subject: [PATCH] feat(server): Show code action to add a language --- server/root-handler/code-actions.go | 43 +++++++++++++++++++ server/root-handler/lsp-utils.go | 12 +++--- .../root-handler/text-document-code-action.go | 2 +- 3 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 server/root-handler/code-actions.go diff --git a/server/root-handler/code-actions.go b/server/root-handler/code-actions.go new file mode 100644 index 0000000..61a530e --- /dev/null +++ b/server/root-handler/code-actions.go @@ -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 +} diff --git a/server/root-handler/lsp-utils.go b/server/root-handler/lsp-utils.go index b0cc79a..0e9ce5f 100644 --- a/server/root-handler/lsp-utils.go +++ b/server/root-handler/lsp-utils.go @@ -83,9 +83,14 @@ var filenameToLanguageMap = map[string]SupportedLanguage{ "sshd_config": LanguageSSHDConfig, "sshdconfig": LanguageSSHDConfig, "sshd": LanguageSSHDConfig, + "sshd_conf": LanguageSSHDConfig, + "sshdconf": LanguageSSHDConfig, "ssh_config": LanguageSSHConfig, "sshconfig": LanguageSSHConfig, + "ssh": LanguageSSHConfig, + "ssh_conf": LanguageSSHConfig, + "sshconf": LanguageSSHConfig, "fstab": LanguageFstab, @@ -93,15 +98,10 @@ var filenameToLanguageMap = map[string]SupportedLanguage{ "aliases": 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 wireguardPattern = regexp.MustCompile(`/wg\d+\.conf$`) +var wireguardPattern = regexp.MustCompile(`wg(\d+)?(\.conf)?$`) var undetectableError = common.ParseError{ Line: 0, diff --git a/server/root-handler/text-document-code-action.go b/server/root-handler/text-document-code-action.go index 8cbf1a2..737911e 100644 --- a/server/root-handler/text-document-code-action.go +++ b/server/root-handler/text-document-code-action.go @@ -20,7 +20,7 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa undetectableError, ) - return nil, nil + return fetchAddLanguageActions(params.TextDocument.URI) } switch *language {