summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/components/Editor/BlackboxLines.js
blob: 1281bc635acb44f49cc9a252829097092c39371b (plain)
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
/* 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 PropTypes from "devtools/client/shared/vendor/react-prop-types";
import { Component } from "devtools/client/shared/vendor/react";
import { toEditorLine, fromEditorLine } from "../../utils/editor/index";
import { isLineBlackboxed } from "../../utils/source";
import { isWasm } from "../../utils/wasm";

// This renders blackbox line highlighting in the editor
class BlackboxLines extends Component {
  static get propTypes() {
    return {
      editor: PropTypes.object.isRequired,
      selectedSource: PropTypes.object.isRequired,
      blackboxedRangesForSelectedSource: PropTypes.array,
      isSourceOnIgnoreList: PropTypes.bool,
    };
  }

  componentDidMount() {
    const { selectedSource, blackboxedRangesForSelectedSource, editor } =
      this.props;

    if (this.props.isSourceOnIgnoreList) {
      this.setAllBlackboxLines(editor);
      return;
    }

    // When `blackboxedRangesForSelectedSource` is defined and the array is empty,
    // the whole source was blackboxed.
    if (!blackboxedRangesForSelectedSource.length) {
      this.setAllBlackboxLines(editor);
    } else {
      editor.codeMirror.operation(() => {
        blackboxedRangesForSelectedSource.forEach(range => {
          const start = toEditorLine(selectedSource.id, range.start.line);
          // CodeMirror.eachLine doesn't include `end` line offset, so bump by one
          const end = toEditorLine(selectedSource.id, range.end.line) + 1;
          editor.codeMirror.eachLine(start, end, lineHandle => {
            this.setBlackboxLine(editor, lineHandle);
          });
        });
      });
    }
  }

  componentDidUpdate() {
    const {
      selectedSource,
      blackboxedRangesForSelectedSource,
      editor,
      isSourceOnIgnoreList,
    } = this.props;

    if (this.props.isSourceOnIgnoreList) {
      this.setAllBlackboxLines(editor);
      return;
    }

    // when unblackboxed
    if (!blackboxedRangesForSelectedSource) {
      this.clearAllBlackboxLines(editor);
      return;
    }

    // When the whole source is blackboxed
    if (!blackboxedRangesForSelectedSource.length) {
      this.setAllBlackboxLines(editor);
      return;
    }

    const sourceIsWasm = isWasm(selectedSource.id);

    // TODO: Possible perf improvement. Instead of going
    // over all the lines each time get diffs of what has
    // changed and update those.
    editor.codeMirror.operation(() => {
      editor.codeMirror.eachLine(lineHandle => {
        const line = fromEditorLine(
          selectedSource.id,
          editor.codeMirror.getLineNumber(lineHandle),
          sourceIsWasm
        );

        if (
          isLineBlackboxed(
            blackboxedRangesForSelectedSource,
            line,
            isSourceOnIgnoreList
          )
        ) {
          this.setBlackboxLine(editor, lineHandle);
        } else {
          this.clearBlackboxLine(editor, lineHandle);
        }
      });
    });
  }

  componentWillUnmount() {
    // Lets make sure we remove everything  relating to
    // blackboxing lines when this component is unmounted.
    this.clearAllBlackboxLines(this.props.editor);
  }

  clearAllBlackboxLines(editor) {
    editor.codeMirror.operation(() => {
      editor.codeMirror.eachLine(lineHandle => {
        this.clearBlackboxLine(editor, lineHandle);
      });
    });
  }

  setAllBlackboxLines(editor) {
    //TODO:We might be able to handle the whole source
    // than adding the blackboxing line by line
    editor.codeMirror.operation(() => {
      editor.codeMirror.eachLine(lineHandle => {
        this.setBlackboxLine(editor, lineHandle);
      });
    });
  }

  clearBlackboxLine(editor, lineHandle) {
    editor.codeMirror.removeLineClass(lineHandle, "wrap", "blackboxed-line");
  }

  setBlackboxLine(editor, lineHandle) {
    editor.codeMirror.addLineClass(lineHandle, "wrap", "blackboxed-line");
  }

  render() {
    return null;
  }
}

export default BlackboxLines;