|
|
@@ -1,5 +1,14 @@
|
|
|
-import { EditorState, SelectionRange, Text } from '@codemirror/state'
|
|
|
-import { CloseBracketConfig, prevChar } from '@codemirror/autocomplete'
|
|
|
+import {
|
|
|
+ CharCategory,
|
|
|
+ EditorState,
|
|
|
+ SelectionRange,
|
|
|
+ Text,
|
|
|
+} from '@codemirror/state'
|
|
|
+import {
|
|
|
+ CloseBracketConfig,
|
|
|
+ nextChar,
|
|
|
+ prevChar,
|
|
|
+} from '@codemirror/autocomplete'
|
|
|
|
|
|
export const closeBracketConfig: CloseBracketConfig = {
|
|
|
brackets: ['$', '$$', '[', '{', '('],
|
|
|
@@ -22,6 +31,21 @@ export const closeBracketConfig: CloseBracketConfig = {
|
|
|
// don't auto-close \$
|
|
|
return open
|
|
|
}
|
|
|
+
|
|
|
+ const next = nextChar(state.doc, range.head)
|
|
|
+ if (next === '\\') {
|
|
|
+ // avoid auto-closing $ before a TeX command
|
|
|
+ const pos = range.head + prev.length
|
|
|
+ const postnext = nextChar(state.doc, pos)
|
|
|
+
|
|
|
+ if (state.charCategorizer(pos)(postnext) !== CharCategory.Word) {
|
|
|
+ return open + '$'
|
|
|
+ }
|
|
|
+
|
|
|
+ // don't auto-close $\command
|
|
|
+ return open
|
|
|
+ }
|
|
|
+
|
|
|
// avoid creating an odd number of dollar signs
|
|
|
const count = countSurroundingCharacters(state.doc, range.from, open)
|
|
|
if (count % 2 !== 0) {
|