From c2f7de5f13c7c57a2fb0bf5ca34df976493b00ee Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Tue, 11 Feb 2025 19:50:47 +0100 Subject: [PATCH] feat(server): Improvements; Allow disabling errors for undetectable files --- server/main.go | 2 ++ .../lsp/text-document-code-action.go | 9 +++++++- .../lsp/text-document-definition.go | 7 +++++- .../lsp/text-document-did-change.go | 8 ++++++- .../lsp/text-document-did-open.go | 13 ++++++++--- .../lsp/text-document-prepare-rename.go | 7 +++++- .../lsp/text-document-range-formatting.go | 7 +++++- .../root-handler/lsp/text-document-rename.go | 7 +++++- server/root-handler/utils/code-actions.go | 4 ++-- .../root-handler/utils/language-detection.go | 7 +----- server/root-handler/utils/notification.go | 22 +++++++++++++------ 11 files changed, 69 insertions(+), 24 deletions(-) diff --git a/server/main.go b/server/main.go index 9e9f2a2..031f76a 100644 --- a/server/main.go +++ b/server/main.go @@ -1,6 +1,7 @@ package main import ( + "config-lsp/common" roothandler "config-lsp/root-handler" "fmt" "os" @@ -23,5 +24,6 @@ func main() { // This increases logging verbosity (optional) commonlog.Configure(1, nil) + common.InitServerOptions() roothandler.SetUpRootHandler() } diff --git a/server/root-handler/lsp/text-document-code-action.go b/server/root-handler/lsp/text-document-code-action.go index bf4cc45..2b0a146 100644 --- a/server/root-handler/lsp/text-document-code-action.go +++ b/server/root-handler/lsp/text-document-code-action.go @@ -1,6 +1,7 @@ package lsp import ( + "config-lsp/common" aliases "config-lsp/handlers/aliases/lsp" hosts "config-lsp/handlers/hosts/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp" @@ -16,7 +17,13 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - return utils.FetchAddLanguageActions(params.TextDocument.URI) + actions := utils.FetchAddLanguageActions(params.TextDocument.URI) + + if common.ServerOptions.NoUndetectableErrors { + return actions, nil + } else { + return actions, utils.LanguageUndetectableError{} + } } switch *language { diff --git a/server/root-handler/lsp/text-document-definition.go b/server/root-handler/lsp/text-document-definition.go index c28b1f3..4cee179 100644 --- a/server/root-handler/lsp/text-document-definition.go +++ b/server/root-handler/lsp/text-document-definition.go @@ -1,6 +1,7 @@ package lsp import ( + "config-lsp/common" aliases "config-lsp/handlers/aliases/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp" sshdconfig "config-lsp/handlers/sshd_config/lsp" @@ -15,7 +16,11 @@ func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionPa language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - return nil, utils.LanguageUndetectableError{} + if common.ServerOptions.NoUndetectableErrors { + return nil, nil + } else { + return nil, utils.LanguageUndetectableError{} + } } switch *language { diff --git a/server/root-handler/lsp/text-document-did-change.go b/server/root-handler/lsp/text-document-did-change.go index 63edf6e..dc30ca7 100644 --- a/server/root-handler/lsp/text-document-did-change.go +++ b/server/root-handler/lsp/text-document-did-change.go @@ -1,6 +1,7 @@ package lsp import ( + "config-lsp/common" aliases "config-lsp/handlers/aliases/lsp" fstab "config-lsp/handlers/fstab/lsp" hosts "config-lsp/handlers/hosts/lsp" @@ -8,6 +9,7 @@ import ( sshdconfig "config-lsp/handlers/sshd_config/lsp" wireguard "config-lsp/handlers/wireguard/lsp" "config-lsp/root-handler/shared" + "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -24,7 +26,11 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText ) if err != nil { - return err + if common.ServerOptions.NoUndetectableErrors { + return nil + } else { + return err + } } if newLanguage != language { diff --git a/server/root-handler/lsp/text-document-did-open.go b/server/root-handler/lsp/text-document-did-open.go index 20ebc44..e1ddd28 100644 --- a/server/root-handler/lsp/text-document-did-open.go +++ b/server/root-handler/lsp/text-document-did-open.go @@ -30,7 +30,11 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu ) if err != nil { - return err + if common.ServerOptions.NoUndetectableErrors { + return nil + } else { + return err + } } switch *language { @@ -57,16 +61,19 @@ func initFile( uri protocol.DocumentUri, advertisedLanguage string, ) (*shared.SupportedLanguage, error) { + println("Initializing the file") + println(advertisedLanguage) + println(uri) language, err := utils.DetectLanguage(content, advertisedLanguage, uri) if err != nil { utils.NotifyLanguageUndetectable(context, uri) return nil, utils.LanguageUndetectableError{} - } else { - utils.NotifyDetectedLanguage(context, uri, language) } + utils.NotifyDetectedLanguage(context, uri, language) + shared.OpenedFiles[uri] = struct{}{} // Everything okay, now we can handle the file diff --git a/server/root-handler/lsp/text-document-prepare-rename.go b/server/root-handler/lsp/text-document-prepare-rename.go index 812908f..7a8ca59 100644 --- a/server/root-handler/lsp/text-document-prepare-rename.go +++ b/server/root-handler/lsp/text-document-prepare-rename.go @@ -1,6 +1,7 @@ package lsp import ( + "config-lsp/common" aliases "config-lsp/handlers/aliases/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp" "config-lsp/root-handler/shared" @@ -15,7 +16,11 @@ func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRe language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - return nil, utils.LanguageUndetectableError{} + if common.ServerOptions.NoUndetectableErrors { + return nil, nil + } else { + return nil, utils.LanguageUndetectableError{} + } } switch *language { diff --git a/server/root-handler/lsp/text-document-range-formatting.go b/server/root-handler/lsp/text-document-range-formatting.go index 19fc00b..6642f51 100644 --- a/server/root-handler/lsp/text-document-range-formatting.go +++ b/server/root-handler/lsp/text-document-range-formatting.go @@ -1,6 +1,7 @@ package lsp import ( + "config-lsp/common" sshconfig "config-lsp/handlers/ssh_config/lsp" sshdconfig "config-lsp/handlers/sshd_config/lsp" "config-lsp/root-handler/shared" @@ -17,7 +18,11 @@ func TextDocumentRangeFormattingFunc( language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - return nil, utils.LanguageUndetectableError{} + if common.ServerOptions.NoUndetectableErrors { + return nil, nil + } else { + return nil, utils.LanguageUndetectableError{} + } } switch *language { diff --git a/server/root-handler/lsp/text-document-rename.go b/server/root-handler/lsp/text-document-rename.go index d630bb3..1d72088 100644 --- a/server/root-handler/lsp/text-document-rename.go +++ b/server/root-handler/lsp/text-document-rename.go @@ -1,6 +1,7 @@ package lsp import ( + "config-lsp/common" aliases "config-lsp/handlers/aliases/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp" "config-lsp/root-handler/shared" @@ -14,7 +15,11 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (* language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) if language == nil { - return nil, utils.LanguageUndetectableError{} + if common.ServerOptions.NoUndetectableErrors { + return nil, nil + } else { + return nil, utils.LanguageUndetectableError{} + } } switch *language { diff --git a/server/root-handler/utils/code-actions.go b/server/root-handler/utils/code-actions.go index b86027d..3994bf4 100644 --- a/server/root-handler/utils/code-actions.go +++ b/server/root-handler/utils/code-actions.go @@ -7,7 +7,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 { actions := make([]protocol.CodeAction, 0, len(shared.AllSupportedLanguages)) kind := protocol.CodeActionKindQuickFix @@ -40,5 +40,5 @@ func FetchAddLanguageActions(uri protocol.DocumentUri) ([]protocol.CodeAction, e }) } - return actions, nil + return actions } diff --git a/server/root-handler/utils/language-detection.go b/server/root-handler/utils/language-detection.go index 24c91d7..ea2fb9b 100644 --- a/server/root-handler/utils/language-detection.go +++ b/server/root-handler/utils/language-detection.go @@ -75,11 +75,6 @@ var filenameToLanguageMap = map[string]shared.SupportedLanguage{ var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`) var wireguardPattern = regexp.MustCompile(`wg(\d+)?(\.conf)?$`) -var undetectableError = common.ParseError{ - Line: 0, - Err: LanguageUndetectableError{}, -} - func DetectLanguage( content string, advertisedLanguage string, @@ -154,5 +149,5 @@ func DetectLanguage( return shared.LanguageSSHConfig, nil } - return "", undetectableError + return "", LanguageUndetectableError{} } diff --git a/server/root-handler/utils/notification.go b/server/root-handler/utils/notification.go index 2153ce3..73cd733 100644 --- a/server/root-handler/utils/notification.go +++ b/server/root-handler/utils/notification.go @@ -1,7 +1,9 @@ package utils import ( + "config-lsp/common" "config-lsp/root-handler/shared" + "github.com/tliron/glsp" protocol "github.com/tliron/glsp/protocol_3_16" ) @@ -17,6 +19,8 @@ type lspDetectedLanguage struct { } func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) { + // We always send this undetectable message, as it's a custom message. + // The client can handle it themselves. go context.Notify( "$/config-lsp/languageUndetectable", lspNotification{ @@ -24,13 +28,17 @@ func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) }, ) - 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='.", - }, - ) + // The native showMessage notification however, should only be shown + // if the user wishes to. + if !common.ServerOptions.NoUndetectableErrors { + 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 shared.SupportedLanguage) {