diff --git a/common/location.go b/common/location.go index fc7b1c8..344b5c1 100644 --- a/common/location.go +++ b/common/location.go @@ -15,6 +15,17 @@ type LocationRange struct { End Location } +var GlobalLocationRange = LocationRange{ + Start: Location{ + Line: 0, + Character: 0, + }, + End: Location{ + Line: 0, + Character: 0, + }, +} + func (l LocationRange) ToLSPRange() protocol.Range { return protocol.Range{ Start: protocol.Position{ diff --git a/handlers/aliases/analyzer/analyzer.go b/handlers/aliases/analyzer/analyzer.go index 2f399d1..abd32d0 100644 --- a/handlers/aliases/analyzer/analyzer.go +++ b/handlers/aliases/analyzer/analyzer.go @@ -23,6 +23,7 @@ func Analyze( } errors = append(errors, analyzeDoubleKeys(d)...) + errors = append(errors, analyzeContainsRequiredKeys(*d)...) return utils.Map( errors, diff --git a/handlers/aliases/analyzer/parser.go b/handlers/aliases/analyzer/parser.go index 1a11fde..25a1f70 100644 --- a/handlers/aliases/analyzer/parser.go +++ b/handlers/aliases/analyzer/parser.go @@ -20,7 +20,7 @@ func analyzeValuesAreValid( if entry.Key == nil { errors = append(errors, common.LSPError{ Range: entry.Location, - Err: ers.New("A name is required"), + Err: ers.New("An alias is required"), }) continue diff --git a/handlers/aliases/analyzer/required_keys.go b/handlers/aliases/analyzer/required_keys.go new file mode 100644 index 0000000..ea320a4 --- /dev/null +++ b/handlers/aliases/analyzer/required_keys.go @@ -0,0 +1,33 @@ +package analyzer + +import ( + "config-lsp/common" + "config-lsp/handlers/aliases" + "config-lsp/handlers/aliases/indexes" + "fmt" + + ers "errors" +) + +var requiredFields = []string{ + indexes.NormalizeKey("mailer-daemon"), + indexes.NormalizeKey("hostmaster"), + indexes.NormalizeKey("postmaster"), +} + +func analyzeContainsRequiredKeys( + d aliases.AliasesDocument, +) []common.LSPError { + errors := make([]common.LSPError, 0) + + for _, requiredField := range requiredFields { + if _, found := d.Indexes.Keys[requiredField]; !found { + errors = append(errors, common.LSPError{ + Range: common.GlobalLocationRange, + Err: ers.New(fmt.Sprintf("Please add the alias '%s'. It is required by the aliases file.", requiredField)), + }) + } + } + + return errors +} diff --git a/handlers/aliases/indexes/indexes.go b/handlers/aliases/indexes/indexes.go index e87cb47..682b0f6 100644 --- a/handlers/aliases/indexes/indexes.go +++ b/handlers/aliases/indexes/indexes.go @@ -11,6 +11,10 @@ type AliasesIndexes struct { Keys map[string]*ast.AliasKey } +func NormalizeKey(key string) string { + return strings.ToLower(key) +} + func CreateIndexes(parser ast.AliasesParser) (AliasesIndexes, []common.LSPError) { errors := make([]common.LSPError, 0) indexes := &AliasesIndexes{ @@ -22,7 +26,7 @@ func CreateIndexes(parser ast.AliasesParser) (AliasesIndexes, []common.LSPError) for it.Next() { entry := it.Value().(*ast.AliasEntry) - normalizedAlias := strings.ToLower(entry.Key.Value) + normalizedAlias := NormalizeKey(entry.Key.Value) if existingEntry, found := indexes.Keys[normalizedAlias]; found { errors = append(errors, common.LSPError{