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
|
/* 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/>. */
export * from "./source-documents";
export * from "./source-search";
export * from "../ui";
export * from "./tokens";
import { createEditor } from "./create-editor";
import { isWasm, lineToWasmOffset, wasmOffsetToLine } from "../wasm";
import { createLocation } from "../location";
import { features } from "../prefs";
let editor;
export function getEditor(useCm6) {
if (editor) {
return editor;
}
editor = createEditor(useCm6);
return editor;
}
export function removeEditor() {
editor = null;
}
/**
* Update line wrapping for the codemirror editor.
*/
export function updateEditorLineWrapping(value) {
if (!editor) {
return;
}
editor.setLineWrapping(value);
}
function getCodeMirror() {
return editor && editor.hasCodeMirror ? editor.codeMirror : null;
}
export function startOperation() {
const codeMirror = getCodeMirror();
if (!codeMirror) {
return;
}
codeMirror.startOperation();
}
export function endOperation() {
const codeMirror = getCodeMirror();
if (!codeMirror) {
return;
}
codeMirror.endOperation();
}
export function toEditorLine(sourceId, lineOrOffset) {
if (isWasm(sourceId)) {
// TODO ensure offset is always "mappable" to edit line.
return wasmOffsetToLine(sourceId, lineOrOffset) || 0;
}
if (features.codemirrorNext) {
return lineOrOffset;
}
return lineOrOffset ? lineOrOffset - 1 : 1;
}
export function fromEditorLine(sourceId, line, sourceIsWasm) {
if (sourceIsWasm) {
return lineToWasmOffset(sourceId, line) || 0;
}
if (features.codemirrorNext) {
return line;
}
return line + 1;
}
export function toEditorPosition(location) {
// Note that Spidermonkey, Debugger frontend and CodeMirror are all consistant regarding column
// and are 0-based. But only CodeMirror consider the line to be 0-based while the two others
// consider lines to be 1-based.
return {
line: toEditorLine(location.source.id, location.line),
column:
isWasm(location.source.id) || (!location.column ? 0 : location.column),
};
}
export function toSourceLine(sourceId, line) {
if (isWasm(sourceId)) {
return lineToWasmOffset(sourceId, line);
}
if (features.codemirrorNext) {
return line;
}
return line + 1;
}
export function getLocationsInViewport(
{ codeMirror },
// Offset represents an allowance of characters or lines offscreen to improve
// perceived performance of column breakpoint rendering
offsetHorizontalCharacters = 100,
offsetVerticalLines = 20
) {
// Get scroll position
if (!codeMirror) {
return {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
};
}
const charWidth = codeMirror.defaultCharWidth();
const scrollArea = codeMirror.getScrollInfo();
const { scrollLeft } = codeMirror.doc;
const rect = codeMirror.getWrapperElement().getBoundingClientRect();
const topVisibleLine =
codeMirror.lineAtHeight(rect.top, "window") - offsetVerticalLines;
const bottomVisibleLine =
codeMirror.lineAtHeight(rect.bottom, "window") + offsetVerticalLines;
const leftColumn = Math.floor(
scrollLeft > 0 ? scrollLeft / charWidth - offsetHorizontalCharacters : 0
);
const rightPosition = scrollLeft + (scrollArea.clientWidth - 30);
const rightCharacter =
Math.floor(rightPosition / charWidth) + offsetHorizontalCharacters;
return {
start: {
line: topVisibleLine || 0,
column: leftColumn || 0,
},
end: {
line: bottomVisibleLine || 0,
column: rightCharacter,
},
};
}
export function markText({ codeMirror }, className, { start, end }) {
return codeMirror.markText(
{ ch: start.column, line: start.line },
{ ch: end.column, line: end.line },
{ className }
);
}
export function lineAtHeight({ codeMirror }, sourceId, event) {
const _editorLine = codeMirror.lineAtHeight(event.clientY);
return toSourceLine(sourceId, _editorLine);
}
export function getSourceLocationFromMouseEvent({ codeMirror }, source, e) {
const { line, ch } = codeMirror.coordsChar({
left: e.clientX,
top: e.clientY,
});
return createLocation({
source,
line: fromEditorLine(source.id, line, isWasm(source.id)),
column: isWasm(source.id) ? 0 : ch + 1,
});
}
export function forEachLine(codeMirror, iter) {
codeMirror.operation(() => {
codeMirror.doc.iter(0, codeMirror.lineCount(), iter);
});
}
export function removeLineClass(codeMirror, line, className) {
codeMirror.removeLineClass(line, "wrap", className);
}
export function clearLineClass(codeMirror, className) {
forEachLine(codeMirror, line => {
removeLineClass(codeMirror, line, className);
});
}
export function getTextForLine(codeMirror, line) {
return codeMirror.getLine(line - 1).trim();
}
export function getCursorLine(codeMirror) {
return codeMirror.getCursor().line;
}
export function getCursorColumn(codeMirror) {
return codeMirror.getCursor().ch;
}
|