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
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()
}

View File

@ -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 {

View File

@ -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,8 +16,12 @@ func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionPa
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil {
if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
}
switch *language {
case shared.LanguageHosts:

View File

@ -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,8 +26,12 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText
)
if err != nil {
if common.ServerOptions.NoUndetectableErrors {
return nil
} else {
return err
}
}
if newLanguage != language {
language = newLanguage

View File

@ -30,8 +30,12 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu
)
if err != nil {
if common.ServerOptions.NoUndetectableErrors {
return nil
} else {
return err
}
}
switch *language {
case shared.LanguageFstab:
@ -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

View File

@ -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,8 +16,12 @@ func TextDocumentPrepareRename(context *glsp.Context, params *protocol.PrepareRe
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil {
if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
}
switch *language {
case shared.LanguageHosts:

View File

@ -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,8 +18,12 @@ func TextDocumentRangeFormattingFunc(
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil {
if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
}
switch *language {
case shared.LanguageHosts:

View File

@ -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,8 +15,12 @@ func TextDocumentRename(context *glsp.Context, params *protocol.RenameParams) (*
language := shared.Handler.GetLanguageForDocument(params.TextDocument.URI)
if language == nil {
if common.ServerOptions.NoUndetectableErrors {
return nil, nil
} else {
return nil, utils.LanguageUndetectableError{}
}
}
switch *language {
case shared.LanguageHosts:

View File

@ -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
}

View File

@ -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{}
}

View File

@ -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,6 +28,9 @@ func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri)
},
)
// 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{
@ -31,6 +38,7 @@ func NotifyLanguageUndetectable(context *glsp.Context, uri protocol.DocumentUri)
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) {