diff --git a/server/root-handler/lsp/text-document-code-action.go b/server/root-handler/lsp/text-document-code-action.go index 0cda9b8..bf4cc45 100644 --- a/server/root-handler/lsp/text-document-code-action.go +++ b/server/root-handler/lsp/text-document-code-action.go @@ -20,17 +20,17 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa } switch *language { - case utils.LanguageFstab: + case shared.LanguageFstab: return nil, nil - case utils.LanguageHosts: + case shared.LanguageHosts: return hosts.TextDocumentCodeAction(context, params) - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return nil, nil - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentCodeAction(context, params) - case utils.LanguageWireguard: + case shared.LanguageWireguard: return wireguard.TextDocumentCodeAction(context, params) - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentCodeAction(context, params) } diff --git a/server/root-handler/lsp/text-document-completion.go b/server/root-handler/lsp/text-document-completion.go index 062aaee..f452090 100644 --- a/server/root-handler/lsp/text-document-completion.go +++ b/server/root-handler/lsp/text-document-completion.go @@ -8,8 +8,6 @@ import ( sshdconfig "config-lsp/handlers/sshd_config/lsp" wireguard "config-lsp/handlers/wireguard/lsp" "config-lsp/root-handler/shared" - "config-lsp/root-handler/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -22,17 +20,17 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa } switch *language { - case utils.LanguageFstab: + case shared.LanguageFstab: return fstab.TextDocumentCompletion(context, params) - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentCompletion(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentCompletion(context, params) - case utils.LanguageWireguard: + case shared.LanguageWireguard: return wireguard.TextDocumentCompletion(context, params) - case utils.LanguageHosts: + case shared.LanguageHosts: return hosts.TextDocumentCompletion(context, params) - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentCompletion(context, params) } diff --git a/server/root-handler/lsp/text-document-definition.go b/server/root-handler/lsp/text-document-definition.go index 14aea5e..c28b1f3 100644 --- a/server/root-handler/lsp/text-document-definition.go +++ b/server/root-handler/lsp/text-document-definition.go @@ -19,17 +19,17 @@ func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionPa } switch *language { - case utils.LanguageHosts: + case shared.LanguageHosts: return nil, nil - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentDefinition(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentDefinition(context, params) - case utils.LanguageFstab: + case shared.LanguageFstab: return nil, nil - case utils.LanguageWireguard: + case shared.LanguageWireguard: return nil, nil - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentDefinition(context, params) } diff --git a/server/root-handler/lsp/text-document-did-change.go b/server/root-handler/lsp/text-document-did-change.go index 781f32a..63edf6e 100644 --- a/server/root-handler/lsp/text-document-did-change.go +++ b/server/root-handler/lsp/text-document-did-change.go @@ -8,8 +8,6 @@ import ( sshdconfig "config-lsp/handlers/sshd_config/lsp" wireguard "config-lsp/handlers/wireguard/lsp" "config-lsp/root-handler/shared" - "config-lsp/root-handler/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -17,19 +15,19 @@ import ( func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeTextDocumentParams) error { language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) - if language == nil { - content := params.ContentChanges[0].(protocol.TextDocumentContentChangeEventWhole).Text - newLanguage, err := initFile( - context, - content, - params.TextDocument.URI, - "", - ) + content := params.ContentChanges[0].(protocol.TextDocumentContentChangeEventWhole).Text + newLanguage, err := initFile( + context, + content, + params.TextDocument.URI, + "", + ) - if err != nil { - return err - } + if err != nil { + return err + } + if newLanguage != language { language = newLanguage params := &protocol.DidOpenTextDocumentParams{ @@ -42,33 +40,33 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText } switch *language { - case utils.LanguageFstab: + case shared.LanguageFstab: return fstab.TextDocumentDidOpen(context, params) - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentDidOpen(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentDidOpen(context, params) - case utils.LanguageWireguard: + case shared.LanguageWireguard: return wireguard.TextDocumentDidOpen(context, params) - case utils.LanguageHosts: + case shared.LanguageHosts: return hosts.TextDocumentDidOpen(context, params) - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentDidOpen(context, params) } } switch *language { - case utils.LanguageFstab: + case shared.LanguageFstab: return fstab.TextDocumentDidChange(context, params) - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentDidChange(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentDidChange(context, params) - case utils.LanguageWireguard: + case shared.LanguageWireguard: return wireguard.TextDocumentDidChange(context, params) - case utils.LanguageHosts: + case shared.LanguageHosts: return hosts.TextDocumentDidChange(context, params) - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentDidChange(context, params) } diff --git a/server/root-handler/lsp/text-document-did-close.go b/server/root-handler/lsp/text-document-did-close.go index fef74f7..7dd0bce 100644 --- a/server/root-handler/lsp/text-document-did-close.go +++ b/server/root-handler/lsp/text-document-did-close.go @@ -8,8 +8,6 @@ import ( sshdconfig "config-lsp/handlers/sshd_config/lsp" wireguard "config-lsp/handlers/wireguard/lsp" "config-lsp/root-handler/shared" - "config-lsp/root-handler/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -22,20 +20,21 @@ func TextDocumentDidClose(context *glsp.Context, params *protocol.DidCloseTextDo } delete(shared.OpenedFiles, params.TextDocument.URI) + delete(shared.LanguagesOverwrites, params.TextDocument.URI) shared.Handler.RemoveDocument(params.TextDocument.URI) switch *language { - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentDidClose(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentDidClose(context, params) - case utils.LanguageFstab: + case shared.LanguageFstab: return fstab.TextDocumentDidClose(context, params) - case utils.LanguageWireguard: + case shared.LanguageWireguard: return wireguard.TextDocumentDidClose(context, params) - case utils.LanguageHosts: + case shared.LanguageHosts: return hosts.TextDocumentDidClose(context, params) - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentDidClose(context, params) default: } diff --git a/server/root-handler/lsp/text-document-did-open.go b/server/root-handler/lsp/text-document-did-open.go index 0690c9e..20ebc44 100644 --- a/server/root-handler/lsp/text-document-did-open.go +++ b/server/root-handler/lsp/text-document-did-open.go @@ -34,17 +34,17 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu } switch *language { - case utils.LanguageFstab: + case shared.LanguageFstab: return fstab.TextDocumentDidOpen(context, params) - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentDidOpen(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentDidOpen(context, params) - case utils.LanguageWireguard: + case shared.LanguageWireguard: return wireguard.TextDocumentDidOpen(context, params) - case utils.LanguageHosts: + case shared.LanguageHosts: return hosts.TextDocumentDidOpen(context, params) - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentDidOpen(context, params) } @@ -56,13 +56,15 @@ func initFile( content string, uri protocol.DocumentUri, advertisedLanguage string, -) (*utils.SupportedLanguage, error) { +) (*shared.SupportedLanguage, error) { language, err := utils.DetectLanguage(content, advertisedLanguage, uri) if err != nil { utils.NotifyLanguageUndetectable(context, uri) return nil, utils.LanguageUndetectableError{} + } else { + utils.NotifyDetectedLanguage(context, uri, language) } shared.OpenedFiles[uri] = struct{}{} diff --git a/server/root-handler/lsp/text-document-hover.go b/server/root-handler/lsp/text-document-hover.go index fb15c0e..d64a53a 100644 --- a/server/root-handler/lsp/text-document-hover.go +++ b/server/root-handler/lsp/text-document-hover.go @@ -8,8 +8,6 @@ import ( sshdconfig "config-lsp/handlers/sshd_config/lsp" wireguard "config-lsp/handlers/wireguard/lsp" "config-lsp/root-handler/shared" - "config-lsp/root-handler/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -22,17 +20,17 @@ func TextDocumentHover(context *glsp.Context, params *protocol.HoverParams) (*pr } switch *language { - case utils.LanguageHosts: + case shared.LanguageHosts: return hosts.TextDocumentHover(context, params) - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentHover(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentHover(context, params) - case utils.LanguageFstab: + case shared.LanguageFstab: return fstab.TextDocumentHover(context, params) - case utils.LanguageWireguard: + case shared.LanguageWireguard: return wireguard.TextDocumentHover(context, params) - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentHover(context, params) } diff --git a/server/root-handler/lsp/text-document-prepare-rename.go b/server/root-handler/lsp/text-document-prepare-rename.go index ac92eb6..812908f 100644 --- a/server/root-handler/lsp/text-document-prepare-rename.go +++ b/server/root-handler/lsp/text-document-prepare-rename.go @@ -19,17 +19,17 @@ func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRe } switch *language { - case utils.LanguageHosts: + case shared.LanguageHosts: return nil, nil - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return nil, nil - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentPrepareRename(context, params) - case utils.LanguageFstab: + case shared.LanguageFstab: return nil, nil - case utils.LanguageWireguard: + case shared.LanguageWireguard: return nil, nil - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentPrepareRename(context, params) } diff --git a/server/root-handler/lsp/text-document-range-formatting.go b/server/root-handler/lsp/text-document-range-formatting.go index 6604292..19fc00b 100644 --- a/server/root-handler/lsp/text-document-range-formatting.go +++ b/server/root-handler/lsp/text-document-range-formatting.go @@ -21,17 +21,17 @@ func TextDocumentRangeFormattingFunc( } switch *language { - case utils.LanguageHosts: + case shared.LanguageHosts: return nil, nil - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentRangeFormatting(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentRangeFormatting(context, params) - case utils.LanguageFstab: + case shared.LanguageFstab: return nil, nil - case utils.LanguageWireguard: + case shared.LanguageWireguard: return nil, nil - case utils.LanguageAliases: + case shared.LanguageAliases: return nil, nil } diff --git a/server/root-handler/lsp/text-document-rename.go b/server/root-handler/lsp/text-document-rename.go index fa779a2..d630bb3 100644 --- a/server/root-handler/lsp/text-document-rename.go +++ b/server/root-handler/lsp/text-document-rename.go @@ -18,17 +18,17 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (* } switch *language { - case utils.LanguageHosts: + case shared.LanguageHosts: return nil, nil - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return nil, nil - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentRename(context, params) - case utils.LanguageFstab: + case shared.LanguageFstab: return nil, nil - case utils.LanguageWireguard: + case shared.LanguageWireguard: return nil, nil - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentRename(context, params) } diff --git a/server/root-handler/lsp/text-document-signature-help.go b/server/root-handler/lsp/text-document-signature-help.go index 02e88c7..375ee40 100644 --- a/server/root-handler/lsp/text-document-signature-help.go +++ b/server/root-handler/lsp/text-document-signature-help.go @@ -5,8 +5,6 @@ import ( sshconfig "config-lsp/handlers/ssh_config/lsp" sshdconfig "config-lsp/handlers/sshd_config/lsp" "config-lsp/root-handler/shared" - "config-lsp/root-handler/utils" - "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -19,17 +17,17 @@ func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.Signature } switch *language { - case utils.LanguageHosts: + case shared.LanguageHosts: return nil, nil - case utils.LanguageSSHDConfig: + case shared.LanguageSSHDConfig: return sshdconfig.TextDocumentSignatureHelp(context, params) - case utils.LanguageSSHConfig: + case shared.LanguageSSHConfig: return sshconfig.TextDocumentSignatureHelp(context, params) - case utils.LanguageFstab: + case shared.LanguageFstab: return nil, nil - case utils.LanguageWireguard: + case shared.LanguageWireguard: return nil, nil - case utils.LanguageAliases: + case shared.LanguageAliases: return aliases.TextDocumentSignatureHelp(context, params) } diff --git a/server/root-handler/shared/constants.go b/server/root-handler/shared/constants.go new file mode 100644 index 0000000..c3aceb3 --- /dev/null +++ b/server/root-handler/shared/constants.go @@ -0,0 +1,21 @@ +package shared + +type SupportedLanguage string + +const ( + LanguageSSHConfig SupportedLanguage = "ssh_config" + LanguageSSHDConfig SupportedLanguage = "sshd_config" + LanguageFstab SupportedLanguage = "fstab" + LanguageWireguard SupportedLanguage = "languagewireguard" + LanguageHosts SupportedLanguage = "hosts" + LanguageAliases SupportedLanguage = "aliases" +) + +var AllSupportedLanguages = []string{ + string(LanguageSSHConfig), + string(LanguageSSHDConfig), + string(LanguageFstab), + string(LanguageWireguard), + string(LanguageHosts), + string(LanguageAliases), +} diff --git a/server/root-handler/shared/indexes.go b/server/root-handler/shared/indexes.go new file mode 100644 index 0000000..514d7cb --- /dev/null +++ b/server/root-handler/shared/indexes.go @@ -0,0 +1,16 @@ +package shared + +import ( + protocol "github.com/tliron/glsp/protocol_3_16" +) + +type LanguageOverwrite struct { + Language SupportedLanguage + + // The start of the overwrite + Raw string + Line uint32 + Character uint32 +} + +var LanguagesOverwrites = map[protocol.DocumentUri]LanguageOverwrite{} diff --git a/server/root-handler/shared/singleton.go b/server/root-handler/shared/singleton.go index 05ac59c..cb83c1d 100644 --- a/server/root-handler/shared/singleton.go +++ b/server/root-handler/shared/singleton.go @@ -1,8 +1,6 @@ package shared import ( - "config-lsp/root-handler/utils" - protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -11,20 +9,20 @@ var Handler RootHandler var OpenedFiles = make(map[protocol.DocumentUri]struct{}) type RootHandler struct { - languageMap map[protocol.DocumentUri]utils.SupportedLanguage + languageMap map[protocol.DocumentUri]SupportedLanguage } func NewRootHandler() RootHandler { return RootHandler{ - languageMap: make(map[protocol.DocumentUri]utils.SupportedLanguage), + languageMap: make(map[protocol.DocumentUri]SupportedLanguage), } } -func (h *RootHandler) AddDocument(uri protocol.DocumentUri, language utils.SupportedLanguage) { +func (h *RootHandler) AddDocument(uri protocol.DocumentUri, language SupportedLanguage) { h.languageMap[uri] = language } -func (h *RootHandler) GetLanguageForDocument(uri protocol.DocumentUri) *utils.SupportedLanguage { +func (h *RootHandler) GetLanguageForDocument(uri protocol.DocumentUri) *SupportedLanguage { language, found := h.languageMap[uri] if !found { diff --git a/server/root-handler/utils/code-actions.go b/server/root-handler/utils/code-actions.go index 193cc61..b86027d 100644 --- a/server/root-handler/utils/code-actions.go +++ b/server/root-handler/utils/code-actions.go @@ -1,18 +1,19 @@ package utils import ( + "config-lsp/root-handler/shared" "fmt" protocol "github.com/tliron/glsp/protocol_3_16" ) func FetchAddLanguageActions(uri protocol.DocumentUri) ([]protocol.CodeAction, error) { - actions := make([]protocol.CodeAction, 0, len(AllSupportedLanguages)) + actions := make([]protocol.CodeAction, 0, len(shared.AllSupportedLanguages)) kind := protocol.CodeActionKindQuickFix isPreferred := true - for _, language := range AllSupportedLanguages { + for _, language := range shared.AllSupportedLanguages { actions = append(actions, protocol.CodeAction{ Title: fmt.Sprintf("Use %s for this file", language), Kind: &kind, diff --git a/server/root-handler/utils/language-detection.go b/server/root-handler/utils/language-detection.go index 679349f..24c91d7 100644 --- a/server/root-handler/utils/language-detection.go +++ b/server/root-handler/utils/language-detection.go @@ -2,6 +2,7 @@ package utils import ( "config-lsp/common" + "config-lsp/root-handler/shared" "config-lsp/utils" "fmt" "path" @@ -11,32 +12,12 @@ import ( protocol "github.com/tliron/glsp/protocol_3_16" ) -type SupportedLanguage string - -const ( - LanguageSSHConfig SupportedLanguage = "ssh_config" - LanguageSSHDConfig SupportedLanguage = "sshd_config" - LanguageFstab SupportedLanguage = "fstab" - LanguageWireguard SupportedLanguage = "languagewireguard" - LanguageHosts SupportedLanguage = "hosts" - LanguageAliases SupportedLanguage = "aliases" -) - -var AllSupportedLanguages = []string{ - string(LanguageSSHConfig), - string(LanguageSSHDConfig), - string(LanguageFstab), - string(LanguageWireguard), - string(LanguageHosts), - string(LanguageAliases), -} - type UnsupportedLanguageError struct { SuggestedLanguage string } func (e UnsupportedLanguageError) Error() string { - return fmt.Sprintf("Language '%s' is not supported. Choose one of: %s", e.SuggestedLanguage, strings.Join(AllSupportedLanguages, ", ")) + return fmt.Sprintf("Language '%s' is not supported. Choose one of: %s", e.SuggestedLanguage, strings.Join(shared.AllSupportedLanguages, ", ")) } type LanguageUndetectableError struct{} @@ -45,50 +26,50 @@ func (e LanguageUndetectableError) Error() string { return "Please add: '#?lsp.language=' to the top of the file. config-lsp was unable to detect the appropriate language for this file." } -var valueToLanguageMap = map[string]SupportedLanguage{ - "sshd_config": LanguageSSHDConfig, - "sshdconfig": LanguageSSHDConfig, +var valueToLanguageMap = map[string]shared.SupportedLanguage{ + "sshd_config": shared.LanguageSSHDConfig, + "sshdconfig": shared.LanguageSSHDConfig, - "ssh_config": LanguageSSHConfig, - "sshconfig": LanguageSSHConfig, + "ssh_config": shared.LanguageSSHConfig, + "sshconfig": shared.LanguageSSHConfig, - ".ssh/config": LanguageSSHConfig, - "~/.ssh/config": LanguageSSHConfig, + ".ssh/config": shared.LanguageSSHConfig, + "~/.ssh/config": shared.LanguageSSHConfig, - "fstab": LanguageFstab, - "etc/fstab": LanguageFstab, + "fstab": shared.LanguageFstab, + "etc/fstab": shared.LanguageFstab, - "wireguard": LanguageWireguard, - "wg": LanguageWireguard, - "languagewireguard": LanguageWireguard, - "host": LanguageHosts, - "hosts": LanguageHosts, - "etc/hosts": LanguageHosts, + "wireguard": shared.LanguageWireguard, + "wg": shared.LanguageWireguard, + "languagewireguard": shared.LanguageWireguard, + "host": shared.LanguageHosts, + "hosts": shared.LanguageHosts, + "etc/hosts": shared.LanguageHosts, - "aliases": LanguageAliases, - "mailaliases": LanguageAliases, - "etc/aliases": LanguageAliases, + "aliases": shared.LanguageAliases, + "mailaliases": shared.LanguageAliases, + "etc/aliases": shared.LanguageAliases, } -var filenameToLanguageMap = map[string]SupportedLanguage{ - "sshd_config": LanguageSSHDConfig, - "sshdconfig": LanguageSSHDConfig, - "sshd": LanguageSSHDConfig, - "sshd_conf": LanguageSSHDConfig, - "sshdconf": LanguageSSHDConfig, +var filenameToLanguageMap = map[string]shared.SupportedLanguage{ + "sshd_config": shared.LanguageSSHDConfig, + "sshdconfig": shared.LanguageSSHDConfig, + "sshd": shared.LanguageSSHDConfig, + "sshd_conf": shared.LanguageSSHDConfig, + "sshdconf": shared.LanguageSSHDConfig, - "ssh_config": LanguageSSHConfig, - "sshconfig": LanguageSSHConfig, - "ssh": LanguageSSHConfig, - "ssh_conf": LanguageSSHConfig, - "sshconf": LanguageSSHConfig, + "ssh_config": shared.LanguageSSHConfig, + "sshconfig": shared.LanguageSSHConfig, + "ssh": shared.LanguageSSHConfig, + "ssh_conf": shared.LanguageSSHConfig, + "sshconf": shared.LanguageSSHConfig, - "fstab": LanguageFstab, + "fstab": shared.LanguageFstab, - "hosts": LanguageHosts, + "hosts": shared.LanguageHosts, - "aliases": LanguageAliases, - "mailaliases": LanguageAliases, + "aliases": shared.LanguageAliases, + "mailaliases": shared.LanguageAliases, } var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`) @@ -103,19 +84,28 @@ func DetectLanguage( content string, advertisedLanguage string, uri protocol.DocumentUri, -) (SupportedLanguage, error) { - if match := typeOverwriteRegex.FindStringSubmatch(content); match != nil { - suggestedLanguage := strings.ToLower(match[1]) +) (shared.SupportedLanguage, error) { + if match := typeOverwriteRegex.FindStringSubmatchIndex(content); match != nil { + text := content[match[0]:match[1]] + language := content[match[2]:match[3]] + suggestedLanguage := strings.ToLower(language) foundLanguage, ok := valueToLanguageMap[suggestedLanguage] + contentUntilMatch := content[:match[0]] + if ok { + line := uint32(utils.CountCharacterOccurrences(contentUntilMatch, '\n')) + shared.LanguagesOverwrites[uri] = shared.LanguageOverwrite{ + Language: foundLanguage, + Raw: text, + Line: line, + Character: uint32(match[0]), + } + return foundLanguage, nil } - matchIndex := strings.Index(content, match[0]) - contentUntilMatch := content[:matchIndex] - return "", common.ParseError{ Line: uint32(utils.CountCharacterOccurrences(contentUntilMatch, '\n')), Err: UnsupportedLanguageError{ @@ -132,22 +122,22 @@ func DetectLanguage( case "file:///etc/ssh/sshd_config": fallthrough case "file:///etc/ssh/ssh_config": - return LanguageSSHDConfig, nil + return shared.LanguageSSHDConfig, nil case "file:///etc/fstab": - return LanguageFstab, nil + return shared.LanguageFstab, nil // Darwin case "file:///private/etc/hosts": fallthrough case "file:///etc/hosts": - return LanguageHosts, nil + return shared.LanguageHosts, nil // Darwin case "file:///private/etc/aliases": fallthrough case "file:///etc/aliases": - return LanguageAliases, nil + return shared.LanguageAliases, nil } filename := path.Base(string(uri)) @@ -157,11 +147,11 @@ func DetectLanguage( } if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) { - return LanguageWireguard, nil + return shared.LanguageWireguard, nil } if strings.HasSuffix(uri, ".ssh/config") { - return LanguageSSHConfig, nil + return shared.LanguageSSHConfig, nil } return "", undetectableError diff --git a/server/root-handler/utils/notification.go b/server/root-handler/utils/notification.go index b55a99b..2153ce3 100644 --- a/server/root-handler/utils/notification.go +++ b/server/root-handler/utils/notification.go @@ -1,25 +1,26 @@ package utils import ( + "config-lsp/root-handler/shared" "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) type lspNotification struct { - uri string + Uri string } type lspDetectedLanguage struct { lspNotification - language string + Language string } func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) { go context.Notify( "$/config-lsp/languageUndetectable", lspNotification{ - uri: string(uri), + Uri: string(uri), }, ) @@ -32,14 +33,14 @@ func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) ) } -func NotifyDetectedLanguage(context *glsp.Context, uri protocol.DocumentUri, language SupportedLanguage) { +func NotifyDetectedLanguage(context *glsp.Context, uri protocol.DocumentUri, language shared.SupportedLanguage) { go context.Notify( "$/config-lsp/detectedLanguage", lspDetectedLanguage{ lspNotification: lspNotification{ - uri: string(uri), + Uri: string(uri), }, - language: string(language), + Language: string(language), }, ) }