forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisibleColumnBreakpoints.js
More file actions
188 lines (159 loc) · 4.94 KB
/
Copy pathvisibleColumnBreakpoints.js
File metadata and controls
188 lines (159 loc) · 4.94 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
/* 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 { groupBy } from "lodash";
import { createSelector } from "reselect";
import {
getViewport,
getSource,
getSelectedSource,
getBreakpointPositions,
getBreakpointPositionsForSource
} from "../selectors";
import { getVisibleBreakpoints } from "./visibleBreakpoints";
import { getSelectedLocation } from "../utils/selected-location";
import { sortSelectedLocations } from "../utils/location";
import type { Selector, State } from "../reducers/types";
import type {
SourceLocation,
PartialPosition,
Breakpoint,
Range,
BreakpointPositions,
BreakpointPosition,
Source
} from "../types";
export type ColumnBreakpoint = {|
+location: SourceLocation,
+breakpoint: ?Breakpoint
|};
export type ColumnBreakpoints = Array<ColumnBreakpoint>;
function contains(location: PartialPosition, range: Range) {
return (
location.line >= range.start.line &&
location.line <= range.end.line &&
(!location.column ||
(location.column >= range.start.column &&
location.column <= range.end.column))
);
}
function groupBreakpoints(breakpoints, selectedSource) {
if (!breakpoints) {
return {};
}
const map: any = groupBy(
breakpoints.filter(breakpoint => !breakpoint.options.hidden),
breakpoint => getSelectedLocation(breakpoint, selectedSource).line
);
for (const line in map) {
map[line] = groupBy(
map[line],
breakpoint => getSelectedLocation(breakpoint, selectedSource).column
);
}
return map;
}
function findBreakpoint(location, breakpointMap) {
const { line, column } = location;
const breakpoints = breakpointMap[line] && breakpointMap[line][column];
if (breakpoints) {
return breakpoints[0];
}
}
function filterByLineCount(positions, selectedSource) {
const lineCount = {};
for (const breakpoint of positions) {
const { line } = getSelectedLocation(breakpoint, selectedSource);
if (!lineCount[line]) {
lineCount[line] = 0;
}
lineCount[line] = lineCount[line] + 1;
}
return positions.filter(
breakpoint =>
lineCount[getSelectedLocation(breakpoint, selectedSource).line] > 1
);
}
function filterVisible(positions, selectedSource, viewport) {
return positions.filter(columnBreakpoint => {
const location = getSelectedLocation(columnBreakpoint, selectedSource);
return viewport && contains(location, viewport);
});
}
function filterByBreakpoints(positions, selectedSource, breakpointMap) {
return positions.filter(position => {
const location = getSelectedLocation(position, selectedSource);
return breakpointMap[location.line];
});
}
function formatPositions(
positions: BreakpointPosition[],
selectedSource,
breakpointMap
) {
return (positions: any).map((position: BreakpointPosition) => {
const location = getSelectedLocation(position, selectedSource);
return {
location,
breakpoint: findBreakpoint(location, breakpointMap)
};
});
}
function convertToList(
breakpointPositions: BreakpointPositions
): BreakpointPosition[] {
return ([].concat(...Object.values(breakpointPositions)): any);
}
export function getColumnBreakpoints(
positions: ?BreakpointPositions,
breakpoints: ?(Breakpoint[]),
viewport: Range,
selectedSource: ?Source
) {
if (!positions) {
return [];
}
// We only want to show a column breakpoint if several conditions are matched
// - it is the first breakpoint to appear at an the original location
// - the position is in the current viewport
// - there is atleast one other breakpoint on that line
// - there is a breakpoint on that line
const breakpointMap = groupBreakpoints(breakpoints, selectedSource);
let newPositions = convertToList(positions);
newPositions = filterByLineCount(newPositions, selectedSource);
newPositions = filterVisible(newPositions, selectedSource, viewport);
newPositions = filterByBreakpoints(
newPositions,
selectedSource,
breakpointMap
);
return formatPositions(newPositions, selectedSource, breakpointMap);
}
const getVisibleBreakpointPositions = createSelector(
getSelectedSource,
getBreakpointPositions,
(source, positions) => source && positions[source.id]
);
export const visibleColumnBreakpoints: Selector<
ColumnBreakpoints
> = createSelector(
getVisibleBreakpointPositions,
getVisibleBreakpoints,
getViewport,
getSelectedSource,
getColumnBreakpoints
);
export function getFirstBreakpointPosition(
state: State,
{ line, sourceId }: SourceLocation
): ?BreakpointPosition {
const positions = getBreakpointPositionsForSource(state, sourceId);
const source = getSource(state, sourceId);
if (!source || !positions) {
return;
}
return sortSelectedLocations(convertToList(positions), source).find(
position => getSelectedLocation(position, source).line == line
);
}