[cm6+rt] Make formatting commands work after UnknownCommands without arguments (#12625)
GitOrigin-RevId: 0eb59e6580d6f217c46424ede0fa6f79c8786940
This commit is contained in:
committed by
Copybot
parent
4b2cc907e2
commit
73191f56e1
@@ -212,11 +212,27 @@ function getParentNode(
|
|||||||
assoc: 0 | 1 | -1 = 1
|
assoc: 0 | 1 | -1 = 1
|
||||||
): SyntaxNode | undefined {
|
): SyntaxNode | undefined {
|
||||||
const tree = ensureSyntaxTree(state, 1000)
|
const tree = ensureSyntaxTree(state, 1000)
|
||||||
let node: SyntaxNode | undefined | null =
|
let node: SyntaxNode | undefined | null = null
|
||||||
typeof position === 'number'
|
if (typeof position === 'number') {
|
||||||
? tree?.resolveInner(position, assoc)
|
node = tree?.resolveInner(position, assoc)?.parent
|
||||||
: position
|
// HACK: Spaces after UnknownCommands (and other commands without arguments)
|
||||||
node = node?.parent
|
// are included in the Command node. So we have to adjust for that here.
|
||||||
|
const preceedingCharacter = state.sliceDoc(
|
||||||
|
Math.max(0, position - 1),
|
||||||
|
position
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
preceedingCharacter === ' ' &&
|
||||||
|
['UnknownCommand', 'Item', 'Left', 'Right'].some(name =>
|
||||||
|
node?.type.is(name)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
node = ancestorOfNodeWithType(node, 'Command')?.parent
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
node = position?.parent
|
||||||
|
}
|
||||||
|
|
||||||
while (
|
while (
|
||||||
['LongArg', 'TextArgument', 'OpenBrace', 'CloseBrace'].includes(
|
['LongArg', 'TextArgument', 'OpenBrace', 'CloseBrace'].includes(
|
||||||
node?.type.name || ''
|
node?.type.name || ''
|
||||||
|
|||||||
@@ -161,5 +161,23 @@ describe('toggleRanges', function () {
|
|||||||
expect(cm).line(1).to.equal('\\textbf{\\textit{this <is} my} range>')
|
expect(cm).line(1).to.equal('\\textbf{\\textit{this <is} my} range>')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('when range is after a command', function () {
|
||||||
|
it('still formats list items', function () {
|
||||||
|
const cm = new CodemirrorTestSession([
|
||||||
|
'\\begin{itemize}',
|
||||||
|
' \\item <My item>',
|
||||||
|
'\\end{itemize}',
|
||||||
|
])
|
||||||
|
cm.applyCommand(BOLD_COMMAND)
|
||||||
|
expect(cm).line(2).to.equal(' \\item \\textbf{<My item>}')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('still formats after command', function () {
|
||||||
|
const cm = new CodemirrorTestSession(['\\noindent <My paragraph>'])
|
||||||
|
cm.applyCommand(BOLD_COMMAND)
|
||||||
|
expect(cm).line(1).to.equal('\\noindent \\textbf{<My paragraph>}')
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user