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