]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/pretty-json/node_modules/jsonminify/report/assets/scripts/vendor/codemirror/util/search.js
266b2c92ee4f1e2c79227dc7b9bd9887dc67388a
1 // Define search commands. Depends on dialog.js or another
2 // implementation of the openDialog method.
4 // Replace works a little oddly -- it will do the replace on the next
5 // Ctrl-G (or whatever is bound to findNext) press. You prevent a
6 // replace by making sure the match is no longer selected when hitting
10 function SearchState() {
11 this.posFrom
= this.posTo
= this.query
= null;
14 function getSearchState(cm
) {
15 return cm
._searchState
|| (cm
._searchState
= new SearchState());
17 function getSearchCursor(cm
, query
, pos
) {
18 // Heuristic: if the query string is all lowercase, do a case insensitive search.
19 return cm
.getSearchCursor(query
, pos
, typeof query
== "string" && query
== query
.toLowerCase());
21 function dialog(cm
, text
, shortText
, f
) {
22 if (cm
.openDialog
) cm
.openDialog(text
, f
);
23 else f(prompt(shortText
, ""));
25 function confirmDialog(cm
, text
, shortText
, fs
) {
26 if (cm
.openConfirm
) cm
.openConfirm(text
, fs
);
27 else if (confirm(shortText
)) fs
[0]();
29 function parseQuery(query
) {
30 var isRE
= query
.match(/^\/(.*)\/([a-z]*)$/);
31 return isRE
? new RegExp(isRE
[1], isRE
[2].indexOf("i") == -1 ? "" : "i") : query
;
34 'Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
35 function doSearch(cm
, rev
) {
36 var state
= getSearchState(cm
);
37 if (state
.query
) return findNext(cm
, rev
);
38 dialog(cm
, queryDialog
, "Search for:", function(query
) {
39 cm
.operation(function() {
40 if (!query
|| state
.query
) return;
41 state
.query
= parseQuery(query
);
42 if (cm
.lineCount() < 2000) { // This is too expensive on big documents.
43 for (var cursor
= getSearchCursor(cm
, state
.query
); cursor
.findNext();)
44 state
.marked
.push(cm
.markText(cursor
.from(), cursor
.to(),
45 {className: "CodeMirror-searching"}));
47 state
.posFrom
= state
.posTo
= cm
.getCursor();
52 function findNext(cm
, rev
) {cm
.operation(function() {
53 var state
= getSearchState(cm
);
54 var cursor
= getSearchCursor(cm
, state
.query
, rev
? state
.posFrom : state
.posTo
);
55 if (!cursor
.find(rev
)) {
56 cursor
= getSearchCursor(cm
, state
.query
, rev
? {line: cm
.lineCount() - 1} : {line: 0, ch: 0});
57 if (!cursor
.find(rev
)) return;
59 cm
.setSelection(cursor
.from(), cursor
.to());
60 state
.posFrom
= cursor
.from(); state
.posTo
= cursor
.to();
62 function clearSearch(cm
) {cm
.operation(function() {
63 var state
= getSearchState(cm
);
64 if (!state
.query
) return;
66 for (var i
= 0; i
< state
.marked
.length
; ++i
) state
.marked
[i
].clear();
67 state
.marked
.length
= 0;
70 var replaceQueryDialog
=
71 'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
72 var replacementQueryDialog
= 'With: <input type="text" style="width: 10em"/>';
73 var doReplaceConfirm
= "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
74 function replace(cm
, all
) {
75 dialog(cm
, replaceQueryDialog
, "Replace:", function(query
) {
77 query
= parseQuery(query
);
78 dialog(cm
, replacementQueryDialog
, "Replace with:", function(text
) {
80 cm
.operation(function() {
81 for (var cursor
= getSearchCursor(cm
, query
); cursor
.findNext();) {
82 if (typeof query
!= "string") {
83 var match
= cm
.getRange(cursor
.from(), cursor
.to()).match(query
);
84 cursor
.replace(text
.replace(/\$(\d)/, function(_
, i
) {return match
[i
];}));
85 } else cursor
.replace(text
);
90 var cursor
= getSearchCursor(cm
, query
, cm
.getCursor());
92 var start
= cursor
.from(), match
;
93 if (!(match
= cursor
.findNext())) {
94 cursor
= getSearchCursor(cm
, query
);
95 if (!(match
= cursor
.findNext()) ||
96 (start
&& cursor
.from().line
== start
.line
&& cursor
.from().ch
== start
.ch
)) return;
98 cm
.setSelection(cursor
.from(), cursor
.to());
99 confirmDialog(cm
, doReplaceConfirm
, "Replace?",
100 [function() {doReplace(match
);}, advance
]);
102 function doReplace(match
) {
103 cursor
.replace(typeof query
== "string" ? text :
104 text
.replace(/\$(\d)/, function(_
, i
) {return match
[i
];}));
113 CodeMirror
.commands
.find = function(cm
) {clearSearch(cm
); doSearch(cm
);};
114 CodeMirror
.commands
.findNext
= doSearch
;
115 CodeMirror
.commands
.findPrev = function(cm
) {doSearch(cm
, true);};
116 CodeMirror
.commands
.clearSearch
= clearSearch
;
117 CodeMirror
.commands
.replace
= replace
;
118 CodeMirror
.commands
.replaceAll = function(cm
) {replace(cm
, true);};