overleaf/services/web/frontend/js/features/source-editor/extensions/auto-pair.ts
Alf Eaton 6c21f0821c [cm6] Handle multiple selection ranges when applying snippets (#12830)
* Use fork of @codemirror/autocomplete
* Handle multiple selection ranges when applying snippets

GitOrigin-RevId: 04afc087ac127206463ea3d4950284a50308364a
2023-05-04 08:04:39 +00:00

23 lines
726 B
TypeScript

import { keymap } from '@codemirror/view'
import { Compartment, Prec, TransactionSpec } from '@codemirror/state'
import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete'
const autoPairConf = new Compartment()
export const autoPair = ({
autoPairDelimiters,
}: {
autoPairDelimiters: boolean
}) => autoPairConf.of(autoPairDelimiters ? extension : [])
export const setAutoPair = (autoPairDelimiters: boolean): TransactionSpec => {
return {
effects: autoPairConf.reconfigure(autoPairDelimiters ? extension : []),
}
}
const extension = [
closeBrackets(),
// NOTE: using Prec.highest as this needs to run before the default Backspace handler
Prec.highest(keymap.of(closeBracketsKeymap)),
]