MCPcopy Create free account
hub / github.com/ronreiter/interactive-tutorials / findWord

Function findWord

static/js/codemirror/keymap/vim.js:1600–1648  ·  view source on GitHub ↗
(cm, cur, forward, bigWord)

Source from the content-addressed store, hash-verified

1598 */
1599 // TODO: Treat empty lines (with no whitespace) as words.
1600 function findWord(cm, cur, forward, bigWord) {
1601 var lineNum = cur.line;
1602 var pos = cur.ch;
1603 var line = cm.getLine(lineNum);
1604 var dir = forward ? 1 : -1;
1605 var regexps = bigWord ? bigWordRegexp : wordRegexp;
1606
1607 while (true) {
1608 var stop = (dir > 0) ? line.length : -1;
1609 var wordStart = stop, wordEnd = stop;
1610 // Find bounds of next word.
1611 while (pos != stop) {
1612 var foundWord = false;
1613 for (var i = 0; i < regexps.length && !foundWord; ++i) {
1614 if (regexps[i].test(line.charAt(pos))) {
1615 wordStart = pos;
1616 // Advance to end of word.
1617 while (pos != stop && regexps[i].test(line.charAt(pos))) {
1618 pos += dir;
1619 }
1620 wordEnd = pos;
1621 foundWord = wordStart != wordEnd;
1622 if (wordStart == cur.ch && lineNum == cur.line &&
1623 wordEnd == wordStart + dir) {
1624 // We started at the end of a word. Find the next one.
1625 continue;
1626 } else {
1627 return {
1628 from: Math.min(wordStart, wordEnd + 1),
1629 to: Math.max(wordStart, wordEnd),
1630 line: lineNum };
1631 }
1632 }
1633 }
1634 if (!foundWord) {
1635 pos += dir;
1636 }
1637 }
1638 // Advance to next/prev line.
1639 lineNum += dir;
1640 if (!isLine(cm, lineNum)) {
1641 return null;
1642 }
1643 line = cm.getLine(lineNum);
1644 pos = (dir > 0) ? 0 : line.length;
1645 }
1646 // Should never get here.
1647 throw 'The impossible happened.';
1648 }
1649
1650 /**
1651 * @param {CodeMirror} cm CodeMirror object.

Callers 1

moveToWordFunction · 0.85

Calls 2

isLineFunction · 0.70
testMethod · 0.45

Tested by

no test coverage detected