Skip to content

Commit

Permalink
Fix text annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
pomber committed Apr 2, 2023
1 parent 9872fd4 commit bc57e68
Show file tree
Hide file tree
Showing 12 changed files with 728 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-comics-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@code-hike/lighter": patch
---

Fix text annotations
4 changes: 3 additions & 1 deletion lib/dist/browser.esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,9 @@ const commentsTheme = {
],
};
function extractCommentsFromCode(code, grammar, annotationNames) {
const lines = highlightTokens(code, grammar, commentsTheme);
const lines = !grammar
? highlightText(code)
: highlightTokens(code, grammar, commentsTheme);
const allAnnotations = [];
let lineNumber = 1;
const cleanLines = lines
Expand Down
4 changes: 3 additions & 1 deletion lib/dist/index.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/dist/index.esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3008,7 +3008,9 @@ const commentsTheme = {
],
};
function extractCommentsFromCode(code, grammar, annotationNames) {
const lines = highlightTokens(code, grammar, commentsTheme);
const lines = !grammar
? highlightText(code)
: highlightTokens(code, grammar, commentsTheme);
const allAnnotations = [];
let lineNumber = 1;
const cleanLines = lines
Expand Down
6 changes: 4 additions & 2 deletions lib/src/comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IGrammar } from "vscode-textmate";
import { Token } from "./annotations";
import { highlightTokens } from "./highlighter";
import { highlightText, highlightTokens } from "./highlighter";
import { CodeRange, parseRelativeRanges } from "./range";
import { FinalTheme } from "./theme";

Expand Down Expand Up @@ -35,7 +35,9 @@ export function extractCommentsFromCode(
grammar: IGrammar,
annotationNames: string[]
) {
const lines = highlightTokens(code, grammar, commentsTheme);
const lines = !grammar
? highlightText(code)
: highlightTokens(code, grammar, commentsTheme);

const allAnnotations: Annotation[] = [];

Expand Down
223 changes: 223 additions & 0 deletions lib/test/__snapshots__/browser.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,228 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`extract annottations 1`] = `
{
"annotations": [
{
"name": "foo",
"query": "",
"ranges": [
{
"fromLineNumber": 2,
"toLineNumber": 2,
},
],
},
],
"code": "const x = 1;
const y = 2;",
}
`;

exports[`extract annottations 2`] = `
{
"colors": {
"activeTabBackground": "#1E1E1E",
"activeTabBorder": "#1E1E1E",
"activeTabForeground": "#ffffff",
"activeTabTopBorder": undefined,
"background": "#1E1E1E",
"colorScheme": "dark",
"diffInsertedLineBackground": "#9bb95533",
"diffInsertedTextBackground": "#9ccc2c33",
"diffRemovedLineBackground": "#ff000033",
"diffRemovedTextBackground": "#ff000033",
"editorBackground": "#1E1E1E",
"editorGroupHeaderBackground": "#252526",
"foreground": "#D4D4D4",
"hoverTabBackground": "#2D2D2D",
"hoverTabForeground": "#ffffff80",
"iconForeground": "#C5C5C5",
"inactiveTabBackground": "#2D2D2D",
"inactiveTabForeground": "#ffffff80",
"lineNumberForeground": "#858585",
"listHoverBackground": "#2A2D2E",
"listHoverForeground": undefined,
"listSelectionBackground": "#37373D",
"listSelectionForeground": undefined,
"selectionBackground": "#264F78",
"sideBarBackground": "#252526",
"sideBarBorder": "#252526",
"sideBarForeground": "#D4D4D4",
"tabBorder": "#252526",
"tabsBorder": undefined,
},
"lang": "javascript",
"lines": [
{
"lineNumber": 1,
"tokens": [
{
"content": "const",
"style": {
"color": "#569CD6",
},
},
{
"content": " ",
"style": {
"color": "#D4D4D4",
},
},
{
"content": "x",
"style": {
"color": "#4FC1FF",
},
},
{
"content": " = ",
"style": {
"color": "#D4D4D4",
},
},
{
"content": "1",
"style": {
"color": "#B5CEA8",
},
},
{
"content": ";",
"style": {
"color": "#D4D4D4",
},
},
],
},
{
"annotationName": "foo",
"annotationQuery": "",
"fromLineNumber": 2,
"lines": [
{
"lineNumber": 2,
"tokens": [
{
"content": "const",
"style": {
"color": "#569CD6",
},
},
{
"content": " ",
"style": {
"color": "#D4D4D4",
},
},
{
"content": "y",
"style": {
"color": "#4FC1FF",
},
},
{
"content": " = ",
"style": {
"color": "#D4D4D4",
},
},
{
"content": "2",
"style": {
"color": "#B5CEA8",
},
},
{
"content": ";",
"style": {
"color": "#D4D4D4",
},
},
],
},
],
"toLineNumber": 2,
},
],
}
`;

exports[`extract annottations from text 1`] = `
{
"annotations": [],
"code": "const x = 1;
// foo[3:5]
const y = 2;",
}
`;

exports[`extract annottations from text 2`] = `
{
"colors": {
"activeTabBackground": "#1E1E1E",
"activeTabBorder": "#1E1E1E",
"activeTabForeground": "#ffffff",
"activeTabTopBorder": undefined,
"background": "#1E1E1E",
"colorScheme": "dark",
"diffInsertedLineBackground": "#9bb95533",
"diffInsertedTextBackground": "#9ccc2c33",
"diffRemovedLineBackground": "#ff000033",
"diffRemovedTextBackground": "#ff000033",
"editorBackground": "#1E1E1E",
"editorGroupHeaderBackground": "#252526",
"foreground": "#D4D4D4",
"hoverTabBackground": "#2D2D2D",
"hoverTabForeground": "#ffffff80",
"iconForeground": "#C5C5C5",
"inactiveTabBackground": "#2D2D2D",
"inactiveTabForeground": "#ffffff80",
"lineNumberForeground": "#858585",
"listHoverBackground": "#2A2D2E",
"listHoverForeground": undefined,
"listSelectionBackground": "#37373D",
"listSelectionForeground": undefined,
"selectionBackground": "#264F78",
"sideBarBackground": "#252526",
"sideBarBorder": "#252526",
"sideBarForeground": "#D4D4D4",
"tabBorder": "#252526",
"tabsBorder": undefined,
},
"lang": "text",
"lines": [
{
"lineNumber": 1,
"tokens": [
{
"content": "const x = 1;",
"style": {},
},
],
},
{
"lineNumber": 2,
"tokens": [
{
"content": "// foo[3:5]",
"style": {},
},
],
},
{
"lineNumber": 3,
"tokens": [
{
"content": "const y = 2;",
"style": {},
},
],
},
],
}
`;

exports[`highlight js 1`] = `
{
"colors": {
Expand Down
Loading

1 comment on commit bc57e68

@vercel
Copy link

@vercel vercel bot commented on bc57e68 Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lighter – ./

lighter-git-main-codehike.vercel.app
lighter-codehike.vercel.app
lighter.codehike.org

Please sign in to comment.