Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: simple spliter for $/textDocument/statementRanges in lsp (#15320)

h3n4laf7571be0791
diff --git a/backend/api/lsp/custom.go b/backend/api/lsp/custom.go
new file mode 100644
index 0000000000..9c31d4a4b1
--- /dev/null
+++ b/backend/api/lsp/custom.go
@@ -0,0 +1,12 @@
+package lsp
+
+import "github.com/sourcegraph/go-lsp"
+
+type PingResult struct {
+ Result string `json:"result"`
+}
+
+type SQLStatementRangesParams struct {
+ URI lsp.DocumentURI `json:"uri"`
+ Ranges []lsp.Range `json:"ranges"`
+}
diff --git a/backend/api/lsp/fs_handler.go b/backend/api/lsp/fs_handler.go
index 7126193856..f6542e3d41 100644
--- a/backend/api/lsp/fs_handler.go
+++ b/backend/api/lsp/fs_handler.go
@@ -13,6 +13,7 @@ import (
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/proto/generated-go/store"
)
// GetFS returns the file system.
@@ -86,10 +87,22 @@ func (h *Handler) handleFileSystemRequest(ctx context.Context, conn *jsonrpc2.Co
if err != nil {
slog.Warn("diagnose error", log.BBError(err))
}
- return conn.Notify(ctx, string(LSPMethodPublishDiagnostics), &lsp.PublishDiagnosticsParams{
+ if err := conn.Notify(ctx, string(LSPMethodPublishDiagnostics), &lsp.PublishDiagnosticsParams{
URI: uri,
Diagnostics: diagnostics,
- })
+ }); err != nil {
+ return err
+ }
+ // TODO(zp): Simple PostgreSQL splitter, replace it with our regular splitter later.
+ if h.getEngineType(ctx) == store.Engine_POSTGRES {
+ ranges := getSQLStatementRangesUTF16Position(content)
+ return conn.Notify(ctx, string(LSPCustomMethodSQLStatementRanges), &SQLStatementRangesParams{
+ URI: uri,
+ Ranges: ranges,
+ })
+ }
+
+ return nil
})
case LSPMethodTextDocumentDidClose:
var params lsp.DidCloseTextDocumentParams
diff --git a/backend/api/lsp/handler.go b/backend/api/lsp/handler.go
index fba95442f6..f9d57e49fa 100644
--- a/backend/api/lsp/handler.go
+++ b/backend/api/lsp/handler.go
@@ -39,11 +39,11 @@ const (
// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics.
LSPMethodPublishDiagnostics Method = "textDocument/publishDiagnostics"
-)
-type PingResult struct {
- Result string `json:"result"`
-}
+ // Custom Methods.
+ // See dollar request: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#dollarRequests.
+ LSPCustomMethodSQLStatementRanges Method = "$/textDocument/statementRanges"
+)
// NewHandler creates a new Language Server Protocol handler.
func NewHandler(s *store.Store, profile *config.Profile) jsonrpc2.Handler {
diff --git a/backend/api/lsp/utils.go b/backend/api/lsp/utils.go
index 0e3f1f84dc..e9b1530000 100644
--- a/backend/api/lsp/utils.go
+++ b/backend/api/lsp/utils.go
@@ -4,6 +4,7 @@ import (
"bytes"
"net/url"
"strings"
+ "unicode"
"unicode/utf8"
"github.com/pkg/errors"
@@ -41,7 +42,7 @@ func offsetForPosition(content []byte, p lsp.Position) (byteOffset int, whyInval
}
}
- // Validate line number, notes that the Posititon in LSP is 0-based.
+ // Validate line number, notes that the Position in LSP is 0-based.
if p.Line >= lineNumber {
return 0, errors.Errorf("line number %d out of range [0, %d)", p.Line, lineNumber)
}
@@ -79,3 +80,75 @@ func offsetForPosition(content []byte, p lsp.Position) (byteOffset int, whyInval
}
return offset + col8, nil
}
+
+func getSQLStatementRangesUTF16Position(content []byte) []lsp.Range {
+ s := strings.TrimRightFunc(string(content), unicode.IsSpace)
+ // Assuming the content is UTF-8 encoded.
+ statements := strings.Split(s, ";")
+
+ var ranges []lsp.Range
+ // 0-based UTF-16 encoded line and character.
+ line, character := 0, 0
+
+ for i, statement := range statements {
+ // Trim left space to provide more accurate range.
+ statement = strings.TrimLeftFunc(statement, func(r rune) bool {
+ if !unicode.IsSpace(r) {
+ return false
+ }
+ if r == '\n' {
+ line++
+ character = 0
+ } else {
+ // Check rune utf16 length by BMP.
+ if r <= 0xFFFF {
+ character++
+ } else {
+ character += 2
+ }
+ }
+ return true
+ })
+
+ // If the statement is empty, skip it.
+ if statement == "" {
+ continue
+ }
+
+ begin := lsp.Position{Line: line, Character: character}
+ for _, r := range statement {
+ if r == '\n' {
+ line++
+ character = 0
+ } else {
+ // Check rune utf16 length by BMP.
+ if r <= 0xFFFF {
+ character++
+ } else {
+ character += 2
+ }
+ }
+ }
+
+ endLine, endCharacter := line, character
+ // End is exclusive, so we check the next byte.
+ if i == len(statements)-1 {
+ // End of the content.
+ endLine++
+ endCharacter = 0
+ } else {
+ // Next byte is ';', include it.
+ character++
+ endCharacter++
+ if nextStatement := statements[i+1]; len(nextStatement) > 0 && nextStatement[0] == '\n' {
+ endLine++
+ endCharacter = 0
+ } else {
+ endCharacter++
+ }
+ }
+ end := lsp.Position{Line: endLine, Character: endCharacter}
+ ranges = append(ranges, lsp.Range{Start: begin, End: end})
+ }
+ return ranges
+}
diff --git a/backend/api/lsp/utils_test.go b/backend/api/lsp/utils_test.go
index 23394aeb42..811fef13ba 100644
--- a/backend/api/lsp/utils_test.go
+++ b/backend/api/lsp/utils_test.go
@@ -85,3 +85,34 @@ func TestOffsetForPosition(t *testing.T) {
require.Equal(t, tc.expected, offset, "test cases %d", idx)
}
}
+
+func TestGetSQLStatementRangesUTF16Position(t *testing.T) {
+ testCases := []struct {
+ content []byte
+ ranges []lsp.Range
+ }{
+ {
+ content: []byte("SELECT 1;\nSELECT 2"),
+ ranges: []lsp.Range{
+ {Start: lsp.Position{Line: 0, Character: 0}, End: lsp.Position{Line: 1, Character: 0}},
+ {Start: lsp.Position{Line: 1, Character: 0}, End: lsp.Position{Line: 2, Character: 0}},
+ },
+ },
+ {
+ content: []byte(`SELECT 1;
+
+
+
+SELECT * FROM public.anomaly;`),
+ ranges: []lsp.Range{
+ {Start: lsp.Position{Line: 0, Character: 0}, End: lsp.Position{Line: 1, Character: 0}},
+ {Start: lsp.Position{Line: 4, Character: 0}, End: lsp.Position{Line: 4, Character: 30}},
+ },
+ },
+ }
+
+ for idx, tc := range testCases {
+ ranges := getSQLStatementRangesUTF16Position(tc.content)
+ require.Equal(t, tc.ranges, ranges, "test cases %d", idx)
+ }
+}