From 9bc931c686c162e00abd2cd64d3b382d0479bb46 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:32:30 +0200 Subject: [PATCH] fix: Improve applyEdit; Improvements --- flake.nix | 18 ++++++------- server/common/diagnostics.go | 8 +++--- server/handlers/ssh_config/analyzer/values.go | 17 ++++++------- .../ssh_config/diagnostics/diagnostics.go | 19 ++++++++++++++ .../ssh_config/handlers/code-actions.go | 6 ++--- .../ssh_config/handlers/fetch-code-actions.go | 9 +++++++ server/root-handler/handler.go | 13 ++++++++++ server/root-handler/text-document-did-open.go | 25 ++++--------------- .../root-handler/workspace-execute-command.go | 6 +++-- vs-code-extension/src/extension.ts | 16 ++++++------ 10 files changed, 81 insertions(+), 56 deletions(-) create mode 100644 server/handlers/ssh_config/diagnostics/diagnostics.go diff --git a/flake.nix b/flake.nix index b1f5a7f..be65ac1 100644 --- a/flake.nix +++ b/flake.nix @@ -28,15 +28,15 @@ pkgs.go_1_22 ]; server = pkgs.buildGoModule { - nativeBuildInputs = inputs; - pname = "github.com/Myzel394/config-lsp"; - version = "v0.0.1"; - src = ./server; - vendorHash = "sha256-s+sjOVvqU20+mbEFKg+RCD+dhScqSatk9eseX2IioPI="; - checkPhase = '' - go test -v $(pwd)/... - ''; - }; + nativeBuildInputs = inputs; + pname = "github.com/Myzel394/config-lsp"; + version = "v0.0.1"; + src = ./server; + vendorHash = "sha256-s+sjOVvqU20+mbEFKg+RCD+dhScqSatk9eseX2IioPI"; + checkPhase = '' + go test -v $(pwd)/... + ''; + }; in { packages = { default = server; diff --git a/server/common/diagnostics.go b/server/common/diagnostics.go index 06fcb71..fbd5fdb 100644 --- a/server/common/diagnostics.go +++ b/server/common/diagnostics.go @@ -6,8 +6,8 @@ import ( ) func ClearDiagnostics(context *glsp.Context, uri protocol.DocumentUri) { - context.Notify( - "textDocument/publishDiagnostics", + go context.Notify( + protocol.ServerTextDocumentPublishDiagnostics, protocol.PublishDiagnosticsParams{ URI: uri, Diagnostics: []protocol.Diagnostic{}, @@ -16,8 +16,8 @@ func ClearDiagnostics(context *glsp.Context, uri protocol.DocumentUri) { } func SendDiagnostics(context *glsp.Context, uri protocol.DocumentUri, diagnostics []protocol.Diagnostic) { - context.Notify( - "textDocument/publishDiagnostics", + go context.Notify( + protocol.ServerTextDocumentPublishDiagnostics, protocol.PublishDiagnosticsParams{ URI: uri, Diagnostics: diagnostics, diff --git a/server/handlers/ssh_config/analyzer/values.go b/server/handlers/ssh_config/analyzer/values.go index 4752220..e10321f 100644 --- a/server/handlers/ssh_config/analyzer/values.go +++ b/server/handlers/ssh_config/analyzer/values.go @@ -1,11 +1,8 @@ package analyzer import ( - "config-lsp/common" + "config-lsp/handlers/ssh_config/diagnostics" "config-lsp/handlers/ssh_config/fields" - "fmt" - - protocol "github.com/tliron/glsp/protocol_3_16" ) func analyzeValuesAreValid( @@ -23,12 +20,12 @@ func analyzeValuesAreValid( continue } - ctx.diagnostics = append(ctx.diagnostics, - protocol.Diagnostic{ - Range: option.Key.ToLSPRange(), - Message: fmt.Sprintf("Unknown option: %s", option.Key.Value.Value), - Severity: &common.SeverityError, - }, + ctx.diagnostics = append( + ctx.diagnostics, + diagnostics.GenerateUnknownOption( + option.Key.ToLSPRange(), + option.Key.Value.Value, + ), ) ctx.document.Indexes.UnknownOptions[info.Option.Start.Line] = info diff --git a/server/handlers/ssh_config/diagnostics/diagnostics.go b/server/handlers/ssh_config/diagnostics/diagnostics.go new file mode 100644 index 0000000..86b96f0 --- /dev/null +++ b/server/handlers/ssh_config/diagnostics/diagnostics.go @@ -0,0 +1,19 @@ +package diagnostics + +import ( + "config-lsp/common" + "fmt" + + protocol "github.com/tliron/glsp/protocol_3_16" +) + +func GenerateUnknownOption( + diagnosticRange protocol.Range, + optionName string, +) protocol.Diagnostic { + return protocol.Diagnostic{ + Range: diagnosticRange, + Message: fmt.Sprintf("Unknown option: %s", optionName), + Severity: &common.SeverityError, + } +} diff --git a/server/handlers/ssh_config/handlers/code-actions.go b/server/handlers/ssh_config/handlers/code-actions.go index 73bcebd..f60d783 100644 --- a/server/handlers/ssh_config/handlers/code-actions.go +++ b/server/handlers/ssh_config/handlers/code-actions.go @@ -98,14 +98,13 @@ func (args codeActionAddToUnknownArgs) RunCommand(d *sshconfig.SSHDocument) (*pr rawOptionName := option.Key.Value.Raw optionName := addToUnknownOptionTemplate.Format(formatting.DefaultFormattingOptions, rawOptionName) + label := fmt.Sprintf("Add %s to unknown options", option.Key.Key) // We got everything, let's build the edit! if ignoreUnknownOption == nil { // Insert a completely new IgnoreUnknown option - if block == nil { // Global - label := fmt.Sprintf("Add %s to unknown options", option.Key.Key) return &protocol.ApplyWorkspaceEditParams{ Label: &label, Edit: protocol.WorkspaceEdit{ @@ -129,7 +128,7 @@ func (args codeActionAddToUnknownArgs) RunCommand(d *sshconfig.SSHDocument) (*pr }, }, nil } else { - label := fmt.Sprintf("Add %s to unknown options", option.Key.Key) + // Block return &protocol.ApplyWorkspaceEditParams{ Label: &label, Edit: protocol.WorkspaceEdit{ @@ -155,7 +154,6 @@ func (args codeActionAddToUnknownArgs) RunCommand(d *sshconfig.SSHDocument) (*pr } } else { // Append to the existing IgnoreUnknown option - label := fmt.Sprintf("Add %s to unknown options", option.Key.Key) return &protocol.ApplyWorkspaceEditParams{ Label: &label, Edit: protocol.WorkspaceEdit{ diff --git a/server/handlers/ssh_config/handlers/fetch-code-actions.go b/server/handlers/ssh_config/handlers/fetch-code-actions.go index a2a2759..cca5d7a 100644 --- a/server/handlers/ssh_config/handlers/fetch-code-actions.go +++ b/server/handlers/ssh_config/handlers/fetch-code-actions.go @@ -2,6 +2,7 @@ package handlers import ( sshconfig "config-lsp/handlers/ssh_config" + "config-lsp/handlers/ssh_config/diagnostics" "fmt" protocol "github.com/tliron/glsp/protocol_3_16" @@ -33,9 +34,17 @@ func FetchCodeActions( }, }, } + kind := protocol.CodeActionKindQuickFix codeAction := &protocol.CodeAction{ Title: fmt.Sprintf("Add %s to unknown options", unknownOption.Option.Key.Key), Command: &command, + Kind: &kind, + Diagnostics: []protocol.Diagnostic{ + diagnostics.GenerateUnknownOption( + unknownOption.Option.Key.ToLSPRange(), + unknownOption.Option.Key.Value.Value, + ), + }, } return []protocol.CodeAction{ diff --git a/server/root-handler/handler.go b/server/root-handler/handler.go index 91de471..afc666d 100644 --- a/server/root-handler/handler.go +++ b/server/root-handler/handler.go @@ -44,6 +44,19 @@ func initialize(context *glsp.Context, params *protocol.InitializeParams) (any, capabilities := lspHandler.CreateServerCapabilities() capabilities.TextDocumentSync = protocol.TextDocumentSyncKindFull capabilities.SignatureHelpProvider = &protocol.SignatureHelpOptions{} + capabilities.ExecuteCommandProvider = &protocol.ExecuteCommandOptions{ + Commands: []string{ + "aliases.sendTestMail", + + "hosts.inlineAliases", + + "sshconfig.addToUnknown", + + "wireguard.generatePrivateKey", + "wireguard.generatePresharedKey", + "wireguard.addKeepalive", + }, + } if (*params.Capabilities.TextDocument.Rename.PrepareSupport) == true { // Client supports rename preparation diff --git a/server/root-handler/text-document-did-open.go b/server/root-handler/text-document-did-open.go index 68365f6..abe9a2b 100644 --- a/server/root-handler/text-document-did-open.go +++ b/server/root-handler/text-document-did-open.go @@ -54,26 +54,11 @@ func showParseError( uri protocol.DocumentUri, err common.ParseError, ) { - severity := protocol.DiagnosticSeverityError - - common.SendDiagnostics( - context, - uri, - []protocol.Diagnostic{ - { - Severity: &severity, - Message: err.Err.Error(), - Range: protocol.Range{ - Start: protocol.Position{ - Line: err.Line, - Character: 0, - }, - End: protocol.Position{ - Line: err.Line, - Character: 99999, - }, - }, - }, + context.Notify( + "window/showMessage", + protocol.ShowMessageParams{ + Type: protocol.MessageTypeError, + Message: err.Err.Error(), }, ) } diff --git a/server/root-handler/workspace-execute-command.go b/server/root-handler/workspace-execute-command.go index 191fb40..bd04e34 100644 --- a/server/root-handler/workspace-execute-command.go +++ b/server/root-handler/workspace-execute-command.go @@ -33,9 +33,11 @@ func WorkspaceExecuteCommand(context *glsp.Context, params *protocol.ExecuteComm return nil, err } - context.Notify( - "workspace/applyEdit", + // Seems like `context.Call` is blocking, so we move it to a goroutine + go context.Call( + protocol.ServerWorkspaceApplyEdit, edit, + nil, ) return nil, nil diff --git a/vs-code-extension/src/extension.ts b/vs-code-extension/src/extension.ts index 1035343..661ed43 100644 --- a/vs-code-extension/src/extension.ts +++ b/vs-code-extension/src/extension.ts @@ -1,8 +1,8 @@ -import * as path from "path"; -import { ExtensionContext } from 'vscode'; +import * as path from "path" +import { ExtensionContext, workspace } from 'vscode'; import { - Executable, + Executable, LanguageClient, type LanguageClientOptions, type ServerOptions, @@ -12,7 +12,8 @@ const IS_DEBUG = process.env.VSCODE_DEBUG_MODE === 'true' || process.env.NODE_EN let client: LanguageClient; export function activate(context: ExtensionContext) { - console.info("config-lsp activated"); + console.info("config-lsp activated"); + const initOptions = workspace.getConfiguration('config-lsp'); const clientOptions: LanguageClientOptions = { documentSelector: [ { @@ -26,11 +27,12 @@ export function activate(context: ExtensionContext) { language: 'yaml', pattern: "**/{config,sshconfig,sshd_config,sshdconfig,fstab,hosts,aliases}", }, - ] + ], + initializationOptions: initOptions, }; - + const path = getBundledPath(); - console.info(`Found config-lsp path at ${path}`); + console.info(`Found config-lsp path at ${path}`); const run: Executable = { command: getBundledPath(), }