ConceptPortal-public/rsconcept/frontend/src/components/RSInput/bracketMatching.ts

30 lines
849 B
TypeScript
Raw Normal View History

2023-08-14 23:02:41 +03:00
import { bracketMatching, MatchResult } from '@codemirror/language';
import { Decoration, EditorView } from '@codemirror/view';
import { BRACKETS_THEME } from '@/styling/color';
2023-08-27 15:39:49 +03:00
2023-12-28 14:04:44 +03:00
const matchingMark = Decoration.mark({ class: 'cc-matchingBracket' });
const nonMatchingMark = Decoration.mark({ class: 'cc-nonmatchingBracket' });
2023-08-14 23:02:41 +03:00
function bracketRender(match: MatchResult) {
2023-08-15 00:41:09 +03:00
const decorations = [];
2023-12-27 19:34:39 +03:00
const mark = match.matched ? matchingMark : nonMatchingMark;
2023-08-15 00:41:09 +03:00
decorations.push(mark.range(match.start.from, match.start.to));
if (match.end) {
decorations.push(mark.range(match.end.from, match.end.to));
}
return decorations;
2023-08-14 23:02:41 +03:00
}
const theme = EditorView.baseTheme(BRACKETS_THEME);
2023-08-15 00:41:09 +03:00
export function ccBracketMatching() {
2023-08-27 19:48:08 +03:00
return [
2023-12-28 14:04:44 +03:00
bracketMatching({
2023-08-27 19:48:08 +03:00
renderMatch: bracketRender,
2023-12-28 14:04:44 +03:00
brackets: '{}[]()'
2023-08-27 19:48:08 +03:00
}),
theme
2023-08-27 19:48:08 +03:00
];
2023-12-28 14:04:44 +03:00
}