Skip to content

Commit

Permalink
fix: make character positions for newlines the same as vscode (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored Jun 20, 2022
1 parent e6317f3 commit 2ad4628
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-months-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": major
---

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.
26 changes: 17 additions & 9 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ for (const entry of fs.readdirSync(FIXTURES)) {
end: inputRange.end,
}
: inputRange;
const pos = parser.positionAt(range.start);
const pos = positionAt(range.start);
partsByLine[pos.line].push({
label,
range,
Expand All @@ -60,6 +60,14 @@ for (const entry of fs.readdirSync(FIXTURES)) {
}
}
};
const positionAt = (offset: number): Position => {
if (offset && lines.includes(offset + 1)) {
// Normalize lines to their first char.
return parser.positionAt(offset + 1);
}

return parser.positionAt(offset);
};
const tagStack: Ranges.TagName[] = [];
const parser = createParser({
onError(range) {
Expand Down Expand Up @@ -187,23 +195,23 @@ for (const entry of fs.readdirSync(FIXTURES)) {
linePrefix +
read({
start: 0,
end: lines[1],
end: lines.length > 1 ? lines[1] - 1 : src.length,
})
}`;
} else {
result += `\n${
linePrefix +
read({
start: lines[line] + 1,
end: lines[line + 1],
start: lines[line],
end: lines.length > line + 1 ? lines[line + 1] - 1 : src.length,
})
}`;
}

if (len) {
const padding = " ".repeat(linePrefix.length - 3);
parts.sort((a, b) => {
const delta = (a.pos.character || 1) - (b.pos.character || 1);
const delta = a.pos.character - b.pos.character;
return delta === 0 ? b.range.start - a.range.start : delta;
});

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

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

if (delta > 0) {
Expand All @@ -234,15 +242,15 @@ for (const entry of fs.readdirSync(FIXTURES)) {
}
}

const column = pos.character || 1;
const column = pos.character;

if (prevPart && (prevPart.pos.character || 1) === column) {
if (prevPart && prevPart.pos.character === column) {
label = `├${label}`;
} else {
label = `╰${label}`;
}

label = `${columns.slice(0, column - 1)}${label}`;
label = `${columns.slice(0, column)}${label}`;
result += `\n${padding + (prevPart ? `│ ${label}` : `╰─ ${label}`)}`;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export function getPos(
}

export function getLines(src: string) {
const lines = [-1];
const lines = [0];
for (let i = 0; i < src.length; i++) {
if (src.charCodeAt(i) === CODE.NEWLINE) {
lines.push(i);
lines.push(i + 1);
}
}

Expand Down

0 comments on commit 2ad4628

Please sign in to comment.