byteOffsetToLineCol returns the 1-based line and 1-based column for a byte offset within the statement. Counts \n as line breaks. The 1-based column matches the storepb.Position convention that ConvertSyntaxErrorToDiagnostic expects (it subtracts 1 internally to land on the 0-based LSP offset).
(s string, offset int)
| 44 | // matches the storepb.Position convention that ConvertSyntaxErrorToDiagnostic |
| 45 | // expects (it subtracts 1 internally to land on the 0-based LSP offset). |
| 46 | func byteOffsetToLineCol(s string, offset int) (int, int) { |
| 47 | if offset < 0 { |
| 48 | return 1, 1 |
| 49 | } |
| 50 | if offset > len(s) { |
| 51 | offset = len(s) |
| 52 | } |
| 53 | line, col := 1, 1 |
| 54 | for i := 0; i < offset; { |
| 55 | r, size := utf8.DecodeRuneInString(s[i:]) |
| 56 | if r == '\n' { |
| 57 | line++ |
| 58 | col = 1 |
| 59 | } else { |
| 60 | col++ |
| 61 | } |
| 62 | i += size |
| 63 | } |
| 64 | return line, col |
| 65 | } |