forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighlightLine.js
More file actions
135 lines (113 loc) · 3.53 KB
/
Copy pathHighlightLine.js
File metadata and controls
135 lines (113 loc) · 3.53 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
/* 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 { Component } from "react";
import { toEditorLine } from "../../utils/editor";
import { getDocument, hasDocument } from "../../utils/editor/source-documents";
import { isLoaded } from "../../utils/source";
import { connect } from "react-redux";
import {
getVisibleSelectedFrame,
getSelectedLocation,
getSelectedSource,
getPauseCommand
} from "../../selectors";
import type { Frame, Location, SourceRecord } from "../../types";
import type { Command } from "../../reducers/types";
type Props = {
pauseCommand: Command,
selectedFrame: Frame,
selectedLocation: Location,
selectedSource: SourceRecord
};
function isDebugLine(selectedFrame: Frame, selectedLocation: Location) {
if (!selectedFrame) {
return;
}
return (
selectedFrame.location.sourceId == selectedLocation.sourceId &&
selectedFrame.location.line == selectedLocation.line
);
}
function isDocumentReady(selectedSource, selectedLocation) {
return (
selectedLocation &&
isLoaded(selectedSource) &&
hasDocument(selectedLocation.sourceId)
);
}
export class HighlightLine extends Component<Props> {
isStepping: boolean = false;
previousEditorLine: ?number = null;
shouldComponentUpdate(nextProps: Props) {
const { selectedLocation, selectedSource } = nextProps;
return this.shouldSetHighlightLine(selectedLocation, selectedSource);
}
shouldSetHighlightLine(
selectedLocation: Location,
selectedSource: SourceRecord
) {
const { sourceId, line } = selectedLocation;
const editorLine = toEditorLine(sourceId, line);
if (!isDocumentReady(selectedSource, selectedLocation)) {
return false;
}
if (this.isStepping && editorLine === this.previousEditorLine) {
return false;
}
return true;
}
componentDidUpdate(prevProps: Props) {
const {
pauseCommand,
selectedLocation,
selectedFrame,
selectedSource
} = this.props;
if (pauseCommand) {
this.isStepping = true;
}
this.clearHighlightLine(
prevProps.selectedLocation,
prevProps.selectedSource
);
this.setHighlightLine(selectedLocation, selectedFrame, selectedSource);
}
setHighlightLine(
selectedLocation: Location,
selectedFrame: Frame,
selectedSource: SourceRecord
) {
const { sourceId, line } = selectedLocation;
if (!this.shouldSetHighlightLine(selectedLocation, selectedSource)) {
return;
}
this.isStepping = false;
const editorLine = toEditorLine(sourceId, line);
this.previousEditorLine = editorLine;
if (!line || isDebugLine(selectedFrame, selectedLocation)) {
return;
}
const doc = getDocument(sourceId);
doc.addLineClass(editorLine, "line", "highlight-line");
}
clearHighlightLine(selectedLocation: Location, selectedSource: SourceRecord) {
if (!isDocumentReady(selectedSource, selectedLocation)) {
return;
}
const { line, sourceId } = selectedLocation;
const editorLine = toEditorLine(sourceId, line);
const doc = getDocument(sourceId);
doc.removeLineClass(editorLine, "line", "highlight-line");
}
render() {
return null;
}
}
export default connect(state => ({
pauseCommand: getPauseCommand(state),
selectedFrame: getVisibleSelectedFrame(state),
selectedLocation: getSelectedLocation(state),
selectedSource: getSelectedSource(state)
}))(HighlightLine);