fix(server): Improve quotes

This commit is contained in:
Myzel394 2024-11-29 19:04:47 +01:00
parent 4a3ab6e040
commit ea667653cd
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
5 changed files with 33 additions and 24 deletions

View File

@ -27,15 +27,18 @@ func checkIsUsingDoubleQuotes(
valueRange common.LocationRange, valueRange common.LocationRange,
) { ) {
quoteRanges := utils.GetQuoteRanges(value.Raw) quoteRanges := utils.GetQuoteRanges(value.Raw)
singleQuotePosition := strings.Index(value.Raw, "'") invertedRanges := quoteRanges.GetInvertedRanges(len(value.Raw))
// Single quote for _, rang := range invertedRanges {
if singleQuotePosition != -1 && !quoteRanges.IsIndexInsideQuotes(singleQuotePosition) { text := value.Raw[rang[0]:rang[1]]
ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{
Range: valueRange.ToLSPRange(), if strings.Contains(text, "'") {
Message: "ssh_config does not support single quotes. Use double quotes (\") instead.", ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{
Severity: &common.SeverityError, Range: valueRange.ToLSPRange(),
}) Message: "ssh_config does not support single quotes. Use double quotes (\") instead.",
Severity: &common.SeverityError,
})
}
} }
} }

View File

@ -101,7 +101,8 @@ func TestValidDependentOptionsExample(
d := testutils_test.DocumentFromInput(t, ` d := testutils_test.DocumentFromInput(t, `
Port 1234 Port 1234
CanonicalizeHostname yes CanonicalizeHostname yes
CanonicalDomains example.com CanonicalDomains "example.com"
Test "hello world 'test' "
`) `)
ctx := &analyzerContext{ ctx := &analyzerContext{
document: d, document: d,

View File

@ -3,7 +3,6 @@ package analyzer
import ( import (
"config-lsp/common" "config-lsp/common"
commonparser "config-lsp/common/parser" commonparser "config-lsp/common/parser"
"config-lsp/utils"
"strings" "strings"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
@ -26,14 +25,10 @@ func checkIsUsingDoubleQuotes(
value commonparser.ParsedString, value commonparser.ParsedString,
valueRange common.LocationRange, valueRange common.LocationRange,
) { ) {
quoteRanges := utils.GetQuoteRanges(value.Raw) if strings.HasPrefix(value.Raw, "'") && strings.HasSuffix(value.Raw, "'") {
singleQuotePosition := strings.Index(value.Raw, "'")
// Single quote
if singleQuotePosition != -1 && !quoteRanges.IsIndexInsideQuotes(singleQuotePosition) {
ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{
Range: valueRange.ToLSPRange(), Range: valueRange.ToLSPRange(),
Message: "sshd_config does not support single quotes. Use double quotes (\") instead.", Message: "ssh_config does not support single quotes. Use double quotes (\") instead.",
Severity: &common.SeverityError, Severity: &common.SeverityError,
}) })
} }

View File

@ -55,20 +55,18 @@ func (q quoteRanges) GetInvertedRanges(textLength int) [][2]int {
inverted = append(inverted, [2]int{0, firstRange[0]}) inverted = append(inverted, [2]int{0, firstRange[0]})
} }
if len(q) == 1 { if len(q) > 1 {
return inverted for index, currentRange := range q[:len(q)-1] {
} nextRange := q[index+1]
for index, currentRange := range q[:len(q)-1] { inverted = append(inverted, [2]int{currentRange[1] + 1, nextRange[0]})
nextRange := q[index+1] }
inverted = append(inverted, [2]int{currentRange[1] + 1, nextRange[0]})
} }
lastRange := q[len(q)-1] lastRange := q[len(q)-1]
if lastRange[1] != (textLength - 1) { if lastRange[1] != (textLength - 1) {
inverted = append(inverted, [2]int{lastRange[1], textLength - 1}) inverted = append(inverted, [2]int{lastRange[1] + 1, textLength})
} }
return inverted return inverted

View File

@ -81,3 +81,15 @@ func TestInvertedQuotesFullyQuoted(
t.Fatalf("Unexpected inverted quote ranges: %v", inverted) t.Fatalf("Unexpected inverted quote ranges: %v", inverted)
} }
} }
func TestInvertedQuotesFirstThenRemaining(
t *testing.T,
) {
text := `"hello world" i am here`
quoteRanges := GetQuoteRanges(text)
inverted := quoteRanges.GetInvertedRanges(len(text))
if !(len(inverted) == 1 && inverted[0][0] == 13 && inverted[0][1] == 23) {
t.Fatalf("Unexpected inverted quote ranges: %v", inverted)
}
}