MCPcopy Create free account
hub / github.com/bytebase/bytebase / GetStatementRanges

Function GetStatementRanges

backend/plugin/parser/mongodb/statement_ranges.go:20–43  ·  view source on GitHub ↗

GetStatementRanges returns the ranges of statements in the MongoDB shell script. Uses best-effort parsing so that valid statements are returned even when other statements in the input have syntax errors (e.g. user is mid-typing).

(_ context.Context, _ base.StatementRangeContext, statement string)

Source from the content-addressed store, hash-verified

18// Uses best-effort parsing so that valid statements are returned even when other
19// statements in the input have syntax errors (e.g. user is mid-typing).
20func GetStatementRanges(_ context.Context, _ base.StatementRangeContext, statement string) ([]base.Range, error) {
21 stmts, _ := ParseMongoShellBestEffort(statement)
22
23 // Build byte offset to LSP position mapping.
24 positions := buildBytePositionMap(statement)
25
26 var ranges []base.Range
27 for _, stmt := range stmts {
28 if stmt.Range == nil {
29 continue
30 }
31 startPos := getPositionByByteOffset(int(stmt.Range.Start), positions)
32 endPos := getPositionByByteOffset(int(stmt.Range.End), positions)
33 if startPos == nil || endPos == nil {
34 continue
35 }
36 ranges = append(ranges, base.Range{
37 Start: *startPos,
38 End: *endPos,
39 })
40 }
41
42 return ranges, nil
43}
44
45// bytePositionEntry stores the LSP position for a given byte offset.
46type bytePositionEntry struct {

Calls 3

getPositionByByteOffsetFunction · 0.85
buildBytePositionMapFunction · 0.70

Tested by 2

TestGetStatementRangesFunction · 0.56