2023-08-14 23:02:41 +03:00
|
|
|
import { bracketMatching, MatchResult } from '@codemirror/language';
|
|
|
|
import { Decoration, EditorView } from '@codemirror/view';
|
|
|
|
|
2023-08-27 15:39:49 +03:00
|
|
|
import { bracketsDarkT, bracketsLightT } from '../../utils/color';
|
|
|
|
|
2023-09-03 18:26:50 +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 = [];
|
|
|
|
const mark = match.matched ? matchingMark : nonmatchingMark;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-27 15:39:49 +03:00
|
|
|
const darkTheme = EditorView.baseTheme(bracketsDarkT);
|
2023-08-15 00:41:09 +03:00
|
|
|
|
2023-08-27 15:39:49 +03:00
|
|
|
const lightTheme = EditorView.baseTheme(bracketsLightT);
|
2023-08-14 23:02:41 +03:00
|
|
|
|
2023-08-15 00:41:09 +03:00
|
|
|
export function ccBracketMatching(darkMode: boolean) {
|
2023-08-27 19:48:08 +03:00
|
|
|
return [
|
|
|
|
bracketMatching({
|
|
|
|
renderMatch: bracketRender,
|
|
|
|
brackets:'{}[]()'
|
|
|
|
}),
|
|
|
|
darkMode ? darkTheme : lightTheme
|
|
|
|
];
|
2023-08-14 23:02:41 +03:00
|
|
|
}
|