buildBytePositionMap creates a mapping from byte offset to LSP position.
(statement string)
| 50 | |
| 51 | // buildBytePositionMap creates a mapping from byte offset to LSP position. |
| 52 | func 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. |
| 83 | func getPositionByByteOffset(byteOffset int, positions map[int]bytePositionEntry) *lsp.Position { |
no outgoing calls
no test coverage detected