forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-search.js
More file actions
201 lines (168 loc) · 4.79 KB
/
file-search.js
File metadata and controls
201 lines (168 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import {
clearSearch,
find,
findNext,
findPrev,
removeOverlay,
searchSourceForHighlight
} from "../utils/editor";
import { isWasm, renderWasmText } from "../utils/wasm";
import { getMatches } from "../workers/search";
import type { Action, FileTextSearchModifier, ThunkArgs } from "./types";
import type { WasmSource } from "../types";
import {
getSelectedSource,
getFileSearchModifiers,
getFileSearchQuery,
getFileSearchResults
} from "../selectors";
import {
closeActiveSearch,
clearHighlightLineRange,
setActiveSearch
} from "./ui";
type Editor = Object;
type Match = Object;
export function doSearch(query: string, editor: Editor) {
return ({ getState, dispatch }: ThunkArgs) => {
const selectedSource = getSelectedSource(getState());
if (!selectedSource || !selectedSource.text) {
return;
}
dispatch(setFileSearchQuery(query));
dispatch(searchContents(query, editor));
};
}
export function doSearchForHighlight(
query: string,
editor: Editor,
line: number,
ch: number
) {
return async ({ getState, dispatch }: ThunkArgs) => {
const selectedSource = getSelectedSource(getState());
if (!selectedSource || !selectedSource.text) {
return;
}
dispatch(searchContentsForHighlight(query, editor, line, ch));
};
}
export function setFileSearchQuery(query: string): Action {
return {
type: "UPDATE_FILE_SEARCH_QUERY",
query
};
}
export function toggleFileSearchModifier(
modifier: FileTextSearchModifier
): Action {
return { type: "TOGGLE_FILE_SEARCH_MODIFIER", modifier };
}
export function updateSearchResults(
characterIndex: number,
line: number,
matches: Match[]
): Action {
const matchIndex = matches.findIndex(
elm => elm.line === line && elm.ch === characterIndex
);
return {
type: "UPDATE_SEARCH_RESULTS",
results: {
matches,
matchIndex,
count: matches.length,
index: characterIndex
}
};
}
export function searchContents(query: string, editor: Object) {
return async ({ getState, dispatch }: ThunkArgs) => {
const modifiers = getFileSearchModifiers(getState());
const selectedSource = getSelectedSource(getState());
if (!editor || !selectedSource || !selectedSource.text || !modifiers) {
return;
}
const ctx = { ed: editor, cm: editor.codeMirror };
if (!query) {
clearSearch(ctx.cm, query);
return;
}
const _modifiers = modifiers.toJS();
let text = selectedSource.text;
if (isWasm(selectedSource.id)) {
text = renderWasmText(((selectedSource: any): WasmSource)).join("\n");
}
const matches = await getMatches(query, text, _modifiers);
const res = find(ctx, query, true, _modifiers);
if (!res) {
return;
}
const { ch, line } = res;
dispatch(updateSearchResults(ch, line, matches));
};
}
export function searchContentsForHighlight(
query: string,
editor: Object,
line: number,
ch: number
) {
return async ({ getState, dispatch }: ThunkArgs) => {
const modifiers = getFileSearchModifiers(getState());
const selectedSource = getSelectedSource(getState());
if (
!query ||
!editor ||
!selectedSource ||
!selectedSource.text ||
!modifiers
) {
return;
}
const ctx = { ed: editor, cm: editor.codeMirror };
const _modifiers = modifiers.toJS();
searchSourceForHighlight(ctx, false, query, true, _modifiers, line, ch);
};
}
export function traverseResults(rev: boolean, editor: Editor) {
return async ({ getState, dispatch }: ThunkArgs) => {
if (!editor) {
return;
}
const ctx = { ed: editor, cm: editor.codeMirror };
const query = getFileSearchQuery(getState());
const modifiers = getFileSearchModifiers(getState());
const { matches } = getFileSearchResults(getState());
if (query === "") {
dispatch(setActiveSearch("file"));
}
if (modifiers) {
const matchedLocations = matches || [];
const results = rev
? findPrev(ctx, query, true, modifiers.toJS())
: findNext(ctx, query, true, modifiers.toJS());
if (!results) {
return;
}
const { ch, line } = results;
dispatch(updateSearchResults(ch, line, matchedLocations));
}
};
}
export function closeFileSearch(editor: Editor) {
return ({ getState, dispatch }: ThunkArgs) => {
if (editor) {
const query = getFileSearchQuery(getState());
const ctx = { ed: editor, cm: editor.codeMirror };
removeOverlay(ctx, query);
}
dispatch(setFileSearchQuery(""));
dispatch(closeActiveSearch());
dispatch(clearHighlightLineRange());
};
}