diff --git a/src/components/Editor/CallSite.js b/src/components/Editor/CallSite.js index c3dda92871..8327782e78 100644 --- a/src/components/Editor/CallSite.js +++ b/src/components/Editor/CallSite.js @@ -1,7 +1,7 @@ // @flow import { Component } from "react"; -import { markText, toEditorLocation } from "../../utils/editor"; +import { markText, toEditorRange } from "../../utils/editor"; require("./CallSite.css"); type MarkerType = { @@ -34,8 +34,8 @@ export default class CallSite extends Component { const { editor, callSite, breakpoint, source } = nextProps || this.props; const className = !breakpoint ? "call-site" : "call-site-bp"; const sourceId = source.get("id"); - const editorLocation = toEditorLocation(sourceId, callSite.location); - this.marker = markText(editor, className, editorLocation); + const editorRange = toEditorRange(sourceId, callSite.location); + this.marker = markText(editor, className, editorRange); } clearCallSite() { diff --git a/src/components/Editor/Preview.js b/src/components/Editor/Preview.js index 25ec8b7a19..dcff8a8aed 100644 --- a/src/components/Editor/Preview.js +++ b/src/components/Editor/Preview.js @@ -19,6 +19,8 @@ import { markText } from "../../utils/editor"; import Rep from "../shared/Rep"; import { MODE } from "devtools-reps"; +import type { EditorRange } from "../../utils/editor/types"; + import "./Preview.css"; class Preview extends Component { @@ -32,7 +34,7 @@ class Preview extends Component { value: Object, expression: string, onClose: () => void, - location: Object, + range: EditorRange, editor: any, selectSourceURL: (string, Object) => void }; @@ -43,10 +45,10 @@ class Preview extends Component { loadedObjects, value, editor, - location + range } = this.props; - this.marker = markText(editor, "selection", location); + this.marker = markText(editor, "selection", range); if (!value || !value.type == "object") { return; diff --git a/src/components/Editor/index.js b/src/components/Editor/index.js index 166d058490..85898a3abe 100644 --- a/src/components/Editor/index.js +++ b/src/components/Editor/index.js @@ -65,7 +65,8 @@ import { lineAtHeight, toSourceLine, toEditorLine, - toEditorLocation, + toEditorPosition, + toEditorRange, resetLineNumberFormat } from "../../utils/editor"; @@ -483,17 +484,10 @@ class Editor extends PureComponent { selectedLocation && selectedFrame.location.sourceId === selectedLocation.sourceId ) { - const { - sourceId, - line: sourceLine, - column: sourceColumn - } = selectedFrame.location; - let { line, column } = toEditorLocation(sourceId, { - line: sourceLine, - column: sourceColumn - }); - this.state.editor.codeMirror.addLineClass(line, "line", "new-debug-line"); + const { location, sourceId } = selectedFrame; + const { line, column } = toEditorPosition(sourceId, location); + this.state.editor.codeMirror.addLineClass(line, "line", "new-debug-line"); this.debugExpression = markText(this.state.editor, "debug-expression", { start: { line, column }, end: { line, column: null } @@ -615,12 +609,12 @@ class Editor extends PureComponent { return; } - const editorLocation = toEditorLocation(selectedSource.get("id"), location); + const editorRange = toEditorRange(selectedSource.get("id"), location); return Preview({ value, editor: this.state.editor, - location: editorLocation, + range: editorRange, expression: expression, popoverPos: cursorPos, onClose: () => this.clearPreviewSelection() diff --git a/src/utils/editor/index.js b/src/utils/editor/index.js index cc610121ca..3e53a190f6 100644 --- a/src/utils/editor/index.js +++ b/src/utils/editor/index.js @@ -1,3 +1,5 @@ +// @flow + import { isEnabled } from "devtools-config"; import { isPretty, isJavaScript } from "../source"; import { isOriginalId } from "devtools-source-map"; @@ -12,6 +14,9 @@ import { isWasm, lineToWasmOffset, wasmOffsetToLine } from "../wasm"; import { SourceEditor, SourceEditorUtils } from "devtools-source-editor"; +import type { AstPosition, AstLocation } from "../parser/types"; +import type { EditorPosition, EditorRange } from "../editor/types"; + function shouldShowPrettyPrint(selectedSource) { if (!selectedSource) { return false; @@ -78,31 +83,45 @@ function createEditor() { }); } -function toEditorLine(sourceId: string, lineOrOffset: number) { +function toEditorLine(sourceId: string, lineOrOffset: number): ?number { return isWasm(sourceId) ? wasmOffsetToLine(sourceId, lineOrOffset) : lineOrOffset - 1; } -function toEditorLocation(sourceId: string, location: any) { +function toEditorPosition( + sourceId: string, + location: AstPosition +): EditorPosition { return { line: toEditorLine(sourceId, location.line), column: isWasm(sourceId) ? 0 : location.column }; } -function toSourceLine(sourceId: string, line: number) { +function toEditorRange(sourceId: string, location: AstLocation): EditorRange { + const { start, end } = location; + return { + start: toEditorPosition(sourceId, start), + end: toEditorPosition(sourceId, end) + }; +} + +function toSourceLine(sourceId: string, line: number): ?number { return isWasm(sourceId) ? lineToWasmOffset(sourceId, line) : line + 1; } -function toSourceLocation(sourceId: string, location: any) { +function toSourceLocation( + sourceId: string, + location: EditorPosition +): AstPosition { return { line: toSourceLine(sourceId, location.line), column: isWasm(sourceId) ? undefined : location.column }; } -function markText(editor: any, className, location: any) { +function markText(editor: any, className, location: EditorRange) { const { start, end } = location; return editor.codeMirror.markText( @@ -127,7 +146,8 @@ module.exports = Object.assign( createEditor, isWasm, toEditorLine, - toEditorLocation, + toEditorPosition, + toEditorRange, toSourceLine, toSourceLocation, shouldShowPrettyPrint, diff --git a/src/utils/editor/types.js b/src/utils/editor/types.js new file mode 100644 index 0000000000..f0ac38685c --- /dev/null +++ b/src/utils/editor/types.js @@ -0,0 +1,3 @@ +export type EditorPosition = { line: number, column: number }; + +export type EditorRange = { end: EditorPosition, start: EditorPosition }; diff --git a/src/utils/wasm.js b/src/utils/wasm.js index 9f05ce9ae4..775f1b220a 100644 --- a/src/utils/wasm.js +++ b/src/utils/wasm.js @@ -56,11 +56,11 @@ function getWasmLineNumberFormatter(sourceId: string) { let last0 = 7; return function(number: number) { let offset = lineToWasmOffset(sourceId, number - 1); - if (offset === undefined) { + if (offset == undefined) { return ""; } let i = 7; - for (let n = offset | 0; n !== 0 && i >= 0; n >>= 4, i--) { + for (let n = offset; n !== 0 && i >= 0; n >>= 4, i--) { let nibble = n & 15; buffer[i] = nibble < 10 ? codeOf0 + nibble : codeOfA - 10 + nibble; } @@ -84,7 +84,7 @@ function isWasm(sourceId: string) { * @memberof utils/wasm * @static */ -function lineToWasmOffset(sourceId: string, number: number) { +function lineToWasmOffset(sourceId: string, number: number): ?number { let wasmState = wasmStates[sourceId]; if (!wasmState) { return undefined; @@ -100,7 +100,7 @@ function lineToWasmOffset(sourceId: string, number: number) { * @memberof utils/wasm * @static */ -function wasmOffsetToLine(sourceId: string, offset: number) { +function wasmOffsetToLine(sourceId: string, offset: number): ?number { let wasmState = wasmStates[sourceId]; if (!wasmState) { return undefined;