ranges.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. import { EditorView } from '@codemirror/view'
  2. import { EditorSelection, EditorState, SelectionRange } from '@codemirror/state'
  3. import {
  4. ensureSyntaxTree,
  5. foldedRanges,
  6. foldEffect,
  7. syntaxTree,
  8. } from '@codemirror/language'
  9. import { SyntaxNode } from '@lezer/common'
  10. import {
  11. ancestorOfNodeWithType,
  12. isUnknownCommandWithName,
  13. } from '../utils/tree-query'
  14. import { lastAncestorAtEndPosition } from '../utils/tree-operations/ancestors'
  15. export const wrapRanges =
  16. (
  17. prefix: string,
  18. suffix: string,
  19. wrapWholeLine = false,
  20. selection?: (range: SelectionRange) => SelectionRange
  21. ) =>
  22. (view: EditorView): boolean => {
  23. if (view.state.readOnly) {
  24. return false
  25. }
  26. view.dispatch(
  27. view.state.changeByRange(range => {
  28. const insert = { prefix, suffix }
  29. if (wrapWholeLine) {
  30. const line = view.state.doc.lineAt(range.anchor)
  31. if (range.empty) {
  32. // expand range to cover the whole line
  33. range = EditorSelection.range(line.from, line.to)
  34. }
  35. // add a newline at the start if needed
  36. if (range.from !== line.from) {
  37. insert.prefix = `\n${prefix}`
  38. }
  39. // add a newline at the end if needed
  40. if (range.to !== line.to) {
  41. insert.suffix = `${suffix}\n`
  42. }
  43. }
  44. const content = view.state.sliceDoc(range.from, range.to)
  45. // map through the prefix only
  46. const changedRange = range.map(
  47. view.state.changes([
  48. { from: range.from, insert: `${insert.prefix}` },
  49. ]),
  50. 1
  51. )
  52. return {
  53. range: selection ? selection(changedRange) : changedRange,
  54. // create a single change, including the content
  55. changes: [
  56. {
  57. from: range.from,
  58. to: range.to,
  59. insert: `${insert.prefix}${content}${insert.suffix}`,
  60. },
  61. ],
  62. }
  63. }),
  64. { scrollIntoView: true }
  65. )
  66. return true
  67. }
  68. export const changeCase =
  69. (upper = true) =>
  70. (view: EditorView) => {
  71. if (view.state.readOnly) {
  72. return false
  73. }
  74. view.dispatch(
  75. view.state.changeByRange(range => {
  76. // ignore empty ranges
  77. if (range.empty) {
  78. return { range }
  79. }
  80. const text = view.state.doc.sliceString(range.from, range.to)
  81. return {
  82. range,
  83. changes: [
  84. {
  85. from: range.from,
  86. to: range.to,
  87. insert: upper ? text.toUpperCase() : text.toLowerCase(),
  88. },
  89. ],
  90. }
  91. })
  92. )
  93. return true
  94. }
  95. export const duplicateSelection = (view: EditorView) => {
  96. if (view.state.readOnly) {
  97. return false
  98. }
  99. const foldedRangesInDocument = foldedRanges(view.state)
  100. view.dispatch(
  101. view.state.changeByRange(range => {
  102. const folds: { offset: number; base: number; length: number }[] = []
  103. if (range.empty) {
  104. const line = view.state.doc.lineAt(range.from)
  105. let lineStart = line.from
  106. let lineEnd = line.to
  107. // Calculate line start/end including folded ranges
  108. //
  109. // Note that at each iteration of the while loop, new folded ranges
  110. // can be included. This happens when there are multiple folded ranges
  111. // on a single editor line (but spanning multiple actual lines)
  112. //
  113. // For example, the following document:
  114. // 1: \begin{document}
  115. // 2: test
  116. // 3: \end{document}\begin{document}
  117. // 4: test
  118. // 5: \end{document}
  119. //
  120. // Can be folded to:
  121. // \begin{document}<...>\end{document}\begin{document}<...>\end{document}
  122. //
  123. // In this case, the first iterations of the while loop below will only
  124. // include lines 3-5, since the overlapping folded range for line 5
  125. // is only the fold on lines 3-5. Hence in the while loop, we expand the
  126. // selection until we include all the ranges.
  127. let changed
  128. do {
  129. changed = false
  130. foldedRangesInDocument.between(lineStart, lineEnd, (from, to) => {
  131. const newLineStart = Math.min(
  132. view.state.doc.lineAt(from).from,
  133. lineStart
  134. )
  135. const newLineEnd = Math.max(view.state.doc.lineAt(to).to, lineEnd)
  136. if (newLineStart !== lineStart || newLineEnd !== lineEnd) {
  137. changed = true
  138. lineStart = newLineStart
  139. lineEnd = newLineEnd
  140. }
  141. })
  142. } while (changed)
  143. // Collect information needed to fold duplicated lines
  144. foldedRangesInDocument.between(lineStart, lineEnd, (from, to) => {
  145. folds.push({
  146. offset: from - lineStart,
  147. base: lineEnd + view.state.lineBreak.length,
  148. length: to - from,
  149. })
  150. })
  151. // Duplicate the selected lines downwards
  152. return {
  153. range,
  154. changes: [
  155. {
  156. from: lineEnd,
  157. insert:
  158. view.state.lineBreak + view.state.doc.slice(lineStart, lineEnd),
  159. },
  160. ],
  161. // Add a fold effect for each fold in the original line
  162. effects: folds.map(fold =>
  163. foldEffect.of({
  164. from: fold.base + fold.offset,
  165. to: fold.base + fold.offset + fold.length,
  166. })
  167. ),
  168. }
  169. } else {
  170. // Duplicate selected text at head of selection
  171. let newSelectionRange = range
  172. if (range.head > range.anchor) {
  173. // Duplicating to the right, so we need to update the selected range
  174. newSelectionRange = EditorSelection.range(
  175. range.head,
  176. range.head + (range.to - range.from)
  177. )
  178. }
  179. return {
  180. // The new range is the duplicated section, placed at the head of the
  181. // original selection
  182. range: newSelectionRange,
  183. changes: [
  184. {
  185. from: range.head,
  186. insert: view.state.doc.slice(range.from, range.to),
  187. },
  188. ],
  189. }
  190. }
  191. })
  192. )
  193. return true
  194. }
  195. function getParentNode(
  196. position: number | SyntaxNode,
  197. state: EditorState,
  198. assoc: 0 | 1 | -1 = 1
  199. ): SyntaxNode | undefined {
  200. const tree = ensureSyntaxTree(state, 1000)
  201. let node: SyntaxNode | undefined | null = null
  202. if (typeof position === 'number') {
  203. node = tree?.resolveInner(position, assoc)?.parent
  204. const ancestorAtEndPos = lastAncestorAtEndPosition(node, position)
  205. if (ancestorAtEndPos?.parent) {
  206. node = ancestorAtEndPos.parent
  207. }
  208. } else {
  209. node = position?.parent
  210. }
  211. while (
  212. ['LongArg', 'TextArgument', 'OpenBrace', 'CloseBrace'].includes(
  213. node?.type.name || ''
  214. )
  215. ) {
  216. node = node!.parent
  217. }
  218. return node || undefined
  219. }
  220. function wrapRangeInCommand(
  221. state: EditorState,
  222. range: SelectionRange,
  223. command: string
  224. ) {
  225. const content = state.sliceDoc(range.from, range.to)
  226. const changes = state.changes([
  227. {
  228. from: range.from,
  229. to: range.to,
  230. insert: `${command}{${content}}`,
  231. },
  232. ])
  233. return {
  234. changes,
  235. range: moveRange(
  236. range,
  237. range.from + command.length + 1,
  238. range.from + command.length + content.length + 1
  239. ),
  240. }
  241. }
  242. function moveRange(range: SelectionRange, newFrom: number, newTo: number) {
  243. const forwards = range.from === range.anchor
  244. return forwards
  245. ? EditorSelection.range(newFrom, newTo)
  246. : EditorSelection.range(newTo, newFrom)
  247. }
  248. function validateReplacement(expected: string, actual: string) {
  249. if (expected !== actual) {
  250. throw new Error('Replacement in toggleRange failed validation.')
  251. }
  252. }
  253. function getWrappingAncestor(
  254. node: SyntaxNode,
  255. command: string,
  256. state: EditorState
  257. ): SyntaxNode | null {
  258. for (
  259. let ancestor: SyntaxNode | null = node;
  260. ancestor;
  261. ancestor = ancestor.parent
  262. ) {
  263. if (isUnknownCommandWithName(ancestor, command, state)) {
  264. return ancestor
  265. }
  266. if (ancestor.type.is('UnknownCommand')) {
  267. // We could multiple levels deep in bold/non-bold. So bail out in this case
  268. return null
  269. }
  270. }
  271. return null
  272. }
  273. function adjustRangeIfNeeded(
  274. command: string,
  275. range: SelectionRange,
  276. state: EditorState
  277. ) {
  278. // Try to adjust the selection, if it is either
  279. // 1. \textbf<{test>}
  280. // 2. \textbf{<test}>
  281. // 3. \textbf<{test}>
  282. // 4. <\textbf{test}>
  283. // 4. \textbf<>{test}
  284. const tree = syntaxTree(state)
  285. if (tree.length < range.to) {
  286. return range
  287. }
  288. const nodeLeft = tree.resolveInner(range.from, 1)
  289. const nodeRight = tree.resolveInner(range.to, -1)
  290. const parentLeft = getWrappingAncestor(nodeLeft, command, state)
  291. const parentRight = getWrappingAncestor(nodeRight, command, state)
  292. const parent = getParentNode(nodeLeft, state)
  293. if (parent?.type.is('UnknownCommand') && spansWholeArgument(parent, range)) {
  294. return bubbleUpRange(
  295. command,
  296. ancestorOfNodeWithType(nodeLeft, 'UnknownCommand'),
  297. range,
  298. state
  299. )
  300. }
  301. if (!parentLeft) {
  302. // We're not trying to unbold, so don't bother adjusting range
  303. return bubbleUpRange(
  304. command,
  305. ancestorOfNodeWithType(nodeLeft, 'UnknownCommand'),
  306. range,
  307. state
  308. )
  309. }
  310. if (nodeLeft.type.is('CtrlSeq') && range.from === range.to) {
  311. const command = nodeLeft.parent?.parent
  312. if (!command) {
  313. return range
  314. }
  315. return EditorSelection.cursor(command.from)
  316. }
  317. let { from, to } = range
  318. if (nodeLeft.type.is('CtrlSeq')) {
  319. from = nodeLeft.to + 1
  320. }
  321. if (nodeLeft.type.is('OpenBrace')) {
  322. from = nodeLeft.to
  323. }
  324. // We know that parentLeft is the UnknownCommand, so now we check if we're
  325. // to the right of the closing brace. (parent is TextArgument, grandparent is
  326. // UnknownCommand)
  327. if (parentLeft === parentRight && nodeRight.type.is('CloseBrace')) {
  328. to = nodeRight.from
  329. }
  330. return bubbleUpRange(command, parentLeft, moveRange(range, from, to), state)
  331. }
  332. function spansWholeArgument(
  333. commandNode: SyntaxNode | null,
  334. range: SelectionRange
  335. ): boolean {
  336. const argument = commandNode?.getChild('TextArgument')?.getChild('LongArg')
  337. const res = Boolean(
  338. argument && argument.from === range.from && argument.to === range.to
  339. )
  340. return res
  341. }
  342. function bubbleUpRange(
  343. command: string,
  344. node: SyntaxNode | null,
  345. range: SelectionRange,
  346. state: EditorState
  347. ) {
  348. let currentRange = range
  349. for (
  350. let ancestorCommand: SyntaxNode | null = ancestorOfNodeWithType(
  351. node,
  352. 'UnknownCommand'
  353. );
  354. spansWholeArgument(ancestorCommand, currentRange);
  355. ancestorCommand = ancestorOfNodeWithType(
  356. ancestorCommand.parent,
  357. 'UnknownCommand'
  358. )
  359. ) {
  360. if (!ancestorCommand) {
  361. break
  362. }
  363. currentRange = moveRange(
  364. currentRange,
  365. ancestorCommand.from,
  366. ancestorCommand.to
  367. )
  368. if (isUnknownCommandWithName(ancestorCommand, command, state)) {
  369. const argumentNode = ancestorCommand
  370. .getChild('TextArgument')
  371. ?.getChild('LongArg')
  372. if (!argumentNode) {
  373. return range
  374. }
  375. return moveRange(range, argumentNode.from, argumentNode.to)
  376. }
  377. }
  378. return range
  379. }
  380. export function toggleRanges(command: string) {
  381. /* There are a number of situations we need to handle in this function.
  382. * In the following examples, the selection range is marked within <>
  383. * 1. If the parent node at start and end of selection is different -> do
  384. * nothing & show error. Case 8 is an exception to this.
  385. * -> For good and bad, this disallows \textbf{this <is} weird
  386. * \textit{to> do}
  387. * 2. If selection contains a BlankLine (i.e. two newlines in a row) -> do
  388. * nothing & show error
  389. * -> \textbf doesn't allow paragraph breaks).
  390. * 3. If the selection is not within a \textbf -> wrap it in a \textbf
  391. * 4. If selection is at the beginning of a \textbf -> shrink the \textbf
  392. * command
  393. * -> e.g. \textbf{<this is> a test} → <this is>\textbf{ a test}
  394. * 5. Similarly for selection at end of \textbf command
  395. * 6. If selection is in the middle of a \textbf command -> split the command
  396. * into two
  397. * -> e.g. \textbf{this <is a> test} → \textbf{this }<is a>\textbf{ test}
  398. * 7. If the selection is a whole \textbf command → remove the wrapping
  399. * command.
  400. * 8. If the selection spans two \textbf's with the same parent then join the
  401. * two
  402. * -> e.g. \textbf{this <is} a \textbf{te>st} → \textbf{this <is a te>st}
  403. * 9. If the selection spans the end of a \textbf, into the parent of the
  404. * command, then extend the \textbf.
  405. * -> e.g. \textbf{this <is} a test> → \textbf{this <is a test>}
  406. * 10. Similarly for selections spanning the beginning of the selection
  407. */
  408. return (view: EditorView): boolean => {
  409. if (view.state.readOnly) {
  410. return false
  411. }
  412. view.dispatch(
  413. view.state.changeByRange(initialRange => {
  414. const range = adjustRangeIfNeeded(command, initialRange, view.state)
  415. const content = view.state.sliceDoc(range.from, range.to)
  416. const ancestorAtStartOfRange = getParentNode(
  417. range.from,
  418. view.state,
  419. range.from === 0 ? 1 : -1
  420. )
  421. const ancestorAtEndOfRange = range.empty
  422. ? ancestorAtStartOfRange
  423. : getParentNode(
  424. range.to,
  425. view.state,
  426. range.to === view.state.doc.length ? -1 : 1
  427. )
  428. if (ancestorAtStartOfRange !== ancestorAtEndOfRange) {
  429. // But handle the exception of case 8
  430. const ancestorAtStartIsWrappingCommand =
  431. ancestorAtStartOfRange &&
  432. isUnknownCommandWithName(
  433. ancestorAtStartOfRange,
  434. command,
  435. view.state
  436. )
  437. const ancestorAtEndIsWrappingCommand =
  438. ancestorAtEndOfRange &&
  439. isUnknownCommandWithName(ancestorAtEndOfRange, command, view.state)
  440. if (
  441. ancestorAtStartIsWrappingCommand &&
  442. ancestorAtEndIsWrappingCommand &&
  443. ancestorAtStartOfRange?.parent?.parent &&
  444. ancestorAtEndOfRange?.parent?.parent
  445. ) {
  446. // Test for case 8
  447. const nextAncestorAtStartOfRange =
  448. ancestorAtStartOfRange.parent.parent
  449. const nextAncestorAtEndOfRange = ancestorAtEndOfRange.parent.parent
  450. if (nextAncestorAtStartOfRange === nextAncestorAtEndOfRange) {
  451. // Join the two ranges
  452. const textBetweenRanges = view.state.sliceDoc(
  453. ancestorAtStartOfRange.to,
  454. ancestorAtEndOfRange.from
  455. )
  456. const ancestorStartArgumentNode =
  457. ancestorAtStartOfRange.lastChild?.getChild('LongArg')
  458. const ancestorEndArgumentNode =
  459. ancestorAtEndOfRange.lastChild?.getChild('LongArg')
  460. if (!ancestorStartArgumentNode || !ancestorEndArgumentNode) {
  461. throw new Error("Can't find argument node")
  462. }
  463. const actualContent = view.state.sliceDoc(
  464. ancestorAtStartOfRange.from,
  465. ancestorAtEndOfRange.to
  466. )
  467. const firstCommandArgument = view.state.sliceDoc(
  468. ancestorStartArgumentNode.from,
  469. ancestorStartArgumentNode.to
  470. )
  471. const secondCommandArgument = view.state.sliceDoc(
  472. ancestorEndArgumentNode.from,
  473. ancestorEndArgumentNode.to
  474. )
  475. validateReplacement(
  476. `${command}{${firstCommandArgument}}${textBetweenRanges}${command}{${secondCommandArgument}}`,
  477. actualContent
  478. )
  479. const changes = view.state.changes([
  480. {
  481. from: ancestorAtStartOfRange.from,
  482. to: ancestorAtEndOfRange.to,
  483. insert: `${command}{${firstCommandArgument}${textBetweenRanges}${secondCommandArgument}}`,
  484. },
  485. ])
  486. return {
  487. changes,
  488. range: moveRange(
  489. range,
  490. range.from,
  491. range.to - command.length - 1 - 1
  492. ),
  493. }
  494. }
  495. }
  496. if (
  497. ancestorAtEndIsWrappingCommand &&
  498. ancestorAtEndOfRange.parent?.parent === ancestorAtStartOfRange
  499. ) {
  500. // Extend to the left. Case 10
  501. const contentUpToCommand = view.state.sliceDoc(
  502. range.from,
  503. ancestorAtEndOfRange.from
  504. )
  505. const ancestorEndArgumentNode =
  506. ancestorAtEndOfRange.lastChild?.getChild('LongArg')
  507. if (!ancestorEndArgumentNode) {
  508. throw new Error("Can't find argument node")
  509. }
  510. const commandContent = view.state.sliceDoc(
  511. ancestorEndArgumentNode.from,
  512. ancestorEndArgumentNode.to
  513. )
  514. const actualContent = view.state.sliceDoc(
  515. range.from,
  516. ancestorAtEndOfRange.to
  517. )
  518. validateReplacement(
  519. `${contentUpToCommand}${command}{${commandContent}}`,
  520. actualContent
  521. )
  522. const changes = view.state.changes([
  523. {
  524. from: range.from,
  525. to: ancestorAtEndOfRange.to,
  526. insert: `${command}{${contentUpToCommand}${commandContent}}`,
  527. },
  528. ])
  529. return {
  530. changes,
  531. range: moveRange(
  532. range,
  533. range.from + command.length + 1,
  534. range.to
  535. ),
  536. }
  537. }
  538. if (
  539. ancestorAtStartIsWrappingCommand &&
  540. ancestorAtStartOfRange.parent?.parent === ancestorAtEndOfRange
  541. ) {
  542. // Extend to the right. Case 9
  543. const contentAfterCommand = view.state.sliceDoc(
  544. ancestorAtStartOfRange.to,
  545. range.to
  546. )
  547. const ancestorStartArgumentNode =
  548. ancestorAtStartOfRange.lastChild?.getChild('LongArg')
  549. if (!ancestorStartArgumentNode) {
  550. throw new Error("Can't find argument node")
  551. }
  552. const commandContent = view.state.sliceDoc(
  553. ancestorStartArgumentNode.from,
  554. ancestorStartArgumentNode.to
  555. )
  556. const actualContent = view.state.sliceDoc(
  557. ancestorAtStartOfRange.from,
  558. range.to
  559. )
  560. validateReplacement(
  561. `${command}{${commandContent}}${contentAfterCommand}`,
  562. actualContent
  563. )
  564. const changes = view.state.changes([
  565. {
  566. from: ancestorAtStartOfRange.from,
  567. to: range.to,
  568. insert: `${command}{${commandContent}${contentAfterCommand}}`,
  569. },
  570. ])
  571. return {
  572. changes,
  573. range: moveRange(range, range.from, range.to - 1),
  574. }
  575. }
  576. // Bail out in case 1
  577. // TODO: signal error to the user
  578. return { range }
  579. }
  580. const ancestor = ancestorAtStartOfRange
  581. // Bail out in case 2
  582. if (content.includes('\n\n')) {
  583. // TODO: signal error to the user
  584. return { range }
  585. }
  586. const isCursorBeforeAncestor =
  587. range.empty &&
  588. ancestor &&
  589. range.from === ancestor.from &&
  590. isUnknownCommandWithName(ancestor, command, view.state)
  591. // If we can't find an ancestor node, or if the parent is not an exsting
  592. // \textbf, then we just wrap it in a range. Case 3.
  593. if (
  594. isCursorBeforeAncestor ||
  595. !ancestor ||
  596. !isUnknownCommandWithName(ancestor, command, view.state)
  597. ) {
  598. return wrapRangeInCommand(view.state, range, command)
  599. }
  600. const argumentNode = ancestor.lastChild?.getChild('LongArg')
  601. if (!argumentNode) {
  602. throw new Error("Can't find argument node")
  603. }
  604. // We should trim from the beginning. Case 4
  605. if (range.from === argumentNode.from && range.to !== argumentNode.to) {
  606. const textAfterSelection = view.state.sliceDoc(
  607. range.to,
  608. argumentNode.to
  609. )
  610. const wholeCommand = view.state.sliceDoc(ancestor.from, ancestor.to)
  611. validateReplacement(
  612. `${command}{${content}${textAfterSelection}}`,
  613. wholeCommand
  614. )
  615. const changes = view.state.changes([
  616. {
  617. from: ancestor.from,
  618. to: ancestor.to,
  619. insert: `${content}${command}{${textAfterSelection}}`,
  620. },
  621. ])
  622. return {
  623. range: moveRange(
  624. range,
  625. range.from - command.length - 1,
  626. range.to - command.length - 1
  627. ),
  628. changes,
  629. }
  630. }
  631. // We should trim from the end. Case 5
  632. if (range.to === argumentNode.to && range.from !== argumentNode.from) {
  633. const textBeforeSelection = view.state.sliceDoc(
  634. ancestor.from,
  635. range.from
  636. )
  637. const wholeCommand = view.state.sliceDoc(ancestor.from, ancestor.to)
  638. validateReplacement(`${textBeforeSelection}${content}}`, wholeCommand)
  639. const changes = view.state.changes([
  640. {
  641. from: ancestor.from,
  642. to: ancestor.to,
  643. insert: `${textBeforeSelection}}${content}`,
  644. },
  645. ])
  646. // We should shift selection forward by the } we insert
  647. return {
  648. range: moveRange(range, range.from + 1, range.to + 1),
  649. changes,
  650. }
  651. }
  652. // We should split the command in two. Case 6
  653. if (range.from !== argumentNode.from && range.to !== argumentNode.to) {
  654. const textBeforeSelection = view.state.sliceDoc(
  655. ancestor.from,
  656. range.from
  657. )
  658. const textAfterSelection = view.state.sliceDoc(range.to, ancestor.to)
  659. const wholeCommand = view.state.sliceDoc(ancestor.from, ancestor.to)
  660. validateReplacement(
  661. `${textBeforeSelection}${content}${textAfterSelection}`,
  662. wholeCommand
  663. )
  664. const changes = view.state.changes([
  665. {
  666. from: ancestor.from,
  667. to: ancestor.to,
  668. insert: `${textBeforeSelection}}${content}${command}{${textAfterSelection}`,
  669. },
  670. ])
  671. return {
  672. range: moveRange(range, range.from + 1, range.to + 1),
  673. changes,
  674. }
  675. }
  676. // Remove the wrapping command. Case 7
  677. if (spansWholeArgument(ancestor, range)) {
  678. const argumentContent = view.state.sliceDoc(
  679. argumentNode.from,
  680. argumentNode.to
  681. )
  682. const wholeCommand = view.state.sliceDoc(ancestor.from, ancestor.to)
  683. validateReplacement(`${command}{${content}}`, wholeCommand)
  684. const changes = view.state.changes([
  685. { from: ancestor.from, to: ancestor.to, insert: argumentContent },
  686. ])
  687. return {
  688. range: moveRange(
  689. range,
  690. range.from - command.length - 1,
  691. range.to - command.length - 1
  692. ),
  693. changes,
  694. }
  695. }
  696. // Shouldn't happen, but default to just wrapping the content
  697. return wrapRangeInCommand(view.state, range, command)
  698. }),
  699. { scrollIntoView: true }
  700. )
  701. return true
  702. }
  703. }