Skip to content

Commit 2ad4628

Browse files
authored
fix: make character positions for newlines the same as vscode (#112)
1 parent e6317f3 commit 2ad4628

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

.changeset/clean-months-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": major
3+
---
4+
5+
Switch character position offsets for newlines to be to similar to vscode. Previously the newline was counted as the first character of the line, now it is the last character of the previous line.

src/__tests__/main.test.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
3535
end: inputRange.end,
3636
}
3737
: inputRange;
38-
const pos = parser.positionAt(range.start);
38+
const pos = positionAt(range.start);
3939
partsByLine[pos.line].push({
4040
label,
4141
range,
@@ -60,6 +60,14 @@ for (const entry of fs.readdirSync(FIXTURES)) {
6060
}
6161
}
6262
};
63+
const positionAt = (offset: number): Position => {
64+
if (offset && lines.includes(offset + 1)) {
65+
// Normalize lines to their first char.
66+
return parser.positionAt(offset + 1);
67+
}
68+
69+
return parser.positionAt(offset);
70+
};
6371
const tagStack: Ranges.TagName[] = [];
6472
const parser = createParser({
6573
onError(range) {
@@ -187,23 +195,23 @@ for (const entry of fs.readdirSync(FIXTURES)) {
187195
linePrefix +
188196
read({
189197
start: 0,
190-
end: lines[1],
198+
end: lines.length > 1 ? lines[1] - 1 : src.length,
191199
})
192200
}`;
193201
} else {
194202
result += `\n${
195203
linePrefix +
196204
read({
197-
start: lines[line] + 1,
198-
end: lines[line + 1],
205+
start: lines[line],
206+
end: lines.length > line + 1 ? lines[line + 1] - 1 : src.length,
199207
})
200208
}`;
201209
}
202210

203211
if (len) {
204212
const padding = " ".repeat(linePrefix.length - 3);
205213
parts.sort((a, b) => {
206-
const delta = (a.pos.character || 1) - (b.pos.character || 1);
214+
const delta = a.pos.character - b.pos.character;
207215
return delta === 0 ? b.range.start - a.range.start : delta;
208216
});
209217

@@ -212,7 +220,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
212220

213221
for (let i = 0; i < len; i++) {
214222
const part = parts[i];
215-
const col = part.pos.character || 1;
223+
const col = part.pos.character + 1;
216224
const delta = col - lastCol;
217225

218226
if (delta > 0) {
@@ -234,15 +242,15 @@ for (const entry of fs.readdirSync(FIXTURES)) {
234242
}
235243
}
236244

237-
const column = pos.character || 1;
245+
const column = pos.character;
238246

239-
if (prevPart && (prevPart.pos.character || 1) === column) {
247+
if (prevPart && prevPart.pos.character === column) {
240248
label = `├${label}`;
241249
} else {
242250
label = `╰${label}`;
243251
}
244252

245-
label = `${columns.slice(0, column - 1)}${label}`;
253+
label = `${columns.slice(0, column)}${label}`;
246254
result += `\n${padding + (prevPart ? `│ ${label}` : `╰─ ${label}`)}`;
247255
}
248256
}

src/util/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export function getPos(
4141
}
4242

4343
export function getLines(src: string) {
44-
const lines = [-1];
44+
const lines = [0];
4545
for (let i = 0; i < src.length; i++) {
4646
if (src.charCodeAt(i) === CODE.NEWLINE) {
47-
lines.push(i);
47+
lines.push(i + 1);
4848
}
4949
}
5050

0 commit comments

Comments
 (0)