movement.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { Pos } from "../line/pos.js"
  2. import { prepareMeasureForLine, measureCharPrepared, wrappedLineExtentChar } from "../measurement/position_measurement.js"
  3. import { getBidiPartAt, getOrder } from "../util/bidi.js"
  4. import { findFirst, lst, skipExtendingChars } from "../util/misc.js"
  5. function moveCharLogically(line, ch, dir) {
  6. let target = skipExtendingChars(line.text, ch + dir, dir)
  7. return target < 0 || target > line.text.length ? null : target
  8. }
  9. export function moveLogically(line, start, dir) {
  10. let ch = moveCharLogically(line, start.ch, dir)
  11. return ch == null ? null : new Pos(start.line, ch, dir < 0 ? "after" : "before")
  12. }
  13. export function endOfLine(visually, cm, lineObj, lineNo, dir) {
  14. if (visually) {
  15. let order = getOrder(lineObj, cm.doc.direction)
  16. if (order) {
  17. let part = dir < 0 ? lst(order) : order[0]
  18. let moveInStorageOrder = (dir < 0) == (part.level == 1)
  19. let sticky = moveInStorageOrder ? "after" : "before"
  20. let ch
  21. // With a wrapped rtl chunk (possibly spanning multiple bidi parts),
  22. // it could be that the last bidi part is not on the last visual line,
  23. // since visual lines contain content order-consecutive chunks.
  24. // Thus, in rtl, we are looking for the first (content-order) character
  25. // in the rtl chunk that is on the last line (that is, the same line
  26. // as the last (content-order) character).
  27. if (part.level > 0 || cm.doc.direction == "rtl") {
  28. let prep = prepareMeasureForLine(cm, lineObj)
  29. ch = dir < 0 ? lineObj.text.length - 1 : 0
  30. let targetTop = measureCharPrepared(cm, prep, ch).top
  31. ch = findFirst(ch => measureCharPrepared(cm, prep, ch).top == targetTop, (dir < 0) == (part.level == 1) ? part.from : part.to - 1, ch)
  32. if (sticky == "before") ch = moveCharLogically(lineObj, ch, 1)
  33. } else ch = dir < 0 ? part.to : part.from
  34. return new Pos(lineNo, ch, sticky)
  35. }
  36. }
  37. return new Pos(lineNo, dir < 0 ? lineObj.text.length : 0, dir < 0 ? "before" : "after")
  38. }
  39. export function moveVisually(cm, line, start, dir) {
  40. let bidi = getOrder(line, cm.doc.direction)
  41. if (!bidi) return moveLogically(line, start, dir)
  42. if (start.ch >= line.text.length) {
  43. start.ch = line.text.length
  44. start.sticky = "before"
  45. } else if (start.ch <= 0) {
  46. start.ch = 0
  47. start.sticky = "after"
  48. }
  49. let partPos = getBidiPartAt(bidi, start.ch, start.sticky), part = bidi[partPos]
  50. if (cm.doc.direction == "ltr" && part.level % 2 == 0 && (dir > 0 ? part.to > start.ch : part.from < start.ch)) {
  51. // Case 1: We move within an ltr part in an ltr editor. Even with wrapped lines,
  52. // nothing interesting happens.
  53. return moveLogically(line, start, dir)
  54. }
  55. let mv = (pos, dir) => moveCharLogically(line, pos instanceof Pos ? pos.ch : pos, dir)
  56. let prep
  57. let getWrappedLineExtent = ch => {
  58. if (!cm.options.lineWrapping) return {begin: 0, end: line.text.length}
  59. prep = prep || prepareMeasureForLine(cm, line)
  60. return wrappedLineExtentChar(cm, line, prep, ch)
  61. }
  62. let wrappedLineExtent = getWrappedLineExtent(start.sticky == "before" ? mv(start, -1) : start.ch)
  63. if (cm.doc.direction == "rtl" || part.level == 1) {
  64. let moveInStorageOrder = (part.level == 1) == (dir < 0)
  65. let ch = mv(start, moveInStorageOrder ? 1 : -1)
  66. if (ch != null && (!moveInStorageOrder ? ch >= part.from && ch >= wrappedLineExtent.begin : ch <= part.to && ch <= wrappedLineExtent.end)) {
  67. // Case 2: We move within an rtl part or in an rtl editor on the same visual line
  68. let sticky = moveInStorageOrder ? "before" : "after"
  69. return new Pos(start.line, ch, sticky)
  70. }
  71. }
  72. // Case 3: Could not move within this bidi part in this visual line, so leave
  73. // the current bidi part
  74. let searchInVisualLine = (partPos, dir, wrappedLineExtent) => {
  75. let getRes = (ch, moveInStorageOrder) => moveInStorageOrder
  76. ? new Pos(start.line, mv(ch, 1), "before")
  77. : new Pos(start.line, ch, "after")
  78. for (; partPos >= 0 && partPos < bidi.length; partPos += dir) {
  79. let part = bidi[partPos]
  80. let moveInStorageOrder = (dir > 0) == (part.level != 1)
  81. let ch = moveInStorageOrder ? wrappedLineExtent.begin : mv(wrappedLineExtent.end, -1)
  82. if (part.from <= ch && ch < part.to) return getRes(ch, moveInStorageOrder)
  83. ch = moveInStorageOrder ? part.from : mv(part.to, -1)
  84. if (wrappedLineExtent.begin <= ch && ch < wrappedLineExtent.end) return getRes(ch, moveInStorageOrder)
  85. }
  86. }
  87. // Case 3a: Look for other bidi parts on the same visual line
  88. let res = searchInVisualLine(partPos + dir, dir, wrappedLineExtent)
  89. if (res) return res
  90. // Case 3b: Look for other bidi parts on the next visual line
  91. let nextCh = dir > 0 ? wrappedLineExtent.end : mv(wrappedLineExtent.begin, -1)
  92. if (nextCh != null && !(dir > 0 && nextCh == line.text.length)) {
  93. res = searchInVisualLine(dir > 0 ? 0 : bidi.length - 1, dir, getWrappedLineExtent(nextCh))
  94. if (res) return res
  95. }
  96. // Case 4: Nowhere to move
  97. return null
  98. }