Skip to content

Commit 7956302

Browse files
committed
fix: find in files and search ux issues
1 parent ca16b50 commit 7956302

File tree

5 files changed

+81
-13
lines changed

5 files changed

+81
-13
lines changed

src/extensions/default/DarkTheme/main.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
color: @foreground !important;
9494
}
9595

96-
.CodeMirror-matchingtag {
96+
.CodeMirror-matchingtag:not(.CodeMirror-searching) {
9797
/* Ensure visibility against gray inline editor background */
9898
background-color: @matching-tags;
9999
--border-height: calc((var(--editor-line-height) *1em - 1em) / 2);

src/extensions/default/LightTheme/main.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
@matching-bracket: #cfead6;
2626

27-
.CodeMirror-matchingtag {
27+
.CodeMirror-matchingtag:not(.CodeMirror-searching) {
2828
background: @matching-bracket;
2929
--border-height: calc((var(--editor-line-height) *1em - 1em) / 2);
3030
border-top: var(--border-height) solid @matching-bracket;

src/search/FindBar.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ define(function (require, exports, module) {
5353
let intervalId = 0,
5454
lastQueriedText = "",
5555
lastTypedText = "",
56-
lastTypedTextWasRegexp = false;
56+
lastTypedTextWasRegexp = false,
57+
lastClosedQuery = null;
5758
const MAX_HISTORY_RESULTS = 50;
5859
const PREF_MAX_HISTORY = "maxSearchHistory";
5960

@@ -631,6 +632,12 @@ define(function (require, exports, module) {
631632
FindBar.prototype.close = function (suppressAnimation) {
632633
lastQueriedText = "";
633634
if (this._modalBar) {
635+
lastClosedQuery = {
636+
query: this.$("#find-what").val() || "",
637+
replaceText: this.getReplaceText(),
638+
isCaseSensitive: this.$("#find-case-sensitive").is(".active"),
639+
isRegexp: this.$("#find-regexp").is(".active")
640+
};
634641
this._addElementToSearchHistory($("#find-what").val(), $("#fif-filter-input").val());
635642
// 1st arg = restore scroll pos; 2nd arg = no animation, since getting replaced immediately
636643
this._modalBar.close(true, !suppressAnimation);
@@ -656,15 +663,18 @@ define(function (require, exports, module) {
656663
* @return {{query: string, caseSensitive: boolean, isRegexp: boolean}}
657664
*/
658665
FindBar.prototype.getQueryInfo = function (usePlatformLineEndings = true) {
659-
let query = this.$("#find-what").val() || "";
666+
const $findWhat = this.$("#find-what");
667+
const findTextArea = $findWhat[0];
668+
let query = $findWhat.val() || "";
660669
const lineEndings = FileUtils.sniffLineEndings(query);
661670
if (usePlatformLineEndings && lineEndings === FileUtils.LINE_ENDINGS_LF && brackets.platform === "win") {
662671
query = query.replace(/\n/g, "\r\n");
663672
}
664673
return {
665674
query: query,
666675
isCaseSensitive: this.$("#find-case-sensitive").is(".active"),
667-
isRegexp: this.$("#find-regexp").is(".active")
676+
isRegexp: this.$("#find-regexp").is(".active"),
677+
isQueryTextSelected: findTextArea.selectionStart !== findTextArea.selectionEnd
668678
};
669679
};
670680

@@ -851,7 +861,7 @@ define(function (require, exports, module) {
851861
* @private
852862
* @static
853863
* @param {?FindBar} currentFindBar - The currently open Find Bar, if any.
854-
* @param {?Editor} activeEditor - The active editor, if any.
864+
* @param {?Editor} editor - The active editor, if any.
855865
* @return {{query: string, replaceText: string}} An object containing the query and replacement text
856866
* to prepopulate the Find Bar.
857867
*/
@@ -872,11 +882,31 @@ define(function (require, exports, module) {
872882
return !bar.isClosed();
873883
}
874884
);
875-
876-
if (openedFindBar) {
885+
// this happens when the find in files bar is opened and we are trying to open single file search or
886+
// vice versa. we need to detect the other findbar and determine what is the search term to use
887+
888+
//debugger
889+
const currentQueryInfo = openedFindBar && openedFindBar.getQueryInfo();
890+
if(!openedFindBar && selection){
891+
// when no findbar is open, the selected text always takes precedence in both single and multi file
892+
query = selection;
893+
} else if(openedFindBar && selection && currentQueryInfo && !currentQueryInfo.isRegexp && currentQueryInfo.isQueryTextSelected) {
894+
// we are switching between single<>multi file search without the user editing the search text in between
895+
// while there is an active selection, the selection takes precedence.
896+
query = selection;
897+
replaceText = openedFindBar.getReplaceText();
898+
} else if (openedFindBar) {
899+
// there is no selection and we are switching between single<>multi file search, copy the
900+
// current query from the open findbar as is
877901
query = openedFindBar.getQueryInfo().query;
878902
replaceText = openedFindBar.getReplaceText();
903+
} else if (lastClosedQuery) {
904+
// these is no open find bar currently and there is no selection, but there is a last saved query, so
905+
// load the last query. this happenes on all freash search cases apart from the very first time
906+
query = lastClosedQuery.query;
907+
replaceText = lastClosedQuery.replaceText;
879908
} else if (editor) {
909+
// the very first query after app start, nothing to restore.
880910
query = (!lastTypedTextWasRegexp && selection) || lastQueriedText || lastTypedText;
881911
}
882912
}

src/search/FindReplace.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,45 @@ define(function (require, exports, module) {
400400
}
401401
}
402402

403+
function _markCurrentPos(editor) {
404+
var cm = editor._codeMirror;
405+
const pos = editor.getCursorPos(false, "start");
406+
cm.operation(function () {
407+
var state = getSearchState(cm);
408+
clearCurrentMatchHighlight(cm, state);
409+
410+
let curIndex = editor.indexFromPos(pos);
411+
curIndex = curIndex >= 1 ? curIndex-- : 0;
412+
let thisMatch = _getNextMatch(editor, false, editor.posFromIndex(curIndex));
413+
if (thisMatch) {
414+
let previousMatch = _getNextMatch(editor, true, thisMatch.start);
415+
if(previousMatch){
416+
const start = editor.indexFromPos(previousMatch.start), end = editor.indexFromPos(previousMatch.end);
417+
if(curIndex >= start && curIndex <= end) {
418+
thisMatch = previousMatch;
419+
}
420+
}
421+
// Update match index indicators - only possible if we have resultSet saved (missing if FIND_MAX_FILE_SIZE threshold hit)
422+
if (state.resultSet.length) {
423+
_updateFindBarWithMatchInfo(state,
424+
{from: thisMatch.start, to: thisMatch.end}, false);
425+
// Update current-tickmark indicator - only if highlighting enabled (disabled if FIND_HIGHLIGHT_MAX threshold hit)
426+
if (state.marked.length) {
427+
ScrollTrackMarkers.markCurrent(state.matchIndex); // _updateFindBarWithMatchInfo() has updated this index
428+
}
429+
}
430+
431+
// Only mark text with "current match" highlight if search bar still open
432+
if (findBar && !findBar.isClosed()) {
433+
// If highlighting disabled, apply both match AND current-match styles for correct appearance
434+
var curentMatchClassName = state.marked.length ? "searching-current-match" : "CodeMirror-searching searching-current-match";
435+
state.markedCurrent = cm.markText(thisMatch.start, thisMatch.end,
436+
{ className: curentMatchClassName, startStyle: "searching-first", endStyle: "searching-last" });
437+
}
438+
}
439+
});
440+
}
441+
403442
/**
404443
* Selects the next match (or prev match, if searchBackwards==true) starting from either the current position
405444
* (if pos unspecified) or the given position (if pos specified explicitly). The starting position
@@ -574,13 +613,12 @@ define(function (require, exports, module) {
574613
setQueryInfo(state, findBar.getQueryInfo(false));
575614
updateResultSet(editor);
576615

577-
if (state.parsedQuery) {
616+
if(initial){
617+
_markCurrentPos(editor);
618+
} else if (state.parsedQuery && !initial) {
578619
// 3rd arg: prefer to avoid scrolling if result is anywhere within view, since in this case user
579620
// is in the middle of typing, not navigating explicitly; viewport jumping would be distracting.
580621
findNext(editor, false, true, state.searchStartPos);
581-
} else if (!initial) {
582-
// Blank or invalid query: just jump back to initial pos
583-
editor._codeMirror.setCursor(state.searchStartPos);
584622
}
585623

586624
editor.lastParsedQuery = state.parsedQuery;

src/styles/brackets_codemirror_override.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ div.CodeMirror-cursors {
271271
}
272272
}
273273

274-
.CodeMirror-matchingtag {
274+
.CodeMirror-matchingtag:not(.CodeMirror-searching) {
275275
background: @matching-bracket;
276276
}
277277
}

0 commit comments

Comments
 (0)