Skip to content

Commit dadb687

Browse files
committed
Fix annotation nesting
1 parent 37a99f6 commit dadb687

10 files changed

Lines changed: 420 additions & 99 deletions

File tree

.changeset/six-windows-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@code-hike/lighter": patch
3+
---
4+
5+
Fix annotation nesting

lib/dist/index.cjs.js

Lines changed: 75 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dist/index.d.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
type LanguageAlias = "abap" | "actionscript-3" | "ada" | "apache" | "apex" | "apl" | "applescript" | "asm" | "astro" | "awk" | "ballerina" | "bat" | "batch" | "berry" | "be" | "bibtex" | "bicep" | "blade" | "c" | "cadence" | "cdc" | "clarity" | "clojure" | "clj" | "cmake" | "cobol" | "codeql" | "ql" | "coffee" | "cpp" | "crystal" | "csharp" | "c#" | "cs" | "css" | "cue" | "d" | "dart" | "diff" | "docker" | "dream-maker" | "elixir" | "elm" | "erb" | "erlang" | "erl" | "fish" | "fsharp" | "f#" | "fs" | "gherkin" | "git-commit" | "git-rebase" | "glsl" | "gnuplot" | "go" | "graphql" | "groovy" | "hack" | "haml" | "handlebars" | "hbs" | "haskell" | "hs" | "hcl" | "hlsl" | "html" | "http" | "imba" | "ini" | "java" | "javascript" | "js" | "jinja-html" | "json" | "json5" | "jsonc" | "jsonnet" | "jssm" | "fsl" | "jsx" | "julia" | "kotlin" | "latex" | "less" | "liquid" | "lisp" | "logo" | "lua" | "make" | "makefile" | "markdown" | "md" | "marko" | "matlab" | "mdx" | "mermaid" | "nginx" | "nim" | "nix" | "objective-c" | "objc" | "objective-cpp" | "ocaml" | "pascal" | "perl" | "php" | "plsql" | "postcss" | "powershell" | "ps" | "ps1" | "prisma" | "prolog" | "proto" | "pug" | "jade" | "puppet" | "purescript" | "python" | "py" | "r" | "raku" | "perl6" | "razor" | "rel" | "riscv" | "rst" | "ruby" | "rb" | "rust" | "rs" | "sas" | "sass" | "scala" | "scheme" | "scss" | "shaderlab" | "shader" | "shellscript" | "shell" | "bash" | "sh" | "zsh" | "smalltalk" | "solidity" | "sparql" | "sql" | "ssh-config" | "stata" | "stylus" | "styl" | "svelte" | "swift" | "system-verilog" | "tasl" | "tcl" | "tex" | "toml" | "tsx" | "turtle" | "twig" | "typescript" | "ts" | "v" | "vb" | "cmd" | "verilog" | "vhdl" | "viml" | "vim" | "vimscript" | "vue-html" | "vue" | "wasm" | "wenyan" | "文言" | "xml" | "xsl" | "yaml" | "yml" | "zenscript";
22
type LanguageName = "abap" | "actionscript-3" | "ada" | "apache" | "apex" | "apl" | "applescript" | "asm" | "astro" | "awk" | "ballerina" | "bat" | "berry" | "bibtex" | "bicep" | "blade" | "c" | "cadence" | "clarity" | "clojure" | "cmake" | "cobol" | "codeql" | "coffee" | "cpp" | "crystal" | "csharp" | "css" | "cue" | "d" | "dart" | "diff" | "docker" | "dream-maker" | "elixir" | "elm" | "erb" | "erlang" | "fish" | "fsharp" | "gherkin" | "git-commit" | "git-rebase" | "glsl" | "gnuplot" | "go" | "graphql" | "groovy" | "hack" | "haml" | "handlebars" | "haskell" | "hcl" | "hlsl" | "html" | "http" | "imba" | "ini" | "java" | "javascript" | "jinja-html" | "json" | "json5" | "jsonc" | "jsonnet" | "jssm" | "jsx" | "julia" | "kotlin" | "latex" | "less" | "liquid" | "lisp" | "logo" | "lua" | "make" | "markdown" | "marko" | "matlab" | "mdx" | "mermaid" | "nginx" | "nim" | "nix" | "objective-c" | "objective-cpp" | "ocaml" | "pascal" | "perl" | "php" | "plsql" | "postcss" | "powershell" | "prisma" | "prolog" | "proto" | "pug" | "puppet" | "purescript" | "python" | "r" | "raku" | "razor" | "rel" | "riscv" | "rst" | "ruby" | "rust" | "sas" | "sass" | "scala" | "scheme" | "scss" | "shaderlab" | "shellscript" | "smalltalk" | "solidity" | "sparql" | "sql" | "ssh-config" | "stata" | "stylus" | "svelte" | "swift" | "system-verilog" | "tasl" | "tcl" | "tex" | "toml" | "tsx" | "turtle" | "twig" | "typescript" | "v" | "vb" | "verilog" | "vhdl" | "viml" | "vue-html" | "vue" | "wasm" | "wenyan" | "xml" | "xsl" | "yaml" | "zenscript";
33

4+
type RawTheme = {
5+
name?: string;
6+
type?: string;
7+
tokenColors?: ThemeSetting[];
8+
colors?: {
9+
[key: string]: string;
10+
};
11+
[key: string]: any;
12+
};
13+
type ThemeSetting = {
14+
name?: string;
15+
scope?: string | string[];
16+
settings: {
17+
fontStyle?: string;
18+
foreground?: string;
19+
background?: string;
20+
};
21+
};
22+
declare const ALL_NAMES: readonly ["dark-plus", "dracula-soft", "dracula", "github-dark", "github-dark-dimmed", "github-light", "light-plus", "material-darker", "material-default", "material-lighter", "material-ocean", "material-palenight", "min-dark", "min-light", "monokai", "nord", "one-dark-pro", "poimandres", "slack-dark", "slack-ochin", "solarized-dark", "solarized-light"];
23+
type NamesTuple = typeof ALL_NAMES;
24+
type StringTheme = NamesTuple[number];
25+
type Theme = StringTheme | RawTheme;
26+
427
type LineNumber = number;
528
type ColumnNumber = number;
629
type MultiLineRange = {
@@ -50,29 +73,6 @@ type LineGroup = {
5073
};
5174
type Lines = (Line | LineGroup)[];
5275

53-
type RawTheme = {
54-
name?: string;
55-
type?: string;
56-
tokenColors?: ThemeSetting[];
57-
colors?: {
58-
[key: string]: string;
59-
};
60-
[key: string]: any;
61-
};
62-
type ThemeSetting = {
63-
name?: string;
64-
scope?: string | string[];
65-
settings: {
66-
fontStyle?: string;
67-
foreground?: string;
68-
background?: string;
69-
};
70-
};
71-
declare const ALL_NAMES: readonly ["dark-plus", "dracula-soft", "dracula", "github-dark", "github-dark-dimmed", "github-light", "light-plus", "material-darker", "material-default", "material-lighter", "material-ocean", "material-palenight", "min-dark", "min-light", "monokai", "nord", "one-dark-pro", "poimandres", "slack-dark", "slack-ochin", "solarized-dark", "solarized-light"];
72-
type NamesTuple = typeof ALL_NAMES;
73-
type StringTheme = NamesTuple[number];
74-
type Theme = StringTheme | RawTheme;
75-
7676
declare class UnknownLanguageError extends Error {
7777
alias: string;
7878
constructor(alias: string);
@@ -117,4 +117,4 @@ declare class UnknownThemeError extends Error {
117117
constructor(theme: string);
118118
}
119119

120-
export { Annotation, LanguageAlias, Lines, RawTheme, StringTheme, Theme, UnknownLanguageError, UnknownThemeError, annotatedHighlight, extractAnnotations, highlight };
120+
export { Annotation, LanguageAlias, Line, LineGroup, Lines, RawTheme, StringTheme, Theme, Token, TokenGroup, Tokens, UnknownLanguageError, UnknownThemeError, annotatedHighlight, extractAnnotations, highlight };

lib/dist/index.esm.mjs

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,9 @@ function extractCommentsFromCode(code, grammar, annotationNames) {
25032503
.map((line) => {
25042504
const { annotations, lineWithoutComments } = getAnnotationsFromLine(line, annotationNames, lineNumber);
25052505
allAnnotations.push(...annotations);
2506+
if (lineWithoutComments) {
2507+
lineNumber++;
2508+
}
25062509
return lineWithoutComments;
25072510
})
25082511
.filter((line) => line !== null);
@@ -2588,6 +2591,7 @@ function annotateLine(line, annotations) {
25882591
toColumn: columnNumber + token.content.length - 1,
25892592
token,
25902593
});
2594+
columnNumber += token.content.length;
25912595
});
25922596
annotations.forEach((annotation) => {
25932597
annotatedLine = reannotateLine(annotatedLine, annotation);
@@ -2610,7 +2614,7 @@ function removeFakeGroups$1(group) {
26102614
}
26112615
}
26122616
function reannotateLine(annotatedLine, annotation) {
2613-
const { range, name, query } = annotation;
2617+
const { range } = annotation;
26142618
const { fromColumn, toColumn } = range;
26152619
const newAnnotatedLine = [];
26162620
let i = 0;
@@ -2631,8 +2635,9 @@ function reannotateLine(annotatedLine, annotation) {
26312635
const firstGroup = annotatedLine[i];
26322636
if (firstGroup.fromColumn < fromColumn) {
26332637
// we need to split the first group in two
2634-
newGroup.tokens.push(Object.assign(Object.assign({}, firstGroup), { toColumn: fromColumn - 1 }));
2635-
newGroup.tokens.push(Object.assign(Object.assign({}, firstGroup), { fromColumn }));
2638+
const [firstHalf, secondHalf] = splitGroup$1(firstGroup, fromColumn);
2639+
newAnnotatedLine.push(firstHalf);
2640+
newGroup.tokens.push(secondHalf);
26362641
i++;
26372642
}
26382643
while (i < annotatedLine.length && annotatedLine[i].toColumn < toColumn) {
@@ -2646,15 +2651,45 @@ function reannotateLine(annotatedLine, annotation) {
26462651
const lastGroup = annotatedLine[i];
26472652
if (lastGroup.toColumn > toColumn) {
26482653
// we need to split the last group in two
2649-
newGroup.tokens.push(Object.assign(Object.assign({}, lastGroup), { toColumn }));
2650-
newGroup.tokens.push(Object.assign(Object.assign({}, lastGroup), { fromColumn: toColumn + 1 }));
2654+
const [firstHalf, secondHalf] = splitGroup$1(lastGroup, toColumn + 1);
2655+
newGroup.tokens.push(firstHalf);
2656+
newAnnotatedLine.push(secondHalf);
26512657
i++;
26522658
}
26532659
while (i < annotatedLine.length) {
26542660
newAnnotatedLine.push(annotatedLine[i]);
26552661
i++;
26562662
}
26572663
return newAnnotatedLine;
2664+
}
2665+
function splitGroup$1(group, column) {
2666+
if ("token" in group) {
2667+
const firstToken = Object.assign(Object.assign({}, group.token), { content: group.token.content.slice(0, column - group.fromColumn) });
2668+
const secondToken = Object.assign(Object.assign({}, group.token), { content: group.token.content.slice(column - group.fromColumn) });
2669+
const firstGroup = Object.assign(Object.assign({}, group), { toColumn: column - 1, token: firstToken });
2670+
const secondGroup = Object.assign(Object.assign({}, group), { fromColumn: column, token: secondToken });
2671+
return [firstGroup, secondGroup];
2672+
}
2673+
else {
2674+
const firstTokens = [];
2675+
const secondTokens = [];
2676+
group.tokens.forEach((token) => {
2677+
if (token.toColumn < column) {
2678+
firstTokens.push(token);
2679+
}
2680+
else if (token.fromColumn >= column) {
2681+
secondTokens.push(token);
2682+
}
2683+
else {
2684+
const [firstGroup, secondGroup] = splitGroup$1(token, column);
2685+
firstTokens.push(firstGroup);
2686+
secondTokens.push(secondGroup);
2687+
}
2688+
});
2689+
const firstGroup = Object.assign(Object.assign({}, group), { toColumn: column - 1, tokens: firstTokens });
2690+
const secondGroup = Object.assign(Object.assign({}, group), { fromColumn: column, tokens: secondTokens });
2691+
return [firstGroup, secondGroup];
2692+
}
26582693
}
26592694

26602695
function annotateLines(lines, annotations) {
@@ -2707,8 +2742,9 @@ function reannotateLines(annotatedLines, annotation) {
27072742
};
27082743
const firstGroup = annotatedLines[i];
27092744
if (firstGroup.fromLineNumber < fromLineNumber) {
2710-
newGroup.lines.push(Object.assign(Object.assign({}, firstGroup), { toLineNumber: fromLineNumber - 1 }));
2711-
newGroup.lines.push(Object.assign(Object.assign({}, firstGroup), { fromLineNumber }));
2745+
const [firstHalf, secondHalf] = splitGroup(firstGroup, fromLineNumber);
2746+
newAnnotatedLines.push(firstHalf);
2747+
newGroup.lines.push(secondHalf);
27122748
i++;
27132749
}
27142750
while (i < annotatedLines.length &&
@@ -2722,15 +2758,45 @@ function reannotateLines(annotatedLines, annotation) {
27222758
}
27232759
const lastGroup = annotatedLines[i];
27242760
if (lastGroup.toLineNumber > toLineNumber) {
2725-
newAnnotatedLines.push(Object.assign(Object.assign({}, lastGroup), { toLineNumber }));
2726-
newAnnotatedLines.push(Object.assign(Object.assign({}, lastGroup), { fromLineNumber: toLineNumber + 1 }));
2761+
const [firstHalf, secondHalf] = splitGroup(lastGroup, toLineNumber + 1);
2762+
newGroup.lines.push(firstHalf);
2763+
newAnnotatedLines.push(secondHalf);
27272764
i++;
27282765
}
27292766
while (i < annotatedLines.length) {
27302767
newAnnotatedLines.push(annotatedLines[i]);
27312768
i++;
27322769
}
27332770
return newAnnotatedLines;
2771+
}
2772+
function splitGroup(group, lineNumber) {
2773+
if ("line" in group) {
2774+
return [
2775+
Object.assign(Object.assign({}, group), { toLineNumber: lineNumber - 1 }),
2776+
Object.assign(Object.assign({}, group), { fromLineNumber: lineNumber }),
2777+
];
2778+
}
2779+
else {
2780+
const firstLines = [];
2781+
const secondLines = [];
2782+
group.lines.forEach((line) => {
2783+
if (line.toLineNumber < lineNumber) {
2784+
firstLines.push(line);
2785+
}
2786+
else if (line.fromLineNumber >= lineNumber) {
2787+
secondLines.push(line);
2788+
}
2789+
else {
2790+
const [firstLine, secondLine] = splitGroup(line, lineNumber);
2791+
firstLines.push(firstLine);
2792+
secondLines.push(secondLine);
2793+
}
2794+
});
2795+
return [
2796+
Object.assign(Object.assign({}, group), { toLineNumber: lineNumber - 1, lines: firstLines }),
2797+
Object.assign(Object.assign({}, group), { fromLineNumber: lineNumber, lines: secondLines }),
2798+
];
2799+
}
27342800
}
27352801

27362802
function applyAnnotations(lines, annotations) {

0 commit comments

Comments
 (0)