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

Commit 5d45b77

Browse files
darkwingjasonLaster
authored andcommitted
[QuickOpen] Filter file search if a project directory root is set (#5784)
1 parent abdc226 commit 5d45b77

File tree

7 files changed

+95
-27
lines changed

7 files changed

+95
-27
lines changed

src/actions/tests/ui.spec.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { createStore, selectors, actions } from "../../utils/test-head";
1+
import {
2+
createStore,
3+
selectors,
4+
actions,
5+
makeSource
6+
} from "../../utils/test-head";
27

38
const {
49
getActiveSearch,
510
getFrameworkGroupingState,
611
getPaneCollapse,
712
getHighlightedLineRange,
8-
getProjectDirectoryRoot
13+
getProjectDirectoryRoot,
14+
getRelativeSources
915
} = selectors;
1016

1117
describe("ui", () => {
@@ -90,4 +96,20 @@ describe("setProjectDirectoryRoot", () => {
9096
dispatch(actions.setProjectDirectoryRoot("/example.com/bar"));
9197
expect(getProjectDirectoryRoot(getState())).toBe("/example.com/bar");
9298
});
99+
100+
it("should filter sources", async () => {
101+
const store = createStore({});
102+
const { dispatch, getState } = store;
103+
await dispatch(actions.newSource(makeSource("js/scopes.js")));
104+
await dispatch(actions.newSource(makeSource("lib/vendor.js")));
105+
dispatch(actions.setProjectDirectoryRoot("/js"));
106+
const filteredSources = getRelativeSources(getState());
107+
const firstSource = filteredSources[0];
108+
109+
expect(firstSource.url).toEqual(
110+
"http://localhost:8000/examples/js/scopes.js"
111+
);
112+
113+
expect(firstSource.relativeUrl).toEqual("scopes.js");
114+
});
93115
});

src/components/QuickOpenModal.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import React, { Component } from "react";
77
import { connect } from "react-redux";
88
import fuzzyAldrin from "fuzzaldrin-plus";
99
import { basename } from "../utils/path";
10+
1011
import actions from "../actions";
1112
import {
12-
getSources,
13+
getRelativeSources,
1314
getQuickOpenEnabled,
1415
getQuickOpenQuery,
1516
getQuickOpenType,
@@ -418,7 +419,7 @@ function mapStateToProps(state) {
418419

419420
return {
420421
enabled: getQuickOpenEnabled(state),
421-
sources: formatSources(getSources(state)),
422+
sources: formatSources(getRelativeSources(state)),
422423
selectedSource,
423424
symbols: formatSymbols(getSymbols(state, selectedSource)),
424425
symbolsLoading: isSymbolsLoading(state, selectedSource),

src/reducers/ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export type SymbolsMap = Map<string, Symbols>;
3131
export type EmptyLinesMap = Map<string, EmptyLinesType>;
3232

3333
export type SourceMetaDataType = {
34-
framework: ?string
34+
framework: string | void
3535
};
3636

3737
export type SourceMetaDataMap = Map<string, SourceMetaDataType>;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// @flow
2+
3+
import { getProjectDirectoryRoot, getSources } from "../selectors";
4+
import type { State } from "../reducers/types";
5+
import type { Source } from "../types";
6+
import { getSourcePath } from "../utils/source";
7+
8+
export type RelativeSource = Source & {
9+
+relativeUrl: string
10+
};
11+
12+
function getRelativeUrl(url, root) {
13+
if (!root) {
14+
return getSourcePath(url);
15+
}
16+
17+
// + 1 removes the leading "/"
18+
return url.slice(url.indexOf(root) + root.length + 1);
19+
}
20+
21+
function formatSource(source: Source, root): RelativeSource {
22+
return {
23+
...source,
24+
relativeUrl: getRelativeUrl(source.url, root)
25+
};
26+
}
27+
28+
/*
29+
* Gets the sources that are below a project root
30+
*/
31+
export function getRelativeSources(state: State): RelativeSource[] {
32+
const sources = getSources(state);
33+
const root = getProjectDirectoryRoot(state);
34+
35+
return sources
36+
.valueSeq()
37+
.toJS()
38+
.filter(({ url }) => url && url.includes(root))
39+
.map(source => formatSource(source, root));
40+
}

src/selectors/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ export { getVisibleBreakpoints } from "./visibleBreakpoints";
2929
export { isSelectedFrameVisible } from "./isSelectedFrameVisible";
3030
export { getCallStackFrames } from "./getCallStackFrames";
3131
export { getVisibleSelectedFrame } from "./visibleSelectedFrame";
32+
export { getRelativeSources } from "./getRelativeSources";

src/selectors/visibleBreakpoints.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
44

5+
// @flow
6+
57
import { getBreakpoints } from "../reducers/breakpoints";
68
import { getSelectedSource } from "../reducers/sources";
79
import { isGeneratedId } from "devtools-source-map";
810

11+
import type { State } from "../reducers/types";
12+
913
function getLocation(breakpoint, isGeneratedSource) {
1014
return isGeneratedSource
1115
? breakpoint.generatedLocation || breakpoint.location
@@ -33,12 +37,13 @@ function isVisible(breakpoint, selectedSource) {
3337
const location = getLocation(breakpoint, isGeneratedSource);
3438
return location.sourceId === sourceId;
3539
}
40+
3641
/*
3742
* Finds the breakpoints, which appear in the selected source.
3843
*
3944
* This
4045
*/
41-
export function getVisibleBreakpoints(state: OuterState) {
46+
export function getVisibleBreakpoints(state: State) {
4247
const selectedSource = getSelectedSource(state);
4348
if (!selectedSource) {
4449
return null;

src/utils/quick-open.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
// @flow
66
import { endTruncateStr } from "./utils";
7-
import { isPretty, getSourcePath } from "./source";
7+
import { isPretty, getFilename } from "./source";
88

99
import type { Location as BabelLocation } from "@babel/types";
10-
import type { SourcesMap } from "../reducers/sources";
1110
import type { Symbols } from "../reducers/ast";
1211
import type { QuickOpenType } from "../reducers/quick-open";
1312
import type { SymbolDeclaration } from "../workers/parser";
13+
import type { RelativeSource } from "../selectors/getRelativeSources";
1414

1515
export const MODIFIERS = {
1616
"@": "functions",
@@ -49,6 +49,19 @@ export function parseLineColumn(query: string) {
4949
}
5050
}
5151

52+
export function formatSourcesForList(source: RelativeSource) {
53+
const title = getFilename(source);
54+
const subtitle = endTruncateStr(source.relativeUrl, 100);
55+
56+
return {
57+
value: source.relativeUrl,
58+
title,
59+
subtitle,
60+
id: source.id,
61+
url: source.url
62+
};
63+
}
64+
5265
export type QuickOpenResult = {|
5366
id: string,
5467
value: string,
@@ -106,25 +119,11 @@ export function formatShortcutResults(): Array<QuickOpenResult> {
106119
];
107120
}
108121

109-
export function formatSources(sources: SourcesMap): Array<QuickOpenResult> {
122+
export function formatSources(
123+
sources: RelativeSource[]
124+
): Array<QuickOpenResult> {
110125
return sources
111-
.valueSeq()
112126
.filter(source => !isPretty(source))
113-
.map(source => {
114-
const sourcePath = getSourcePath(source.url);
115-
return {
116-
value: sourcePath,
117-
title: sourcePath
118-
.split("/")
119-
.pop()
120-
.split("?")[0],
121-
subtitle: endTruncateStr(sourcePath, 100)
122-
.replace(sourcePath.split("/").pop(), "")
123-
.slice(1, -1),
124-
id: source.id,
125-
url: source.url
126-
};
127-
})
128-
.filter(({ value }) => value != "")
129-
.toJS();
127+
.map(source => formatSourcesForList(source))
128+
.filter(({ value }) => value != "");
130129
}

0 commit comments

Comments
 (0)