mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
refactor(server): Improvements
This commit is contained in:
parent
7cdf25cc3b
commit
9d48d52509
@ -20,17 +20,17 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return hosts.TextDocumentCodeAction(context, params)
|
return hosts.TextDocumentCodeAction(context, params)
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentCodeAction(context, params)
|
return sshconfig.TextDocumentCodeAction(context, params)
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return wireguard.TextDocumentCodeAction(context, params)
|
return wireguard.TextDocumentCodeAction(context, params)
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentCodeAction(context, params)
|
return aliases.TextDocumentCodeAction(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,8 +8,6 @@ import (
|
|||||||
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
||||||
wireguard "config-lsp/handlers/wireguard/lsp"
|
wireguard "config-lsp/handlers/wireguard/lsp"
|
||||||
"config-lsp/root-handler/shared"
|
"config-lsp/root-handler/shared"
|
||||||
"config-lsp/root-handler/utils"
|
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
@ -22,17 +20,17 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return fstab.TextDocumentCompletion(context, params)
|
return fstab.TextDocumentCompletion(context, params)
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentCompletion(context, params)
|
return sshdconfig.TextDocumentCompletion(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentCompletion(context, params)
|
return sshconfig.TextDocumentCompletion(context, params)
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return wireguard.TextDocumentCompletion(context, params)
|
return wireguard.TextDocumentCompletion(context, params)
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return hosts.TextDocumentCompletion(context, params)
|
return hosts.TextDocumentCompletion(context, params)
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentCompletion(context, params)
|
return aliases.TextDocumentCompletion(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,17 +19,17 @@ func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionPa
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentDefinition(context, params)
|
return sshdconfig.TextDocumentDefinition(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentDefinition(context, params)
|
return sshconfig.TextDocumentDefinition(context, params)
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentDefinition(context, params)
|
return aliases.TextDocumentDefinition(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,8 +8,6 @@ import (
|
|||||||
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
||||||
wireguard "config-lsp/handlers/wireguard/lsp"
|
wireguard "config-lsp/handlers/wireguard/lsp"
|
||||||
"config-lsp/root-handler/shared"
|
"config-lsp/root-handler/shared"
|
||||||
"config-lsp/root-handler/utils"
|
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
@ -17,19 +15,19 @@ import (
|
|||||||
func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeTextDocumentParams) error {
|
func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeTextDocumentParams) error {
|
||||||
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
|
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
|
||||||
|
|
||||||
if language == nil {
|
content := params.ContentChanges[0].(protocol.TextDocumentContentChangeEventWhole).Text
|
||||||
content := params.ContentChanges[0].(protocol.TextDocumentContentChangeEventWhole).Text
|
newLanguage, err := initFile(
|
||||||
newLanguage, err := initFile(
|
context,
|
||||||
context,
|
content,
|
||||||
content,
|
params.TextDocument.URI,
|
||||||
params.TextDocument.URI,
|
"",
|
||||||
"",
|
)
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if newLanguage != language {
|
||||||
language = newLanguage
|
language = newLanguage
|
||||||
|
|
||||||
params := &protocol.DidOpenTextDocumentParams{
|
params := &protocol.DidOpenTextDocumentParams{
|
||||||
@ -42,33 +40,33 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return fstab.TextDocumentDidOpen(context, params)
|
return fstab.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentDidOpen(context, params)
|
return sshdconfig.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentDidOpen(context, params)
|
return sshconfig.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return wireguard.TextDocumentDidOpen(context, params)
|
return wireguard.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return hosts.TextDocumentDidOpen(context, params)
|
return hosts.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentDidOpen(context, params)
|
return aliases.TextDocumentDidOpen(context, params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return fstab.TextDocumentDidChange(context, params)
|
return fstab.TextDocumentDidChange(context, params)
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentDidChange(context, params)
|
return sshdconfig.TextDocumentDidChange(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentDidChange(context, params)
|
return sshconfig.TextDocumentDidChange(context, params)
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return wireguard.TextDocumentDidChange(context, params)
|
return wireguard.TextDocumentDidChange(context, params)
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return hosts.TextDocumentDidChange(context, params)
|
return hosts.TextDocumentDidChange(context, params)
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentDidChange(context, params)
|
return aliases.TextDocumentDidChange(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,8 +8,6 @@ import (
|
|||||||
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
||||||
wireguard "config-lsp/handlers/wireguard/lsp"
|
wireguard "config-lsp/handlers/wireguard/lsp"
|
||||||
"config-lsp/root-handler/shared"
|
"config-lsp/root-handler/shared"
|
||||||
"config-lsp/root-handler/utils"
|
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
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.OpenedFiles, params.TextDocument.URI)
|
||||||
|
delete(shared.LanguagesOverwrites, params.TextDocument.URI)
|
||||||
shared.Handler.RemoveDocument(params.TextDocument.URI)
|
shared.Handler.RemoveDocument(params.TextDocument.URI)
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentDidClose(context, params)
|
return sshdconfig.TextDocumentDidClose(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentDidClose(context, params)
|
return sshconfig.TextDocumentDidClose(context, params)
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return fstab.TextDocumentDidClose(context, params)
|
return fstab.TextDocumentDidClose(context, params)
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return wireguard.TextDocumentDidClose(context, params)
|
return wireguard.TextDocumentDidClose(context, params)
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return hosts.TextDocumentDidClose(context, params)
|
return hosts.TextDocumentDidClose(context, params)
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentDidClose(context, params)
|
return aliases.TextDocumentDidClose(context, params)
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
@ -34,17 +34,17 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return fstab.TextDocumentDidOpen(context, params)
|
return fstab.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentDidOpen(context, params)
|
return sshdconfig.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentDidOpen(context, params)
|
return sshconfig.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return wireguard.TextDocumentDidOpen(context, params)
|
return wireguard.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return hosts.TextDocumentDidOpen(context, params)
|
return hosts.TextDocumentDidOpen(context, params)
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentDidOpen(context, params)
|
return aliases.TextDocumentDidOpen(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,13 +56,15 @@ func initFile(
|
|||||||
content string,
|
content string,
|
||||||
uri protocol.DocumentUri,
|
uri protocol.DocumentUri,
|
||||||
advertisedLanguage string,
|
advertisedLanguage string,
|
||||||
) (*utils.SupportedLanguage, error) {
|
) (*shared.SupportedLanguage, error) {
|
||||||
language, err := utils.DetectLanguage(content, advertisedLanguage, uri)
|
language, err := utils.DetectLanguage(content, advertisedLanguage, uri)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.NotifyLanguageUndetectable(context, uri)
|
utils.NotifyLanguageUndetectable(context, uri)
|
||||||
|
|
||||||
return nil, utils.LanguageUndetectableError{}
|
return nil, utils.LanguageUndetectableError{}
|
||||||
|
} else {
|
||||||
|
utils.NotifyDetectedLanguage(context, uri, language)
|
||||||
}
|
}
|
||||||
|
|
||||||
shared.OpenedFiles[uri] = struct{}{}
|
shared.OpenedFiles[uri] = struct{}{}
|
||||||
|
@ -8,8 +8,6 @@ import (
|
|||||||
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
||||||
wireguard "config-lsp/handlers/wireguard/lsp"
|
wireguard "config-lsp/handlers/wireguard/lsp"
|
||||||
"config-lsp/root-handler/shared"
|
"config-lsp/root-handler/shared"
|
||||||
"config-lsp/root-handler/utils"
|
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
@ -22,17 +20,17 @@ func TextDocumentHover(context *glsp.Context, params *protocol.HoverParams) (*pr
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return hosts.TextDocumentHover(context, params)
|
return hosts.TextDocumentHover(context, params)
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentHover(context, params)
|
return sshdconfig.TextDocumentHover(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentHover(context, params)
|
return sshconfig.TextDocumentHover(context, params)
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return fstab.TextDocumentHover(context, params)
|
return fstab.TextDocumentHover(context, params)
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return wireguard.TextDocumentHover(context, params)
|
return wireguard.TextDocumentHover(context, params)
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentHover(context, params)
|
return aliases.TextDocumentHover(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,17 +19,17 @@ func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentPrepareRename(context, params)
|
return sshconfig.TextDocumentPrepareRename(context, params)
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentPrepareRename(context, params)
|
return aliases.TextDocumentPrepareRename(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,17 +21,17 @@ func TextDocumentRangeFormattingFunc(
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentRangeFormatting(context, params)
|
return sshdconfig.TextDocumentRangeFormatting(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentRangeFormatting(context, params)
|
return sshconfig.TextDocumentRangeFormatting(context, params)
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,17 +18,17 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentRename(context, params)
|
return sshconfig.TextDocumentRename(context, params)
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentRename(context, params)
|
return aliases.TextDocumentRename(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,8 +5,6 @@ import (
|
|||||||
sshconfig "config-lsp/handlers/ssh_config/lsp"
|
sshconfig "config-lsp/handlers/ssh_config/lsp"
|
||||||
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
sshdconfig "config-lsp/handlers/sshd_config/lsp"
|
||||||
"config-lsp/root-handler/shared"
|
"config-lsp/root-handler/shared"
|
||||||
"config-lsp/root-handler/utils"
|
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
@ -19,17 +17,17 @@ func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.Signature
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch *language {
|
switch *language {
|
||||||
case utils.LanguageHosts:
|
case shared.LanguageHosts:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageSSHDConfig:
|
case shared.LanguageSSHDConfig:
|
||||||
return sshdconfig.TextDocumentSignatureHelp(context, params)
|
return sshdconfig.TextDocumentSignatureHelp(context, params)
|
||||||
case utils.LanguageSSHConfig:
|
case shared.LanguageSSHConfig:
|
||||||
return sshconfig.TextDocumentSignatureHelp(context, params)
|
return sshconfig.TextDocumentSignatureHelp(context, params)
|
||||||
case utils.LanguageFstab:
|
case shared.LanguageFstab:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageWireguard:
|
case shared.LanguageWireguard:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case utils.LanguageAliases:
|
case shared.LanguageAliases:
|
||||||
return aliases.TextDocumentSignatureHelp(context, params)
|
return aliases.TextDocumentSignatureHelp(context, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
21
server/root-handler/shared/constants.go
Normal file
21
server/root-handler/shared/constants.go
Normal file
@ -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),
|
||||||
|
}
|
16
server/root-handler/shared/indexes.go
Normal file
16
server/root-handler/shared/indexes.go
Normal file
@ -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{}
|
@ -1,8 +1,6 @@
|
|||||||
package shared
|
package shared
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"config-lsp/root-handler/utils"
|
|
||||||
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,20 +9,20 @@ var Handler RootHandler
|
|||||||
var OpenedFiles = make(map[protocol.DocumentUri]struct{})
|
var OpenedFiles = make(map[protocol.DocumentUri]struct{})
|
||||||
|
|
||||||
type RootHandler struct {
|
type RootHandler struct {
|
||||||
languageMap map[protocol.DocumentUri]utils.SupportedLanguage
|
languageMap map[protocol.DocumentUri]SupportedLanguage
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRootHandler() RootHandler {
|
func NewRootHandler() RootHandler {
|
||||||
return 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
|
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]
|
language, found := h.languageMap[uri]
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"config-lsp/root-handler/shared"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FetchAddLanguageActions(uri protocol.DocumentUri) ([]protocol.CodeAction, error) {
|
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
|
kind := protocol.CodeActionKindQuickFix
|
||||||
isPreferred := true
|
isPreferred := true
|
||||||
|
|
||||||
for _, language := range AllSupportedLanguages {
|
for _, language := range shared.AllSupportedLanguages {
|
||||||
actions = append(actions, protocol.CodeAction{
|
actions = append(actions, protocol.CodeAction{
|
||||||
Title: fmt.Sprintf("Use %s for this file", language),
|
Title: fmt.Sprintf("Use %s for this file", language),
|
||||||
Kind: &kind,
|
Kind: &kind,
|
||||||
|
@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
|
"config-lsp/root-handler/shared"
|
||||||
"config-lsp/utils"
|
"config-lsp/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
@ -11,32 +12,12 @@ import (
|
|||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
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 {
|
type UnsupportedLanguageError struct {
|
||||||
SuggestedLanguage string
|
SuggestedLanguage string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e UnsupportedLanguageError) Error() 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{}
|
type LanguageUndetectableError struct{}
|
||||||
@ -45,50 +26,50 @@ func (e LanguageUndetectableError) Error() string {
|
|||||||
return "Please add: '#?lsp.language=<language>' to the top of the file. config-lsp was unable to detect the appropriate language for this file."
|
return "Please add: '#?lsp.language=<language>' to the top of the file. config-lsp was unable to detect the appropriate language for this file."
|
||||||
}
|
}
|
||||||
|
|
||||||
var valueToLanguageMap = map[string]SupportedLanguage{
|
var valueToLanguageMap = map[string]shared.SupportedLanguage{
|
||||||
"sshd_config": LanguageSSHDConfig,
|
"sshd_config": shared.LanguageSSHDConfig,
|
||||||
"sshdconfig": LanguageSSHDConfig,
|
"sshdconfig": shared.LanguageSSHDConfig,
|
||||||
|
|
||||||
"ssh_config": LanguageSSHConfig,
|
"ssh_config": shared.LanguageSSHConfig,
|
||||||
"sshconfig": LanguageSSHConfig,
|
"sshconfig": shared.LanguageSSHConfig,
|
||||||
|
|
||||||
".ssh/config": LanguageSSHConfig,
|
".ssh/config": shared.LanguageSSHConfig,
|
||||||
"~/.ssh/config": LanguageSSHConfig,
|
"~/.ssh/config": shared.LanguageSSHConfig,
|
||||||
|
|
||||||
"fstab": LanguageFstab,
|
"fstab": shared.LanguageFstab,
|
||||||
"etc/fstab": LanguageFstab,
|
"etc/fstab": shared.LanguageFstab,
|
||||||
|
|
||||||
"wireguard": LanguageWireguard,
|
"wireguard": shared.LanguageWireguard,
|
||||||
"wg": LanguageWireguard,
|
"wg": shared.LanguageWireguard,
|
||||||
"languagewireguard": LanguageWireguard,
|
"languagewireguard": shared.LanguageWireguard,
|
||||||
"host": LanguageHosts,
|
"host": shared.LanguageHosts,
|
||||||
"hosts": LanguageHosts,
|
"hosts": shared.LanguageHosts,
|
||||||
"etc/hosts": LanguageHosts,
|
"etc/hosts": shared.LanguageHosts,
|
||||||
|
|
||||||
"aliases": LanguageAliases,
|
"aliases": shared.LanguageAliases,
|
||||||
"mailaliases": LanguageAliases,
|
"mailaliases": shared.LanguageAliases,
|
||||||
"etc/aliases": LanguageAliases,
|
"etc/aliases": shared.LanguageAliases,
|
||||||
}
|
}
|
||||||
|
|
||||||
var filenameToLanguageMap = map[string]SupportedLanguage{
|
var filenameToLanguageMap = map[string]shared.SupportedLanguage{
|
||||||
"sshd_config": LanguageSSHDConfig,
|
"sshd_config": shared.LanguageSSHDConfig,
|
||||||
"sshdconfig": LanguageSSHDConfig,
|
"sshdconfig": shared.LanguageSSHDConfig,
|
||||||
"sshd": LanguageSSHDConfig,
|
"sshd": shared.LanguageSSHDConfig,
|
||||||
"sshd_conf": LanguageSSHDConfig,
|
"sshd_conf": shared.LanguageSSHDConfig,
|
||||||
"sshdconf": LanguageSSHDConfig,
|
"sshdconf": shared.LanguageSSHDConfig,
|
||||||
|
|
||||||
"ssh_config": LanguageSSHConfig,
|
"ssh_config": shared.LanguageSSHConfig,
|
||||||
"sshconfig": LanguageSSHConfig,
|
"sshconfig": shared.LanguageSSHConfig,
|
||||||
"ssh": LanguageSSHConfig,
|
"ssh": shared.LanguageSSHConfig,
|
||||||
"ssh_conf": LanguageSSHConfig,
|
"ssh_conf": shared.LanguageSSHConfig,
|
||||||
"sshconf": LanguageSSHConfig,
|
"sshconf": shared.LanguageSSHConfig,
|
||||||
|
|
||||||
"fstab": LanguageFstab,
|
"fstab": shared.LanguageFstab,
|
||||||
|
|
||||||
"hosts": LanguageHosts,
|
"hosts": shared.LanguageHosts,
|
||||||
|
|
||||||
"aliases": LanguageAliases,
|
"aliases": shared.LanguageAliases,
|
||||||
"mailaliases": LanguageAliases,
|
"mailaliases": shared.LanguageAliases,
|
||||||
}
|
}
|
||||||
|
|
||||||
var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`)
|
var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`)
|
||||||
@ -103,19 +84,28 @@ func DetectLanguage(
|
|||||||
content string,
|
content string,
|
||||||
advertisedLanguage string,
|
advertisedLanguage string,
|
||||||
uri protocol.DocumentUri,
|
uri protocol.DocumentUri,
|
||||||
) (SupportedLanguage, error) {
|
) (shared.SupportedLanguage, error) {
|
||||||
if match := typeOverwriteRegex.FindStringSubmatch(content); match != nil {
|
if match := typeOverwriteRegex.FindStringSubmatchIndex(content); match != nil {
|
||||||
suggestedLanguage := strings.ToLower(match[1])
|
text := content[match[0]:match[1]]
|
||||||
|
language := content[match[2]:match[3]]
|
||||||
|
suggestedLanguage := strings.ToLower(language)
|
||||||
|
|
||||||
foundLanguage, ok := valueToLanguageMap[suggestedLanguage]
|
foundLanguage, ok := valueToLanguageMap[suggestedLanguage]
|
||||||
|
|
||||||
|
contentUntilMatch := content[:match[0]]
|
||||||
|
|
||||||
if ok {
|
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
|
return foundLanguage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
matchIndex := strings.Index(content, match[0])
|
|
||||||
contentUntilMatch := content[:matchIndex]
|
|
||||||
|
|
||||||
return "", common.ParseError{
|
return "", common.ParseError{
|
||||||
Line: uint32(utils.CountCharacterOccurrences(contentUntilMatch, '\n')),
|
Line: uint32(utils.CountCharacterOccurrences(contentUntilMatch, '\n')),
|
||||||
Err: UnsupportedLanguageError{
|
Err: UnsupportedLanguageError{
|
||||||
@ -132,22 +122,22 @@ func DetectLanguage(
|
|||||||
case "file:///etc/ssh/sshd_config":
|
case "file:///etc/ssh/sshd_config":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "file:///etc/ssh/ssh_config":
|
case "file:///etc/ssh/ssh_config":
|
||||||
return LanguageSSHDConfig, nil
|
return shared.LanguageSSHDConfig, nil
|
||||||
|
|
||||||
case "file:///etc/fstab":
|
case "file:///etc/fstab":
|
||||||
return LanguageFstab, nil
|
return shared.LanguageFstab, nil
|
||||||
|
|
||||||
// Darwin
|
// Darwin
|
||||||
case "file:///private/etc/hosts":
|
case "file:///private/etc/hosts":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "file:///etc/hosts":
|
case "file:///etc/hosts":
|
||||||
return LanguageHosts, nil
|
return shared.LanguageHosts, nil
|
||||||
|
|
||||||
// Darwin
|
// Darwin
|
||||||
case "file:///private/etc/aliases":
|
case "file:///private/etc/aliases":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "file:///etc/aliases":
|
case "file:///etc/aliases":
|
||||||
return LanguageAliases, nil
|
return shared.LanguageAliases, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := path.Base(string(uri))
|
filename := path.Base(string(uri))
|
||||||
@ -157,11 +147,11 @@ func DetectLanguage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) {
|
if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) {
|
||||||
return LanguageWireguard, nil
|
return shared.LanguageWireguard, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasSuffix(uri, ".ssh/config") {
|
if strings.HasSuffix(uri, ".ssh/config") {
|
||||||
return LanguageSSHConfig, nil
|
return shared.LanguageSSHConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", undetectableError
|
return "", undetectableError
|
||||||
|
@ -1,25 +1,26 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"config-lsp/root-handler/shared"
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
type lspNotification struct {
|
type lspNotification struct {
|
||||||
uri string
|
Uri string
|
||||||
}
|
}
|
||||||
|
|
||||||
type lspDetectedLanguage struct {
|
type lspDetectedLanguage struct {
|
||||||
lspNotification
|
lspNotification
|
||||||
|
|
||||||
language string
|
Language string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) {
|
func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) {
|
||||||
go context.Notify(
|
go context.Notify(
|
||||||
"$/config-lsp/languageUndetectable",
|
"$/config-lsp/languageUndetectable",
|
||||||
lspNotification{
|
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(
|
go context.Notify(
|
||||||
"$/config-lsp/detectedLanguage",
|
"$/config-lsp/detectedLanguage",
|
||||||
lspDetectedLanguage{
|
lspDetectedLanguage{
|
||||||
lspNotification: lspNotification{
|
lspNotification: lspNotification{
|
||||||
uri: string(uri),
|
Uri: string(uri),
|
||||||
},
|
},
|
||||||
language: string(language),
|
Language: string(language),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user