diff --git a/services/web/frontend/js/features/source-editor/commands/ranges.ts b/services/web/frontend/js/features/source-editor/commands/ranges.ts index 808602ddb9..2267db7e5b 100644 --- a/services/web/frontend/js/features/source-editor/commands/ranges.ts +++ b/services/web/frontend/js/features/source-editor/commands/ranges.ts @@ -467,7 +467,15 @@ export function toggleRanges( range.to === view.state.doc.length ? -1 : 1 ) - if (ancestorAtStartOfRange !== ancestorAtEndOfRange) { + const tree = ensureSyntaxTree(view.state, 1000) + const nodeAtFrom = tree?.resolveInner(range.from, 1) + const nodeAtTo = tree?.resolveInner(range.to, -1) + const isSingleNodeSelected = nodeAtFrom === nodeAtTo + + if ( + !isSingleNodeSelected && + ancestorAtStartOfRange !== ancestorAtEndOfRange + ) { // But handle the exception of case 8 const ancestorAtStartIsWrappingCommand = ancestorAtStartOfRange && diff --git a/services/web/test/frontend/features/source-editor/commands/ranges.test.ts b/services/web/test/frontend/features/source-editor/commands/ranges.test.ts index b7fd567f3b..9d5123dd75 100644 --- a/services/web/test/frontend/features/source-editor/commands/ranges.test.ts +++ b/services/web/test/frontend/features/source-editor/commands/ranges.test.ts @@ -196,4 +196,28 @@ describe('toggleRanges', function () { }) }) }) + + it('still formats text next to a command', function () { + const cm = new CodemirrorTestSession(['\\foo']) + cm.applyCommand(BOLD_COMMAND) + expect(cm).line(1).to.equal('\\textbf{item}\\foo') + }) + + it('still formats part of a text next to command', function () { + const cm = new CodemirrorTestSession(['hello \\foo']) + cm.applyCommand(BOLD_COMMAND) + expect(cm).line(1).to.equal('hello \\textbf{world}\\foo') + }) + + it('still formats command without arguments', function () { + const cm = new CodemirrorTestSession(['\\item<\\foo>']) + cm.applyCommand(BOLD_COMMAND) + expect(cm).line(1).to.equal('\\item\\textbf{<\\foo>}') + }) + + it('skips formatting if in the middle of two commands', function () { + const cm = new CodemirrorTestSession(['\\far']) + cm.applyCommand(BOLD_COMMAND) + expect(cm).line(1).to.equal('\\foo\\bar') + }) })