mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-19 07:25:27 +02:00
refactor(hosts): Improve hosts structure
This commit is contained in:
parent
a5f834a38b
commit
48bbb0f68b
@ -2,13 +2,14 @@ package analyzer
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/utils"
|
||||
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
func Analyze(parser *HostsParser) []protocol.Diagnostic {
|
||||
errors := analyzeEntriesSetCorrectly(*parser)
|
||||
func Analyze(document *hosts.HostsDocument) []protocol.Diagnostic {
|
||||
errors := analyzeEntriesSetCorrectly(*document.Parser)
|
||||
|
||||
if len(errors) > 0 {
|
||||
return utils.Map(
|
||||
@ -19,7 +20,7 @@ func Analyze(parser *HostsParser) []protocol.Diagnostic {
|
||||
)
|
||||
}
|
||||
|
||||
errors = analyzeEntriesAreValid(*parser)
|
||||
errors = analyzeEntriesAreValid(*document.Parser)
|
||||
|
||||
if len(errors) > 0 {
|
||||
return utils.Map(
|
||||
@ -30,8 +31,8 @@ func Analyze(parser *HostsParser) []protocol.Diagnostic {
|
||||
)
|
||||
}
|
||||
|
||||
errors = append(errors, analyzeDoubleIPs(parser)...)
|
||||
errors = append(errors, analyzeDoubleHostNames(parser)...)
|
||||
errors = append(errors, analyzeDoubleIPs(document)...)
|
||||
errors = append(errors, analyzeDoubleHostNames(document)...)
|
||||
|
||||
return utils.Map(
|
||||
errors,
|
||||
|
@ -2,6 +2,8 @@ package analyzer
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/shared"
|
||||
"net"
|
||||
)
|
||||
|
||||
@ -9,22 +11,22 @@ func ipToString(ip net.IPAddr) string {
|
||||
return ip.IP.String()
|
||||
}
|
||||
|
||||
func analyzeDoubleIPs(p *HostsParser) []common.LSPError {
|
||||
func analyzeDoubleIPs(d *hosts.HostsDocument) []common.LSPError {
|
||||
errors := make([]common.LSPError, 0)
|
||||
ips := make(map[string]uint32)
|
||||
|
||||
p.DoubleIPs = make(map[uint32]DuplicateIPDeclaration)
|
||||
d.Indexes.DoubleIPs = make(map[uint32]shared.DuplicateIPDeclaration)
|
||||
|
||||
for lineNumber, entry := range p.Tree.Entries {
|
||||
for lineNumber, entry := range d.Parser.Tree.Entries {
|
||||
if entry.IPAddress != nil {
|
||||
key := ipToString(entry.IPAddress.Value)
|
||||
|
||||
if foundLine, found := ips[key]; found {
|
||||
err := DuplicateIPDeclaration{
|
||||
err := shared.DuplicateIPDeclaration{
|
||||
AlreadyFoundAt: foundLine,
|
||||
}
|
||||
|
||||
p.DoubleIPs[lineNumber] = err
|
||||
d.Indexes.DoubleIPs[lineNumber] = err
|
||||
errors = append(errors, common.LSPError{
|
||||
Range: entry.IPAddress.Location,
|
||||
Err: err,
|
||||
|
@ -1,6 +1,10 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/handlers/hosts/indexes"
|
||||
"config-lsp/handlers/hosts/shared"
|
||||
"config-lsp/utils"
|
||||
"testing"
|
||||
)
|
||||
@ -15,14 +19,19 @@ func TestWorksWithNonDoubleIPs(
|
||||
1.2.3.6 bar.com
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
i := indexes.NewHostsIndexes()
|
||||
document := hosts.HostsDocument{
|
||||
Parser: &parser,
|
||||
Indexes: &i,
|
||||
}
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) != 0 {
|
||||
t.Fatalf("PARER FAILED! Expected no errors, but got %v", errors)
|
||||
}
|
||||
|
||||
errors = analyzeDoubleIPs(&parser)
|
||||
errors = analyzeDoubleIPs(&document)
|
||||
|
||||
if len(errors) != 0 {
|
||||
t.Errorf("Expected no errors, but got %v", errors)
|
||||
@ -38,14 +47,19 @@ func TestWorksWithDoubleIPs(
|
||||
1.2.3.4 foo.com
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
i := indexes.NewHostsIndexes()
|
||||
document := hosts.HostsDocument{
|
||||
Parser: &parser,
|
||||
Indexes: &i,
|
||||
}
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) != 0 {
|
||||
t.Fatalf("PARER FAILED! Expected no errors, but got %v", errors)
|
||||
}
|
||||
|
||||
errors = analyzeDoubleIPs(&parser)
|
||||
errors = analyzeDoubleIPs(&document)
|
||||
|
||||
if !(len(errors) == 1) {
|
||||
t.Errorf("Expected 1 error, but got %v", len(errors))
|
||||
@ -55,7 +69,7 @@ func TestWorksWithDoubleIPs(
|
||||
t.Errorf("Expected error on line 2, but got %v", errors[0].Range.Start.Line)
|
||||
}
|
||||
|
||||
if !(errors[0].Err.(DuplicateIPDeclaration).AlreadyFoundAt == 0) {
|
||||
t.Errorf("Expected error on line 0, but got %v", errors[0].Err.(DuplicateIPDeclaration).AlreadyFoundAt)
|
||||
if !(errors[0].Err.(shared.DuplicateIPDeclaration).AlreadyFoundAt == 0) {
|
||||
t.Errorf("Expected error on line 0, but got %v", errors[0].Err.(shared.DuplicateIPDeclaration).AlreadyFoundAt)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/utils"
|
||||
"net"
|
||||
"testing"
|
||||
@ -13,7 +14,7 @@ func TestValidSimpleExampleWorks(
|
||||
1.2.3.4 hello.com
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) != 0 {
|
||||
@ -80,7 +81,7 @@ func TestValidComplexExampleWorks(
|
||||
1.2.3.4 example.com check.com
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) != 0 {
|
||||
@ -115,7 +116,7 @@ func TestInvalidExampleWorks(
|
||||
1.2.3.4
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) == 0 {
|
||||
|
@ -2,33 +2,19 @@ package analyzer
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/handlers/hosts/indexes"
|
||||
"config-lsp/handlers/hosts/shared"
|
||||
"config-lsp/utils"
|
||||
"net"
|
||||
)
|
||||
|
||||
type ResolverEntry struct {
|
||||
IPv4Address net.IP
|
||||
IPv6Address net.IP
|
||||
Line uint32
|
||||
}
|
||||
|
||||
func (e ResolverEntry) GetInfo() string {
|
||||
if e.IPv4Address != nil {
|
||||
return e.IPv4Address.String()
|
||||
}
|
||||
|
||||
return e.IPv6Address.String()
|
||||
}
|
||||
|
||||
type Resolver struct {
|
||||
Entries map[string]ResolverEntry
|
||||
}
|
||||
|
||||
func createEntry(
|
||||
line uint32,
|
||||
ip net.IP,
|
||||
) ResolverEntry {
|
||||
entry := ResolverEntry{
|
||||
) indexes.ResolverEntry {
|
||||
entry := indexes.ResolverEntry{
|
||||
Line: line,
|
||||
}
|
||||
|
||||
@ -46,10 +32,10 @@ type hostnameEntry struct {
|
||||
HostName string
|
||||
}
|
||||
|
||||
func createResolverFromParser(p HostsParser) (Resolver, []common.LSPError) {
|
||||
func createResolverFromParser(p ast.HostsParser) (indexes.Resolver, []common.LSPError) {
|
||||
errors := make([]common.LSPError, 0)
|
||||
resolver := Resolver{
|
||||
Entries: make(map[string]ResolverEntry),
|
||||
resolver := indexes.Resolver{
|
||||
Entries: make(map[string]indexes.ResolverEntry),
|
||||
}
|
||||
|
||||
for lineNumber, entry := range p.Tree.Entries {
|
||||
@ -63,7 +49,7 @@ func createResolverFromParser(p HostsParser) (Resolver, []common.LSPError) {
|
||||
},
|
||||
utils.Map(
|
||||
entry.Aliases,
|
||||
func(alias *HostsHostname) hostnameEntry {
|
||||
func(alias *ast.HostsHostname) hostnameEntry {
|
||||
return hostnameEntry{
|
||||
Location: alias.Location,
|
||||
HostName: alias.Value,
|
||||
@ -83,7 +69,7 @@ func createResolverFromParser(p HostsParser) (Resolver, []common.LSPError) {
|
||||
errors,
|
||||
common.LSPError{
|
||||
Range: hostName.Location,
|
||||
Err: DuplicateHostEntry{
|
||||
Err: shared.DuplicateHostEntry{
|
||||
AlreadyFoundAt: resolv.Line,
|
||||
Hostname: hostName.HostName,
|
||||
},
|
||||
@ -99,10 +85,10 @@ func createResolverFromParser(p HostsParser) (Resolver, []common.LSPError) {
|
||||
return resolver, errors
|
||||
}
|
||||
|
||||
func analyzeDoubleHostNames(p *HostsParser) []common.LSPError {
|
||||
resolver, errors := createResolverFromParser(*p)
|
||||
func analyzeDoubleHostNames(d *hosts.HostsDocument) []common.LSPError {
|
||||
resolver, errors := createResolverFromParser(*d.Parser)
|
||||
|
||||
p.Resolver = &resolver
|
||||
d.Indexes.Resolver = &resolver
|
||||
|
||||
return errors
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/utils"
|
||||
"testing"
|
||||
)
|
||||
@ -13,7 +14,7 @@ func TestResolverEntriesWorksWithNonOverlapping(
|
||||
5.5.5.5 world.com
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) != 0 {
|
||||
@ -55,7 +56,7 @@ func TestResolverEntriesWithSimpleOverlapping(
|
||||
5.5.5.5 hello.com
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) != 0 {
|
||||
@ -85,7 +86,7 @@ func TestResolverEntriesWithComplexOverlapping(
|
||||
5.5.5.5 check.com test.com
|
||||
`)
|
||||
|
||||
parser := CreateNewHostsParser()
|
||||
parser := ast.NewHostsParser()
|
||||
errors := parser.Parse(input)
|
||||
|
||||
if len(errors) != 0 {
|
||||
|
@ -4,12 +4,13 @@ import (
|
||||
"config-lsp/common"
|
||||
docvalues "config-lsp/doc-values"
|
||||
"config-lsp/handlers/hosts/fields"
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/utils"
|
||||
"errors"
|
||||
)
|
||||
|
||||
func analyzeEntriesSetCorrectly(
|
||||
parser HostsParser,
|
||||
parser ast.HostsParser,
|
||||
) []common.LSPError {
|
||||
err := make([]common.LSPError, 0)
|
||||
|
||||
@ -35,7 +36,7 @@ func analyzeEntriesSetCorrectly(
|
||||
}
|
||||
|
||||
func analyzeEntriesAreValid(
|
||||
parser HostsParser,
|
||||
parser ast.HostsParser,
|
||||
) []common.LSPError {
|
||||
err := make([]common.LSPError, 0)
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package analyzer
|
||||
package ast
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
@ -9,9 +9,6 @@ import (
|
||||
type HostsParser struct {
|
||||
Tree HostsTree
|
||||
CommentLines map[uint32]struct{}
|
||||
Resolver *Resolver
|
||||
// [line]error
|
||||
DoubleIPs map[uint32]DuplicateIPDeclaration
|
||||
}
|
||||
|
||||
type HostsTree struct {
|
@ -1,4 +1,4 @@
|
||||
package analyzer
|
||||
package ast
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
@ -1,4 +1,4 @@
|
||||
package analyzer
|
||||
package ast
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
@ -74,7 +74,7 @@ func (p *HostsParser) Parse(input string) []common.LSPError {
|
||||
return errors
|
||||
}
|
||||
|
||||
func CreateNewHostsParser() HostsParser {
|
||||
func NewHostsParser() HostsParser {
|
||||
p := HostsParser{}
|
||||
p.Clear()
|
||||
|
@ -1,7 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts/handlers/analyzer"
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/utils"
|
||||
"fmt"
|
||||
"strings"
|
||||
@ -16,7 +16,7 @@ const (
|
||||
)
|
||||
|
||||
type CodeAction interface {
|
||||
RunCommand(analyzer.HostsParser) (*protocol.ApplyWorkspaceEditParams, error)
|
||||
RunCommand(ast.HostsParser) (*protocol.ApplyWorkspaceEditParams, error)
|
||||
}
|
||||
|
||||
type CodeActionArgs interface{}
|
||||
@ -35,7 +35,7 @@ func CodeActionInlineAliasesArgsFromArguments(arguments map[string]any) CodeActi
|
||||
}
|
||||
}
|
||||
|
||||
func (args CodeActionInlineAliasesArgs) RunCommand(hostsParser analyzer.HostsParser) (*protocol.ApplyWorkspaceEditParams, error) {
|
||||
func (args CodeActionInlineAliasesArgs) RunCommand(hostsParser ast.HostsParser) (*protocol.ApplyWorkspaceEditParams, error) {
|
||||
fromEntry := hostsParser.Tree.Entries[args.FromLine]
|
||||
toEntry := hostsParser.Tree.Entries[args.ToLine]
|
||||
|
||||
@ -58,7 +58,7 @@ func (args CodeActionInlineAliasesArgs) RunCommand(hostsParser analyzer.HostsPar
|
||||
},
|
||||
utils.Map(
|
||||
fromEntry.Aliases,
|
||||
func(alias *analyzer.HostsHostname) string {
|
||||
func(alias *ast.HostsHostname) string {
|
||||
return alias.Value
|
||||
},
|
||||
)...,
|
||||
|
@ -1,17 +1,17 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts/handlers/analyzer"
|
||||
"config-lsp/handlers/hosts"
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
func GetInlineAliasesCodeAction(
|
||||
p analyzer.HostsParser,
|
||||
d hosts.HostsDocument,
|
||||
params *protocol.CodeActionParams,
|
||||
) []protocol.CodeAction {
|
||||
line := params.Range.Start.Line
|
||||
|
||||
if duplicateInfo, found := p.DoubleIPs[line]; found {
|
||||
if duplicateInfo, found := d.Indexes.DoubleIPs[line]; found {
|
||||
commandID := "hosts." + CodeActionInlineAliases
|
||||
command := protocol.Command{
|
||||
Title: "Inline Aliases",
|
||||
|
@ -1,7 +1,8 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts/handlers/analyzer"
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@ -14,7 +15,7 @@ const (
|
||||
)
|
||||
|
||||
func GetHoverTargetInEntry(
|
||||
e analyzer.HostsEntry,
|
||||
e ast.HostsEntry,
|
||||
cursor uint32,
|
||||
) *HoverTarget {
|
||||
if e.IPAddress != nil && e.IPAddress.Location.ContainsCursorByCharacter(cursor) {
|
||||
@ -38,11 +39,11 @@ func GetHoverTargetInEntry(
|
||||
}
|
||||
|
||||
func GetHoverInfoForHostname(
|
||||
parser analyzer.HostsParser,
|
||||
hostname analyzer.HostsHostname,
|
||||
d hosts.HostsDocument,
|
||||
hostname ast.HostsHostname,
|
||||
cursor uint32,
|
||||
) []string {
|
||||
ipAddress := parser.Resolver.Entries[hostname.Value]
|
||||
ipAddress := d.Indexes.Resolver.Entries[hostname.Value]
|
||||
|
||||
return []string{
|
||||
fmt.Sprintf("**%s** maps to _%s_", hostname.Value, ipAddress.GetInfo()),
|
||||
|
15
handlers/hosts/indexes/indexes.go
Normal file
15
handlers/hosts/indexes/indexes.go
Normal file
@ -0,0 +1,15 @@
|
||||
package indexes
|
||||
|
||||
import "config-lsp/handlers/hosts/shared"
|
||||
|
||||
type HostsIndexes struct {
|
||||
Resolver *Resolver
|
||||
// [line]error
|
||||
DoubleIPs map[uint32]shared.DuplicateIPDeclaration
|
||||
}
|
||||
|
||||
func NewHostsIndexes() HostsIndexes {
|
||||
return HostsIndexes{
|
||||
DoubleIPs: make(map[uint32]shared.DuplicateIPDeclaration),
|
||||
}
|
||||
}
|
23
handlers/hosts/indexes/resolver.go
Normal file
23
handlers/hosts/indexes/resolver.go
Normal file
@ -0,0 +1,23 @@
|
||||
package indexes
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
type ResolverEntry struct {
|
||||
IPv4Address net.IP
|
||||
IPv6Address net.IP
|
||||
Line uint32
|
||||
}
|
||||
|
||||
func (e ResolverEntry) GetInfo() string {
|
||||
if e.IPv4Address != nil {
|
||||
return e.IPv4Address.String()
|
||||
}
|
||||
|
||||
return e.IPv6Address.String()
|
||||
}
|
||||
|
||||
type Resolver struct {
|
||||
Entries map[string]ResolverEntry
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package lsp
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts/handlers/analyzer"
|
||||
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
var documentParserMap = map[protocol.DocumentUri]*analyzer.HostsParser{}
|
@ -1,18 +1,18 @@
|
||||
package lsp
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/handlers"
|
||||
|
||||
"github.com/tliron/glsp"
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
||||
parser := documentParserMap[params.TextDocument.URI]
|
||||
document := hosts.DocumentParserMap[params.TextDocument.URI]
|
||||
|
||||
actions := make([]protocol.CodeAction, 0, 1)
|
||||
|
||||
actions = append(actions, handlers.GetInlineAliasesCodeAction(*parser, params)...)
|
||||
actions = append(actions, handlers.GetInlineAliasesCodeAction(*document, params)...)
|
||||
|
||||
if len(actions) > 0 {
|
||||
return actions, nil
|
||||
|
@ -2,6 +2,7 @@ package lsp
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/handlers/analyzer"
|
||||
"config-lsp/utils"
|
||||
|
||||
@ -16,11 +17,11 @@ func TextDocumentDidChange(
|
||||
content := params.ContentChanges[0].(protocol.TextDocumentContentChangeEventWhole).Text
|
||||
common.ClearDiagnostics(context, params.TextDocument.URI)
|
||||
|
||||
parser := documentParserMap[params.TextDocument.URI]
|
||||
parser.Clear()
|
||||
document := hosts.DocumentParserMap[params.TextDocument.URI]
|
||||
document.Parser.Clear()
|
||||
|
||||
diagnostics := make([]protocol.Diagnostic, 0)
|
||||
errors := parser.Parse(content)
|
||||
errors := document.Parser.Parse(content)
|
||||
|
||||
if len(errors) > 0 {
|
||||
diagnostics = append(diagnostics, utils.Map(
|
||||
@ -31,7 +32,7 @@ func TextDocumentDidChange(
|
||||
)...)
|
||||
}
|
||||
|
||||
diagnostics = append(diagnostics, analyzer.Analyze(parser)...)
|
||||
diagnostics = append(diagnostics, analyzer.Analyze(document)...)
|
||||
|
||||
if len(diagnostics) > 0 {
|
||||
common.SendDiagnostics(context, params.TextDocument.URI, diagnostics)
|
||||
|
@ -1,12 +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(documentParserMap, params.TextDocument.URI)
|
||||
delete(hosts.DocumentParserMap, params.TextDocument.URI)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -2,7 +2,10 @@ package lsp
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/handlers/analyzer"
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/handlers/hosts/indexes"
|
||||
"config-lsp/utils"
|
||||
|
||||
"github.com/tliron/glsp"
|
||||
@ -15,8 +18,13 @@ func TextDocumentDidOpen(
|
||||
) error {
|
||||
common.ClearDiagnostics(context, params.TextDocument.URI)
|
||||
|
||||
parser := analyzer.CreateNewHostsParser()
|
||||
documentParserMap[params.TextDocument.URI] = &parser
|
||||
parser := ast.NewHostsParser()
|
||||
i := indexes.NewHostsIndexes()
|
||||
document := hosts.HostsDocument{
|
||||
Parser: &parser,
|
||||
Indexes: &i,
|
||||
}
|
||||
hosts.DocumentParserMap[params.TextDocument.URI] = &document
|
||||
|
||||
errors := parser.Parse(params.TextDocument.Text)
|
||||
|
||||
@ -29,7 +37,7 @@ func TextDocumentDidOpen(
|
||||
|
||||
diagnostics = append(
|
||||
diagnostics,
|
||||
analyzer.Analyze(&parser)...,
|
||||
analyzer.Analyze(&document)...,
|
||||
)
|
||||
|
||||
if len(diagnostics) > 0 {
|
||||
|
@ -1,9 +1,10 @@
|
||||
package lsp
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/fields"
|
||||
"config-lsp/handlers/hosts/handlers"
|
||||
"config-lsp/handlers/hosts/handlers/analyzer"
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"strings"
|
||||
|
||||
"github.com/tliron/glsp"
|
||||
@ -14,17 +15,17 @@ func TextDocumentHover(
|
||||
context *glsp.Context,
|
||||
params *protocol.HoverParams,
|
||||
) (*protocol.Hover, error) {
|
||||
parser := documentParserMap[params.TextDocument.URI]
|
||||
document := hosts.DocumentParserMap[params.TextDocument.URI]
|
||||
|
||||
line := params.Position.Line
|
||||
character := params.Position.Character
|
||||
|
||||
if _, found := parser.CommentLines[line]; found {
|
||||
if _, found := document.Parser.CommentLines[line]; found {
|
||||
// Comment
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
entry, found := parser.Tree.Entries[line]
|
||||
entry, found := document.Parser.Tree.Entries[line]
|
||||
|
||||
if !found {
|
||||
// Empty line
|
||||
@ -33,7 +34,7 @@ func TextDocumentHover(
|
||||
|
||||
target := handlers.GetHoverTargetInEntry(*entry, character)
|
||||
|
||||
var hostname *analyzer.HostsHostname
|
||||
var hostname *ast.HostsHostname
|
||||
|
||||
switch *target {
|
||||
case handlers.HoverTargetIPAddress:
|
||||
@ -74,7 +75,7 @@ func TextDocumentHover(
|
||||
)
|
||||
contents = append(
|
||||
contents,
|
||||
handlers.GetHoverInfoForHostname(*parser, *hostname, character)...,
|
||||
handlers.GetHoverInfoForHostname(*document, *hostname, character)...,
|
||||
)
|
||||
|
||||
return &protocol.Hover{
|
||||
|
@ -1,6 +1,7 @@
|
||||
package lsp
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts"
|
||||
"config-lsp/handlers/hosts/handlers"
|
||||
"strings"
|
||||
|
||||
@ -15,9 +16,9 @@ func WorkspaceExecuteCommand(context *glsp.Context, params *protocol.ExecuteComm
|
||||
case string(handlers.CodeActionInlineAliases):
|
||||
args := handlers.CodeActionInlineAliasesArgsFromArguments(params.Arguments[0].(map[string]any))
|
||||
|
||||
parser := documentParserMap[args.URI]
|
||||
document := hosts.DocumentParserMap[args.URI]
|
||||
|
||||
return args.RunCommand(*parser)
|
||||
return args.RunCommand(*document.Parser)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
15
handlers/hosts/shared.go
Normal file
15
handlers/hosts/shared.go
Normal file
@ -0,0 +1,15 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"config-lsp/handlers/hosts/handlers/ast"
|
||||
"config-lsp/handlers/hosts/indexes"
|
||||
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
type HostsDocument struct {
|
||||
Parser *ast.HostsParser
|
||||
Indexes *indexes.HostsIndexes
|
||||
}
|
||||
|
||||
var DocumentParserMap = map[protocol.DocumentUri]*HostsDocument{}
|
@ -1,4 +1,4 @@
|
||||
package analyzer
|
||||
package shared
|
||||
|
||||
import "fmt"
|
||||
|
Loading…
x
Reference in New Issue
Block a user