index.js (2114B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 export * from "./source-search"; 6 export * from "../ui"; 7 export * from "./tokens"; 8 9 import { createEditor } from "./create-editor"; 10 11 let editor; 12 13 export function getEditor() { 14 if (editor) { 15 return editor; 16 } 17 18 editor = createEditor({ cm6: true }); 19 return editor; 20 } 21 22 export function removeEditor() { 23 editor = null; 24 } 25 26 /** 27 * Update line wrapping for the codemirror editor. 28 */ 29 export function updateEditorLineWrapping(value) { 30 if (!editor) { 31 return; 32 } 33 editor.setLineWrapping(value); 34 } 35 36 export function toWasmSourceLine(offset) { 37 return editor.wasmOffsetToLine(offset) || 0; 38 } 39 40 /** 41 * Convert source lines / WASM line offsets to Codemirror lines 42 * 43 * @param {object} source 44 * @param {number} lineOrOffset 45 * @returns 46 */ 47 export function toEditorLine(source, lineOrOffset) { 48 if (editor.isWasm && !source.isOriginal) { 49 // TODO ensure offset is always "mappable" to edit line. 50 return toWasmSourceLine(lineOrOffset) + 1; 51 } 52 return lineOrOffset; 53 } 54 55 export function fromEditorLine(source, line) { 56 // Also ignore the original source related to the .wasm file. 57 if (editor.isWasm && !source.isOriginal) { 58 // Content lines is 1-based in CM6 and 0-based in WASM 59 return editor.lineToWasmOffset(line - 1); 60 } 61 return line; 62 } 63 64 export function toEditorPosition(location) { 65 // Note that Spidermonkey, Debugger frontend and CodeMirror are all consistent regarding column 66 // and are 0-based. But only CodeMirror consider the line to be 0-based while the two others 67 // consider lines to be 1-based. 68 const isSourceWasm = editor.isWasm && !location.source.isOriginal; 69 return { 70 line: toEditorLine(location.source, location.line), 71 column: isSourceWasm || !location.column ? 0 : location.column, 72 }; 73 } 74 75 export function toSourceLine(source, line) { 76 if (editor.isWasm && !source.isOriginal) { 77 return editor.lineToWasmOffset(line - 1); 78 } 79 return line; 80 }