From ec4deeb59f3392b6a7dc9eae7c6bb94734fe214b Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:24:00 +0200 Subject: [PATCH 1/9] feat(server): Detect language based on the filename --- server/root-handler/lsp-utils.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/server/root-handler/lsp-utils.go b/server/root-handler/lsp-utils.go index 983faaf..b0cc79a 100644 --- a/server/root-handler/lsp-utils.go +++ b/server/root-handler/lsp-utils.go @@ -4,6 +4,7 @@ import ( "config-lsp/common" "config-lsp/utils" "fmt" + "path" "regexp" "strings" @@ -78,6 +79,27 @@ var valueToLanguageMap = map[string]SupportedLanguage{ "etc/aliases": LanguageAliases, } +var filenameToLanguageMap = map[string]SupportedLanguage{ + "sshd_config": LanguageSSHDConfig, + "sshdconfig": LanguageSSHDConfig, + "sshd": LanguageSSHDConfig, + + "ssh_config": LanguageSSHConfig, + "sshconfig": LanguageSSHConfig, + + "fstab": LanguageFstab, + + "hosts": LanguageHosts, + + "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$`) @@ -137,6 +159,12 @@ func DetectLanguage( return LanguageAliases, nil } + filename := path.Base(string(uri)) + + if language, found := filenameToLanguageMap[filename]; found { + return language, nil + } + if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) { return LanguageWireguard, nil } 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 2/9] 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 { From 87c68de419b6cb44c4d6db347a4759739ff5b736 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:47:53 +0200 Subject: [PATCH 3/9] docs: Add installation instructions --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8c24ad5..c6debff 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,52 @@ You are welcome to request any config file, as far as it's fairly well known. ## Installation +### VS Code Extension + +[Install the extension from the marketplace](https://marketplace.visualstudio.com/items?itemName=myzel394.config-lsp) + +Alternatively, you can also manually install the extension: + +1. Download the latest extension version from the [release page](https://github.com/Myzel394/config-lsp/releases) - You can find the extension under the "assets" section. The filename ends with `.vsix` +2. Open VS Code +3. Open the extensions sidebar +4. In the top bar, click on the three dots and select "Install from VSIX..." +5. Select the just downloaded `.vsix` file +6. You may need to restart VS Code +7. Enjoy! + +### Manual installation + +To use `config-lsp` in any other editor, you'll need to install it manually. +Don't worry, it's easy! + +#### Installing the latest Binary + +##### Brew + +```sh +brew install myzel394/formulae/config-lsp +``` + +##### Manual Binary + Download the latest binary from the [releases page](https://github.com/Myzel394/config-lsp/releases) and put it in your PATH. -Follow the instructions for your editor below. +##### Compiling -### Neovim installation +You can either compile the binary using go: + +```sh +go build -o config-lsp +``` + +or build it using Nix: + +```sh +nix flake build +``` + +#### Neovim installation Using [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) you can add `config-lsp` by adding the following to your `lsp.lua` (filename might differ): @@ -57,14 +98,6 @@ end lspconfig.config_lsp.setup {} ````` -### VS Code installation - -The VS Code extension is currently in development. An official extension will be released soon. - -However, at the moment you can also compile the extension yourself and run it in development mode. - -**Do not create an extension and publish it yourself. Contribute to the official extension instead.** - ## Supporting config-lsp You can either contribute to the project, [see CONTRIBUTING.md](CONTRIBUTING.md), or you can sponsor me via [GitHub Sponsors](https://github.com/sponsors/Myzel394) or via [crypto currencies](https://github.com/Myzel394/contact-me?tab=readme-ov-file#donations). From 57ce6c1f4f12074be37f23b837267d307d565b9c Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:45:46 +0200 Subject: [PATCH 4/9] refactor(server): Improve root handler structure --- server/root-handler/handler.go | 29 +++++++----- .../{ => lsp}/text-document-code-action.go | 26 +++++----- .../{ => lsp}/text-document-completion.go | 26 +++++----- .../{ => lsp}/text-document-definition.go | 26 +++++----- .../{ => lsp}/text-document-did-change.go | 30 ++++++------ .../{ => lsp}/text-document-did-close.go | 30 +++++------- .../{ => lsp}/text-document-did-open.go | 47 ++++++------------- .../{ => lsp}/text-document-hover.go | 26 +++++----- .../{ => lsp}/text-document-prepare-rename.go | 26 +++++----- .../text-document-range-formatting.go | 26 +++++----- .../{ => lsp}/text-document-rename.go | 26 +++++----- .../{ => lsp}/text-document-signature-help.go | 26 +++++----- .../{ => lsp}/workspace-execute-command.go | 2 +- server/root-handler/shared.go | 5 -- server/root-handler/{ => shared}/singleton.go | 16 ++++--- .../root-handler/{ => utils}/code-actions.go | 4 +- .../language-detection.go} | 11 +---- server/root-handler/utils/notification.go | 45 ++++++++++++++++++ 18 files changed, 207 insertions(+), 220 deletions(-) rename server/root-handler/{ => lsp}/text-document-code-action.go (65%) rename server/root-handler/{ => lsp}/text-document-completion.go (72%) rename server/root-handler/{ => lsp}/text-document-definition.go (64%) rename server/root-handler/{ => lsp}/text-document-did-change.go (77%) rename server/root-handler/{ => lsp}/text-document-did-close.go (65%) rename server/root-handler/{ => lsp}/text-document-did-open.go (66%) rename server/root-handler/{ => lsp}/text-document-hover.go (71%) rename server/root-handler/{ => lsp}/text-document-prepare-rename.go (61%) rename server/root-handler/{ => lsp}/text-document-range-formatting.go (63%) rename server/root-handler/{ => lsp}/text-document-rename.go (61%) rename server/root-handler/{ => lsp}/text-document-signature-help.go (67%) rename server/root-handler/{ => lsp}/workspace-execute-command.go (98%) delete mode 100644 server/root-handler/shared.go rename server/root-handler/{ => shared}/singleton.go (60%) rename server/root-handler/{ => utils}/code-actions.go (91%) rename server/root-handler/{lsp-utils.go => utils/language-detection.go} (93%) create mode 100644 server/root-handler/utils/notification.go diff --git a/server/root-handler/handler.go b/server/root-handler/handler.go index 81e36cd..abf81d1 100644 --- a/server/root-handler/handler.go +++ b/server/root-handler/handler.go @@ -1,6 +1,8 @@ package roothandler import ( + "config-lsp/root-handler/lsp" + "config-lsp/root-handler/shared" "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" @@ -17,24 +19,25 @@ var lspHandler protocol.Handler // The root handler which handles all the LSP requests and then forwards them to the appropriate handler func SetUpRootHandler() { - rootHandler = NewRootHandler() + shared.Handler = shared.NewRootHandler() + lspHandler = protocol.Handler{ Initialize: initialize, Initialized: initialized, Shutdown: shutdown, SetTrace: setTrace, - TextDocumentDidOpen: TextDocumentDidOpen, - TextDocumentDidChange: TextDocumentDidChange, - TextDocumentCompletion: TextDocumentCompletion, - TextDocumentHover: TextDocumentHover, - TextDocumentDidClose: TextDocumentDidClose, - TextDocumentCodeAction: TextDocumentCodeAction, - TextDocumentDefinition: TextDocumentDefinition, - WorkspaceExecuteCommand: WorkspaceExecuteCommand, - TextDocumentRename: TextDocumentRename, - TextDocumentPrepareRename: TextDocumentPrepareRename, - TextDocumentSignatureHelp: TextDocumentSignatureHelp, - TextDocumentRangeFormatting: TextDocumentRangeFormattingFunc, + TextDocumentDidOpen: lsp.TextDocumentDidOpen, + TextDocumentDidChange: lsp.TextDocumentDidChange, + TextDocumentCompletion: lsp.TextDocumentCompletion, + TextDocumentHover: lsp.TextDocumentHover, + TextDocumentDidClose: lsp.TextDocumentDidClose, + TextDocumentCodeAction: lsp.TextDocumentCodeAction, + TextDocumentDefinition: lsp.TextDocumentDefinition, + WorkspaceExecuteCommand: lsp.WorkspaceExecuteCommand, + TextDocumentRename: lsp.TextDocumentRename, + TextDocumentPrepareRename: lsp.TextDocumentPrepareRename, + TextDocumentSignatureHelp: lsp.TextDocumentSignatureHelp, + TextDocumentRangeFormatting: lsp.TextDocumentRangeFormattingFunc, } server := server.NewServer(&lspHandler, lsName, false) diff --git a/server/root-handler/text-document-code-action.go b/server/root-handler/lsp/text-document-code-action.go similarity index 65% rename from server/root-handler/text-document-code-action.go rename to server/root-handler/lsp/text-document-code-action.go index 737911e..0cda9b8 100644 --- a/server/root-handler/text-document-code-action.go +++ b/server/root-handler/lsp/text-document-code-action.go @@ -1,40 +1,36 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" hosts "config-lsp/handlers/hosts/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp" wireguard "config-lsp/handlers/wireguard/lsp" + "config-lsp/root-handler/shared" + utils "config-lsp/root-handler/utils" "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionParams) (any, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return fetchAddLanguageActions(params.TextDocument.URI) + return utils.FetchAddLanguageActions(params.TextDocument.URI) } switch *language { - case LanguageFstab: + case utils.LanguageFstab: return nil, nil - case LanguageHosts: + case utils.LanguageHosts: return hosts.TextDocumentCodeAction(context, params) - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return nil, nil - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentCodeAction(context, params) - case LanguageWireguard: + case utils.LanguageWireguard: return wireguard.TextDocumentCodeAction(context, params) - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentCodeAction(context, params) } diff --git a/server/root-handler/text-document-completion.go b/server/root-handler/lsp/text-document-completion.go similarity index 72% rename from server/root-handler/text-document-completion.go rename to server/root-handler/lsp/text-document-completion.go index a82a4eb..062aaee 100644 --- a/server/root-handler/text-document-completion.go +++ b/server/root-handler/lsp/text-document-completion.go @@ -1,4 +1,4 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" @@ -7,36 +7,32 @@ import ( sshconfig "config-lsp/handlers/ssh_config/lsp" 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" ) func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionParams) (any, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return nil, undetectableError.Err + return nil, nil } switch *language { - case LanguageFstab: + case utils.LanguageFstab: return fstab.TextDocumentCompletion(context, params) - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentCompletion(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentCompletion(context, params) - case LanguageWireguard: + case utils.LanguageWireguard: return wireguard.TextDocumentCompletion(context, params) - case LanguageHosts: + case utils.LanguageHosts: return hosts.TextDocumentCompletion(context, params) - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentCompletion(context, params) } diff --git a/server/root-handler/text-document-definition.go b/server/root-handler/lsp/text-document-definition.go similarity index 64% rename from server/root-handler/text-document-definition.go rename to server/root-handler/lsp/text-document-definition.go index 593cfad..14aea5e 100644 --- a/server/root-handler/text-document-definition.go +++ b/server/root-handler/lsp/text-document-definition.go @@ -1,39 +1,35 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" 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" ) func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionParams) (any, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return nil, undetectableError.Err + return nil, utils.LanguageUndetectableError{} } switch *language { - case LanguageHosts: + case utils.LanguageHosts: return nil, nil - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentDefinition(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentDefinition(context, params) - case LanguageFstab: + case utils.LanguageFstab: return nil, nil - case LanguageWireguard: + case utils.LanguageWireguard: return nil, nil - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentDefinition(context, params) } diff --git a/server/root-handler/text-document-did-change.go b/server/root-handler/lsp/text-document-did-change.go similarity index 77% rename from server/root-handler/text-document-did-change.go rename to server/root-handler/lsp/text-document-did-change.go index 27c79e9..781f32a 100644 --- a/server/root-handler/text-document-did-change.go +++ b/server/root-handler/lsp/text-document-did-change.go @@ -1,4 +1,4 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" @@ -7,13 +7,15 @@ import ( sshconfig "config-lsp/handlers/ssh_config/lsp" 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" ) func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeTextDocumentParams) error { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { content := params.ContentChanges[0].(protocol.TextDocumentContentChangeEventWhole).Text @@ -40,33 +42,33 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText } switch *language { - case LanguageFstab: + case utils.LanguageFstab: return fstab.TextDocumentDidOpen(context, params) - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentDidOpen(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentDidOpen(context, params) - case LanguageWireguard: + case utils.LanguageWireguard: return wireguard.TextDocumentDidOpen(context, params) - case LanguageHosts: + case utils.LanguageHosts: return hosts.TextDocumentDidOpen(context, params) - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentDidOpen(context, params) } } switch *language { - case LanguageFstab: + case utils.LanguageFstab: return fstab.TextDocumentDidChange(context, params) - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentDidChange(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentDidChange(context, params) - case LanguageWireguard: + case utils.LanguageWireguard: return wireguard.TextDocumentDidChange(context, params) - case LanguageHosts: + case utils.LanguageHosts: return hosts.TextDocumentDidChange(context, params) - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentDidChange(context, params) } diff --git a/server/root-handler/text-document-did-close.go b/server/root-handler/lsp/text-document-did-close.go similarity index 65% rename from server/root-handler/text-document-did-close.go rename to server/root-handler/lsp/text-document-did-close.go index 724d9a6..fef74f7 100644 --- a/server/root-handler/text-document-did-close.go +++ b/server/root-handler/lsp/text-document-did-close.go @@ -1,4 +1,4 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" @@ -7,39 +7,35 @@ import ( sshconfig "config-lsp/handlers/ssh_config/lsp" 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" ) func TextDocumentDidClose(context *glsp.Context, params *protocol.DidCloseTextDocumentParams) error { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return undetectableError.Err + return nil } - delete(openedFiles, params.TextDocument.URI) - rootHandler.RemoveDocument(params.TextDocument.URI) + delete(shared.OpenedFiles, params.TextDocument.URI) + shared.Handler.RemoveDocument(params.TextDocument.URI) switch *language { - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentDidClose(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentDidClose(context, params) - case LanguageFstab: + case utils.LanguageFstab: return fstab.TextDocumentDidClose(context, params) - case LanguageWireguard: + case utils.LanguageWireguard: return wireguard.TextDocumentDidClose(context, params) - case LanguageHosts: + case utils.LanguageHosts: return hosts.TextDocumentDidClose(context, params) - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentDidClose(context, params) default: } diff --git a/server/root-handler/text-document-did-open.go b/server/root-handler/lsp/text-document-did-open.go similarity index 66% rename from server/root-handler/text-document-did-open.go rename to server/root-handler/lsp/text-document-did-open.go index abe9a2b..0690c9e 100644 --- a/server/root-handler/text-document-did-open.go +++ b/server/root-handler/lsp/text-document-did-open.go @@ -1,7 +1,9 @@ -package roothandler +package lsp import ( "config-lsp/common" + "config-lsp/root-handler/shared" + "config-lsp/root-handler/utils" "fmt" aliases "config-lsp/handlers/aliases/lsp" @@ -32,60 +34,41 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu } switch *language { - case LanguageFstab: + case utils.LanguageFstab: return fstab.TextDocumentDidOpen(context, params) - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentDidOpen(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentDidOpen(context, params) - case LanguageWireguard: + case utils.LanguageWireguard: return wireguard.TextDocumentDidOpen(context, params) - case LanguageHosts: + case utils.LanguageHosts: return hosts.TextDocumentDidOpen(context, params) - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentDidOpen(context, params) } panic(fmt.Sprintf("unexpected roothandler.SupportedLanguage: %#v", language)) } -func showParseError( - context *glsp.Context, - uri protocol.DocumentUri, - err common.ParseError, -) { - context.Notify( - "window/showMessage", - protocol.ShowMessageParams{ - Type: protocol.MessageTypeError, - Message: err.Err.Error(), - }, - ) -} - func initFile( context *glsp.Context, content string, uri protocol.DocumentUri, advertisedLanguage string, -) (*SupportedLanguage, error) { - language, err := DetectLanguage(content, advertisedLanguage, uri) +) (*utils.SupportedLanguage, error) { + language, err := utils.DetectLanguage(content, advertisedLanguage, uri) if err != nil { - parseError := err.(common.ParseError) - showParseError( - context, - uri, - parseError, - ) + utils.NotifyLanguageUndetectable(context, uri) - return nil, parseError.Err + return nil, utils.LanguageUndetectableError{} } - openedFiles[uri] = struct{}{} + shared.OpenedFiles[uri] = struct{}{} // Everything okay, now we can handle the file - rootHandler.AddDocument(uri, language) + shared.Handler.AddDocument(uri, language) return &language, nil } diff --git a/server/root-handler/text-document-hover.go b/server/root-handler/lsp/text-document-hover.go similarity index 71% rename from server/root-handler/text-document-hover.go rename to server/root-handler/lsp/text-document-hover.go index 72f6c65..fb15c0e 100644 --- a/server/root-handler/text-document-hover.go +++ b/server/root-handler/lsp/text-document-hover.go @@ -1,4 +1,4 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" @@ -7,36 +7,32 @@ import ( sshconfig "config-lsp/handlers/ssh_config/lsp" 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" ) func TextDocumentHover(context *glsp.Context, params *protocol.HoverParams) (*protocol.Hover, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return nil, undetectableError.Err + return nil, nil } switch *language { - case LanguageHosts: + case utils.LanguageHosts: return hosts.TextDocumentHover(context, params) - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentHover(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentHover(context, params) - case LanguageFstab: + case utils.LanguageFstab: return fstab.TextDocumentHover(context, params) - case LanguageWireguard: + case utils.LanguageWireguard: return wireguard.TextDocumentHover(context, params) - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentHover(context, params) } diff --git a/server/root-handler/text-document-prepare-rename.go b/server/root-handler/lsp/text-document-prepare-rename.go similarity index 61% rename from server/root-handler/text-document-prepare-rename.go rename to server/root-handler/lsp/text-document-prepare-rename.go index 19b6fd4..ac92eb6 100644 --- a/server/root-handler/text-document-prepare-rename.go +++ b/server/root-handler/lsp/text-document-prepare-rename.go @@ -1,8 +1,10 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp" + "config-lsp/root-handler/shared" + "config-lsp/root-handler/utils" "github.com/tliron/glsp" @@ -10,30 +12,24 @@ import ( ) func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRenameParams) (any, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return nil, undetectableError.Err + return nil, utils.LanguageUndetectableError{} } switch *language { - case LanguageHosts: + case utils.LanguageHosts: return nil, nil - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return nil, nil - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentPrepareRename(context, params) - case LanguageFstab: + case utils.LanguageFstab: return nil, nil - case LanguageWireguard: + case utils.LanguageWireguard: return nil, nil - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentPrepareRename(context, params) } diff --git a/server/root-handler/text-document-range-formatting.go b/server/root-handler/lsp/text-document-range-formatting.go similarity index 63% rename from server/root-handler/text-document-range-formatting.go rename to server/root-handler/lsp/text-document-range-formatting.go index 5f917f2..6604292 100644 --- a/server/root-handler/text-document-range-formatting.go +++ b/server/root-handler/lsp/text-document-range-formatting.go @@ -1,8 +1,10 @@ -package roothandler +package lsp 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" @@ -12,30 +14,24 @@ func TextDocumentRangeFormattingFunc( context *glsp.Context, params *protocol.DocumentRangeFormattingParams, ) ([]protocol.TextEdit, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return nil, undetectableError.Err + return nil, utils.LanguageUndetectableError{} } switch *language { - case LanguageHosts: + case utils.LanguageHosts: return nil, nil - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentRangeFormatting(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentRangeFormatting(context, params) - case LanguageFstab: + case utils.LanguageFstab: return nil, nil - case LanguageWireguard: + case utils.LanguageWireguard: return nil, nil - case LanguageAliases: + case utils.LanguageAliases: return nil, nil } diff --git a/server/root-handler/text-document-rename.go b/server/root-handler/lsp/text-document-rename.go similarity index 61% rename from server/root-handler/text-document-rename.go rename to server/root-handler/lsp/text-document-rename.go index 084d4f8..fa779a2 100644 --- a/server/root-handler/text-document-rename.go +++ b/server/root-handler/lsp/text-document-rename.go @@ -1,38 +1,34 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp" + "config-lsp/root-handler/shared" + "config-lsp/root-handler/utils" "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return nil, undetectableError.Err + return nil, utils.LanguageUndetectableError{} } switch *language { - case LanguageHosts: + case utils.LanguageHosts: return nil, nil - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return nil, nil - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentRename(context, params) - case LanguageFstab: + case utils.LanguageFstab: return nil, nil - case LanguageWireguard: + case utils.LanguageWireguard: return nil, nil - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentRename(context, params) } diff --git a/server/root-handler/text-document-signature-help.go b/server/root-handler/lsp/text-document-signature-help.go similarity index 67% rename from server/root-handler/text-document-signature-help.go rename to server/root-handler/lsp/text-document-signature-help.go index a144644..02e88c7 100644 --- a/server/root-handler/text-document-signature-help.go +++ b/server/root-handler/lsp/text-document-signature-help.go @@ -1,39 +1,35 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" 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" ) func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.SignatureHelpParams) (*protocol.SignatureHelp, error) { - language := rootHandler.GetLanguageForDocument(params.TextDocument.URI) + language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - showParseError( - context, - params.TextDocument.URI, - undetectableError, - ) - - return nil, undetectableError.Err + return nil, nil } switch *language { - case LanguageHosts: + case utils.LanguageHosts: return nil, nil - case LanguageSSHDConfig: + case utils.LanguageSSHDConfig: return sshdconfig.TextDocumentSignatureHelp(context, params) - case LanguageSSHConfig: + case utils.LanguageSSHConfig: return sshconfig.TextDocumentSignatureHelp(context, params) - case LanguageFstab: + case utils.LanguageFstab: return nil, nil - case LanguageWireguard: + case utils.LanguageWireguard: return nil, nil - case LanguageAliases: + case utils.LanguageAliases: return aliases.TextDocumentSignatureHelp(context, params) } diff --git a/server/root-handler/workspace-execute-command.go b/server/root-handler/lsp/workspace-execute-command.go similarity index 98% rename from server/root-handler/workspace-execute-command.go rename to server/root-handler/lsp/workspace-execute-command.go index bd04e34..4deb017 100644 --- a/server/root-handler/workspace-execute-command.go +++ b/server/root-handler/lsp/workspace-execute-command.go @@ -1,4 +1,4 @@ -package roothandler +package lsp import ( aliases "config-lsp/handlers/aliases/lsp" diff --git a/server/root-handler/shared.go b/server/root-handler/shared.go deleted file mode 100644 index e0575e0..0000000 --- a/server/root-handler/shared.go +++ /dev/null @@ -1,5 +0,0 @@ -package roothandler - -import protocol "github.com/tliron/glsp/protocol_3_16" - -var openedFiles = make(map[protocol.DocumentUri]struct{}) diff --git a/server/root-handler/singleton.go b/server/root-handler/shared/singleton.go similarity index 60% rename from server/root-handler/singleton.go rename to server/root-handler/shared/singleton.go index f06aae0..05ac59c 100644 --- a/server/root-handler/singleton.go +++ b/server/root-handler/shared/singleton.go @@ -1,26 +1,30 @@ -package roothandler +package shared import ( + "config-lsp/root-handler/utils" + protocol "github.com/tliron/glsp/protocol_3_16" ) -var rootHandler RootHandler +var Handler RootHandler + +var OpenedFiles = make(map[protocol.DocumentUri]struct{}) type RootHandler struct { - languageMap map[protocol.DocumentUri]SupportedLanguage + languageMap map[protocol.DocumentUri]utils.SupportedLanguage } func NewRootHandler() RootHandler { return RootHandler{ - languageMap: make(map[protocol.DocumentUri]SupportedLanguage), + languageMap: make(map[protocol.DocumentUri]utils.SupportedLanguage), } } -func (h *RootHandler) AddDocument(uri protocol.DocumentUri, language SupportedLanguage) { +func (h *RootHandler) AddDocument(uri protocol.DocumentUri, language utils.SupportedLanguage) { h.languageMap[uri] = language } -func (h *RootHandler) GetLanguageForDocument(uri protocol.DocumentUri) *SupportedLanguage { +func (h *RootHandler) GetLanguageForDocument(uri protocol.DocumentUri) *utils.SupportedLanguage { language, found := h.languageMap[uri] if !found { diff --git a/server/root-handler/code-actions.go b/server/root-handler/utils/code-actions.go similarity index 91% rename from server/root-handler/code-actions.go rename to server/root-handler/utils/code-actions.go index 61a530e..193cc61 100644 --- a/server/root-handler/code-actions.go +++ b/server/root-handler/utils/code-actions.go @@ -1,4 +1,4 @@ -package roothandler +package utils import ( "fmt" @@ -6,7 +6,7 @@ import ( 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)) kind := protocol.CodeActionKindQuickFix diff --git a/server/root-handler/lsp-utils.go b/server/root-handler/utils/language-detection.go similarity index 93% rename from server/root-handler/lsp-utils.go rename to server/root-handler/utils/language-detection.go index 0e9ce5f..679349f 100644 --- a/server/root-handler/lsp-utils.go +++ b/server/root-handler/utils/language-detection.go @@ -1,4 +1,4 @@ -package roothandler +package utils import ( "config-lsp/common" @@ -31,15 +31,6 @@ var AllSupportedLanguages = []string{ string(LanguageAliases), } -type FatalFileNotReadableError struct { - FileURI protocol.DocumentUri - Err error -} - -func (e FatalFileNotReadableError) Error() string { - return fmt.Sprintf("Fatal error! config-lsp was unable to read the file (%s); error: %s", e.FileURI, e.Err.Error()) -} - type UnsupportedLanguageError struct { SuggestedLanguage string } diff --git a/server/root-handler/utils/notification.go b/server/root-handler/utils/notification.go new file mode 100644 index 0000000..b55a99b --- /dev/null +++ b/server/root-handler/utils/notification.go @@ -0,0 +1,45 @@ +package utils + +import ( + "github.com/tliron/glsp" + protocol "github.com/tliron/glsp/protocol_3_16" +) + +type lspNotification struct { + uri string +} + +type lspDetectedLanguage struct { + lspNotification + + language string +} + +func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) { + go context.Notify( + "$/config-lsp/languageUndetectable", + lspNotification{ + uri: string(uri), + }, + ) + + go context.Notify( + "window/showMessage", + protocol.ShowMessageParams{ + Type: protocol.MessageTypeError, + Message: "config-lsp was unable to detect the appropriate language for this file. Please add: '#?lsp.language='.", + }, + ) +} + +func NotifyDetectedLanguage(context *glsp.Context, uri protocol.DocumentUri, language SupportedLanguage) { + go context.Notify( + "$/config-lsp/detectedLanguage", + lspDetectedLanguage{ + lspNotification: lspNotification{ + uri: string(uri), + }, + language: string(language), + }, + ) +} From f56951cb8df9e30248460a0ca2e822b439ee3714 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 26 Oct 2024 21:05:36 +0200 Subject: [PATCH 5/9] feat(server): Show version when asked --- server/main.go | 9 +++++++++ server/root-handler/common.go | 5 +++++ server/root-handler/handler.go | 6 +----- 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 server/root-handler/common.go diff --git a/server/main.go b/server/main.go index 763d422..9e9f2a2 100644 --- a/server/main.go +++ b/server/main.go @@ -2,6 +2,8 @@ package main import ( roothandler "config-lsp/root-handler" + "fmt" + "os" "github.com/tliron/commonlog" @@ -11,6 +13,13 @@ import ( ) func main() { + if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "version") { + fmt.Println(roothandler.Version) + + os.Exit(0) + return + } + // This increases logging verbosity (optional) commonlog.Configure(1, nil) diff --git a/server/root-handler/common.go b/server/root-handler/common.go new file mode 100644 index 0000000..031ff10 --- /dev/null +++ b/server/root-handler/common.go @@ -0,0 +1,5 @@ +package roothandler + +// The comment below at the end of the line is required for the CI:CD to work. +// Do not remove it +var Version = "0.1.1" // CI:CD-VERSION diff --git a/server/root-handler/handler.go b/server/root-handler/handler.go index abf81d1..85af3fa 100644 --- a/server/root-handler/handler.go +++ b/server/root-handler/handler.go @@ -11,10 +11,6 @@ import ( const lsName = "config-lsp" -// The comment below at the end of the line is required for the CI:CD to work. -// Do not remove it -var version = "0.1.1" // CI:CD-VERSION - var lspHandler protocol.Handler // The root handler which handles all the LSP requests and then forwards them to the appropriate handler @@ -75,7 +71,7 @@ func initialize(context *glsp.Context, params *protocol.InitializeParams) (any, Capabilities: capabilities, ServerInfo: &protocol.InitializeResultServerInfo{ Name: lsName, - Version: &version, + Version: &Version, }, }, nil } From 7cdf25cc3b4996f77f32929f31ae6d8589c29510 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 27 Oct 2024 12:09:13 +0100 Subject: [PATCH 6/9] fix(extension): Add languages natively to VS Code --- vs-code-extension/package.json | 62 +++++++++++++++++-- .../src/events/on-undetectable.ts | 34 ++++++++++ vs-code-extension/src/events/types.ts | 6 ++ vs-code-extension/src/extension.ts | 62 ++++++++----------- 4 files changed, 122 insertions(+), 42 deletions(-) create mode 100644 vs-code-extension/src/events/on-undetectable.ts create mode 100644 vs-code-extension/src/events/types.ts diff --git a/vs-code-extension/package.json b/vs-code-extension/package.json index 5705ae6..609b7f6 100644 --- a/vs-code-extension/package.json +++ b/vs-code-extension/package.json @@ -13,6 +13,62 @@ "Formatters" ], "preview": true, + "contributes": { + "languages": [ + { + "id": "sshconfig", + "extensions": ["sshconfig", "ssh_config"], + "aliases": ["SSH Config", "sshconfig"], + "filenames": ["sshconfig", "ssh_config"], + "filenamePatterns": ["~/.ssh/config", "**/sshconfig", "**/ssh_config"] + }, + { + "id": "sshdconfig", + "extensions": ["sshdconfig", "sshd_config"], + "aliases": ["SSH Daemon Config", "sshdconfig"], + "filenames": ["sshdconfig", "sshd_config"], + "filenamePatterns": ["/etc/ssh/sshd_config", "**/sshdconfig", "**/sshd_config"] + }, + { + "id": "aliases", + "extensions": ["aliases", "mailaliases"], + "aliases": ["Mail Aliases", "aliases", "mailaliases"], + "filenames": ["aliases", "mailaliases"], + "filenamePatterns": ["/etc/aliases", "**/aliases", "**/mailaliases"] + }, + { + "id": "fstab", + "extensions": ["fstab"], + "aliases": ["fstab"], + "filenames": ["fstab"], + "filenamePatterns": ["/etc/fstab", "**/fstab", "**/etc/fstab"] + }, + { + "id": "hosts", + "extensions": ["hosts"], + "aliases": ["hosts"], + "filenames": ["hosts"], + "filenamePatterns": ["/etc/hosts", "**/hosts", "**/etc/hosts"] + }, + { + "id": "wireguard", + "extensions": ["wireguard", "wg"], + "aliases": ["WireGuard", "wireguard", "wg"], + "filenames": ["wireguard", "wg0.conf", "wg1.conf", "wg0", "wg1"], + "filenamePatterns": ["/etc/wireguard/*.conf", "**/wireguard", "**/wireguard.conf"] + } + ] + }, + "activationEvents": [ + "onLanguage:plaintext", + "onLanguage:yaml", + "onLanguage:sshconfig", + "onLanguage:sshdconfig", + "onLanguage:aliases", + "onLanguage:fstab", + "onLanguage:hosts", + "onLanguage:wireguard" + ], "sponsor": { "url": "https://github.com/Myzel394/contact-me" }, @@ -50,16 +106,12 @@ "engines": { "vscode": "^1.74.0" }, - "activationEvents": [ - "onLanguage:plaintext", - "onLanguage:yaml" - ], "main": "./out/extension.js", "scripts": { "compile": "node esbuild.js", "compile:prod": "node esbuild.js --production", "watch": "tsc -b -w", - "lint": "eslint ./src --ext .ts,.tsx" + "lint": "eslint ./src" }, "dependencies": { "vscode-languageclient": "^9.0.1", diff --git a/vs-code-extension/src/events/on-undetectable.ts b/vs-code-extension/src/events/on-undetectable.ts new file mode 100644 index 0000000..0802071 --- /dev/null +++ b/vs-code-extension/src/events/on-undetectable.ts @@ -0,0 +1,34 @@ +import { GenericNotificationHandler } from "vscode-languageclient"; +import * as vscode from "vscode"; + +const ACTION_SELECT_LANGUAGE = "Select Language"; +const ACTION_DISABLE = "Ignore for this file"; + +const ignoredFiled = new Set(); + +export const onUndetectable: GenericNotificationHandler = async (params: LSPLanguageUndetectable) => { + if (ignoredFiled.has(params.Uri)) { + return; + } + + const result = await vscode.window.showWarningMessage( + "config-lsp was unable to detect the appropriate language for this file", + { + detail: "Either select a language or add '#?lsp.language=' to the top of the file", + }, + ACTION_SELECT_LANGUAGE, + ACTION_DISABLE, + ) + + switch (result) { + case ACTION_SELECT_LANGUAGE: + vscode.commands.executeCommand("workbench.action.editor.changeLanguageMode"); + break; + case ACTION_DISABLE: + ignoredFiled.add(params.Uri); + break; + undefined: + break; + } +} + diff --git a/vs-code-extension/src/events/types.ts b/vs-code-extension/src/events/types.ts new file mode 100644 index 0000000..8bae362 --- /dev/null +++ b/vs-code-extension/src/events/types.ts @@ -0,0 +1,6 @@ +interface LSPNotification { + Uri: string; +} + +interface LSPLanguageUndetectable extends LSPNotification {} + diff --git a/vs-code-extension/src/extension.ts b/vs-code-extension/src/extension.ts index 661ed43..c07b319 100644 --- a/vs-code-extension/src/extension.ts +++ b/vs-code-extension/src/extension.ts @@ -1,32 +1,30 @@ -import * as path from "path" -import { ExtensionContext, workspace } from 'vscode'; +import * as path from "path"; +import { ExtensionContext, workspace } from "vscode"; import { Executable, LanguageClient, type LanguageClientOptions, type ServerOptions, -} from 'vscode-languageclient/node'; +} from "vscode-languageclient/node"; +import { onUndetectable } from "./events/on-undetectable"; -const IS_DEBUG = process.env.VSCODE_DEBUG_MODE === 'true' || process.env.NODE_ENV === 'development'; +const IS_DEBUG = + process.env.VSCODE_DEBUG_MODE === "true" || + process.env.NODE_ENV === "development"; let client: LanguageClient; -export function activate(context: ExtensionContext) { +export async function activate({subscriptions}: ExtensionContext) { console.info("config-lsp activated"); - const initOptions = workspace.getConfiguration('config-lsp'); + const initOptions = workspace.getConfiguration("config-lsp"); const clientOptions: LanguageClientOptions = { documentSelector: [ - { - scheme: 'file', - language: 'plaintext', - pattern: "**/{config,sshconfig,sshd_config,sshdconfig,fstab,hosts,aliases}", - }, - // Some configs seem to be incorrectly detected as yaml - { - scheme: 'file', - language: 'yaml', - pattern: "**/{config,sshconfig,sshd_config,sshdconfig,fstab,hosts,aliases}", - }, + {language: "sshconfig"}, + {language: "sshdconfig"}, + {language: "aliases"}, + {language: "fstab"}, + {language: "hosts"}, + {language: "wireguard"}, ], initializationOptions: initOptions, }; @@ -34,39 +32,29 @@ export function activate(context: ExtensionContext) { const path = getBundledPath(); console.info(`Found config-lsp path at ${path}`); const run: Executable = { - command: getBundledPath(), - } + command: getBundledPath() , + }; const serverOptions: ServerOptions = { run, debug: run, - } + }; client = new LanguageClient( - 'config-lsp', + "config-lsp", serverOptions, clientOptions, - IS_DEBUG, + IS_DEBUG ); + console.info("Starting config-lsp..."); + await client.start(); + console.info("Started config-lsp"); - client.start(); - console.info("config-lsp started"); - - // const serverOptions: ServerOptions = { - // } - // - // // Create the language client and start the client. - // client = new LanguageClient( - // 'languageServerExample', - // clientOptions - // ); - // - // // Start the client. This will also launch the server - // client.start(); + subscriptions.push(client.onNotification("$/config-lsp/languageUndetectable", onUndetectable)) } function getBundledPath(): string { - const filePath = path.resolve(__dirname, "config-lsp") + const filePath = path.resolve(__dirname, "config-lsp"); return filePath; } From 9d48d525094db78519841cdedb550169eadd9a8e Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 27 Oct 2024 12:10:20 +0100 Subject: [PATCH 7/9] refactor(server): Improvements --- .../lsp/text-document-code-action.go | 12 +- .../lsp/text-document-completion.go | 14 +- .../lsp/text-document-definition.go | 12 +- .../lsp/text-document-did-change.go | 48 ++++--- .../lsp/text-document-did-close.go | 15 +-- .../lsp/text-document-did-open.go | 16 ++- .../root-handler/lsp/text-document-hover.go | 14 +- .../lsp/text-document-prepare-rename.go | 12 +- .../lsp/text-document-range-formatting.go | 12 +- .../root-handler/lsp/text-document-rename.go | 12 +- .../lsp/text-document-signature-help.go | 14 +- server/root-handler/shared/constants.go | 21 +++ server/root-handler/shared/indexes.go | 16 +++ server/root-handler/shared/singleton.go | 10 +- server/root-handler/utils/code-actions.go | 5 +- .../root-handler/utils/language-detection.go | 122 ++++++++---------- server/root-handler/utils/notification.go | 13 +- 17 files changed, 194 insertions(+), 174 deletions(-) create mode 100644 server/root-handler/shared/constants.go create mode 100644 server/root-handler/shared/indexes.go 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), }, ) } From 88b9a51e953176140aac84d1e0586741dcae3062 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 27 Oct 2024 12:10:41 +0100 Subject: [PATCH 8/9] fix(ci-cd): Fix path --- .github/workflows/release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index bcebe07..0040068 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -29,7 +29,7 @@ jobs: - name: Check version in code matches flake version shell: bash run: | - if ! [ $(grep '// CI:CD-VERSION$' server/root-handler/handler.go | cut -d'"' -f 2) = $(grep '# CI:CD-VERSION$' flake.nix | cut -d'"' -f 2) ]; + if ! [ $(grep '// CI:CD-VERSION$' server/root-handler/common.go | cut -d'"' -f 2) = $(grep '# CI:CD-VERSION$' flake.nix | cut -d'"' -f 2) ]; then echo "Version mismatch between code and flake" exit 1 From e225910678bd3d47b4bc59bc0e45007b07caeb28 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 27 Oct 2024 12:13:01 +0100 Subject: [PATCH 9/9] chore: Update version --- flake.nix | 2 +- server/root-handler/common.go | 2 +- vs-code-extension/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.nix b/flake.nix index 3f164af..8f466eb 100644 --- a/flake.nix +++ b/flake.nix @@ -23,7 +23,7 @@ "aarch64-windows" ] (system: let - version = "0.1.1"; # CI:CD-VERSION + version = "0.1.2"; # CI:CD-VERSION pkgs = import nixpkgs { inherit system; overlays = [ diff --git a/server/root-handler/common.go b/server/root-handler/common.go index 031ff10..7ef05c8 100644 --- a/server/root-handler/common.go +++ b/server/root-handler/common.go @@ -2,4 +2,4 @@ package roothandler // The comment below at the end of the line is required for the CI:CD to work. // Do not remove it -var Version = "0.1.1" // CI:CD-VERSION +var Version = "0.1.2" // CI:CD-VERSION diff --git a/vs-code-extension/package.json b/vs-code-extension/package.json index 609b7f6..82a9b0c 100644 --- a/vs-code-extension/package.json +++ b/vs-code-extension/package.json @@ -2,7 +2,7 @@ "name": "config-lsp", "description": "Language Features (completions, diagnostics, etc.) for your config files: gitconfig, fstab, aliases, hosts, wireguard, ssh_config, sshd_config, and more to come!", "author": "Myzel394", - "version": "0.1.1", + "version": "0.1.2", "repository": { "type": "git", "url": "https://github.com/Myzel394/config-lsp"