Editor: use current search state in later added sections
This commit is contained in:
parent
62ec9e1c9e
commit
a830d5b3af
53
edit.js
53
edit.js
|
@ -489,34 +489,45 @@ function setupGlobalSearch() {
|
||||||
findPrev: CodeMirror.commands.findPrev
|
findPrev: CodeMirror.commands.findPrev
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var curState; // cm.state.search for last used 'find'
|
||||||
|
|
||||||
function shouldIgnoreCase(query) { // treat all-lowercase non-regexp queries as case-insensitive
|
function shouldIgnoreCase(query) { // treat all-lowercase non-regexp queries as case-insensitive
|
||||||
return typeof query == "string" && query == query.toLowerCase();
|
return typeof query == "string" && query == query.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateState(cm, newState) {
|
||||||
|
if (!newState) {
|
||||||
|
if (cm.state.search) {
|
||||||
|
return cm.state.search;
|
||||||
|
}
|
||||||
|
newState = curState;
|
||||||
|
}
|
||||||
|
cm.state.search = {
|
||||||
|
query: newState.query,
|
||||||
|
overlay: newState.overlay,
|
||||||
|
annotate: cm.showMatchesOnScrollbar(newState.query, shouldIgnoreCase(newState.query))
|
||||||
|
}
|
||||||
|
cm.addOverlay(newState.overlay);
|
||||||
|
return cm.state.search;
|
||||||
|
}
|
||||||
|
|
||||||
function find(activeCM) {
|
function find(activeCM) {
|
||||||
var originalOpenDialog = activeCM.openDialog;
|
var originalOpenDialog = activeCM.openDialog;
|
||||||
activeCM.openDialog = function(template, callback, options) {
|
activeCM.openDialog = function(template, callback, options) {
|
||||||
originalOpenDialog.call(activeCM, findTemplate, function(query) {
|
originalOpenDialog.call(activeCM, findTemplate, function(query) {
|
||||||
activeCM.openDialog = originalOpenDialog;
|
activeCM.openDialog = originalOpenDialog;
|
||||||
callback(query);
|
callback(query);
|
||||||
var state = activeCM.state.search;
|
curState = activeCM.state.search;
|
||||||
if (editors.length == 1 || !state.query) {
|
if (editors.length == 1 || !curState.query) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (var i=0; i < editors.length; i++) {
|
editors.forEach(function(cm) {
|
||||||
var cm = editors[i];
|
if (cm != activeCM) {
|
||||||
if (cm == activeCM) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
cm.execCommand("clearSearch");
|
cm.execCommand("clearSearch");
|
||||||
cm.state.search = {
|
updateState(cm, curState);
|
||||||
query: state.query,
|
|
||||||
overlay: state.overlay,
|
|
||||||
annotate: cm.showMatchesOnScrollbar(state.query, shouldIgnoreCase(state.query))
|
|
||||||
}
|
}
|
||||||
cm.addOverlay(state.overlay);
|
});
|
||||||
}
|
if (CodeMirror.cmpPos(curState.posFrom, curState.posTo) == 0) {
|
||||||
if (CodeMirror.cmpPos(activeCM.state.search.posFrom, activeCM.state.search.posTo) == 0) {
|
|
||||||
findNext(activeCM);
|
findNext(activeCM);
|
||||||
}
|
}
|
||||||
}, options);
|
}, options);
|
||||||
|
@ -525,20 +536,16 @@ function setupGlobalSearch() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function findNext(activeCM, reverse) {
|
function findNext(activeCM, reverse) {
|
||||||
if (!activeCM.state.search || !activeCM.state.search.query) {
|
var state = updateState(activeCM);
|
||||||
|
if (!state || !state.query) {
|
||||||
find(activeCM);
|
find(activeCM);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var pos = activeCM.getCursor();
|
var pos = activeCM.getCursor(reverse ? "from" : "to");
|
||||||
// check if the search term is currently selected in the editor
|
activeCM.setSelection(activeCM.getCursor()); // clear the selection, don't move the cursor
|
||||||
var m = activeCM.getSelection().match(activeCM.state.search.query);
|
|
||||||
if (m && m[0].length == activeCM.getSelection().length) {
|
|
||||||
pos = activeCM.getCursor(reverse ? "from" : "to");
|
|
||||||
activeCM.setSelection(activeCM.getCursor());
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i=0, cm=activeCM; i < editors.length; i++) {
|
for (var i=0, cm=activeCM; i < editors.length; i++) {
|
||||||
var state = cm.state.search;
|
state = updateState(cm);
|
||||||
if (cm != activeCM) {
|
if (cm != activeCM) {
|
||||||
pos = reverse ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(0, 0);
|
pos = reverse ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(0, 0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user