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
import protocol "github.com/tliron/glsp/protocol_3_16"
// LSPCharacterAsCursorPosition:
// @deprecated
func CursorToCharacterIndex(cursor uint32) uint32 {
@ -17,3 +19,8 @@ func DeprecatedImprovedCursorToIndex(
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"
)
type analyzerContext struct {
document sshconfig.SSHDocument
diagnostics []protocol.Diagnostic
}
func Analyze(
d *sshconfig.SSHDocument,
) []protocol.Diagnostic {
@ -27,7 +32,15 @@ func Analyze(
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, analyzeBlocks(d)...)
errors = append(errors, analyzeMatchBlocks(d)...)

View File

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

View File

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