Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/Editor/CallSite.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 5 additions & 3 deletions src/components/Editor/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,7 +34,7 @@ class Preview extends Component {
value: Object,
expression: string,
onClose: () => void,
location: Object,
range: EditorRange,
editor: any,
selectSourceURL: (string, Object) => void
};
Expand All @@ -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;
Expand Down
20 changes: 7 additions & 13 deletions src/components/Editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ import {
lineAtHeight,
toSourceLine,
toEditorLine,
toEditorLocation,
toEditorPosition,
toEditorRange,
resetLineNumberFormat
} from "../../utils/editor";

Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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()
Expand Down
32 changes: 26 additions & 6 deletions src/utils/editor/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

import { isEnabled } from "devtools-config";
import { isPretty, isJavaScript } from "../source";
import { isOriginalId } from "devtools-source-map";
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -127,7 +146,8 @@ module.exports = Object.assign(
createEditor,
isWasm,
toEditorLine,
toEditorLocation,
toEditorPosition,
toEditorRange,
toSourceLine,
toSourceLocation,
shouldShowPrettyPrint,
Expand Down
3 changes: 3 additions & 0 deletions src/utils/editor/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type EditorPosition = { line: number, column: number };

export type EditorRange = { end: EditorPosition, start: EditorPosition };
8 changes: 4 additions & 4 deletions src/utils/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down