Skip to content

Commit 893666e

Browse files
authored
highlight search during replace command (#175)
1 parent ffe5f4c commit 893666e

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

src/vim.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,7 @@ export function initVim(CodeMirror) {
17761776
vimGlobalState.exCommandHistoryController.reset();
17771777
exCommandDispatcher.processCommand(cm, input);
17781778
if (cm.state.vim) clearInputState(cm);
1779+
clearSearchHighlight(cm);
17791780
}
17801781
/**
17811782
* @arg {KeyboardEvent&{target:HTMLInputElement}} e
@@ -1790,6 +1791,7 @@ export function initVim(CodeMirror) {
17901791
vimGlobalState.exCommandHistoryController.reset();
17911792
CodeMirror.e_stop(e);
17921793
clearInputState(cm);
1794+
clearSearchHighlight(cm);
17931795
close();
17941796
cm.focus();
17951797
}
@@ -1808,17 +1810,42 @@ export function initVim(CodeMirror) {
18081810
vimGlobalState.exCommandHistoryController.reset();
18091811
}
18101812
}
1813+
/**
1814+
* @arg {KeyboardEvent&{target:HTMLInputElement}} e
1815+
* @arg {any} query
1816+
*/
1817+
function onPromptKeyUp(e, query) {
1818+
var inputStream = new CodeMirror.StringStream(query);
1819+
var params = {};
1820+
try {
1821+
exCommandDispatcher.parseInput_(cm, inputStream, params);
1822+
if (params.commandName != "s") {
1823+
clearSearchHighlight(cm);
1824+
return;
1825+
}
1826+
command = exCommandDispatcher.matchCommand_(params.commandName);
1827+
exCommandDispatcher.parseCommandArgs_(inputStream, params, command);
1828+
if (!params.argString) return;
1829+
var regex = parseQuery(params.argString.slice(1), true, true);
1830+
if (regex) highlightSearchMatches(cm, regex);
1831+
} catch(e) {
1832+
}
1833+
}
18111834
if (command.type == 'keyToEx') {
18121835
// Handle user defined Ex to Ex mappings
18131836
exCommandDispatcher.processCommand(cm, command.exArgs.input);
18141837
} else {
1838+
var promptOptions = {
1839+
onClose: onPromptClose,
1840+
onKeyDown: onPromptKeyDown,
1841+
onKeyUp: onPromptKeyUp,
1842+
prefix: ':',
1843+
};
18151844
if (vim.visualMode) {
1816-
showPrompt(cm, { onClose: onPromptClose, prefix: ':', value: '\'<,\'>',
1817-
onKeyDown: onPromptKeyDown, selectValueOnOpen: false});
1818-
} else {
1819-
showPrompt(cm, { onClose: onPromptClose, prefix: ':',
1820-
onKeyDown: onPromptKeyDown});
1845+
promptOptions.value = '\'<,\'>';
1846+
promptOptions.selectValueOnOpen = false;
18211847
}
1848+
showPrompt(cm, promptOptions);
18221849
}
18231850
},
18241851
/**@arg {CodeMirrorV} cm @arg {vimState} vim */

test/vim_test.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ var foldingRangeUp = {
134134
end: new Pos(foldingStart.line, 0)
135135
};
136136

137-
function copyCursor(cur) {
138-
return new Pos(cur.line, cur.ch);
137+
function searchHighlighted(vim) {
138+
return vim.searchState_ && (vim.searchState_.getOverlay() || vim.searchState_.highlightTimeout);
139139
}
140140

141141
function forEach(arr, func) {
@@ -4459,6 +4459,13 @@ testVim('ex_substitute_empty_arguments', function(cm,vim,helpers) {
44594459
helpers.doEx('s');
44604460
eq('b b\nb a', cm.getValue());
44614461
}, {value: 'a a\na a'});
4462+
testVim('ex_substitute_highlight', function(cm,vim,helpers) {
4463+
is(!searchHighlighted(vim));
4464+
helpers.doKeys(':s/a');
4465+
is(searchHighlighted(vim));
4466+
helpers.doKeys('\n');
4467+
is(!searchHighlighted(vim));
4468+
}, {value: 'a a\na a'});
44624469

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

48664875
testVim('map_prompt', function(cm, vim, helpers) {
4867-
function highlighted() {
4868-
return vim.searchState_ && (vim.searchState_.getOverlay() || vim.searchState_.highlightTimeout);
4869-
}
4870-
is(!highlighted());
4876+
is(!searchHighlighted(vim));
48714877

48724878
helpers.doKeys('/a\n');
48734879
helpers.doKeys('i');
4874-
is(highlighted());
4880+
is(searchHighlighted(vim));
48754881
helpers.doKeys('<Esc>');
48764882
helpers.doEx('nohl');
4877-
is(!highlighted());
4883+
is(!searchHighlighted(vim));
48784884
helpers.assertCursorAt(1, 2);
48794885

48804886
helpers.doEx('nnoremap i :nohl<CR>i<space>xx<lt>');
@@ -4887,9 +4893,9 @@ testVim('map_prompt', function(cm, vim, helpers) {
48874893
helpers.doKeys('j');
48884894
eq(cm.getWrapperElement().querySelector("input").value, "ab");
48894895
helpers.doKeys('<CR>');
4890-
is(highlighted());
4896+
is(searchHighlighted(vim));
48914897
helpers.doKeys('i');
4892-
is(!highlighted());
4898+
is(!searchHighlighted(vim));
48934899

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

0 commit comments

Comments
 (0)