mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
fix(ssh_config): Improve single quotes detection
This commit is contained in:
parent
9183a3f004
commit
631de7c33c
@ -4,6 +4,7 @@ import (
|
||||
"config-lsp/common"
|
||||
commonparser "config-lsp/common/parser"
|
||||
sshconfig "config-lsp/handlers/ssh_config"
|
||||
"config-lsp/utils"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
@ -28,9 +29,11 @@ func checkIsUsingDoubleQuotes(
|
||||
value commonparser.ParsedString,
|
||||
valueRange common.LocationRange,
|
||||
) []common.LSPError {
|
||||
quoteRanges := utils.GetQuoteRanges(value.Raw)
|
||||
singleQuotePosition := strings.Index(value.Raw, "'")
|
||||
|
||||
if singleQuotePosition != -1 {
|
||||
// Single quoe
|
||||
if singleQuotePosition != -1 && !quoteRanges.IsCharInside(singleQuotePosition) {
|
||||
return []common.LSPError{
|
||||
{
|
||||
Range: valueRange,
|
||||
|
51
utils/quotes.go
Normal file
51
utils/quotes.go
Normal file
@ -0,0 +1,51 @@
|
||||
package utils
|
||||
|
||||
import "slices"
|
||||
|
||||
type quoteRange [2]int
|
||||
|
||||
func (q quoteRange) IsCharInside(index int) bool {
|
||||
return index >= q[0] && index <= q[1]
|
||||
}
|
||||
|
||||
type quoteRanges []quoteRange
|
||||
|
||||
func (q quoteRanges) IsCharInside(index int) bool {
|
||||
_, found := slices.BinarySearchFunc(
|
||||
q,
|
||||
index,
|
||||
func(current quoteRange, target int) int {
|
||||
if target < current[0] {
|
||||
return -1
|
||||
}
|
||||
|
||||
if target > current[1] {
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
},
|
||||
)
|
||||
|
||||
return found
|
||||
}
|
||||
|
||||
func GetQuoteRanges(s string) quoteRanges {
|
||||
quoteRanges := make(quoteRanges, 0, 2)
|
||||
inQuote := false
|
||||
var quoteStart int
|
||||
|
||||
for index, c := range s {
|
||||
if c == '"' && (index == 0 || s[index-1] != '\\') {
|
||||
if inQuote {
|
||||
quoteRanges = append(quoteRanges, [2]int{quoteStart, index})
|
||||
inQuote = false
|
||||
} else {
|
||||
quoteStart = index
|
||||
inQuote = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return quoteRanges
|
||||
}
|
23
utils/quotes_test.go
Normal file
23
utils/quotes_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestQuotesSimpleExample(
|
||||
t *testing.T,
|
||||
) {
|
||||
quoteRanges := GetQuoteRanges(`"hello" "world"`)
|
||||
|
||||
if !(len(quoteRanges) == 2 && quoteRanges[0][0] == 0 && quoteRanges[0][1] == 6 && quoteRanges[1][0] == 8 && quoteRanges[1][1] == 14) {
|
||||
t.Fatalf("Unexpected quote ranges: %v", quoteRanges)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuotesEscapedQuotes(
|
||||
t *testing.T,
|
||||
) {
|
||||
quoteRanges := GetQuoteRanges(`"hello \"world\""`)
|
||||
|
||||
if !(len(quoteRanges) == 1 && quoteRanges[0][0] == 0 && quoteRanges[0][1] == 16) {
|
||||
t.Fatalf("Unexpected quote ranges: %v", quoteRanges)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user