forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindReplace.js
More file actions
715 lines (614 loc) · 29.2 KB
/
FindReplace.js
File metadata and controls
715 lines (614 loc) · 29.2 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, Mustache */
/*unittests: FindReplace*/
/**
* Adds Find and Replace commands
*
* Originally based on the code in CodeMirror2/lib/util/search.js.
*/
define(function (require, exports, module) {
"use strict";
var CommandManager = require("command/CommandManager"),
KeyBindingManager = require("command/KeyBindingManager"),
AppInit = require("utils/AppInit"),
Commands = require("command/Commands"),
DocumentManager = require("document/DocumentManager"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils"),
Editor = require("editor/Editor"),
EditorManager = require("editor/EditorManager"),
ModalBar = require("widgets/ModalBar").ModalBar,
KeyEvent = require("utils/KeyEvent"),
ScrollTrackMarkers = require("search/ScrollTrackMarkers"),
PanelManager = require("view/PanelManager"),
Resizer = require("utils/Resizer"),
StatusBar = require("widgets/StatusBar"),
PreferencesManager = require("preferences/PreferencesManager"),
ViewUtils = require("utils/ViewUtils");
var searchBarTemplate = require("text!htmlContent/findreplace-bar.html"),
searchReplacePanelTemplate = require("text!htmlContent/search-replace-panel.html"),
searchReplaceResultsTemplate = require("text!htmlContent/search-replace-results.html");
/** @const Maximum file size to search within (in chars) */
var FIND_MAX_FILE_SIZE = 500000;
/** @const If the number of matches exceeds this limit, inline text highlighting and scroll-track tickmarks are disabled */
var FIND_HIGHLIGHT_MAX = 2000;
/** @const Maximum number of matches to collect for Replace All; any additional matches are not listed in the panel & are not replaced */
var REPLACE_ALL_MAX = 300;
/** @type {!Panel} Panel that shows results of replaceAll action */
var replaceAllPanel = null;
/** @type {?Document} Instance of the currently opened document when replaceAllPanel is visible */
var currentDocument = null;
/** @type {$.Element} jQuery elements used in the replaceAll panel */
var $replaceAllContainer,
$replaceAllWhat,
$replaceAllWith,
$replaceAllSummary,
$replaceAllTable;
/** @type {?ModalBar} Currently open Find or Find/Replace bar, if any */
var modalBar;
/** @type {!function():void} API from FindInFiles for closing its conflicting search bar, if open */
var closeFindInFilesBar;
function SearchState() {
this.searchStartPos = null;
this.query = null;
this.foundAny = false;
this.marked = [];
}
function getSearchState(cm) {
if (!cm._searchState) {
cm._searchState = new SearchState();
}
return cm._searchState;
}
function getSearchCursor(cm, query, pos) {
// Heuristic: if the query string is all lowercase, do a case insensitive search.
return cm.getSearchCursor(query, pos, !PreferencesManager.getViewState("caseSensitive"));
}
function _updateSearchBarFromPrefs() {
$("#find-case-sensitive").toggleClass("active", PreferencesManager.getViewState("caseSensitive"));
$("#find-regexp").toggleClass("active", PreferencesManager.getViewState("regexp"));
}
function _updatePrefsFromSearchBar() {
PreferencesManager.setViewState("caseSensitive", $("#find-case-sensitive").is(".active"));
PreferencesManager.setViewState("regexp", $("#find-regexp").is(".active"));
}
function parseQuery(query) {
$(".modal-bar .message").show();
$(".modal-bar .error").hide();
// Is it a (non-blank) regex?
if (query && $("#find-regexp").is(".active")) {
try {
var caseSensitive = $("#find-case-sensitive").is(".active");
return new RegExp(query, caseSensitive ? "" : "i");
} catch (e) {
$(".modal-bar .message").hide();
$(".modal-bar .error")
.show()
.text(e.message);
return "";
}
} else {
return query;
}
}
// NOTE: we can't just use the ordinary replace() function here because the string has been
// extracted from the original text and so might be missing some context that the regexp matched.
function parseDollars(replaceWith, match) {
replaceWith = replaceWith.replace(/(\$+)(\d{1,2}|&)/g, function (whole, dollars, index) {
var parsedIndex = parseInt(index, 10);
if (dollars.length % 2 === 1) { // check if dollar signs escape themselves (for example $$1, $$$$&)
if (index === "&") { // handle $&
return dollars.substr(1) + (match[0] || "");
} else if (parsedIndex !== 0) { // handle $n or $nn, don't handle $0 or $00
return dollars.substr(1) + (match[parsedIndex] || "");
}
}
return whole;
});
replaceWith = replaceWith.replace(/\$\$/g, "$"); // replace escaped dollar signs (for example $$) with single ones
return replaceWith;
}
/**
* Selects the next match (or prev match, if rev==true) starting from either the current position
* (if pos unspecified) or the given position (if pos specified explicitly). The starting position
* need not be an existing match. If a new match is found, sets to state.lastMatch either the regex
* match result, or simply true for a plain-string match. If no match found, sets state.lastMatch
* to false.
* @param {!Editor} editor
* @param {?boolean} rev
* @param {?boolean} preferNoScroll
* @param {?Pos} pos
*/
function findNext(editor, rev, preferNoScroll, pos) {
var cm = editor._codeMirror;
cm.operation(function () {
var state = getSearchState(cm);
var cursor = getSearchCursor(cm, state.query, pos || cm.getCursor(Boolean(rev))); // null and false mean different things to getCursor()
state.lastMatch = cursor.find(rev);
if (!state.lastMatch) {
// If no result found before hitting edge of file, try wrapping around
cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
state.lastMatch = cursor.find(rev);
if (!state.lastMatch) {
// No result found, period: clear selection & bail
cm.setCursor(cm.getCursor()); // collapses selection, keeping cursor in place to avoid scrolling
return;
}
}
var resultVisible = editor.isLineVisible(cursor.from().line),
centerOptions = Editor.BOUNDARY_CHECK_NORMAL;
if (preferNoScroll && resultVisible) {
// no need to scroll if the line with the match is in view
centerOptions = Editor.BOUNDARY_IGNORE_TOP;
}
cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
editor.setSelection(cursor.from(), cursor.to(), true, centerOptions);
});
}
function clearHighlights(cm, state) {
cm.operation(function () {
state.marked.forEach(function (markedRange) {
markedRange.clear();
});
});
state.marked.length = 0;
ScrollTrackMarkers.clear();
}
function clearSearch(cm) {
cm.operation(function () {
var state = getSearchState(cm);
if (!state.query) {
return;
}
state.query = null;
clearHighlights(cm, state);
});
}
function _closeFindBar() {
if (modalBar) {
// 1st arg = restore scroll pos; 2nd arg = no animation, since getting replaced immediately
modalBar.close(true, false);
}
}
function _registerFindInFilesCloser(closer) {
closeFindInFilesBar = closer;
}
function createModalBar(template) {
// Normally, creating a new modal bar will simply cause the old one to close
// automatically. This can cause timing issues because the focus change might
// cause the new one to think it should close, too. The old CodeMirror version
// of this handled it by adding a timeout within which a blur wouldn't cause
// the modal bar to close. Rather than reinstate that hack, we simply explicitly
// close the old modal bar (which may be a Find, Replace, *or* Find in Files bar
// before creating a new one. (TODO: remove once #6203 fixed)
_closeFindBar();
closeFindInFilesBar();
modalBar = new ModalBar(template, true); // 2nd arg = auto-close on Esc/blur
$(modalBar).on("close", function (event) {
modalBar = null;
});
}
function addShortcutToTooltip($elem, commandId) {
var replaceShortcut = KeyBindingManager.getKeyBindings(commandId)[0];
if (replaceShortcut) {
var oldTitle = $elem.attr("title");
oldTitle = (oldTitle ? oldTitle + " " : "");
$elem.attr("title", oldTitle + "(" + KeyBindingManager.formatKeyDescriptor(replaceShortcut.displayKey) + ")");
}
}
function toggleHighlighting(editor, enabled) {
// Temporarily change selection color to improve highlighting - see LESS code for details
if (enabled) {
$(editor.getRootElement()).addClass("find-highlighting");
} else {
$(editor.getRootElement()).removeClass("find-highlighting");
}
ScrollTrackMarkers.setVisible(editor, enabled);
}
/**
* Called each time the search query changes or document is modified (via Replace). Updates
* the match count, match highlights and scrollbar tickmarks. Does not change the cursor pos.
*/
function updateResultSet(editor) {
var cm = editor._codeMirror,
state = getSearchState(cm);
function indicateHasMatches(numResults) {
// Make the field red if it's not blank and it has no matches (which also covers invalid regexes)
ViewUtils.toggleClass($("#find-what"), "no-results", !state.foundAny && $("#find-what").val());
// Buttons disabled if blank, OR if no matches (Replace buttons, Show in list button) /
// < 2 matches (nav buttons)
$("#find-prev, #find-next").prop("disabled", !state.foundAny || numResults < 2);
$("#replace-yes, #replace-all").prop("disabled", !state.foundAny);
$("#find-show-list").prop("disabled", !state.foundAny);
}
cm.operation(function () {
// Clear old highlights
if (state.marked) {
clearHighlights(cm, state);
}
if (!state.query) {
// Search field is empty - no results
$("#find-counter").text("");
state.foundAny = false;
indicateHasMatches();
return;
}
// Find *all* matches, searching from start of document
// (Except on huge documents, where this is too expensive)
var cursor = getSearchCursor(cm, state.query);
if (cm.getValue().length <= FIND_MAX_FILE_SIZE) {
// FUTURE: if last query was prefix of this one, could optimize by filtering last result set
var resultSet = [];
while (cursor.findNext()) {
resultSet.push(cursor.pos); // pos is unique obj per search result
}
// Highlight all matches if there aren't too many
if (resultSet.length <= FIND_HIGHLIGHT_MAX) {
toggleHighlighting(editor, true);
resultSet.forEach(function (result) {
state.marked.push(cm.markText(result.from, result.to,
{ className: "CodeMirror-searching", startStyle: "searching-first", endStyle: "searching-last" }));
});
var scrollTrackPositions = resultSet.map(function (result) {
return result.from;
});
ScrollTrackMarkers.addTickmarks(editor, scrollTrackPositions);
}
if (resultSet.length === 0) {
$("#find-counter").text(Strings.FIND_NO_RESULTS);
} else if (resultSet.length === 1) {
$("#find-counter").text(Strings.FIND_RESULT_COUNT_SINGLE);
} else {
$("#find-counter").text(StringUtils.format(Strings.FIND_RESULT_COUNT, resultSet.length));
}
state.foundAny = (resultSet.length > 0);
indicateHasMatches(resultSet.length);
} else {
// On huge documents, just look for first match & then stop
$("#find-counter").text("");
state.foundAny = cursor.findNext();
indicateHasMatches();
}
});
}
/**
* Called each time the search query field changes. Updates state.query (query will be falsy if the field
* was blank OR contained a regexp with invalid syntax). Then calls updateResultSet(), and then jumps to
* the first matching result, starting from the original cursor position.
*/
function handleQueryChange(editor, state) {
state.query = parseQuery($("#find-what").val());
updateResultSet(editor);
if (state.query) {
// 3rd arg: prefer to avoid scrolling if result is anywhere within view, since in this case user
// is in the middle of typing, not navigating explicitly; viewport jumping would be distracting.
findNext(editor, false, true, state.searchStartPos);
} else {
// Blank or invalid query: just jump back to initial pos
editor._codeMirror.setCursor(state.searchStartPos);
}
}
/**
* Opens the search bar with the given HTML content (Find or Find-Replace), attaches common Find behaviors,
* and prepopulates the query field.
* @param {!Editor} editor
* @param {!Object} templateVars
*/
function openSearchBar(editor, templateVars) {
var cm = editor._codeMirror,
state = getSearchState(cm);
// Use the selection start as the searchStartPos. This way if you
// start with a pre-populated search and enter an additional character,
// it will extend the initial selection instead of jumping to the next
// occurrence.
state.searchStartPos = cm.getCursor(true);
// If a previous search/replace bar was open, capture its query text for use below
var initialQuery;
if (modalBar) {
initialQuery = $("#find-what").val();
}
// Create the search bar UI (closing any previous modalBar in the process)
var htmlContent = Mustache.render(searchBarTemplate, $.extend(templateVars, Strings));
createModalBar(htmlContent);
addShortcutToTooltip($("#find-next"), Commands.EDIT_FIND_NEXT);
addShortcutToTooltip($("#find-prev"), Commands.EDIT_FIND_PREVIOUS);
$(modalBar).on("close", function (e, query) {
// Clear highlights but leave search state in place so Find Next/Previous work after closing
clearHighlights(cm, state);
// Dispose highlighting UI (important to restore normal selection color as soon as focus goes back to the editor)
toggleHighlighting(editor, false);
// Hide error popup, since it hangs down low enough to make the slide-out look awkward
$(".modal-bar .error").hide();
});
modalBar.getRoot()
.on("click", "#find-next", function (e) {
findNext(editor);
})
.on("click", "#find-prev", function (e) {
findNext(editor, true);
})
.on("click", "#find-case-sensitive, #find-regexp", function (e) {
$(e.currentTarget).toggleClass('active');
_updatePrefsFromSearchBar();
handleQueryChange(editor, state);
})
.on("click", "#find-show-list", function (e) {
var query = $("#find-what").val();
modalBar.close(true, true);
CommandManager.execute(Commands.EDIT_FIND_IN_SUBTREE, DocumentManager.getCurrentDocument().file, query);
})
.on("keydown", function (e) {
if (e.keyCode === KeyEvent.DOM_VK_RETURN) {
if (!e.shiftKey) {
findNext(editor);
} else {
findNext(editor, true);
}
}
});
$("#find-what").on("input", function () {
handleQueryChange(editor, state);
});
// Prepopulate the search field
if (!initialQuery) {
// Prepopulate with the current selection, if any
initialQuery = cm.getSelection();
// Eliminate newlines since we don't generally support searching across line boundaries (#2960)
var newline = initialQuery.indexOf("\n");
if (newline !== -1) {
initialQuery = initialQuery.substr(0, newline);
}
}
// Initial UI state
$("#find-what")
.val(initialQuery)
.get(0).select();
_updateSearchBarFromPrefs();
handleQueryChange(editor, state);
}
/**
* If no search pending, opens the Find dialog. If search bar already open, moves to
* next/prev result (depending on 'rev')
*/
function doSearch(editor, rev) {
var state = getSearchState(editor._codeMirror);
if (state.query) {
findNext(editor, rev);
return;
}
openSearchBar(editor, {});
}
/**
* @private
* Closes a panel with search-replace results.
* Main purpose is to make sure that events are correctly detached from current document.
*/
function _closeReplaceAllPanel() {
if (replaceAllPanel !== null && replaceAllPanel.isVisible()) {
replaceAllPanel.hide();
}
$(currentDocument).off("change.replaceAll");
}
/**
* @private
* When the user switches documents (or closes the last document), ensure that the find bar
* closes, and also close the Replace All panel.
*/
function _handleDocumentChange() {
if (modalBar) {
modalBar.close();
}
_closeReplaceAllPanel();
}
/**
* @private
* Shows a panel with search results and offers to replace them,
* user can use checkboxes to select which results he wishes to replace.
* @param {Editor} editor - Currently active editor that was used to invoke this action.
* @param {string|RegExp} replaceWhat - Query that will be passed into CodeMirror Cursor to search for results.
* @param {string} replaceWith - String that should be used to replace chosen results.
*/
function _showReplaceAllPanel(editor, replaceWhat, replaceWith) {
var results = [],
cm = editor._codeMirror,
cursor = getSearchCursor(cm, replaceWhat),
from,
to,
line,
multiLine,
matchResult = cursor.findNext();
// Collect all results from document
while (matchResult) {
from = cursor.from();
to = cursor.to();
line = editor.document.getLine(from.line);
multiLine = from.line !== to.line;
results.push({
index: results.length, // add indexes to array
from: from,
to: to,
line: from.line + 1,
pre: line.slice(0, from.ch),
highlight: line.slice(from.ch, multiLine ? undefined : to.ch),
post: multiLine ? "\u2026" : line.slice(to.ch),
result: matchResult
});
if (results.length >= REPLACE_ALL_MAX) {
break;
}
matchResult = cursor.findNext();
}
// This text contains some formatting, so all the strings are assumed to be already escaped
var resultsLength = results.length,
summary = StringUtils.format(
Strings.FIND_REPLACE_TITLE_PART3,
resultsLength,
resultsLength > 1 ? Strings.FIND_IN_FILES_MATCHES : Strings.FIND_IN_FILES_MATCH,
resultsLength >= REPLACE_ALL_MAX ? Strings.FIND_IN_FILES_MORE_THAN : ""
);
// Insert the search summary
$replaceAllWhat.text(replaceWhat.toString());
$replaceAllWith.text(replaceWith.toString());
$replaceAllSummary.html(summary);
// All checkboxes are checked by default
$replaceAllContainer.find(".check-all").prop("checked", true);
// Attach event to replace button
$replaceAllContainer.find("button.replace-checked").off().on("click", function (e) {
$replaceAllTable.find(".check-one:checked")
.closest(".replace-row")
.toArray()
.reverse()
.forEach(function (checkedRow) {
var match = results[$(checkedRow).data("match")],
rw = typeof replaceWhat === "string" ? replaceWith : parseDollars(replaceWith, match.result);
editor.document.replaceRange(rw, match.from, match.to, "+replaceAll");
});
_closeReplaceAllPanel();
});
// Insert the search results
$replaceAllTable
.empty()
.append(Mustache.render(searchReplaceResultsTemplate, {searchResults: results}))
.off()
.on("click", ".check-one", function (e) {
e.stopPropagation();
})
.on("click", ".replace-row", function (e) {
var match = results[$(e.currentTarget).data("match")];
editor.setSelection(match.from, match.to, true);
});
// we can't safely replace after document has been modified
// this handler is only attached, when replaceAllPanel is visible
currentDocument = DocumentManager.getCurrentDocument();
$(currentDocument).on("change.replaceAll", function () {
_closeReplaceAllPanel();
});
replaceAllPanel.show();
$replaceAllTable.scrollTop(0); // Otherwise scroll pos from previous contents is remembered
}
/** Shows the Find-Replace search bar at top */
function replace(editor) {
// If Replace bar already open, treat the shortcut as a hotkey for the Replace button
var $replaceBtn = $("#replace-yes");
if ($replaceBtn.length) {
if ($replaceBtn.is(":enabled")) {
$replaceBtn.click();
}
return;
}
openSearchBar(editor, {replace: true});
addShortcutToTooltip($("#replace-yes"), Commands.EDIT_REPLACE);
var cm = editor._codeMirror,
state = getSearchState(cm);
function getReplaceWith() {
return $("#replace-with").val() || "";
}
modalBar.getRoot().on("click", function (e) {
if (e.target.id === "replace-yes") {
var text = getReplaceWith();
cm.replaceSelection(typeof state.query === "string" ? text : parseDollars(text, state.lastMatch));
updateResultSet(editor); // we updated the text, so result count & tickmarks must be refreshed
findNext(editor);
if (!state.lastMatch) {
// No more matches, so destroy modalBar
modalBar.close();
}
} else if (e.target.id === "replace-all") {
modalBar.close();
_showReplaceAllPanel(editor, state.query, getReplaceWith());
}
});
// One-off hack to make Find/Replace fields a self-contained tab cycle - TODO: remove once https://trello.com/c/lTSJgOS2 implemented
modalBar.getRoot().on("keydown", function (e) {
if (e.keyCode === KeyEvent.DOM_VK_TAB && !e.ctrlKey && !e.metaKey && !e.altKey) {
if (e.target.id === "replace-with" && !e.shiftKey) {
$("#find-what").focus();
e.preventDefault();
} else if (e.target.id === "find-what" && e.shiftKey) {
$("#replace-with").focus();
e.preventDefault();
}
}
});
}
function _launchFind() {
var editor = EditorManager.getActiveEditor();
if (editor) {
// Create a new instance of the search bar UI
clearSearch(editor._codeMirror);
doSearch(editor, false);
}
}
function _findNext() {
var editor = EditorManager.getActiveEditor();
if (editor) {
doSearch(editor);
}
}
function _findPrevious() {
var editor = EditorManager.getActiveEditor();
if (editor) {
doSearch(editor, true);
}
}
function _replace() {
var editor = EditorManager.getActiveEditor();
if (editor) {
replace(editor);
}
}
PreferencesManager.stateManager.definePreference("caseSensitive", "boolean", false);
PreferencesManager.stateManager.definePreference("regexp", "boolean", false);
PreferencesManager.convertPreferences(module, {"caseSensitive": "user", "regexp": "user"}, true);
// Initialize items dependent on HTML DOM
AppInit.htmlReady(function () {
var panelHtml = Mustache.render(searchReplacePanelTemplate, Strings);
replaceAllPanel = PanelManager.createBottomPanel("findReplace-all.panel", $(panelHtml), 100);
$replaceAllContainer = replaceAllPanel.$panel;
$replaceAllWhat = $replaceAllContainer.find(".replace-what");
$replaceAllWith = $replaceAllContainer.find(".replace-with");
$replaceAllSummary = $replaceAllContainer.find(".replace-summary");
$replaceAllTable = $replaceAllContainer.children(".table-container");
// Attach events to the panel
replaceAllPanel.$panel
.on("click", ".close", function () {
_closeReplaceAllPanel();
})
.on("click", ".check-all", function (e) {
var isChecked = $(this).is(":checked");
replaceAllPanel.$panel.find(".check-one").prop("checked", isChecked);
});
});
$(DocumentManager).on("currentDocumentChange", _handleDocumentChange);
CommandManager.register(Strings.CMD_FIND, Commands.EDIT_FIND, _launchFind);
CommandManager.register(Strings.CMD_FIND_NEXT, Commands.EDIT_FIND_NEXT, _findNext);
CommandManager.register(Strings.CMD_REPLACE, Commands.EDIT_REPLACE, _replace);
CommandManager.register(Strings.CMD_FIND_PREVIOUS, Commands.EDIT_FIND_PREVIOUS, _findPrevious);
// APIs shared with FindInFiles
exports._updatePrefsFromSearchBar = _updatePrefsFromSearchBar;
exports._updateSearchBarFromPrefs = _updateSearchBarFromPrefs;
exports._closeFindBar = _closeFindBar;
exports._registerFindInFilesCloser = _registerFindInFilesCloser;
});