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
112 lines (90 loc) · 2.64 KB
/
Copy pathfile-search.js
File metadata and controls
112 lines (90 loc) · 2.64 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
/* 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
/**
* File Search reducer
* @module reducers/fileSearch
*/
import makeRecord from "../utils/makeRecord";
import { prefs } from "../utils/prefs";
import type { Action } from "../actions/types";
import type { Record } from "../utils/makeRecord";
export type Modifiers = Record<{
caseSensitive: boolean,
wholeWord: boolean,
regexMatch: boolean
}>;
export type MatchedLocations = {
line: number,
ch: number
};
export type SearchResults = {
matches: Array<MatchedLocations>,
matchIndex: number,
index: number,
count: number
};
export type FileSearchState = {
searchResults: SearchResults,
query: string,
modifiers: Modifiers
};
export const createFileSearchState = makeRecord(
({
query: "",
searchResults: {
matches: [],
matchIndex: -1,
index: -1,
count: 0
},
modifiers: makeRecord({
caseSensitive: prefs.fileSearchCaseSensitive,
wholeWord: prefs.fileSearchWholeWord,
regexMatch: prefs.fileSearchRegexMatch
})()
}: FileSearchState)
);
function update(
state: Record<FileSearchState> = createFileSearchState(),
action: Action
): Record<FileSearchState> {
switch (action.type) {
case "UPDATE_FILE_SEARCH_QUERY": {
return state.set("query", action.query);
}
case "UPDATE_SEARCH_RESULTS": {
return state.set("searchResults", action.results);
}
case "TOGGLE_FILE_SEARCH_MODIFIER": {
const actionVal = !state.getIn(["modifiers", action.modifier]);
if (action.modifier == "caseSensitive") {
prefs.fileSearchCaseSensitive = actionVal;
}
if (action.modifier == "wholeWord") {
prefs.fileSearchWholeWord = actionVal;
}
if (action.modifier == "regexMatch") {
prefs.fileSearchRegexMatch = actionVal;
}
return state.setIn(["modifiers", action.modifier], actionVal);
}
default: {
return state;
}
}
}
// NOTE: we'd like to have the app state fully typed
// https://github.com/devtools-html/debugger.html/blob/master/src/reducers/sources.js#L179-L185
type OuterState = { fileSearch: Record<FileSearchState> };
export function getFileSearchQuery(state: OuterState): string {
return state.fileSearch.get("query");
}
export function getFileSearchModifiers(state: OuterState): Modifiers {
return state.fileSearch.get("modifiers");
}
export function getFileSearchResults(state: OuterState) {
return state.fileSearch.get("searchResults");
}
export default update;