Skip to content

Commit

Permalink
highlight search during replace command (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing authored May 13, 2024
1 parent ffe5f4c commit 893666e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
37 changes: 32 additions & 5 deletions src/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ export function initVim(CodeMirror) {
vimGlobalState.exCommandHistoryController.reset();
exCommandDispatcher.processCommand(cm, input);
if (cm.state.vim) clearInputState(cm);
clearSearchHighlight(cm);
}
/**
* @arg {KeyboardEvent&{target:HTMLInputElement}} e
Expand All @@ -1790,6 +1791,7 @@ export function initVim(CodeMirror) {
vimGlobalState.exCommandHistoryController.reset();
CodeMirror.e_stop(e);
clearInputState(cm);
clearSearchHighlight(cm);
close();
cm.focus();
}
Expand All @@ -1808,17 +1810,42 @@ export function initVim(CodeMirror) {
vimGlobalState.exCommandHistoryController.reset();
}
}
/**
* @arg {KeyboardEvent&{target:HTMLInputElement}} e
* @arg {any} query
*/
function onPromptKeyUp(e, query) {
var inputStream = new CodeMirror.StringStream(query);
var params = {};
try {
exCommandDispatcher.parseInput_(cm, inputStream, params);
if (params.commandName != "s") {
clearSearchHighlight(cm);
return;
}
command = exCommandDispatcher.matchCommand_(params.commandName);
exCommandDispatcher.parseCommandArgs_(inputStream, params, command);
if (!params.argString) return;
var regex = parseQuery(params.argString.slice(1), true, true);
if (regex) highlightSearchMatches(cm, regex);
} catch(e) {
}
}
if (command.type == 'keyToEx') {
// Handle user defined Ex to Ex mappings
exCommandDispatcher.processCommand(cm, command.exArgs.input);
} else {
var promptOptions = {
onClose: onPromptClose,
onKeyDown: onPromptKeyDown,
onKeyUp: onPromptKeyUp,
prefix: ':',
};
if (vim.visualMode) {
showPrompt(cm, { onClose: onPromptClose, prefix: ':', value: '\'<,\'>',
onKeyDown: onPromptKeyDown, selectValueOnOpen: false});
} else {
showPrompt(cm, { onClose: onPromptClose, prefix: ':',
onKeyDown: onPromptKeyDown});
promptOptions.value = '\'<,\'>';
promptOptions.selectValueOnOpen = false;
}
showPrompt(cm, promptOptions);
}
},
/**@arg {CodeMirrorV} cm @arg {vimState} vim */
Expand Down
28 changes: 17 additions & 11 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ var foldingRangeUp = {
end: new Pos(foldingStart.line, 0)
};

function copyCursor(cur) {
return new Pos(cur.line, cur.ch);
function searchHighlighted(vim) {
return vim.searchState_ && (vim.searchState_.getOverlay() || vim.searchState_.highlightTimeout);
}

function forEach(arr, func) {
Expand Down Expand Up @@ -4459,6 +4459,13 @@ testVim('ex_substitute_empty_arguments', function(cm,vim,helpers) {
helpers.doEx('s');
eq('b b\nb a', cm.getValue());
}, {value: 'a a\na a'});
testVim('ex_substitute_highlight', function(cm,vim,helpers) {
is(!searchHighlighted(vim));
helpers.doKeys(':s/a');
is(searchHighlighted(vim));
helpers.doKeys('\n');
is(!searchHighlighted(vim));
}, {value: 'a a\na a'});

// More complex substitute tests that test both pcre and nopcre options.
function testSubstitute(name, options) {
Expand Down Expand Up @@ -4690,9 +4697,11 @@ testSubstituteConfirm('ex_substitute_confirm_range_last',
'1,3s/a/b/cg', 'aa\na \na\na', 'bb\nb \na\na', 'yyl', makeCursor(1, 0));
//:noh should clear highlighting of search-results but allow to resume search through n
testVim('ex_noh_clearSearchHighlight', function(cm, vim, helpers) {
is(!searchHighlighted(vim))
helpers.doKeys('?', 'match', '\n');
is(searchHighlighted(vim))
helpers.doEx('noh');
eq(vim.searchState_.getOverlay(),null,'match-highlighting wasn\'t cleared');
is(!searchHighlighted(vim));
helpers.doKeys('n');
helpers.assertCursorAt(0, 11,'can\'t resume search after clearing highlighting');
}, { value: 'match nope match \n nope Match' });
Expand Down Expand Up @@ -4864,17 +4873,14 @@ testVim('ex_set_filetype_null', function(cm, vim, helpers) {
});

testVim('map_prompt', function(cm, vim, helpers) {
function highlighted() {
return vim.searchState_ && (vim.searchState_.getOverlay() || vim.searchState_.highlightTimeout);
}
is(!highlighted());
is(!searchHighlighted(vim));

helpers.doKeys('/a\n');
helpers.doKeys('i');
is(highlighted());
is(searchHighlighted(vim));
helpers.doKeys('<Esc>');
helpers.doEx('nohl');
is(!highlighted());
is(!searchHighlighted(vim));
helpers.assertCursorAt(1, 2);

helpers.doEx('nnoremap i :nohl<CR>i<space>xx<lt>');
Expand All @@ -4887,9 +4893,9 @@ testVim('map_prompt', function(cm, vim, helpers) {
helpers.doKeys('j');
eq(cm.getWrapperElement().querySelector("input").value, "ab");
helpers.doKeys('<CR>');
is(highlighted());
is(searchHighlighted(vim));
helpers.doKeys('i');
is(!highlighted());
is(!searchHighlighted(vim));

eq(cm.getValue(), ' 0 xyz\n hi1 xx<abc \n 2 abc');

Expand Down

0 comments on commit 893666e

Please sign in to comment.