feat(server): Improvements; Allow disabling errors for undetectable files

This commit is contained in:
Myzel394 2025-02-11 19:50:47 +01:00
parent 757f24a521
commit c2f7de5f13
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
11 changed files with 69 additions and 24 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"config-lsp/common"
roothandler "config-lsp/root-handler" roothandler "config-lsp/root-handler"
"fmt" "fmt"
"os" "os"
@ -23,5 +24,6 @@ func main() {
// This increases logging verbosity (optional) // This increases logging verbosity (optional)
commonlog.Configure(1, nil) commonlog.Configure(1, nil)
common.InitServerOptions()
roothandler.SetUpRootHandler() roothandler.SetUpRootHandler()
} }

View File

@ -1,6 +1,7 @@
package lsp package lsp
import ( import (
"config-lsp/common"
aliases "config-lsp/handlers/aliases/lsp" aliases "config-lsp/handlers/aliases/lsp"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
sshconfig "config-lsp/handlers/ssh_config/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) language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil { 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 { switch *language {

View File

@ -1,6 +1,7 @@
package lsp package lsp
import ( import (
"config-lsp/common"
aliases "config-lsp/handlers/aliases/lsp" aliases "config-lsp/handlers/aliases/lsp"
sshconfig "config-lsp/handlers/ssh_config/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp"
sshdconfig "config-lsp/handlers/sshd_config/lsp" sshdconfig "config-lsp/handlers/sshd_config/lsp"
@ -15,7 +16,11 @@ func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionPa
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil { if language == nil {
return nil, utils.LanguageUndetectableError{} if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
} }
switch *language { switch *language {

View File

@ -1,6 +1,7 @@
package lsp package lsp
import ( import (
"config-lsp/common"
aliases "config-lsp/handlers/aliases/lsp" aliases "config-lsp/handlers/aliases/lsp"
fstab "config-lsp/handlers/fstab/lsp" fstab "config-lsp/handlers/fstab/lsp"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
@ -8,6 +9,7 @@ import (
sshdconfig "config-lsp/handlers/sshd_config/lsp" sshdconfig "config-lsp/handlers/sshd_config/lsp"
wireguard "config-lsp/handlers/wireguard/lsp" wireguard "config-lsp/handlers/wireguard/lsp"
"config-lsp/root-handler/shared" "config-lsp/root-handler/shared"
"github.com/tliron/glsp" "github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
) )
@ -24,7 +26,11 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText
) )
if err != nil { if err != nil {
return err if common.ServerOptions.NoUndetectableErrors {
return nil
} else {
return err
}
} }
if newLanguage != language { if newLanguage != language {

View File

@ -30,7 +30,11 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu
) )
if err != nil { if err != nil {
return err if common.ServerOptions.NoUndetectableErrors {
return nil
} else {
return err
}
} }
switch *language { switch *language {
@ -57,16 +61,19 @@ func initFile(
uri protocol.DocumentUri, uri protocol.DocumentUri,
advertisedLanguage string, advertisedLanguage string,
) (*shared.SupportedLanguage, error) { ) (*shared.SupportedLanguage, error) {
println("Initializing the file")
println(advertisedLanguage)
println(uri)
language, err := utils.DetectLanguage(content, advertisedLanguage, uri) language, err := utils.DetectLanguage(content, advertisedLanguage, uri)
if err != nil { if err != nil {
utils.NotifyLanguageUndetectable(context, uri) utils.NotifyLanguageUndetectable(context, uri)
return nil, utils.LanguageUndetectableError{} return nil, utils.LanguageUndetectableError{}
} else {
utils.NotifyDetectedLanguage(context, uri, language)
} }
utils.NotifyDetectedLanguage(context, uri, language)
shared.OpenedFiles[uri] = struct{}{} shared.OpenedFiles[uri] = struct{}{}
// Everything okay, now we can handle the file // Everything okay, now we can handle the file

View File

@ -1,6 +1,7 @@
package lsp package lsp
import ( import (
"config-lsp/common"
aliases "config-lsp/handlers/aliases/lsp" aliases "config-lsp/handlers/aliases/lsp"
sshconfig "config-lsp/handlers/ssh_config/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp"
"config-lsp/root-handler/shared" "config-lsp/root-handler/shared"
@ -15,7 +16,11 @@ func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRe
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil { if language == nil {
return nil, utils.LanguageUndetectableError{} if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
} }
switch *language { switch *language {

View File

@ -1,6 +1,7 @@
package lsp package lsp
import ( import (
"config-lsp/common"
sshconfig "config-lsp/handlers/ssh_config/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp"
sshdconfig "config-lsp/handlers/sshd_config/lsp" sshdconfig "config-lsp/handlers/sshd_config/lsp"
"config-lsp/root-handler/shared" "config-lsp/root-handler/shared"
@ -17,7 +18,11 @@ func TextDocumentRangeFormattingFunc(
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil { if language == nil {
return nil, utils.LanguageUndetectableError{} if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
} }
switch *language { switch *language {

View File

@ -1,6 +1,7 @@
package lsp package lsp
import ( import (
"config-lsp/common"
aliases "config-lsp/handlers/aliases/lsp" aliases "config-lsp/handlers/aliases/lsp"
sshconfig "config-lsp/handlers/ssh_config/lsp" sshconfig "config-lsp/handlers/ssh_config/lsp"
"config-lsp/root-handler/shared" "config-lsp/root-handler/shared"
@ -14,7 +15,11 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI) language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil { if language == nil {
return nil, utils.LanguageUndetectableError{} if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
} }
switch *language { switch *language {

View File

@ -7,7 +7,7 @@ import (
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
) )
func FetchAddLanguageActions(uri protocol.DocumentUri) ([]protocol.CodeAction, error) { func FetchAddLanguageActions(uri protocol.DocumentUri) []protocol.CodeAction {
actions := make([]protocol.CodeAction, 0, len(shared.AllSupportedLanguages)) actions := make([]protocol.CodeAction, 0, len(shared.AllSupportedLanguages))
kind := protocol.CodeActionKindQuickFix kind := protocol.CodeActionKindQuickFix
@ -40,5 +40,5 @@ func FetchAddLanguageActions(uri protocol.DocumentUri) ([]protocol.CodeAction, e
}) })
} }
return actions, nil return actions
} }

View File

@ -75,11 +75,6 @@ var filenameToLanguageMap = map[string]shared.SupportedLanguage{
var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`) 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,
Err: LanguageUndetectableError{},
}
func DetectLanguage( func DetectLanguage(
content string, content string,
advertisedLanguage string, advertisedLanguage string,
@ -154,5 +149,5 @@ func DetectLanguage(
return shared.LanguageSSHConfig, nil return shared.LanguageSSHConfig, nil
} }
return "", undetectableError return "", LanguageUndetectableError{}
} }

View File

@ -1,7 +1,9 @@
package utils package utils
import ( import (
"config-lsp/common"
"config-lsp/root-handler/shared" "config-lsp/root-handler/shared"
"github.com/tliron/glsp" "github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
) )
@ -17,6 +19,8 @@ type lspDetectedLanguage struct {
} }
func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri) { 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( go context.Notify(
"$/config-lsp/languageUndetectable", "$/config-lsp/languageUndetectable",
lspNotification{ lspNotification{
@ -24,13 +28,17 @@ func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri)
}, },
) )
go context.Notify( // The native showMessage notification however, should only be shown
"window/showMessage", // if the user wishes to.
protocol.ShowMessageParams{ if !common.ServerOptions.NoUndetectableErrors {
Type: protocol.MessageTypeError, go context.Notify(
Message: "config-lsp was unable to detect the appropriate language for this file. Please add: '#?lsp.language=<language>'.", "window/showMessage",
}, protocol.ShowMessageParams{
) Type: protocol.MessageTypeError,
Message: "config-lsp was unable to detect the appropriate language for this file. Please add: '#?lsp.language=<language>'.",
},
)
}
} }
func NotifyDetectedLanguage(context *glsp.Context, uri protocol.DocumentUri, language shared.SupportedLanguage) { func NotifyDetectedLanguage(context *glsp.Context, uri protocol.DocumentUri, language shared.SupportedLanguage) {