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

Function buildBytePositionMap

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

buildBytePositionMap creates a mapping from byte offset to LSP position.

(statement string)

Source from the content-addressed store, hash-verified

50
51// buildBytePositionMap creates a mapping from byte offset to LSP position.
52func buildBytePositionMap(statement string) map[int]bytePositionEntry {
53 positions := make(map[int]bytePositionEntry, len(statement)+1)
54
55 var line uint32
56 var character uint32
57 i := 0
58
59 for i < len(statement) {
60 positions[i] = bytePositionEntry{line: line, character: character}
61
62 r, size := utf8.DecodeRuneInString(statement[i:])
63 if r == '\n' {
64 line++
65 character = 0
66 } else {
67 // Count UTF-16 code units.
68 if r > 0xFFFF {
69 character += 2 // surrogate pair
70 } else {
71 character++
72 }
73 }
74 i += size
75 }
76 // Position after the last character.
77 positions[i] = bytePositionEntry{line: line, character: character}
78
79 return positions
80}
81
82// getPositionByByteOffset converts a byte offset to an LSP Position.
83func getPositionByByteOffset(byteOffset int, positions map[int]bytePositionEntry) *lsp.Position {

Callers 1

GetStatementRangesFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected