Files
RuneStone/src/lib/editor/sourceView.ts
T
2026-11-03 00:23:07 -07:00

61 lines
3.8 KiB
TypeScript

import { countColumn, EditorSelection, EditorState, type Extension } from "@codemirror/state";
import { EditorView, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, type Command } from "@codemirror/view";
import { bracketMatching, foldGutter, foldKeymap, getIndentUnit, HighlightStyle, indentOnInput, syntaxHighlighting } from "@codemirror/language";
import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete";
import { highlightSelectionMatches } from "@codemirror/search";
import { indentMore } from "@codemirror/commands";
import { tags } from "@lezer/highlight";
import { chromeLayout } from "../workspace/chrome";
import type { Chrome } from "../workspace/types";
const syntax = (role: string, fallback: string) => `var(--rs-syntax-${role}, ${fallback})`;
/** Syntax colors come from the theme tokens, so a theme change needs no editor update. */
const sourceHighlight = HighlightStyle.define([
{ tag: [tags.keyword, tags.modifier, tags.operatorKeyword, tags.controlKeyword, tags.definitionKeyword, tags.moduleKeyword, tags.tagName], color: syntax("keyword", "var(--rs-accent)") },
{ tag: tags.heading, color: syntax("heading", "var(--rs-accent)"), fontWeight: "600" },
{ tag: [tags.string, tags.special(tags.string), tags.regexp, tags.attributeValue, tags.url, tags.monospace], color: syntax("string", "var(--rs-ok)") },
{ tag: [tags.number, tags.bool, tags.null, tags.atom, tags.unit], color: syntax("number", "var(--rs-special)") },
{ tag: tags.comment, color: syntax("comment", "var(--rs-text-dim)") },
{ tag: [tags.typeName, tags.className, tags.namespace], color: syntax("type", "var(--rs-accent-2)") },
{ tag: [tags.function(tags.variableName), tags.function(tags.propertyName), tags.macroName], color: syntax("function", "var(--rs-accent-2)") },
{ tag: [tags.propertyName, tags.attributeName], color: syntax("property", "var(--rs-accent-2)") },
{ tag: [tags.standard(tags.variableName), tags.self, tags.special(tags.variableName)], color: syntax("constant", "var(--rs-text-dim)") },
{ tag: tags.variableName, color: syntax("variable", "var(--rs-text)") },
{ tag: [tags.punctuation, tags.operator, tags.meta, tags.processingInstruction], color: syntax("punctuation", "var(--rs-text-dim)") },
{ tag: tags.emphasis, fontStyle: "italic" },
{ tag: tags.strong, fontWeight: "600" },
{ tag: tags.strikethrough, textDecoration: "line-through" },
{ tag: tags.link, color: "var(--rs-link)", textDecoration: "underline" },
{ tag: tags.invalid, color: "var(--rs-error)" },
]);
/** Tab inserts spaces to the next indent stop at each caret. With a selection, it indents the lines. */
export const insertIndent: Command = view => {
const { state } = view;
if (state.selection.ranges.some(range => !range.empty)) return indentMore(view);
const unit = getIndentUnit(state);
view.dispatch(state.changeByRange(range => {
const line = state.doc.lineAt(range.head);
const insert = " ".repeat(unit - countColumn(line.text.slice(0, range.head - line.from), state.tabSize) % unit);
return { changes: { from: range.head, insert }, range: EditorSelection.cursor(range.head + insert.length) };
}), { scrollIntoView: true, userEvent: "input.indent" });
return true;
};
/** Code editing support for Source: syntax colors, brackets, several carets, and matches of the selection. */
export const sourceEditing: Extension = [
syntaxHighlighting(sourceHighlight), bracketMatching(), closeBrackets(), indentOnInput(), highlightSelectionMatches(),
EditorState.allowMultipleSelections.of(true),
keymap.of(closeBracketsKeymap),
];
export function sourceChrome(chrome: Chrome): Extension {
const layout = chromeLayout(chrome);
return [
layout.wrap ? EditorView.lineWrapping : [],
layout.gutters ? [lineNumbers(), foldGutter(), highlightActiveLineGutter(), keymap.of(foldKeymap)] : [],
layout.activeLine ? highlightActiveLine() : [],
];
}