Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 2e57a86

Browse files
Alexis DeschampsjasonLaster
authored andcommitted
Started highlighting the searched terms when clicking on a ProjectSearch result (#5854)
* Started highlighting the searched terms when clicking on a ProjectSearch result. * Update jest
1 parent 47e3ae8 commit 2e57a86

File tree

8 files changed

+139
-10
lines changed

8 files changed

+139
-10
lines changed

src/actions/file-search.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
// @flow
66

7-
import { find, findNext, findPrev, removeOverlay } from "../utils/editor";
7+
import {
8+
find,
9+
findNext,
10+
findPrev,
11+
removeOverlay,
12+
searchSourceForHighlight
13+
} from "../utils/editor";
814
import { getMatches } from "../workers/search";
915
import type { ThunkArgs } from "./types";
1016

@@ -35,6 +41,21 @@ export function doSearch(query: string, editor: Editor) {
3541
};
3642
}
3743

44+
export function doSearchForHighlight(
45+
query: string,
46+
editor: Editor,
47+
line: number,
48+
ch: number
49+
) {
50+
return async ({ getState, dispatch }: ThunkArgs) => {
51+
const selectedSource = getSelectedSource(getState());
52+
if (!selectedSource || !selectedSource.text) {
53+
return;
54+
}
55+
dispatch(searchContentsForHighlight(query, editor, line, ch));
56+
};
57+
}
58+
3859
export function setFileSearchQuery(query: string) {
3960
return {
4061
type: "UPDATE_FILE_SEARCH_QUERY",
@@ -96,6 +117,33 @@ export function searchContents(query: string, editor: Object) {
96117
};
97118
}
98119

120+
export function searchContentsForHighlight(
121+
query: string,
122+
editor: Object,
123+
line: number,
124+
ch: number
125+
) {
126+
return async ({ getState, dispatch }: ThunkArgs) => {
127+
const modifiers = getFileSearchModifiers(getState());
128+
const selectedSource = getSelectedSource(getState());
129+
130+
if (
131+
!query ||
132+
!editor ||
133+
!selectedSource ||
134+
!selectedSource.text ||
135+
!modifiers
136+
) {
137+
return;
138+
}
139+
140+
const ctx = { ed: editor, cm: editor.codeMirror };
141+
const _modifiers = modifiers.toJS();
142+
143+
searchSourceForHighlight(ctx, false, query, true, _modifiers, line, ch);
144+
};
145+
}
146+
99147
export function traverseResults(rev: boolean, editor: Editor) {
100148
return async ({ getState, dispatch }: ThunkArgs) => {
101149
if (!editor) {

src/actions/navigation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// @flow
66

7-
import { clearDocuments } from "../utils/editor";
7+
import { clearDocuments, removeEditor } from "../utils/editor";
88
import sourceQueue from "../utils/source-queue";
99
import { getSources } from "../reducers/sources";
1010
import { waitForMs } from "../utils/utils";
@@ -37,6 +37,7 @@ export function willNavigate(event: Object) {
3737
await sourceMaps.clearSourceMaps();
3838
clearWasmStates();
3939
clearDocuments();
40+
removeEditor();
4041
clearSymbols();
4142
clearASTs();
4243
clearScopes();

src/components/Editor/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
getCursorLine,
5454
toSourceLine,
5555
getDocument,
56+
setEditor,
5657
scrollToColumn,
5758
toEditorPosition,
5859
getSourceLocationFromMouseEvent,
@@ -177,6 +178,7 @@ class Editor extends PureComponent<Props, State> {
177178
}
178179

179180
this.setState({ editor });
181+
setEditor(editor);
180182
return editor;
181183
}
182184

src/components/ProjectSearch.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import classnames from "classnames";
1111
import { bindActionCreators } from "redux";
1212
import actions from "../actions";
1313

14+
import { getEditor } from "../utils/editor";
1415
import { highlightMatches } from "../utils/project-search";
1516

1617
import { statusType } from "../reducers/project-text-search";
@@ -31,6 +32,7 @@ import type { List } from "immutable";
3132
import type { Location } from "../types";
3233
import type { ActiveSearchType } from "../reducers/types";
3334
import type { StatusType } from "../reducers/project-text-search";
35+
type Editor = ?Object;
3436

3537
import "./ProjectSearch.css";
3638

@@ -68,7 +70,13 @@ type Props = {
6870
closeProjectSearch: () => void,
6971
searchSources: (query: string) => void,
7072
clearSearch: () => void,
71-
selectLocation: (location: Location, tabIndex?: string) => void
73+
selectLocation: (location: Location, tabIndex?: string) => void,
74+
doSearchForHighlight: (
75+
query: string,
76+
editor: Editor,
77+
line: number,
78+
column: number
79+
) => void
7280
};
7381

7482
function getFilePath(item: Item, index?: number) {
@@ -133,6 +141,12 @@ export class ProjectSearch extends Component<Props, State> {
133141

134142
selectMatchItem = (matchItem: Match) => {
135143
this.props.selectLocation({ ...matchItem });
144+
this.props.doSearchForHighlight(
145+
this.state.inputValue,
146+
getEditor(),
147+
matchItem.line,
148+
matchItem.column
149+
);
136150
};
137151

138152
getResults = (): Result[] => {

src/components/tests/ProjectSearch.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function render(overrides = {}, mounted = false) {
5656
clearSearch: jest.fn(),
5757
updateSearchStatus: jest.fn(),
5858
selectLocation: jest.fn(),
59+
doSearchForHighlight: jest.fn(),
5960
...overrides
6061
};
6162

src/components/tests/__snapshots__/ProjectSearch.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ exports[`ProjectSearch found search results 1`] = `
4141
activeSearch="project"
4242
clearSearch={[Function]}
4343
closeProjectSearch={[Function]}
44+
doSearchForHighlight={[Function]}
4445
query="match"
4546
results={
4647
Immutable.List [

src/utils/editor/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ import { isOriginalId } from "devtools-source-map";
1919
import type { AstLocation } from "../../workers/parser";
2020
import type { EditorPosition, EditorRange } from "../editor/types";
2121
import type { Location } from "../../types";
22+
type Editor = Object;
23+
24+
let editor: ?Editor;
25+
26+
export function setEditor(_editor: Editor) {
27+
editor = _editor;
28+
}
29+
30+
export function getEditor() {
31+
return editor;
32+
}
33+
34+
export function removeEditor() {
35+
editor = null;
36+
}
2237

2338
export function shouldShowPrettyPrint(selectedSource) {
2439
if (!selectedSource) {
@@ -122,21 +137,21 @@ function isVisible(codeMirror: any, top: number, left: number) {
122137
return inXView && inYView;
123138
}
124139

125-
export function markText(editor: any, className, { start, end }: EditorRange) {
126-
return editor.codeMirror.markText(
140+
export function markText(_editor: any, className, { start, end }: EditorRange) {
141+
return _editor.codeMirror.markText(
127142
{ ch: start.column, line: start.line },
128143
{ ch: end.column, line: end.line },
129144
{ className }
130145
);
131146
}
132147

133-
export function lineAtHeight(editor, sourceId, event) {
134-
const editorLine = editor.codeMirror.lineAtHeight(event.clientY);
135-
return toSourceLine(sourceId, editorLine);
148+
export function lineAtHeight(_editor, sourceId, event) {
149+
const _editorLine = _editor.codeMirror.lineAtHeight(event.clientY);
150+
return toSourceLine(sourceId, _editorLine);
136151
}
137152

138-
export function getSourceLocationFromMouseEvent(editor, selectedLocation, e) {
139-
const { line, ch } = editor.codeMirror.coordsChar({
153+
export function getSourceLocationFromMouseEvent(_editor, selectedLocation, e) {
154+
const { line, ch } = _editor.codeMirror.coordsChar({
140155
left: e.clientX,
141156
top: e.clientY
142157
});

src/utils/editor/source-search.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,31 @@ function doSearch(ctx, rev, query, keepSelection, modifiers: SearchModifiers) {
157157
});
158158
}
159159

160+
export function searchSourceForHighlight(
161+
ctx: Object,
162+
rev: boolean,
163+
query: string,
164+
keepSelection: boolean,
165+
modifiers: SearchModifiers,
166+
line: number,
167+
ch: number
168+
) {
169+
const { cm } = ctx;
170+
if (!cm) {
171+
return;
172+
}
173+
174+
return cm.operation(function() {
175+
const state = getSearchState(cm, query);
176+
const isNewQuery = state.query !== query;
177+
state.query = query;
178+
179+
updateOverlay(cm, state, query, modifiers);
180+
updateCursor(cm, state, keepSelection);
181+
findNextOnLine(ctx, rev, query, isNewQuery, modifiers, line, ch);
182+
});
183+
}
184+
160185
function getCursorPos(newQuery, rev, state) {
161186
if (newQuery) {
162187
return rev ? state.posFrom : state.posTo;
@@ -208,6 +233,28 @@ function searchNext(ctx, rev, query, newQuery, modifiers) {
208233
return nextMatch;
209234
}
210235

236+
function findNextOnLine(ctx, rev, query, newQuery, modifiers, line, ch) {
237+
const { cm, ed } = ctx;
238+
cm.operation(function() {
239+
const pos = { line: line - 1, ch };
240+
let cursor = getSearchCursor(cm, query, pos, modifiers);
241+
242+
if (!cursor.find(rev) && query) {
243+
cursor = getSearchCursor(cm, query, pos, modifiers);
244+
if (!cursor.find(rev)) {
245+
return;
246+
}
247+
}
248+
249+
// We don't want to jump the editor
250+
// when we're selecting text
251+
if (!cm.state.selectingText) {
252+
ed.alignLine(cursor.from().line, "center");
253+
cm.setSelection(cursor.from(), cursor.to());
254+
}
255+
});
256+
}
257+
211258
/**
212259
* Remove overlay.
213260
*

0 commit comments

Comments
 (0)