diff --git a/handlers/hosts/handlers/analyzer/analyzer.go b/handlers/hosts/handlers/analyzer/analyzer.go index 3f512b3..9a1c617 100644 --- a/handlers/hosts/handlers/analyzer/analyzer.go +++ b/handlers/hosts/handlers/analyzer/analyzer.go @@ -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, diff --git a/handlers/hosts/handlers/analyzer/double_ips.go b/handlers/hosts/handlers/analyzer/double_ips.go index b3a01e7..237d5a6 100644 --- a/handlers/hosts/handlers/analyzer/double_ips.go +++ b/handlers/hosts/handlers/analyzer/double_ips.go @@ -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, diff --git a/handlers/hosts/handlers/analyzer/double_ips_test.go b/handlers/hosts/handlers/analyzer/double_ips_test.go index 979dd0b..dda6c3f 100644 --- a/handlers/hosts/handlers/analyzer/double_ips_test.go +++ b/handlers/hosts/handlers/analyzer/double_ips_test.go @@ -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) } } diff --git a/handlers/hosts/handlers/analyzer/handler_test.go b/handlers/hosts/handlers/analyzer/handler_test.go index 3f1b00f..b4c674e 100644 --- a/handlers/hosts/handlers/analyzer/handler_test.go +++ b/handlers/hosts/handlers/analyzer/handler_test.go @@ -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 { diff --git a/handlers/hosts/handlers/analyzer/resolver.go b/handlers/hosts/handlers/analyzer/resolve.go similarity index 65% rename from handlers/hosts/handlers/analyzer/resolver.go rename to handlers/hosts/handlers/analyzer/resolve.go index c06ce96..b060700 100644 --- a/handlers/hosts/handlers/analyzer/resolver.go +++ b/handlers/hosts/handlers/analyzer/resolve.go @@ -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 } diff --git a/handlers/hosts/handlers/analyzer/resolver_test.go b/handlers/hosts/handlers/analyzer/resolver_test.go index f2a7247..b0a2a4b 100644 --- a/handlers/hosts/handlers/analyzer/resolver_test.go +++ b/handlers/hosts/handlers/analyzer/resolver_test.go @@ -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 { diff --git a/handlers/hosts/handlers/analyzer/values.go b/handlers/hosts/handlers/analyzer/values.go index d04c415..ed27721 100644 --- a/handlers/hosts/handlers/analyzer/values.go +++ b/handlers/hosts/handlers/analyzer/values.go @@ -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) diff --git a/handlers/hosts/handlers/analyzer/hosts.go b/handlers/hosts/handlers/ast/hosts.go similarity index 88% rename from handlers/hosts/handlers/analyzer/hosts.go rename to handlers/hosts/handlers/ast/hosts.go index b853e55..5f28219 100644 --- a/handlers/hosts/handlers/analyzer/hosts.go +++ b/handlers/hosts/handlers/ast/hosts.go @@ -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 { diff --git a/handlers/hosts/handlers/analyzer/listener.go b/handlers/hosts/handlers/ast/listener.go similarity index 99% rename from handlers/hosts/handlers/analyzer/listener.go rename to handlers/hosts/handlers/ast/listener.go index 0785908..c5f2782 100644 --- a/handlers/hosts/handlers/analyzer/listener.go +++ b/handlers/hosts/handlers/ast/listener.go @@ -1,4 +1,4 @@ -package analyzer +package ast import ( "config-lsp/common" diff --git a/handlers/hosts/handlers/analyzer/handler.go b/handlers/hosts/handlers/ast/parser.go similarity index 96% rename from handlers/hosts/handlers/analyzer/handler.go rename to handlers/hosts/handlers/ast/parser.go index 33adbcd..03f3c92 100644 --- a/handlers/hosts/handlers/analyzer/handler.go +++ b/handlers/hosts/handlers/ast/parser.go @@ -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() diff --git a/handlers/hosts/handlers/code-actions.go b/handlers/hosts/handlers/code-actions.go index 7de425a..b2f6bc4 100644 --- a/handlers/hosts/handlers/code-actions.go +++ b/handlers/hosts/handlers/code-actions.go @@ -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 }, )..., diff --git a/handlers/hosts/handlers/fetch-code-actions.go b/handlers/hosts/handlers/fetch-code-actions.go index 6374c5e..59b13c4 100644 --- a/handlers/hosts/handlers/fetch-code-actions.go +++ b/handlers/hosts/handlers/fetch-code-actions.go @@ -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", diff --git a/handlers/hosts/handlers/hover.go b/handlers/hosts/handlers/hover.go index 168ba47..2361408 100644 --- a/handlers/hosts/handlers/hover.go +++ b/handlers/hosts/handlers/hover.go @@ -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()), diff --git a/handlers/hosts/indexes/indexes.go b/handlers/hosts/indexes/indexes.go new file mode 100644 index 0000000..512a724 --- /dev/null +++ b/handlers/hosts/indexes/indexes.go @@ -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), + } +} diff --git a/handlers/hosts/indexes/resolver.go b/handlers/hosts/indexes/resolver.go new file mode 100644 index 0000000..aabb6f4 --- /dev/null +++ b/handlers/hosts/indexes/resolver.go @@ -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 +} diff --git a/handlers/hosts/lsp/shared.go b/handlers/hosts/lsp/shared.go deleted file mode 100644 index c231057..0000000 --- a/handlers/hosts/lsp/shared.go +++ /dev/null @@ -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{} diff --git a/handlers/hosts/lsp/text-document-code-action.go b/handlers/hosts/lsp/text-document-code-action.go index d15740e..272e7cc 100644 --- a/handlers/hosts/lsp/text-document-code-action.go +++ b/handlers/hosts/lsp/text-document-code-action.go @@ -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 diff --git a/handlers/hosts/lsp/text-document-did-change.go b/handlers/hosts/lsp/text-document-did-change.go index 4aa141e..e6ba5e6 100644 --- a/handlers/hosts/lsp/text-document-did-change.go +++ b/handlers/hosts/lsp/text-document-did-change.go @@ -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) diff --git a/handlers/hosts/lsp/text-document-did-close.go b/handlers/hosts/lsp/text-document-did-close.go index 3961658..3283aad 100644 --- a/handlers/hosts/lsp/text-document-did-close.go +++ b/handlers/hosts/lsp/text-document-did-close.go @@ -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 } diff --git a/handlers/hosts/lsp/text-document-did-open.go b/handlers/hosts/lsp/text-document-did-open.go index 7be0663..a9886cf 100644 --- a/handlers/hosts/lsp/text-document-did-open.go +++ b/handlers/hosts/lsp/text-document-did-open.go @@ -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 { diff --git a/handlers/hosts/lsp/text-document-hover.go b/handlers/hosts/lsp/text-document-hover.go index 39dc2aa..fd76edb 100644 --- a/handlers/hosts/lsp/text-document-hover.go +++ b/handlers/hosts/lsp/text-document-hover.go @@ -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{ diff --git a/handlers/hosts/lsp/workspace-execute-command.go b/handlers/hosts/lsp/workspace-execute-command.go index 75cb9b6..d67898f 100644 --- a/handlers/hosts/lsp/workspace-execute-command.go +++ b/handlers/hosts/lsp/workspace-execute-command.go @@ -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 diff --git a/handlers/hosts/shared.go b/handlers/hosts/shared.go new file mode 100644 index 0000000..d17e04c --- /dev/null +++ b/handlers/hosts/shared.go @@ -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{} diff --git a/handlers/hosts/handlers/analyzer/errors.go b/handlers/hosts/shared/errors.go similarity index 96% rename from handlers/hosts/handlers/analyzer/errors.go rename to handlers/hosts/shared/errors.go index 9b9a7ff..f8885ca 100644 --- a/handlers/hosts/handlers/analyzer/errors.go +++ b/handlers/hosts/shared/errors.go @@ -1,4 +1,4 @@ -package analyzer +package shared import "fmt"