Show index during search traversal. #3230
Show index during search traversal. #3230
Conversation
d7ed24a
to
e5e06d1
Compare
Codecov Report
@@ Coverage Diff @@
## master #3230 +/- ##
==========================================
+ Coverage 49.35% 49.41% +0.06%
==========================================
Files 99 99
Lines 4156 4159 +3
Branches 856 858 +2
==========================================
+ Hits 2051 2055 +4
+ Misses 2105 2104 -1
Continue to review full report at Codecov.
|
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.
some quick thoughts
src/components/Editor/SearchBar.js
Outdated
const newIndex = findFnc(ctx, query, true, modifiers.toJS()); | ||
const findPos = findFnc(ctx, query, true, modifiers.toJS()); | ||
const { ch: newIndex } = findPos; | ||
const matchedLocationIndex = matchedLocations |
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.
It might be nice to move this function to a helper: getSearchIndex(currentPos, matchedLocations)
src/components/Editor/SearchBar.js
Outdated
count | ||
matchedLocations, | ||
matchedLocationIndex, | ||
count: matchedLocations.length, |
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.
could matchedLocations
be matches
with a type definition that makes it clear what is stored?
@@ -0,0 +1,21 @@ | |||
import buildQuery from "./build-query"; |
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.
we might want to add this function to the search worker so that it never slows down the main process:
diff --git a/src/utils/search/index.js b/src/utils/search/index.js
index 2ec930c..fcb55bb 100644
--- a/src/utils/search/index.js
+++ b/src/utils/search/index.js
@@ -6,3 +6,4 @@ export const startSearchWorker = dispatcher.start.bind(dispatcher);
export const stopSearchWorker = dispatcher.stop.bind(dispatcher);
export const countMatches = dispatcher.task("countMatches");
+export const getMatches = dispatcher.task("getMatches");
diff --git a/src/utils/search/worker.js b/src/utils/search/worker.js
index dbba6c1..75f7b2c 100644
--- a/src/utils/search/worker.js
+++ b/src/utils/search/worker.js
@@ -1,4 +1,6 @@
import buildQuery from "./utils/build-query";
+import getMatches from "./getMatches";
+
import { workerUtils } from "devtools-utils";
const { workerHandler } = workerUtils;
@@ -14,4 +16,4 @@ export function countMatches(
return match ? match.length : 0;
}
-self.onmessage = workerHandler({ countMatches });
+self.onmessage = workerHandler({ countMatches, getMatches });
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.
👍
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.
LGTM. I was planning to do some opts as next step.
- Use transfer objects to transfer matches from worker to main
- Make next and previous to be O(1) operation if cursor position has not changed from last search.
Thanks for taking care of the CL. I could not get to it quickly :(.
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.
No problem! Maybe we can do it in a second pr?
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.
A few comments! need to confirm a few things so this might change
src/components/Editor/SearchBar.js
Outdated
@@ -490,10 +496,19 @@ class SearchBar extends Component { | |||
|
|||
if (modifiers) { | |||
const findFnc = rev ? findPrev : findNext; | |||
const newIndex = findFnc(ctx, query, true, modifiers.toJS()); | |||
const findPos = findFnc(ctx, query, true, modifiers.toJS()); |
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 code (lines 499 - 511) is very similar to lines 436-447, perhaps we can consolidate them into a function that is called from both locations.
src/components/Editor/SearchBar.js
Outdated
@@ -433,9 +433,15 @@ class SearchBar extends Component { | |||
clearIndex(ctx, query, modifiers.toJS()); | |||
} | |||
|
|||
const newIndex = find(ctx, query, true, modifiers.toJS()); | |||
const findPos = find(ctx, query, true, modifiers.toJS()); | |||
const { ch: newIndex } = findPos; |
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.
nit (can be taken care of while consolidating the lines): the naming here is confusing. newIndex is more descriptive than ch, but we don't really need this as an alias, especially since we use findPos.ch on line 439. findPos
might also be misleading, since it uses a verb and sounds like it should be a function. Maybe we should choose one way or the other. At the moment I am leaning towards not using newIndex as an alias, and just sticking to const { ch, line } = find(ctx, query, true, modifiers.toJS())
then we can sidestep the whole issue :)
src/utils/editor/source-search.js
Outdated
|
||
matchIndex = searchLocation ? searchLocation.from : null; |
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.
maybe we can write doSearch
like this? we will also be able to check and expect that there is always a { ch: number, line: number }
flowtype being returned. Otherwise, we will end up with sometimes { ch, line }
sometimes null
, which will complicate things for functions relying on this one, and makes it difficult to reason about what is going on.
Also, changed some of the let
declarations to const
as they were not being reassigned, and made some changes to naming.
function doSearch(ctx, rev, query, keepSelection, modifiers: SearchModifiers) {
const { cm } = ctx;
const defaultIndex = { line: -1, ch: -1 };
let matchIndex = null;
cm.operation(function () {
if (!query || isWhitespace(query)) {
return;
}
const state = getSearchState(cm, query, modifiers);
const isNewQuery = state.query !== query;
state.query = query;
updateOverlay(cm, state, query, modifiers);
updateCursor(cm, state, keepSelection);
const searchLocation = searchNext(ctx, rev, query, isNewQuery, modifiers);
matchIndex = searchLocation ? searchLocation.from : defaultIndex;
state.matchIndex = matchIndex.ch;
});
return matchIndex || defaultIndex;
}
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.
looks reasonable to me
src/reducers/ui.js
Outdated
@@ -20,6 +20,8 @@ export type FileSearchModifiers = Record<{ | |||
export type SymbolSearchType = "functions" | "variables"; | |||
|
|||
export type SearchResults = { | |||
matchedLocations: Array<Object>, |
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.
👍 great! more flowtypes!
src/utils/editor/source-search.js
Outdated
// NOTE: We would like to find the correct match index based on where the | ||
// match is in the document. | ||
state.matchIndex = matchIndex; | ||
state.matchIndex = matchIndex ? matchIndex.ch : -1; |
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.
its strange that the matchIndex diverges here... maybe we should refactor to keep them in sync? Also, we might want to take a look at findNext, findPrev, to make sure that their functionality is not broken.
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.
I noticed that state.matchIndex is not used anywhere in the codebase... we are using the return value of doSearch instead. Should I remove it?
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.
sure
src/components/Editor/SearchBar.js
Outdated
@@ -423,7 +423,7 @@ class SearchBar extends Component { | |||
|
|||
const ctx = { ed, cm: ed.codeMirror }; | |||
|
|||
const newCount = await countMatches( | |||
const matchedLocations = await countMatches( |
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 function name countMatches
no longer reflects what it is doing. we want either a new function or rename this function to getMatches
.
ab0c945
to
ddb9eec
Compare
Rebased! added a couple of tweeks. Should be ready to go, @jasonLaster, @zimbabao can you take one more look? |
src/components/Editor/SearchBar.js
Outdated
@@ -67,6 +61,8 @@ class SearchBar extends Component { | |||
selectedSource?: SourceRecord, | |||
highlightLineRange: ({ start: number, end: number }) => void, | |||
clearHighlightLineRange: () => void, | |||
symbolSearchOn?: boolean, | |||
symbolSearchResults: Array<any>, |
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.
I'm a bit confused by why we need symbolSearchResults
and searchResults
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.
did we remove symbolSearch? I can no longer trigger the buttons...
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.
take a look at master. We removed this stuff when we added the search modal.
Must just need a quick cleanup
src/components/Editor/Tabs.js
Outdated
togglePrettyPrint: string => void, | ||
togglePaneCollapse: () => void, | ||
toggleActiveSearch: (?string) => void, |
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.
why do we need toggleProjectSearch
and toggleActiveSearch
?
let state = getSearchState(cm, query, modifiers); | ||
const newQuery = state.query != query; | ||
const state = getSearchState(cm, query, modifiers); | ||
const isNewQuery = state.query !== query; |
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.
👍
ddb9eec
to
213b067
Compare
src/components/Editor/SearchBar.js
Outdated
@@ -67,6 +61,8 @@ class SearchBar extends Component { | |||
selectedSource?: SourceRecord, | |||
highlightLineRange: ({ start: number, end: number }) => void, | |||
clearHighlightLineRange: () => void, | |||
symbolSearchOn?: boolean, | |||
symbolSearchResults: Array<any>, |
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.
take a look at master. We removed this stuff when we added the search modal.
Must just need a quick cleanup
Associated Issue: #3204
Summary of Changes
Shows the index of search
This is bit naive version, easy optimization yet to be done.
Transfer of data from worker can be optimized by using transfer objects.