feat: Add aliases lsp handlers

This commit is contained in:
Myzel394 2024-08-31 21:17:13 +02:00
parent 4ea6f9f160
commit 4c2422c3da
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
16 changed files with 262 additions and 2 deletions

View File

@ -29,7 +29,7 @@ func analyzeValuesAreValid(
if entry.Separator == nil { if entry.Separator == nil {
errors = append(errors, common.LSPError{ errors = append(errors, common.LSPError{
Range: entry.Location, Range: entry.Location,
Err: ers.New("The separator is required"), Err: ers.New("A ':' is required as a separator"),
}) })
continue continue

View File

@ -26,7 +26,7 @@ func CreateIndexes(parser ast.AliasesParser) (AliasesIndexes, []common.LSPError)
if existingEntry, found := indexes.Keys[normalizedAlias]; found { if existingEntry, found := indexes.Keys[normalizedAlias]; found {
errors = append(errors, common.LSPError{ errors = append(errors, common.LSPError{
Range: entry.Location, Range: entry.Key.Location,
Err: shared.DuplicateKeyEntry{ Err: shared.DuplicateKeyEntry{
AlreadyFoundAt: existingEntry.Location.Start.Line, AlreadyFoundAt: existingEntry.Location.Start.Line,
Key: entry.Key.Value, Key: entry.Key.Value,

View File

@ -0,0 +1,14 @@
package lsp
import (
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
// document := hosts.DocumentParserMap[params.TextDocument.URI]
//
// actions := make([]protocol.CodeAction, 0, 1)
return nil, nil
}

View File

@ -0,0 +1,12 @@
package lsp
import (
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionParams) (any, error) {
// p := documentParserMap[params.TextDocument.URI]
return nil, nil
}

View File

@ -0,0 +1,42 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/analyzer"
"config-lsp/utils"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentDidChange(
context *glsp.Context,
params *protocol.DidChangeTextDocumentParams,
) error {
content := params.ContentChanges[0].(protocol.TextDocumentContentChangeEventWhole).Text
common.ClearDiagnostics(context, params.TextDocument.URI)
document := aliases.DocumentParserMap[params.TextDocument.URI]
document.Parser.Clear()
diagnostics := make([]protocol.Diagnostic, 0)
errors := document.Parser.Parse(content)
if len(errors) > 0 {
diagnostics = append(diagnostics, utils.Map(
errors,
func(err common.LSPError) protocol.Diagnostic {
return err.ToDiagnostic()
},
)...)
}
diagnostics = append(diagnostics, analyzer.Analyze(document)...)
if len(diagnostics) > 0 {
common.SendDiagnostics(context, params.TextDocument.URI, diagnostics)
}
return nil
}

View File

@ -0,0 +1,13 @@
package lsp
import (
"config-lsp/handlers/hosts"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentDidClose(context *glsp.Context, params *protocol.DidCloseTextDocumentParams) error {
delete(hosts.DocumentParserMap, params.TextDocument.URI)
return nil
}

View File

@ -0,0 +1,45 @@
package lsp
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/analyzer"
"config-lsp/handlers/aliases/ast"
"config-lsp/utils"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentDidOpen(
context *glsp.Context,
params *protocol.DidOpenTextDocumentParams,
) error {
common.ClearDiagnostics(context, params.TextDocument.URI)
parser := ast.NewAliasesParser()
document := aliases.AliasesDocument{
Parser: &parser,
}
aliases.DocumentParserMap[params.TextDocument.URI] = &document
errors := parser.Parse(params.TextDocument.Text)
diagnostics := utils.Map(
errors,
func(err common.LSPError) protocol.Diagnostic {
return err.ToDiagnostic()
},
)
diagnostics = append(
diagnostics,
analyzer.Analyze(&document)...,
)
if len(diagnostics) > 0 {
common.SendDiagnostics(context, params.TextDocument.URI, diagnostics)
}
return nil
}

View File

@ -0,0 +1,86 @@
package lsp
import (
"config-lsp/handlers/hosts"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentHover(
context *glsp.Context,
params *protocol.HoverParams,
) (*protocol.Hover, error) {
document := hosts.DocumentParserMap[params.TextDocument.URI]
line := params.Position.Line
// character := params.Position.Character
if _, found := document.Parser.CommentLines[line]; found {
// Comment
return nil, nil
}
// entry, found := document.Parser.Tree.Entries[line]
//
// if !found {
// // Empty line
// return nil, nil
// }
//
// target := handlers.GetHoverTargetInEntry(*entry, character)
//
// var hostname *ast.HostsHostname
//
// switch *target {
// case handlers.HoverTargetIPAddress:
// relativeCursor := character - entry.IPAddress.Location.Start.Character
// hover := fields.IPAddressField.FetchHoverInfo(entry.IPAddress.Value.String(), relativeCursor)
//
// return &protocol.Hover{
// Contents: hover,
// }, nil
// case handlers.HoverTargetHostname:
// hostname = entry.Hostname
// case handlers.HoverTargetAlias:
// for _, alias := range entry.Aliases {
// if alias.Location.Start.Character <= character && character <= alias.Location.End.Character {
// hostname = alias
// break
// }
// }
// }
//
// if hostname != nil {
// contents := []string{
// "## Hostname",
// }
// contents = append(
// contents,
// fields.HostnameField.GetTypeDescription()...,
// )
// contents = append(
// contents,
// []string{
// "",
// }...,
// )
// contents = append(
// contents,
// fields.HostnameField.Documentation,
// )
// contents = append(
// contents,
// handlers.GetHoverInfoForHostname(*document, *hostname, character)...,
// )
//
// return &protocol.Hover{
// Contents: &protocol.MarkupContent{
// Kind: protocol.MarkupKindMarkdown,
// Value: strings.Join(contents, "\n"),
// },
// }, nil
// }
return nil, nil
}

View File

@ -0,0 +1,21 @@
package lsp
import (
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func WorkspaceExecuteCommand(context *glsp.Context, params *protocol.ExecuteCommandParams) (*protocol.ApplyWorkspaceEditParams, error) {
// _, command, _ := strings.Cut(params.Command, ".")
//
// switch command {
// case string(handlers.CodeActionInlineAliases):
// args := handlers.CodeActionInlineAliasesArgsFromArguments(params.Arguments[0].(map[string]any))
//
// document := hosts.DocumentParserMap[args.URI]
//
// return args.RunCommand(*document.Parser)
// }
return nil, nil
}

View File

@ -17,6 +17,7 @@ const (
LanguageFstab SupportedLanguage = "fstab" LanguageFstab SupportedLanguage = "fstab"
LanguageWireguard SupportedLanguage = "languagewireguard" LanguageWireguard SupportedLanguage = "languagewireguard"
LanguageHosts SupportedLanguage = "hosts" LanguageHosts SupportedLanguage = "hosts"
LanguageAliases SupportedLanguage = "aliases"
) )
var AllSupportedLanguages = []string{ var AllSupportedLanguages = []string{
@ -24,6 +25,7 @@ var AllSupportedLanguages = []string{
string(LanguageFstab), string(LanguageFstab),
string(LanguageWireguard), string(LanguageWireguard),
string(LanguageHosts), string(LanguageHosts),
string(LanguageAliases),
} }
type FatalFileNotReadableError struct { type FatalFileNotReadableError struct {
@ -64,6 +66,9 @@ var valueToLanguageMap = map[string]SupportedLanguage{
"host": LanguageHosts, "host": LanguageHosts,
"hosts": LanguageHosts, "hosts": LanguageHosts,
"etc/hosts": LanguageHosts, "etc/hosts": LanguageHosts,
"aliases": LanguageAliases,
"etc/aliases": LanguageAliases,
} }
var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`) var typeOverwriteRegex = regexp.MustCompile(`#\?\s*lsp\.language\s*=\s*(\w+)\s*`)
@ -111,6 +116,8 @@ func DetectLanguage(
return LanguageFstab, nil return LanguageFstab, nil
case "file:///etc/hosts": case "file:///etc/hosts":
return LanguageHosts, nil return LanguageHosts, nil
case "file:///etc/aliases":
return LanguageAliases, nil
} }
if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) { if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) {

View File

@ -1,6 +1,7 @@
package roothandler package roothandler
import ( import (
aliases "config-lsp/handlers/aliases/lsp"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
wireguard "config-lsp/handlers/wireguard/lsp" wireguard "config-lsp/handlers/wireguard/lsp"
@ -30,6 +31,8 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa
return nil, nil return nil, nil
case LanguageWireguard: case LanguageWireguard:
return wireguard.TextDocumentCodeAction(context, params) return wireguard.TextDocumentCodeAction(context, params)
case LanguageAliases:
return aliases.TextDocumentCodeAction(context, params)
} }
panic("root-handler/TextDocumentCompletion: unexpected language" + *language) panic("root-handler/TextDocumentCompletion: unexpected language" + *language)

View File

@ -1,6 +1,7 @@
package roothandler package roothandler
import ( import (
aliases "config-lsp/handlers/aliases/lsp"
"config-lsp/handlers/fstab" "config-lsp/handlers/fstab"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
wireguard "config-lsp/handlers/wireguard/lsp" wireguard "config-lsp/handlers/wireguard/lsp"
@ -31,6 +32,8 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
return wireguard.TextDocumentCompletion(context, params) return wireguard.TextDocumentCompletion(context, params)
case LanguageHosts: case LanguageHosts:
return hosts.TextDocumentCompletion(context, params) return hosts.TextDocumentCompletion(context, params)
case LanguageAliases:
return aliases.TextDocumentCompletion(context, params)
} }
panic("root-handler/TextDocumentCompletion: unexpected language" + *language) panic("root-handler/TextDocumentCompletion: unexpected language" + *language)

View File

@ -1,6 +1,7 @@
package roothandler package roothandler
import ( import (
aliases "config-lsp/handlers/aliases/lsp"
"config-lsp/handlers/fstab" "config-lsp/handlers/fstab"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
wireguard "config-lsp/handlers/wireguard/lsp" wireguard "config-lsp/handlers/wireguard/lsp"
@ -45,6 +46,8 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText
return wireguard.TextDocumentDidOpen(context, params) return wireguard.TextDocumentDidOpen(context, params)
case LanguageHosts: case LanguageHosts:
return hosts.TextDocumentDidOpen(context, params) return hosts.TextDocumentDidOpen(context, params)
case LanguageAliases:
return aliases.TextDocumentDidOpen(context, params)
} }
} }
@ -57,6 +60,8 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText
return wireguard.TextDocumentDidChange(context, params) return wireguard.TextDocumentDidChange(context, params)
case LanguageHosts: case LanguageHosts:
return hosts.TextDocumentDidChange(context, params) return hosts.TextDocumentDidChange(context, params)
case LanguageAliases:
return aliases.TextDocumentDidChange(context, params)
} }
panic("root-handler/TextDocumentDidChange: unexpected language" + *language) panic("root-handler/TextDocumentDidChange: unexpected language" + *language)

View File

@ -1,6 +1,7 @@
package roothandler package roothandler
import ( import (
aliases "config-lsp/handlers/aliases/lsp"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
wireguard "config-lsp/handlers/wireguard/lsp" wireguard "config-lsp/handlers/wireguard/lsp"
@ -31,6 +32,8 @@ func TextDocumentDidClose(context *glsp.Context, params *protocol.DidCloseTextDo
return wireguard.TextDocumentDidClose(context, params) return wireguard.TextDocumentDidClose(context, params)
case LanguageHosts: case LanguageHosts:
return hosts.TextDocumentDidClose(context, params) return hosts.TextDocumentDidClose(context, params)
case LanguageAliases:
return aliases.TextDocumentDidClose(context, params)
default: default:
} }

View File

@ -2,6 +2,7 @@ package roothandler
import ( import (
"config-lsp/common" "config-lsp/common"
aliases "config-lsp/handlers/aliases/lsp"
fstab "config-lsp/handlers/fstab" fstab "config-lsp/handlers/fstab"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
wireguard "config-lsp/handlers/wireguard/lsp" wireguard "config-lsp/handlers/wireguard/lsp"
@ -36,6 +37,8 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu
return wireguard.TextDocumentDidOpen(context, params) return wireguard.TextDocumentDidOpen(context, params)
case LanguageHosts: case LanguageHosts:
return hosts.TextDocumentDidOpen(context, params) return hosts.TextDocumentDidOpen(context, params)
case LanguageAliases:
return aliases.TextDocumentDidOpen(context, params)
} }
panic(fmt.Sprintf("unexpected roothandler.SupportedLanguage: %#v", language)) panic(fmt.Sprintf("unexpected roothandler.SupportedLanguage: %#v", language))

View File

@ -1,6 +1,7 @@
package roothandler package roothandler
import ( import (
aliases "config-lsp/handlers/aliases/lsp"
"config-lsp/handlers/fstab" "config-lsp/handlers/fstab"
hosts "config-lsp/handlers/hosts/lsp" hosts "config-lsp/handlers/hosts/lsp"
wireguard "config-lsp/handlers/wireguard/lsp" wireguard "config-lsp/handlers/wireguard/lsp"
@ -31,6 +32,8 @@ func TextDocumentHover(context *glsp.Context, params *protocol.HoverParams) (*pr
return fstab.TextDocumentHover(context, params) return fstab.TextDocumentHover(context, params)
case LanguageWireguard: case LanguageWireguard:
return wireguard.TextDocumentHover(context, params) return wireguard.TextDocumentHover(context, params)
case LanguageAliases:
return aliases.TextDocumentHover(context, params)
} }
panic("root-handler/TextDocumentHover: unexpected language" + *language) panic("root-handler/TextDocumentHover: unexpected language" + *language)