Skip to content

Commit

Permalink
Remove unusued lineNumbersGutter fn
Browse files Browse the repository at this point in the history
It was a copy pasta function from `radicle-explorer` that if we need to
highlight files by line in the future e.g. changesets we should do it
better than this.
  • Loading branch information
sebastinez committed Sep 26, 2024
1 parent 3b29598 commit 665c5a9
Showing 1 changed file with 1 addition and 116 deletions.
117 changes: 1 addition & 116 deletions src/lib/syntax.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ElementContent, Root } from "hast";
import type { Root } from "hast";

import onigurumaWASMUrl from "vscode-oniguruma/release/onig.wasm?url";
import sourceAsciiDoc from "@wooorm/starry-night/text.html.asciidoc";
Expand Down Expand Up @@ -122,118 +122,3 @@ export async function highlight(
const scope = starryNight.flagToScope(grammar);
return starryNight.highlight(content, scope ?? "text.raw");
}

export function lineNumbersGutter(tree: Root) {
const replacement: ElementContent[] = [];
const search = /\r?\n|\r/g;
let index = -1;
let start = 0;
let startTextRemainder = "";
let lineNumber = 0;

while (++index < tree.children.length) {
const child = tree.children[index];

if (child.type === "text") {
let textStart = 0;
let match = search.exec(child.value);

while (match) {
// Nodes in this line.
const line = tree.children.slice(start, index) as ElementContent[];

// Prepend text from a partial matched earlier text.
if (startTextRemainder) {
line.unshift({ type: "text", value: startTextRemainder });
startTextRemainder = "";
}

// Append text from this text.
if (match.index > textStart) {
line.push({
type: "text",
value: child.value.slice(textStart, match.index),
});
}

// Add a line, and the eol.
lineNumber += 1;
replacement.push(createLine(line, lineNumber), {
type: "text",
value: match[0],
});

start = index + 1;
textStart = match.index + match[0].length;
match = search.exec(child.value);
}

// If we matched, make sure to not drop the text after the last line ending.
if (start === index + 1) {
startTextRemainder = child.value.slice(textStart);
}
}
}

const line = tree.children.slice(start) as ElementContent[];
// Prepend text from a partial matched earlier text.
if (startTextRemainder) {
line.unshift({ type: "text", value: startTextRemainder });
startTextRemainder = "";
}

if (line.length > 0) {
lineNumber += 1;
replacement.push(createLine(line, lineNumber));
}

// Replace children with new array.
tree.children = replacement;

return tree;
}

function createLine(children: ElementContent[], line: number): ElementContent {
return {
type: "element",
tagName: "tr",
properties: {
class: "line",
id: "L" + line,
},
children: [
{
type: "element",
tagName: "td",
properties: {
className: "line-number",
},
children: [
{
type: "element",
tagName: "a",
properties: { href: "#L" + line },
children: [{ type: "text", value: line.toString() }],
},
],
},
{
type: "element",
tagName: "td",
properties: {
className: "line-content",
},
children: [
{
type: "element",
tagName: "pre",
properties: {
className: "content",
},
children,
},
],
},
],
};
}

0 comments on commit 665c5a9

Please sign in to comment.