Repository navigation
Expand file tree
/
Copy pathsearch.js
More file actions
147 lines (136 loc) · 4.93 KB
/
Copy pathsearch.js
File metadata and controls
147 lines (136 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Define search commands. Depends on dialog.js or another
// implementation of the openDialog method.
// Replace works a little oddly -- it will do the replace on the next
// Ctrl-G (or whatever is bound to findNext) press. You prevent a
// replace by making sure the match is no longer selected when hitting
// Ctrl-G.
(function() {
function searchOverlay(query) {
if (typeof query == "string") {
var ignoreCase = query == query.toLowerCase();
return {token: function(stream) {
if (stream.match(query, true, ignoreCase)) return "searching";
stream.next();
if(!stream.skipTo(query.charAt(0))) {
if(!ignoreCase || !stream.skipTo(query.charAt(0).toUpperCase())) {
stream.skipToEnd();
}
}
}};
}
return {token: function(stream) {
if (stream.match(query)) return "searching";
while (!stream.eol()) {
stream.next();
if (stream.match(query, false)) break;
}
}};
}
function SearchState() {
this.posFrom = this.posTo = this.query = null;
this.overlay = null;
}
function getSearchState(cm) {
return cm._searchState || (cm._searchState = new SearchState());
}
function getSearchCursor(cm, query, pos) {
// Heuristic: if the query string is all lowercase, do a case insensitive search.
return cm.getSearchCursor(query, pos, typeof query == "string" && query == query.toLowerCase());
}
function parseQuery(query) {
var ignoreCase = query == query.toLowerCase();
var isRE = query.match(/^\/(.+)\/([a-z]*)$/);
try {
var rx = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i");
if(isRE && !"".match(rx)) {
return rx;
} else {
return query;
}
} catch (e) {
return query;
}
}
function doSearch(cm, query, rev) {
var state = getSearchState(cm);
cm.operation(function() {
if(!query) {
clearSearch(cm);
return;
}
if (state.rawQuery == query) return;
clearSearch(cm, false);
state.rawQuery = query;
state.query = parseQuery(query);
cm.removeOverlay(state.overlay);
state.overlay = searchOverlay(state.query);
cm.addOverlay(state.overlay);
state.posFrom = state.posTo = cm.getCursor();
if(!state.origPos) {
state.origPos = state.posFrom;
}
findNext(cm, rev, true);
});
}
function findNext(cm, rev, allowCurrent) {cm.operation(function() {
var state = getSearchState(cm);
var startingPoint = state.posTo;
if(!state.query) { return; }
if(allowCurrent) {
startingPoint = state.origPos ? state.origPos : state.posFrom;
} else {
state.origPos = null;
if(rev) {
startingPoint = state.posFrom;
}
}
var cursor = getSearchCursor(cm, state.query, startingPoint);
if (!cursor.find(rev)) {
cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
if (!cursor.find(rev)) return;
}
//cm.setSelection(cursor.from(), cursor.to());
cm.setCursor(cursor.from());
if(state.mark) { state.mark.clear(); }
state.mark = cm.markText(cursor.from(), cursor.to(), {className: "searching-current"});
state.posFrom = cursor.from(); state.posTo = cursor.to();
});}
function clearSearch(cm, hard) {cm.operation(function() {
var state = getSearchState(cm);
if (!state.query) return;
if (hard) { state.origPos = null; }
state.query = null;
state.rawQuery = null;
if(state.mark) { state.mark.clear(); }
cm.removeOverlay(state.overlay);
});}
function replace(cm, text, rev, all) {
if (all) {
cm.operation(function() {
var state = getSearchState(cm);
for (var cursor = getSearchCursor(cm, state.query); cursor.find(rev);) {
if (typeof state.query != "string") {
var match = cm.getRange(cursor.from(), cursor.to()).match(query);
cursor.replace(text.replace(/\$(\d)/, function(_, i) {return match[i];}));
} else cursor.replace(text);
}
});
} else {
var state = getSearchState(cm);
var cursor = getSearchCursor(cm, state.query, state.posFrom);
if(cursor.findNext(rev)) {
cm.setSelection(cursor.from(), cursor.to());
var match = cm.getRange(cursor.from(), cursor.to()).match(state.query);
cursor.replace(typeof state.query == "string" ? text :
text.replace(/\$(\d)/, function(_, i) {return match[i];}));
}
}
}
CodeMirror.commands.find = function(cm, query, rev) { doSearch(cm, query, rev);};
CodeMirror.commands.findNext = findNext;
CodeMirror.commands.findPrev = function(cm, rev) {findNext(cm, !rev);};
CodeMirror.commands.clearSearch = function(cm) { clearSearch(cm, true); };
CodeMirror.commands.getSearchState = getSearchState;
CodeMirror.commands.replace = replace;
CodeMirror.commands.replaceAll = function(cm, query, replace) { replace(cm, query, replace, true); };
})();