diff --git a/handlers/aliases/analyzer/analyzer.go b/handlers/aliases/analyzer/analyzer.go index 98cd418..2f399d1 100644 --- a/handlers/aliases/analyzer/analyzer.go +++ b/handlers/aliases/analyzer/analyzer.go @@ -1,13 +1,33 @@ package analyzer import ( - "config-lsp/handlers/hosts" + "config-lsp/common" + "config-lsp/handlers/aliases" + "config-lsp/utils" protocol "github.com/tliron/glsp/protocol_3_16" ) func Analyze( - d *hosts.HostsDocument, + d *aliases.AliasesDocument, ) []protocol.Diagnostic { - return nil + errors := analyzeValuesAreValid(*d.Parser) + + if len(errors) > 0 { + return utils.Map( + errors, + func(err common.LSPError) protocol.Diagnostic { + return err.ToDiagnostic() + }, + ) + } + + errors = append(errors, analyzeDoubleKeys(d)...) + + return utils.Map( + errors, + func(err common.LSPError) protocol.Diagnostic { + return err.ToDiagnostic() + }, + ) } diff --git a/handlers/aliases/analyzer/double_keys.go b/handlers/aliases/analyzer/double_keys.go new file mode 100644 index 0000000..68afeb5 --- /dev/null +++ b/handlers/aliases/analyzer/double_keys.go @@ -0,0 +1,21 @@ +package analyzer + +import ( + "config-lsp/common" + "config-lsp/handlers/aliases" + "config-lsp/handlers/aliases/indexes" +) + +func analyzeDoubleKeys( + d *aliases.AliasesDocument, +) []common.LSPError { + indexes, errors := indexes.CreateIndexes(*d.Parser) + + d.Indexes = &indexes + + if len(errors) > 0 { + return errors + } + + return nil +} diff --git a/handlers/aliases/analyzer/double_keys_test.go b/handlers/aliases/analyzer/double_keys_test.go new file mode 100644 index 0000000..c0e9214 --- /dev/null +++ b/handlers/aliases/analyzer/double_keys_test.go @@ -0,0 +1,73 @@ +package analyzer + +import ( + "config-lsp/handlers/aliases" + "config-lsp/handlers/aliases/ast" + "config-lsp/utils" + "testing" +) + +func TestWorks( + t *testing.T, +) { + input := utils.Dedent(` +support: michael +marketing: john +support: jane +`) + p := ast.NewAliasesParser() + errors := p.Parse(input) + + d := aliases.AliasesDocument{ + Parser: &p, + } + + if len(errors) != 0 { + t.Errorf("Expected no errors, got %v", errors) + } + + errors = analyzeDoubleKeys(&d) + + if !(len(errors) == 1) { + t.Errorf("Expected 1 error, got %v", errors) + } + + if d.Indexes == nil { + t.Errorf("Expected indexes to be set") + } +} + +func TestValidWorks( + t *testing.T, +) { + input := utils.Dedent(` +support: michael +marketing: john +supportgroup: jane +suppor: jane +`) + p := ast.NewAliasesParser() + errors := p.Parse(input) + + d := aliases.AliasesDocument{ + Parser: &p, + } + + if len(errors) != 0 { + t.Errorf("Expected no errors, got %v", errors) + } + + errors = analyzeDoubleKeys(&d) + + if !(len(errors) == 0) { + t.Errorf("Expected 0 errors, got %v", errors) + } + + if d.Indexes == nil { + t.Errorf("Expected indexes to be set") + } + + if d.Indexes.Keys["support"] == nil { + t.Errorf("Expected support to be in indexes") + } +} diff --git a/handlers/aliases/shared.go b/handlers/aliases/shared.go index 86ef6be..b47d360 100644 --- a/handlers/aliases/shared.go +++ b/handlers/aliases/shared.go @@ -7,9 +7,9 @@ import ( protocol "github.com/tliron/glsp/protocol_3_16" ) -type HostsDocument struct { +type AliasesDocument struct { Parser *ast.AliasesParser Indexes *indexes.AliasesIndexes } -var DocumentParserMap = map[protocol.DocumentUri]*HostsDocument{} +var DocumentParserMap = map[protocol.DocumentUri]*AliasesDocument{}