Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor: improve parser and mapping between tree-sitter and Lezer #1729

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 6 additions & 2 deletions frontend/src/codemirror/editor-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function scrollToLine(
state: EditorState,
line: number,
): TransactionSpec {
if (line < 1 || line > state.doc.lines) {
return {};
}
const linePos = state.doc.line(line);
return {
selection: { ...linePos, anchor: linePos.from },
Expand All @@ -41,10 +44,11 @@ export function setErrors(
state: EditorState,
errors: BeancountError[],
): TransactionSpec {
const { doc } = state;
const diagnostics = errors.map(({ source, message }): Diagnostic => {
// Show errors without a line number on first line and ensure it is within the document.
const lineno = Math.min(Math.max(source?.lineno ?? 1, 1), state.doc.lines);
const line = state.doc.line(lineno);
const lineno = Math.min(Math.max(source?.lineno ?? 1, 1), doc.lines);
const line = doc.line(lineno);
return {
from: line.from,
to: line.to,
Expand Down
Loading