From 0e18169a3f04852b1dcdbabdc8e49fb52e9fd5e1 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:20:27 +0200 Subject: [PATCH] feat(doc-value): Add respect quote value --- server/doc-values/value-array.go | 23 +++++++++++++++++-- server/handlers/ssh_config/analyzer/quotes.go | 2 +- .../handlers/sshd_config/analyzer/quotes.go | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/server/doc-values/value-array.go b/server/doc-values/value-array.go index 3d330f7..3671973 100644 --- a/server/doc-values/value-array.go +++ b/server/doc-values/value-array.go @@ -42,6 +42,9 @@ type ArrayValue struct { // This is used to extract the value from the user input, // because you may want to preprocess the value before checking for duplicates DuplicatesExtractor *(func(string) string) + + // If true, array ArrayValue ignores the `Separator` if it's within quotes + RespectQuotes bool } func (v ArrayValue) GetTypeDescription() []string { @@ -53,6 +56,7 @@ func (v ArrayValue) GetTypeDescription() []string { ) } +// TODO: Add support for quotes func (v ArrayValue) DeprecatedCheckIsValid(value string) []*InvalidValue { errors := []*InvalidValue{} values := strings.Split(value, v.Separator) @@ -122,9 +126,24 @@ func (v ArrayValue) getCurrentValue(line string, cursor uint32) (string, uint32) MIN := uint32(0) MAX := uint32(len(line) - 1) + var cursorSearchStart = cursor + var cursorSearchEnd = cursor + var start uint32 var end uint32 + // Hello,world,how,are,you + // Hello,"world,how",are,you + if v.RespectQuotes { + quotes := utils.GetQuoteRanges(line) + quote := quotes.GetQuoteForIndex(int(cursor)) + + if quote != nil { + cursorSearchStart = uint32(quote[0]) + cursorSearchEnd = uint32(quote[1]) + } + } + // hello,w[o]rld,and,more // [h]ello,world // hello,[w]orld @@ -135,7 +154,7 @@ func (v ArrayValue) getCurrentValue(line string, cursor uint32) (string, uint32) relativePosition, found := utils.FindPreviousCharacter( line, v.Separator, - int(cursor), + int(cursorSearchStart), ) if found { @@ -151,7 +170,7 @@ func (v ArrayValue) getCurrentValue(line string, cursor uint32) (string, uint32) relativePosition, found = utils.FindNextCharacter( line, v.Separator, - int(start), + int(cursorSearchEnd), ) if found { diff --git a/server/handlers/ssh_config/analyzer/quotes.go b/server/handlers/ssh_config/analyzer/quotes.go index 945bd1a..2155fe9 100644 --- a/server/handlers/ssh_config/analyzer/quotes.go +++ b/server/handlers/ssh_config/analyzer/quotes.go @@ -30,7 +30,7 @@ func checkIsUsingDoubleQuotes( singleQuotePosition := strings.Index(value.Raw, "'") // Single quote - if singleQuotePosition != -1 && !quoteRanges.IsCharInside(singleQuotePosition) { + if singleQuotePosition != -1 && !quoteRanges.IsIndexInsideQuotes(singleQuotePosition) { ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ Range: valueRange.ToLSPRange(), Message: "ssh_config does not support single quotes. Use double quotes (\") instead.", diff --git a/server/handlers/sshd_config/analyzer/quotes.go b/server/handlers/sshd_config/analyzer/quotes.go index 13e9920..7dbf275 100644 --- a/server/handlers/sshd_config/analyzer/quotes.go +++ b/server/handlers/sshd_config/analyzer/quotes.go @@ -30,7 +30,7 @@ func checkIsUsingDoubleQuotes( singleQuotePosition := strings.Index(value.Raw, "'") // Single quote - if singleQuotePosition != -1 && !quoteRanges.IsCharInside(singleQuotePosition) { + if singleQuotePosition != -1 && !quoteRanges.IsIndexInsideQuotes(singleQuotePosition) { ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ Range: valueRange.ToLSPRange(), Message: "sshd_config does not support single quotes. Use double quotes (\") instead.",