forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallSites.js
More file actions
231 lines (190 loc) · 5.86 KB
/
Copy pathCallSites.js
File metadata and controls
231 lines (190 loc) · 5.86 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* 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/>. */
import React, { Component } from "react";
import { connect } from "react-redux";
import { range, keyBy, isEqualWith } from "lodash";
import CallSite from "./CallSite";
import {
getSelectedSource,
getSymbols,
getSelectedLocation,
getBreakpointsForSource
} from "../../selectors";
import { getTokenLocation } from "../../utils/editor";
import { isWasm } from "../../utils/wasm";
import actions from "../../actions";
function getCallSiteAtLocation(callSites, location) {
return callSites.find(callSite =>
isEqualWith(callSite.location, location, (cloc, loc) => {
return (
loc.line === cloc.start.line &&
(loc.column >= cloc.start.column && loc.column <= cloc.end.column)
);
})
);
}
class CallSites extends Component {
props: {
symbols: Array<Symbol>,
callSites: Array<Symbol>,
editor: Object,
breakpoints: Map,
addBreakpoint: Function,
removeBreakpoint: Function,
selectedSource: Object,
selectedLocation: Object
};
constructor(props) {
super(props);
this.state = {
showCallSites: false
};
}
componentDidMount() {
const { editor } = this.props;
const codeMirrorWrapper = editor.codeMirror.getWrapperElement();
codeMirrorWrapper.addEventListener("click", e => this.onTokenClick(e));
document.body.addEventListener("keydown", this.onKeyDown);
document.body.addEventListener("keyup", this.onKeyUp);
}
componentWillUnmount() {
const { editor } = this.props;
const codeMirrorWrapper = editor.codeMirror.getWrapperElement();
codeMirrorWrapper.removeEventListener("click", e => this.onTokenClick(e));
document.body.removeEventListener("keydown", this.onKeyDown);
document.body.removeEventListener("keyup", this.onKeyUp);
}
onKeyUp = e => {
if (e.key === "Alt") {
e.preventDefault();
this.setState({ showCallSites: false });
}
};
onKeyDown = e => {
if (e.key === "Alt") {
e.preventDefault();
this.setState({ showCallSites: true });
}
};
onTokenClick(e) {
const { target } = e;
const { editor, selectedLocation } = this.props;
if (
(!e.altKey && !target.classList.contains("call-site-bp")) ||
(!target.classList.contains("call-site") &&
!target.classList.contains("call-site-bp"))
) {
return;
}
const { sourceId } = selectedLocation;
const { line, column } = getTokenLocation(editor.codeMirror, target);
this.toggleBreakpoint(line, isWasm(sourceId) ? undefined : column);
}
toggleBreakpoint(line, column = undefined) {
const {
selectedSource,
selectedLocation,
addBreakpoint,
removeBreakpoint,
callSites
} = this.props;
const callSite = getCallSiteAtLocation(callSites, { line, column });
if (!callSite) {
return;
}
const bp = callSite.breakpoint;
if ((bp && bp.loading) || !selectedLocation || !selectedSource) {
return;
}
const { sourceId } = selectedLocation;
if (bp) {
// NOTE: it's possible the breakpoint has slid to a column
column = column || bp.location.column;
removeBreakpoint({
sourceId: sourceId,
line: line,
column
});
} else {
addBreakpoint({
sourceId: sourceId,
sourceUrl: selectedSource.get("url"),
line: line,
column: column
});
}
}
render() {
const { editor, callSites, selectedSource } = this.props;
const { showCallSites } = this.state;
let sites;
if (!callSites) {
return null;
}
editor.codeMirror.operation(() => {
const childCallSites = callSites.map((callSite, index) => {
const props = {
key: index,
callSite,
editor,
source: selectedSource,
breakpoint: callSite.breakpoint,
showCallSite: showCallSites
};
return <CallSite {...props} />;
});
sites = <div>{childCallSites}</div>;
});
return sites;
}
}
function getCallSites(symbols, breakpoints) {
if (!symbols || !symbols.callExpressions) {
return;
}
const callSites = symbols.callExpressions;
// NOTE: we create a breakpoint map keyed on location
// to speed up the lookups. Hopefully we'll fix the
// inconsistency with column offsets so that we can expect
// a breakpoint to be added at the beginning of a call expression.
const bpLocationMap = keyBy(breakpoints.valueSeq().toJS(), ({ location }) =>
locationKey(location)
);
function locationKey({ line, column }) {
return `${line}/${column}`;
}
function findBreakpoint(callSite) {
const { location: { start, end } } = callSite;
const breakpointId = range(start.column - 1, end.column)
.map(column => locationKey({ line: start.line, column }))
.find(key => bpLocationMap[key]);
if (breakpointId) {
return bpLocationMap[breakpointId];
}
}
return callSites
.filter(({ location }) => location.start.line === location.end.line)
.map(callSite => ({ ...callSite, breakpoint: findBreakpoint(callSite) }));
}
const { addBreakpoint, removeBreakpoint } = actions;
export default connect(
state => {
const selectedLocation = getSelectedLocation(state);
const selectedSource = getSelectedSource(state);
const sourceId = selectedLocation && selectedLocation.sourceId;
const source = selectedSource && selectedSource.toJS();
const symbols = getSymbols(state, source);
const breakpoints = getBreakpointsForSource(state, sourceId);
return {
selectedLocation,
selectedSource,
callSites: getCallSites(symbols, breakpoints),
breakpoints: breakpoints
};
},
{
addBreakpoint,
removeBreakpoint
}
)(CallSites);