(doc, pos, dir, unit, visually)
| 4634 | // position. The resulting position will have a hitSide=true |
| 4635 | // property if it reached the end of the document. |
| 4636 | function findPosH(doc, pos, dir, unit, visually) { |
| 4637 | var line = pos.line, ch = pos.ch, origDir = dir; |
| 4638 | var lineObj = getLine(doc, line); |
| 4639 | var possible = true; |
| 4640 | function findNextLine() { |
| 4641 | var l = line + dir; |
| 4642 | if (l < doc.first || l >= doc.first + doc.size) return (possible = false); |
| 4643 | line = l; |
| 4644 | return lineObj = getLine(doc, l); |
| 4645 | } |
| 4646 | function moveOnce(boundToLine) { |
| 4647 | var next = (visually ? moveVisually : moveLogically)(lineObj, ch, dir, true); |
| 4648 | if (next == null) { |
| 4649 | if (!boundToLine && findNextLine()) { |
| 4650 | if (visually) ch = (dir < 0 ? lineRight : lineLeft)(lineObj); |
| 4651 | else ch = dir < 0 ? lineObj.text.length : 0; |
| 4652 | } else return (possible = false); |
| 4653 | } else ch = next; |
| 4654 | return true; |
| 4655 | } |
| 4656 | |
| 4657 | if (unit == "char") moveOnce(); |
| 4658 | else if (unit == "column") moveOnce(true); |
| 4659 | else if (unit == "word" || unit == "group") { |
| 4660 | var sawType = null, group = unit == "group"; |
| 4661 | var helper = doc.cm && doc.cm.getHelper(pos, "wordChars"); |
| 4662 | for (var first = true;; first = false) { |
| 4663 | if (dir < 0 && !moveOnce(!first)) break; |
| 4664 | var cur = lineObj.text.charAt(ch) || "\n"; |
| 4665 | var type = isWordChar(cur, helper) ? "w" |
| 4666 | : group && cur == "\n" ? "n" |
| 4667 | : !group || /\s/.test(cur) ? null |
| 4668 | : "p"; |
| 4669 | if (group && !first && !type) type = "s"; |
| 4670 | if (sawType && sawType != type) { |
| 4671 | if (dir < 0) {dir = 1; moveOnce();} |
| 4672 | break; |
| 4673 | } |
| 4674 | |
| 4675 | if (type) sawType = type; |
| 4676 | if (dir > 0 && !moveOnce(!first)) break; |
| 4677 | } |
| 4678 | } |
| 4679 | var result = skipAtomic(doc, Pos(line, ch), origDir, true); |
| 4680 | if (!possible) result.hitSide = true; |
| 4681 | return result; |
| 4682 | } |
| 4683 | |
| 4684 | // For relative vertical movement. Dir may be -1 or 1. Unit can be |
| 4685 | // "page" or "line". The resulting position will have a hitSide=true |
no test coverage detected