Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit ab91035

Browse files
committed
Added Search History UI similar to Quick Open
1 parent aef78dd commit ab91035

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

src/search/FindBar.js

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ define(function (require, exports, module) {
3939
Strings = require("strings"),
4040
ViewUtils = require("utils/ViewUtils"),
4141
FindUtils = require("search/FindUtils"),
42+
QuickSearchField = require("search/QuickSearchField").QuickSearchField,
43+
StringUtils = require("utils/StringUtils"),
4244
HealthLogger = require("utils/HealthLogger");
4345

4446
/**
@@ -226,12 +228,55 @@ define(function (require, exports, module) {
226228
$elem.attr("title", oldTitle + "(" + KeyBindingManager.formatKeyDescriptor(replaceShortcut.displayKey) + ")");
227229
}
228230
};
231+
232+
function highlightMatch(item, matchClass, rangeFilter) {
233+
var label = item.label || item;
234+
matchClass = matchClass || "quicksearch-namematch";
235+
236+
var stringRanges = item.stringRanges;
237+
if (!stringRanges) {
238+
// If result didn't come from stringMatch(), highlight nothing
239+
stringRanges = [{
240+
text: label,
241+
matched: false,
242+
includesLastSegment: true
243+
}];
244+
}
245+
246+
var displayName = "";
247+
if (item.scoreDebug) {
248+
var sd = item.scoreDebug;
249+
displayName += '<span title="sp:' + sd.special + ', m:' + sd.match +
250+
', ls:' + sd.lastSegment + ', b:' + sd.beginning +
251+
', ld:' + sd.lengthDeduction + ', c:' + sd.consecutive + ', nsos: ' +
252+
sd.notStartingOnSpecial + ', upper: ' + sd.upper + '">(' + item.matchGoodness + ') </span>';
253+
}
254+
255+
// Put the path pieces together, highlighting the matched parts
256+
stringRanges.forEach(function (range) {
257+
if (range.matched) {
258+
displayName += "<span class='" + matchClass + "'>";
259+
}
260+
261+
var rangeText = rangeFilter ? rangeFilter(range.includesLastSegment, range.text) : range.text;
262+
displayName += StringUtils.breakableUrl(rangeText);
263+
264+
if (range.matched) {
265+
displayName += "</span>";
266+
}
267+
});
268+
return displayName;
269+
}
229270

230271
/**
231272
* Opens the Find bar, closing any other existing Find bars.
232273
*/
233274
FindBar.prototype.open = function () {
234275
var self = this;
276+
277+
//var searchBarHTML = "<div align='right'><input type='text' autocomplete='off' id='' placeholder='" + Strings.CMD_FIND + "\u2026' style='width: 30em'><span class=''></span></div>";
278+
//this.modalBar = new ModalBar(searchBarHTML, true);
279+
var searchHistory = PreferencesManager.getViewState("searchHistory");
235280

236281
// Normally, creating a new Find bar will simply cause the old one to close
237282
// automatically. This can cause timing issues because the focus change might
@@ -262,6 +307,7 @@ define(function (require, exports, module) {
262307
FindBar._removeFindBar(self);
263308
MainViewManager.focusActivePane();
264309
self.trigger("close");
310+
self.searchField.destroy();
265311
});
266312

267313
FindBar._addFindBar(this);
@@ -311,7 +357,7 @@ define(function (require, exports, module) {
311357
if (intervalId === 0) {
312358
intervalId = window.setInterval(executeSearchIfNeeded, 50);
313359
}
314-
var searchHistory = PreferencesManager.getViewState("searchHistory");
360+
315361
var maxCount = PreferencesManager.get("maxSearchHistory");
316362
if (e.keyCode === KeyEvent.DOM_VK_RETURN) {
317363
e.preventDefault();
@@ -324,7 +370,9 @@ define(function (require, exports, module) {
324370
searchHistory.pop();
325371
}
326372
}
327-
searchHistory.unshift($('#find-what').val());
373+
if ($('#find-what').val()) {
374+
searchHistory.unshift($('#find-what').val());
375+
}
328376
PreferencesManager.setViewState("searchHistory", searchHistory);
329377
lastQueriedText = self.getQueryInfo().query;
330378
if (self._options.multifile) {
@@ -347,15 +395,15 @@ define(function (require, exports, module) {
347395
self.trigger("doFind", e.shiftKey);
348396
}
349397
historyIndex = 0;
350-
} else if (e.keyCode === KeyEvent.DOM_VK_DOWN || e.keyCode === KeyEvent.DOM_VK_UP) {
398+
}/* else if (e.keyCode === KeyEvent.DOM_VK_DOWN || e.keyCode === KeyEvent.DOM_VK_UP) {
351399
if (e.keyCode === KeyEvent.DOM_VK_DOWN) {
352400
historyIndex = (historyIndex - 1 + searchHistory.length) % searchHistory.length;
353401
} else {
354402
historyIndex = (historyIndex + 1 + searchHistory.length) % searchHistory.length;
355403
}
356404
$("#find-what").val(searchHistory[historyIndex]);
357405
self.trigger("queryChange");
358-
}
406+
}*/
359407
});
360408

361409
if (!this._options.multifile) {
@@ -401,11 +449,54 @@ define(function (require, exports, module) {
401449
this.showIndexingSpinner();
402450
}
403451

452+
//this.$searchField.focus();
453+
404454
// Set up the initial UI state.
405455
this._updateSearchBarFromPrefs();
406456
this.focusQuery();
457+
//setTimeout(function() {
458+
self.showSearchHints(searchHistory);
459+
//},0);
407460
};
408461

462+
FindBar.prototype.showSearchHints = function (searchHistory) {
463+
var self = this;
464+
this.$searchField = $("input#find-what");
465+
this.searchField = new QuickSearchField(this.$searchField, {
466+
verticalAdjust: this.$searchField.offset().top > 0 ? 0 : this._modalBar.getRoot().outerHeight(),
467+
maxResults: 20,
468+
resultProvider: function (query) {
469+
var asyncResult = new $.Deferred();
470+
var filteredResults = _.filter(searchHistory,
471+
function(s) { return s.indexOf(query) !== -1; }
472+
);
473+
asyncResult.resolve(filteredResults);
474+
return asyncResult.promise();
475+
}, //this._filterCallback,
476+
formatter: function (item, query) {
477+
//query = query.slice(query.indexOf("@") + 1, query.length);
478+
479+
var displayName = highlightMatch(item);
480+
return "<li>" + displayName + "</li>";
481+
}, //this._resultsFormatterCallback,
482+
onCommit: function (selectedItem, query) {
483+
console.log(selectedItem);
484+
console.log(query);
485+
if (selectedItem) {
486+
$("#find-what").val(selectedItem);
487+
self.trigger("queryChange");
488+
} else if (query.length) {
489+
self.searchField.setText(query);
490+
}
491+
//self.trigger("queryChange");
492+
self.searchField.destroy();
493+
},
494+
onHighlight: function (selectedItem, query, explicit) {}
495+
//verticalAdjust: 44
496+
});
497+
this.searchField.setText($("#find-what").val());
498+
}
499+
409500
/**
410501
* Closes this Find bar. If already closed, does nothing.
411502
* @param {boolean} suppressAnimation If true, don't do the standard closing animation. Default false.
@@ -414,6 +505,7 @@ define(function (require, exports, module) {
414505
if (this._modalBar) {
415506
// 1st arg = restore scroll pos; 2nd arg = no animation, since getting replaced immediately
416507
this._modalBar.close(true, !suppressAnimation);
508+
//this.searchField.destroy();
417509
}
418510
};
419511

src/search/QuickSearchField.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,8 @@ define(function (require, exports, module) {
171171
/** Call onCommit() immediately */
172172
QuickSearchField.prototype._doCommit = function (index) {
173173
var item;
174-
if (this._displayedResults && this._displayedResults.length) {
175-
var committedIndex = index !== undefined ? index : (this._highlightIndex || 0);
176-
item = this._displayedResults[committedIndex];
174+
if (this._displayedResults && this._displayedResults.length && this._highlightIndex>=0) {
175+
item = this._displayedResults[this._highlightIndex];
177176
}
178177
this.options.onCommit(item, this._displayedQuery);
179178
};
@@ -264,7 +263,7 @@ define(function (require, exports, module) {
264263
QuickSearchField.prototype._render = function (results, query) {
265264
this._displayedQuery = query;
266265
this._displayedResults = results;
267-
this._highlightIndex = 0;
266+
this._highlightIndex = null;
268267
// TODO: fixup to match prev value's item if possible?
269268

270269
if (results.error || results.length === 0) {

0 commit comments

Comments
 (0)