Avoid auto-closing $ directly before a TeX command (#15039)

GitOrigin-RevId: e620d6b5c82e179c294930801a95fc4c2c77db57
This commit is contained in:
Alf Eaton 2023-10-04 16:11:31 +01:00 committed by Copybot
parent dd19a014c2
commit 04a4450341
2 changed files with 38 additions and 2 deletions

View file

@ -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) {

View file

@ -138,6 +138,18 @@ describe('close brackets', { scrollBehavior: false }, function () {
cy.get('@active-line').should('have.text', '2$')
})
it('does not auto-close a dollar sign before a command', function () {
cy.get('@active-line').type('\\nu')
cy.get('@active-line').type('{leftArrow}{leftArrow}{leftArrow}$')
cy.get('@active-line').should('have.text', '$\\nu')
})
it('does auto-close a dollar sign before a newline', function () {
cy.get('@active-line').type('\\\\')
cy.get('@active-line').type('{leftArrow}{leftArrow}$')
cy.get('@active-line').should('have.text', '$$\\\\')
})
it('does auto-close a curly bracket before punctuation', function () {
cy.get('@active-line').type(':2')
cy.get('@active-line').type('{leftArrow}{leftArrow}{{}')