Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix repeat support of word text object #146

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions src/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1533,17 +1533,15 @@ export function initVim(CodeMirror) {
}
break;
case 'wordUnderCursor':
var word = expandWordUnderCursor(cm, false /** inclusive */,
false /** innerWord */, false /** bigWord */,
true /** noSymbol */);
var word = expandWordUnderCursor(cm, {noSymbol: true});
var isKeyword = true;
if (!word) {
word = expandWordUnderCursor(cm, false /** inclusive */,
false /** innerWord */, false /** bigWord */,
false /** noSymbol */);
word = expandWordUnderCursor(cm, {noSymbol: false});
isKeyword = false;
}
if (!word) {
showConfirm(cm, 'No word under cursor');
clearInputState(cm);
return;
}
var query = cm.getLine(word.start.line).substring(word.start.ch,
Expand Down Expand Up @@ -2227,12 +2225,21 @@ export function initVim(CodeMirror) {
} else if (selfPaired[character]) {
move = true;
tmp = findBeginningAndEnd(cm, head, character, inclusive);
} else if (character === 'W') {
tmp = expandWordUnderCursor(cm, inclusive, !inclusive /** innerWord */,
true /** bigWord */);
} else if (character === 'w') {
tmp = expandWordUnderCursor(cm, inclusive, !inclusive /** innerWord */,
false /** bigWord */);
} else if (character === 'W' || character === 'w') {
var repeat = motionArgs.repeat || 1;
while (repeat-- > 0) {
var repeated = expandWordUnderCursor(cm, {
inclusive,
innerWord: !inclusive,
bigWord: character === 'W',
noSymbol: character === 'W',
multiline: true
}, tmp && tmp.end);
if (repeated) {
if (!tmp) tmp = repeated;
tmp.end = repeated.end;
}
}
} else if (character === 'p') {
tmp = findParagraph(cm, head, motionArgs.repeat, 0, inclusive);
motionArgs.linewise = true;
Expand Down Expand Up @@ -3515,11 +3522,15 @@ export function initVim(CodeMirror) {
return firstNonWS == -1 ? text.length : firstNonWS;
}

function expandWordUnderCursor(cm, inclusive, innerWord, bigWord, noSymbol) {
var cur = getHead(cm);
function expandWordUnderCursor(cm, {inclusive, innerWord, bigWord, noSymbol, multiline}, cursor) {
var cur = cursor || getHead(cm);
var line = cm.getLine(cur.line);
var endLine = line;
var startLineNumber = cur.line
var endLineNumber = startLineNumber;
var idx = cur.ch;

var wordOnNextLine;
// Seek to first word or non-whitespace character, depending on if
// noSymbol is true.
var test = noSymbol ? wordCharTest[0] : bigWordCharTest [0];
Expand All @@ -3528,7 +3539,12 @@ export function initVim(CodeMirror) {
} else {
while (!test(line.charAt(idx))) {
idx++;
if (idx >= line.length) { return null; }
if (idx >= line.length) {
if (!multiline) return null;
idx--;
wordOnNextLine = findWord(cm, cur, true, bigWord, true);
break
}
}

if (bigWord) {
Expand All @@ -3542,22 +3558,33 @@ export function initVim(CodeMirror) {
}

var end = idx, start = idx;
while (test(line.charAt(end)) && end < line.length) { end++; }
while (test(line.charAt(start)) && start >= 0) { start--; }
start++;
if (wordOnNextLine) {
end = wordOnNextLine.to;
endLineNumber = wordOnNextLine.line;
endLine = cm.getLine(endLineNumber);
if (!endLine && end == 0) end++;
} else {
while (test(line.charAt(end)) && end < line.length) { end++; }
}

if (inclusive) {
// If present, include all whitespace after word.
// Otherwise, include all whitespace before word, except indentation.
var wordEnd = end;
while (/\s/.test(line.charAt(end)) && end < line.length) { end++; }
if (wordEnd == end) {
var startsWithSpace = cur.ch <= start && /\s/.test(line.charAt(cur.ch));
if (!startsWithSpace) {
while (/\s/.test(endLine.charAt(end)) && end < endLine.length) { end++; }
}
if (wordEnd == end || startsWithSpace) {
var wordStart = start;
while (/\s/.test(line.charAt(start - 1)) && start > 0) { start--; }
if (!start) { start = wordStart; }
if (!start && !startsWithSpace) { start = wordStart; }
}
}
return { start: new Pos(cur.line, start), end: new Pos(cur.line, end) };

return { start: new Pos(startLineNumber, start), end: new Pos(endLineNumber, end) };
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,23 @@ testVim('sentence_selections', function(cm, vim, helpers) {

}, { value: 'Test sentence. Test question?\nAgain.Never. Again.Test.\n\nHello. This is more text. No end of sentence symbol\n' });

testVim('w_text_object_repeat', function(cm, vim, helpers) {
cm.setCursor(0, 2);
helpers.doKeys('v', '3', 'a', 'w');
eq('w1 ++ w_2 ', cm.getSelection());
helpers.doKeys('<Esc>', 'v', 'a', 'w');
eq(' \n w3', cm.getSelection());

helpers.doKeys('2', 'a', 'w');
eq(' \n w3 xx \n', cm.getSelection());
helpers.doKeys('a', 'w');
eq(' \n w3 xx \n\nw4', cm.getSelection());

cm.setValue(" w0 word1 word2 word3 word4")
cm.setCursor(0, 8);
helpers.doKeys('c', '3', 'a', 'w');
eq(' w0 word4', cm.getValue());
}, { value: ' w1 ++ w_2 \n w3 xx \n\nw4\nword5\nword6' });

// Operator tests
testVim('dl', function(cm, vim, helpers) {
Expand Down