Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
refactor: Move list into CallFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Oct 17, 2016
1 parent 62afe27 commit 95402ee
Showing 1 changed file with 51 additions and 36 deletions.
87 changes: 51 additions & 36 deletions lib/internal/inspect-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,49 @@ function createRepl(inspector) {
}
}

class SourceSnippet {
constructor(location, delta, scriptSource) {
Object.assign(this, location);
this.scriptSource = scriptSource;
this.delta = delta;
}

toString() {
return this[util.inspect.custom]();
}

[util.inspect.custom]() {
const { scriptId, lineNumber, columnNumber, delta, scriptSource } = this;
const start = Math.max(1, lineNumber - delta + 1);
const end = lineNumber + delta + 1;

const lines = scriptSource.split('\n');
return lines.slice(start - 1, end).map((lineText, offset) => {
const i = start + offset;
const isCurrent = i === (lineNumber + 1);

const markedLine = isCurrent
? markSourceColumn(lineText, columnNumber, inspector.repl)
: lineText;

let isBreakpoint = false;
knownBreakpoints.forEach(({ location }) => {
if (location && location.scriptId === scriptId && (location.lineNumber + 1) === i) {
isBreakpoint = true;
}
});

let prefixChar = ' ';
if (isCurrent) {
prefixChar = '>';
} else if (isBreakpoint) {
prefixChar = '*';
}
return `${leftPad(i, prefixChar, end)} ${markedLine}`;
}).join('\n');
}
}

class CallFrame {
constructor(callFrame) {
Object.assign(this, callFrame);
Expand All @@ -330,6 +373,12 @@ function createRepl(inspector) {
})
);
}

list(delta = 5) {
const { scriptId } = this.location;
return Debugger.getScriptSource({ scriptId })
.then(({ scriptSource }) => new SourceSnippet(this.location, delta, scriptSource));
}
}

class Backtrace extends Array {
Expand Down Expand Up @@ -460,43 +509,9 @@ function createRepl(inspector) {
return formatWatchers(verbose).then(print);
}

function formatSourceContext(delta = 5) {
const { scriptId, lineNumber, columnNumber } = getCurrentLocation();
const start = Math.max(1, lineNumber - delta + 1);
const end = lineNumber + delta + 1;

return Debugger.getScriptSource({ scriptId })
.then(({ scriptSource }) => {
const lines = scriptSource.split('\n');
return lines.slice(start - 1, end).map((lineText, offset) => {
const i = start + offset;
const isCurrent = i === (lineNumber + 1);

const markedLine = isCurrent
? markSourceColumn(lineText, columnNumber, inspector.repl)
: lineText;

let isBreakpoint = false;
knownBreakpoints.forEach(({ location }) => {
if (location && location.scriptId === scriptId && (location.lineNumber + 1) === i) {
isBreakpoint = true;
}
});

let prefixChar = ' ';
if (isCurrent) {
prefixChar = '>';
} else if (isBreakpoint) {
prefixChar = '*';
}
return `${leftPad(i, prefixChar, end)} ${markedLine}`;
}).join('\n');
});
}

// List source code
function list(delta = 5) {
return formatSourceContext(delta)
return selectedFrame.list(delta)
.then(print, (error) => {
print('You can\'t list source code right now');
throw error;
Expand Down Expand Up @@ -684,7 +699,7 @@ function createRepl(inspector) {
print(`${reason === 'other' ? 'break' : reason} in ${scriptUrl}:${lineNumber + 1}`);

inspector.suspendReplWhile(() =>
Promise.all([formatWatchers(true), formatSourceContext(2)])
Promise.all([formatWatchers(true), selectedFrame.list(2)])
.then(([watcherList, context]) => (watcherList ? `${watcherList}\n${context}` : context))
.then(print));
});
Expand Down

0 comments on commit 95402ee

Please sign in to comment.