From b7f01f0bd4a9cd710165a2e37165b12bbce7beb0 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:58:11 +0200 Subject: [PATCH] refactor(ssh_config): Use Diagnostics instead of LSPError --- common/lsp.go | 7 ++++ handlers/ssh_config/analyzer/analyzer.go | 15 ++++++- handlers/ssh_config/analyzer/values.go | 45 +++++++++++---------- handlers/ssh_config/analyzer/values_test.go | 32 ++++++++++----- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/common/lsp.go b/common/lsp.go index fd2e091..42ec460 100644 --- a/common/lsp.go +++ b/common/lsp.go @@ -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 diff --git a/handlers/ssh_config/analyzer/analyzer.go b/handlers/ssh_config/analyzer/analyzer.go index b06312e..5f05f1c 100644 --- a/handlers/ssh_config/analyzer/analyzer.go +++ b/handlers/ssh_config/analyzer/analyzer.go @@ -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)...) diff --git a/handlers/ssh_config/analyzer/values.go b/handlers/ssh_config/analyzer/values.go index 08c383d..063b393 100644 --- a/handlers/ssh_config/analyzer/values.go +++ b/handlers/ssh_config/analyzer/values.go @@ -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 } diff --git a/handlers/ssh_config/analyzer/values_test.go b/handlers/ssh_config/analyzer/values_test.go index ee9da43..ac843d6 100644 --- a/handlers/ssh_config/analyzer/values_test.go +++ b/handlers/ssh_config/analyzer/values_test.go @@ -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)) } }