Skip to content

Commit cf98587

Browse files
committed
refactor: align position apis with language server spec
1 parent 16fd65a commit cf98587

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/__tests__/main.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
1919
}[][] = Array.from({ length: lines.length }, () => []);
2020
const addRange = (label: string, range: Range) => {
2121
const pos = parser.positionAt(range.start);
22-
partsByLine[pos.line - 1].push({
22+
partsByLine[pos.line].push({
2323
label,
2424
range,
2525
pos,
@@ -152,7 +152,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
152152
if (len) {
153153
const padding = " ".repeat(linePrefix.length - 3);
154154
parts.sort((a, b) => {
155-
const delta = (a.pos.column || 1) - (b.pos.column || 1);
155+
const delta = (a.pos.character || 1) - (b.pos.character || 1);
156156
return delta === 0 ? b.range.start - a.range.start : delta;
157157
});
158158

@@ -161,7 +161,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
161161

162162
for (let i = 0; i < len; i++) {
163163
const part = parts[i];
164-
const col = part.pos.column || 1;
164+
const col = part.pos.character || 1;
165165
const delta = col - lastCol;
166166

167167
if (delta > 0) {
@@ -185,13 +185,15 @@ for (const entry of fs.readdirSync(FIXTURES)) {
185185
}
186186
}
187187

188-
if (prevPart && (prevPart.pos.column || 1) === (pos.column || 1)) {
188+
const column = pos.character || 1;
189+
190+
if (prevPart && (prevPart.pos.character || 1) === column) {
189191
label = `├${label}`;
190192
} else {
191193
label = `╰${label}`;
192194
}
193195

194-
label = `${columns.slice(0, (pos.column || 1) - 1)}${label}`;
196+
label = `${columns.slice(0, column - 1)}${label}`;
195197
result += `\n${padding + (prevPart ? `│ ${label}` : `╰─ ${label}`)}`;
196198
}
197199
}

src/util/constants.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,32 @@ export const enum BODY_MODE {
4343
PARSED_TEXT, // Body of a tag is treated as text, but placeholders will be parsed
4444
}
4545

46+
// Same format as https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
4647
export interface Position {
48+
/**
49+
* Line position in a document (zero-based).
50+
*/
4751
line: number;
48-
column: number;
52+
/**
53+
* Character offset on a line in a document (zero-based).
54+
*/
55+
character: number;
4956
}
5057

58+
// Same format as https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#range
5159
export interface Location {
5260
start: Position;
5361
end: Position;
5462
}
5563

5664
export interface Range {
65+
/**
66+
* The start characters offset from the beginning of the document (zero-based).
67+
*/
5768
start: number;
69+
/**
70+
* The end characters offset from the beginning of the document (zero-based).
71+
*/
5872
end: number;
5973
}
6074

src/util/util.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ export function isWhitespaceCode(code: number) {
1212
export function getLoc(lines: number[], range: Range): Location {
1313
const start = getPos(lines, 0, range.start);
1414
const end =
15-
range.start === range.end
16-
? start
17-
: getPos(lines, start.line - 1, range.end);
15+
range.start === range.end ? start : getPos(lines, start.line, range.end);
1816
return { start, end };
1917
}
2018

@@ -37,8 +35,8 @@ export function getPos(
3735
}
3836

3937
return {
40-
line: line + 1,
41-
column: index - lines[line],
38+
line,
39+
character: index - lines[line],
4240
};
4341
}
4442

0 commit comments

Comments
 (0)