This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Updated: Highlight search feature #2662
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c00dd9a
Highlight All Find Matches in Editor.
soswow e73966e
Fixing JSLint test. Add missing spaces.
soswow b0580db
Removing highlight when last char deleted from search
soswow 0440324
Merge branch 'master' into highlight-search-feature
soswow 8b9c538
Tests for highlight feature and bug fix for /.*/ regexp.
soswow 68b9491
Better tests, yellowish color for matches code refactoring.
soswow 596ae32
Color tweak from GarthDB
soswow 2bdd2d0
Merge branch 'master' into highlight-search-feature
soswow bbdf433
Merge branch 'highlight-search-feature' of https://github.com/soswow/…
peterflynn 979ae12
Find highlighting tweaks:
peterflynn 9a08759
Hack to improve Find highlighting color: use a different selection color
peterflynn 0fbaa2f
Find highlighting fixes:
peterflynn cef6f01
Merge remote-tracking branch 'origin/master' into pflynn/highlight-se…
redmunds 5d5e360
Code review: improve comments; rename selector to match naming conven…
peterflynn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,11 @@ define(function (require, exports, module) { | |
| $(".modal-bar .message").css("display", "inline-block"); | ||
| $(".modal-bar .error").css("display", "none"); | ||
| try { | ||
| return isRE ? new RegExp(isRE[1], isRE[2].indexOf("i") === -1 ? "" : "i") : query; | ||
| if (isRE && isRE[1]) { // non-empty regexp | ||
| return new RegExp(isRE[1], isRE[2].indexOf("i") === -1 ? "" : "i"); | ||
| } else { | ||
| return query; | ||
| } | ||
| } catch (e) { | ||
| $(".modal-bar .message").css("display", "none"); | ||
| $(".modal-bar .error") | ||
|
|
@@ -101,20 +105,22 @@ define(function (require, exports, module) { | |
| return found; | ||
| } | ||
|
|
||
| function clearHighlights(state) { | ||
| state.marked.forEach(function (markedRange) { | ||
| markedRange.clear(); | ||
| }); | ||
| state.marked.length = 0; | ||
| } | ||
|
|
||
| function clearSearch(cm) { | ||
| cm.operation(function () { | ||
| var state = getSearchState(cm), | ||
| i; | ||
| var state = getSearchState(cm); | ||
| if (!state.query) { | ||
| return; | ||
| } | ||
| state.query = null; | ||
|
|
||
| // Clear highlights | ||
| for (i = 0; i < state.marked.length; ++i) { | ||
| state.marked[i].clear(); | ||
| } | ||
| state.marked.length = 0; | ||
|
|
||
| clearHighlights(state); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -158,21 +164,33 @@ define(function (require, exports, module) { | |
| // result, starting from the original cursor position | ||
| function findFirst(query) { | ||
| cm.operation(function () { | ||
| if (!query) { | ||
| return; | ||
| } | ||
|
|
||
| if (state.query) { | ||
| clearSearch(cm); // clear highlights from previous query | ||
| clearHighlights(getSearchState(cm)); | ||
| } | ||
| state.query = parseQuery(query); | ||
| if (!state.query) { | ||
| cm.setCursor(searchStartPos); | ||
| return; | ||
| } | ||
|
|
||
| // Highlight all matches | ||
| // FUTURE: if last query was prefix of this one, could optimize by filtering existing result set | ||
| if (cm.lineCount() < 2000) { // This is too expensive on big documents. | ||
| var cursor = getSearchCursor(cm, query); | ||
| // (Except on huge documents, where this is too expensive) | ||
| if (cm.getValue().length < 500000) { | ||
| // Temporarily change selection color to improve highlighting - see LESS code for details | ||
| $(cm.getWrapperElement()).addClass("find-highlighting"); | ||
|
|
||
| // FUTURE: if last query was prefix of this one, could optimize by filtering existing result set | ||
| var cursor = getSearchCursor(cm, state.query); | ||
| while (cursor.findNext()) { | ||
| state.marked.push(cm.markText(cursor.from(), cursor.to(), "CodeMirror-searching")); | ||
|
|
||
| //Remove this section when https://github.com/marijnh/CodeMirror/issues/1155 will be fixed | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. codemirror/codemirror5#1155 has been closed, so you should be able to remove this code.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only implemented in CMv3 and this is going into master, so we can't remove it yet. Happy to do that as a cleanup next sprint once things settle down, though. |
||
| if (cursor.pos.match && cursor.pos.match[0] === "") { | ||
| if (cursor.to().line + 1 === cm.lineCount()) { | ||
| break; | ||
| } | ||
| cursor = getSearchCursor(cm, state.query, {line: cursor.to().line + 1, ch: 0}); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -202,7 +220,13 @@ define(function (require, exports, module) { | |
| findFirst(query); | ||
| } | ||
| }); | ||
|
|
||
| $(modalBar).on("closeOk closeCancel closeBlur", function (e, query) { | ||
| clearHighlights(state); | ||
|
|
||
| // As soon as focus goes back to the editor, restore normal selection color | ||
| $(cm.getWrapperElement()).removeClass("find-highlighting"); | ||
| }); | ||
|
|
||
| var $input = getDialogTextField(); | ||
| $input.on("input", function () { | ||
| findFirst($input.attr("value")); | ||
|
|
@@ -304,7 +328,7 @@ define(function (require, exports, module) { | |
| if (editor) { | ||
| var codeMirror = editor._codeMirror; | ||
|
|
||
| // Bring up CodeMirror's existing search bar UI | ||
| // Create a new instance of the search bar UI | ||
| clearSearch(codeMirror); | ||
| doSearch(codeMirror, false, codeMirror.getSelection()); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function only uses the state.marked member, so that should be pass in instead. This would save a a lot of dereferencing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The forEach() change you suggested above removes most of the dereferencing already, so I left it passing the full 'state' object for clarity. If we do more complex highlighting later we may need access to more of the state properties anyway.