refactor(ssh_config): Use Diagnostics instead of LSPError

This commit is contained in:
Myzel394 2024-10-01 15:58:11 +02:00
parent ed57871b4f
commit b7f01f0bd4
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
4 changed files with 67 additions and 32 deletions

View File

@ -1,5 +1,7 @@
package common package common
import protocol "github.com/tliron/glsp/protocol_3_16"
// LSPCharacterAsCursorPosition: // LSPCharacterAsCursorPosition:
// @deprecated // @deprecated
func CursorToCharacterIndex(cursor uint32) uint32 { func CursorToCharacterIndex(cursor uint32) uint32 {
@ -17,3 +19,8 @@ func DeprecatedImprovedCursorToIndex(
return min(uint32(len(line)-1), uint32(c)-offset+1) return min(uint32(len(line)-1), uint32(c)-offset+1)
} }
var SeverityError = protocol.DiagnosticSeverityError
var SeverityWarning = protocol.DiagnosticSeverityWarning
var SeverityInformation = protocol.DiagnosticSeverityInformation
var SeverityHint = protocol.DiagnosticSeverityHint

View File

@ -8,6 +8,11 @@ import (
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
) )
type analyzerContext struct {
document sshconfig.SSHDocument
diagnostics []protocol.Diagnostic
}
func Analyze( func Analyze(
d *sshconfig.SSHDocument, d *sshconfig.SSHDocument,
) []protocol.Diagnostic { ) []protocol.Diagnostic {
@ -27,7 +32,15 @@ func Analyze(
return common.ErrsToDiagnostics(errors) return common.ErrsToDiagnostics(errors)
} }
errors = append(errors, analyzeValuesAreValid(d)...) ctx := &analyzerContext{
document: *d,
diagnostics: make([]protocol.Diagnostic, 0),
}
analyzeValuesAreValid(ctx)
return ctx.diagnostics
errors = append(errors, analyzeDependents(d)...) errors = append(errors, analyzeDependents(d)...)
errors = append(errors, analyzeBlocks(d)...) errors = append(errors, analyzeBlocks(d)...)
errors = append(errors, analyzeMatchBlocks(d)...) errors = append(errors, analyzeMatchBlocks(d)...)

View File

@ -3,51 +3,52 @@ package analyzer
import ( import (
"config-lsp/common" "config-lsp/common"
docvalues "config-lsp/doc-values" docvalues "config-lsp/doc-values"
sshconfig "config-lsp/handlers/ssh_config"
"config-lsp/handlers/ssh_config/fields" "config-lsp/handlers/ssh_config/fields"
"config-lsp/utils" "config-lsp/utils"
"errors"
"fmt" "fmt"
protocol "github.com/tliron/glsp/protocol_3_16"
) )
func analyzeValuesAreValid( func analyzeValuesAreValid(
d *sshconfig.SSHDocument, ctx *analyzerContext,
) []common.LSPError { ) {
errs := make([]common.LSPError, 0) for _, info := range ctx.document.Config.GetAllOptions() {
for _, info := range d.Config.GetAllOptions() {
option := info.Option option := info.Option
block := info.Block block := info.Block
docOption, found := fields.Options[option.Key.Key] docOption, found := fields.Options[option.Key.Key]
if !found { if !found {
if d.Indexes.CanOptionBeIgnored(option, block) { if ctx.document.Indexes.CanOptionBeIgnored(option, block) {
// Skip // Skip
continue continue
} }
errs = append(errs, common.LSPError{ ctx.diagnostics = append(ctx.diagnostics,
Range: option.Key.LocationRange, protocol.Diagnostic{
Err: errors.New(fmt.Sprintf("Unknown option: %s", option.Key.Value.Value)), Range: option.Key.ToLSPRange(),
}) Message: fmt.Sprintf("Unknown option: %s", option.Key.Value.Value),
Severity: &common.SeverityError,
},
)
continue continue
} }
errs = append( errs := docOption.DeprecatedCheckIsValid(option.OptionValue.Value.Value)
errs, ctx.diagnostics = append(
ctx.diagnostics,
utils.Map( utils.Map(
docOption.DeprecatedCheckIsValid(option.OptionValue.Value.Value), errs,
func(invalidValue *docvalues.InvalidValue) common.LSPError { func(err *docvalues.InvalidValue) protocol.Diagnostic {
err := docvalues.LSPErrorFromInvalidValue(option.Start.Line, *invalidValue) return protocol.Diagnostic{
err.ShiftCharacter(option.OptionValue.Start.Character) Range: option.OptionValue.ToLSPRange(),
Message: err.Err.Error(),
return err Severity: &common.SeverityError,
}
}, },
)..., )...,
) )
} }
return errs
} }

View File

@ -3,6 +3,8 @@ package analyzer
import ( import (
testutils_test "config-lsp/handlers/ssh_config/test_utils" testutils_test "config-lsp/handlers/ssh_config/test_utils"
"testing" "testing"
protocol "github.com/tliron/glsp/protocol_3_16"
) )
func TestUnknownOptionExample( func TestUnknownOptionExample(
@ -11,11 +13,15 @@ func TestUnknownOptionExample(
d := testutils_test.DocumentFromInput(t, ` d := testutils_test.DocumentFromInput(t, `
ThisOptionDoesNotExist okay ThisOptionDoesNotExist okay
`) `)
ctx := &analyzerContext{
document: *d,
diagnostics: make([]protocol.Diagnostic, 0),
}
errors := analyzeValuesAreValid(d) analyzeValuesAreValid(ctx)
if !(len(errors) == 1) { if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(errors)) t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
} }
} }
@ -26,11 +32,15 @@ func TestUnknownOptionButIgnoredExample(
IgnoreUnknown ThisOptionDoesNotExist IgnoreUnknown ThisOptionDoesNotExist
ThisOptionDoesNotExist okay ThisOptionDoesNotExist okay
`) `)
ctx := &analyzerContext{
document: *d,
diagnostics: make([]protocol.Diagnostic, 0),
}
errors := analyzeValuesAreValid(d) analyzeValuesAreValid(ctx)
if len(errors) > 0 { if len(ctx.diagnostics) > 0 {
t.Errorf("Expected no errors, but got %v", len(errors)) t.Errorf("Expected no errors, but got %v", len(ctx.diagnostics))
} }
} }
@ -41,10 +51,14 @@ func TestUnknownOptionIgnoredIsAfterDefinitionExample(
ThisOptionDoesNotExist okay ThisOptionDoesNotExist okay
IgnoreUnknown ThisOptionDoesNotExist IgnoreUnknown ThisOptionDoesNotExist
`) `)
ctx := &analyzerContext{
document: *d,
diagnostics: make([]protocol.Diagnostic, 0),
}
errors := analyzeValuesAreValid(d) analyzeValuesAreValid(ctx)
if !(len(errors) == 1) { if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(errors)) t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
} }
} }