mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(aliases): Add required keys analyzer
This commit is contained in:
parent
4c2422c3da
commit
742dc48656
@ -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{
|
||||
|
@ -23,6 +23,7 @@ func Analyze(
|
||||
}
|
||||
|
||||
errors = append(errors, analyzeDoubleKeys(d)...)
|
||||
errors = append(errors, analyzeContainsRequiredKeys(*d)...)
|
||||
|
||||
return utils.Map(
|
||||
errors,
|
||||
|
@ -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
|
||||
|
33
handlers/aliases/analyzer/required_keys.go
Normal file
33
handlers/aliases/analyzer/required_keys.go
Normal file
@ -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
|
||||
}
|
@ -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{
|
||||
|
Loading…
x
Reference in New Issue
Block a user