Skip to content

Commit

Permalink
editor: improve parser and mapping between tree-sitter and Lezer
Browse files Browse the repository at this point in the history
With this change we reuse the previous parse trees in more cases to
improve performance and also tighten up the handling of some incorrect
states.

Also, re-add highlighting of trailing whitespace and add red background
to parse errors.
  • Loading branch information
yagebu committed Dec 30, 2023
1 parent 915c5db commit ce934fe
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 83 deletions.
2 changes: 1 addition & 1 deletion frontend/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@

/* Editor */
--editor-comment: #998;
--editor-trailing-whitespace: rgb(255 199 199 / 50%);
--editor-directive: #333;
--editor-class: #b84;
--editor-date: #099;
--editor-constant: #008080;
--editor-account: var(--link-color);
--editor-invalid: #333;
--editor-invalid-background: rgb(255 199 199 / 50%);
--editor-activeline: #ffc;

/* Misc */
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/codemirror/beancount-highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ export const beancountHighlight = HighlightStyle.define([
// Invalid token
tag: tags.invalid,
color: "var(--editor-invalid)",
},
{
// Trailing whitespace
tag: tags.special(tags.invalid),
backgroundColor: "var(--editor-trailing-whitespace)",
backgroundColor: "var(--editor-invalid-background)",
},
]);
3 changes: 2 additions & 1 deletion frontend/src/codemirror/beancount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
LanguageSupport,
syntaxHighlighting,
} from "@codemirror/language";
import { keymap } from "@codemirror/view";
import { highlightTrailingWhitespace, keymap } from "@codemirror/view";
import { styleTags, tags } from "@lezer/highlight";
import TSParser from "web-tree-sitter";
import ts_wasm from "web-tree-sitter/tree-sitter.wasm";
Expand Down Expand Up @@ -41,6 +41,7 @@ const beancountLanguageSupportExtensions = [
commentTokens: { line: ";" },
indentOnInput: /^\s+\d\d\d\d/,
}),
highlightTrailingWhitespace(),
];

/** The node props that allow for highlighting/coloring of the code. */
Expand Down
Loading

0 comments on commit ce934fe

Please sign in to comment.