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

Commit 95402ee

Browse files
author
Jan Krems
committed
refactor: Move list into CallFrame
1 parent 62afe27 commit 95402ee

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

lib/internal/inspect-repl.js

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,49 @@ function createRepl(inspector) {
312312
}
313313
}
314314

315+
class SourceSnippet {
316+
constructor(location, delta, scriptSource) {
317+
Object.assign(this, location);
318+
this.scriptSource = scriptSource;
319+
this.delta = delta;
320+
}
321+
322+
toString() {
323+
return this[util.inspect.custom]();
324+
}
325+
326+
[util.inspect.custom]() {
327+
const { scriptId, lineNumber, columnNumber, delta, scriptSource } = this;
328+
const start = Math.max(1, lineNumber - delta + 1);
329+
const end = lineNumber + delta + 1;
330+
331+
const lines = scriptSource.split('\n');
332+
return lines.slice(start - 1, end).map((lineText, offset) => {
333+
const i = start + offset;
334+
const isCurrent = i === (lineNumber + 1);
335+
336+
const markedLine = isCurrent
337+
? markSourceColumn(lineText, columnNumber, inspector.repl)
338+
: lineText;
339+
340+
let isBreakpoint = false;
341+
knownBreakpoints.forEach(({ location }) => {
342+
if (location && location.scriptId === scriptId && (location.lineNumber + 1) === i) {
343+
isBreakpoint = true;
344+
}
345+
});
346+
347+
let prefixChar = ' ';
348+
if (isCurrent) {
349+
prefixChar = '>';
350+
} else if (isBreakpoint) {
351+
prefixChar = '*';
352+
}
353+
return `${leftPad(i, prefixChar, end)} ${markedLine}`;
354+
}).join('\n');
355+
}
356+
}
357+
315358
class CallFrame {
316359
constructor(callFrame) {
317360
Object.assign(this, callFrame);
@@ -330,6 +373,12 @@ function createRepl(inspector) {
330373
})
331374
);
332375
}
376+
377+
list(delta = 5) {
378+
const { scriptId } = this.location;
379+
return Debugger.getScriptSource({ scriptId })
380+
.then(({ scriptSource }) => new SourceSnippet(this.location, delta, scriptSource));
381+
}
333382
}
334383

335384
class Backtrace extends Array {
@@ -460,43 +509,9 @@ function createRepl(inspector) {
460509
return formatWatchers(verbose).then(print);
461510
}
462511

463-
function formatSourceContext(delta = 5) {
464-
const { scriptId, lineNumber, columnNumber } = getCurrentLocation();
465-
const start = Math.max(1, lineNumber - delta + 1);
466-
const end = lineNumber + delta + 1;
467-
468-
return Debugger.getScriptSource({ scriptId })
469-
.then(({ scriptSource }) => {
470-
const lines = scriptSource.split('\n');
471-
return lines.slice(start - 1, end).map((lineText, offset) => {
472-
const i = start + offset;
473-
const isCurrent = i === (lineNumber + 1);
474-
475-
const markedLine = isCurrent
476-
? markSourceColumn(lineText, columnNumber, inspector.repl)
477-
: lineText;
478-
479-
let isBreakpoint = false;
480-
knownBreakpoints.forEach(({ location }) => {
481-
if (location && location.scriptId === scriptId && (location.lineNumber + 1) === i) {
482-
isBreakpoint = true;
483-
}
484-
});
485-
486-
let prefixChar = ' ';
487-
if (isCurrent) {
488-
prefixChar = '>';
489-
} else if (isBreakpoint) {
490-
prefixChar = '*';
491-
}
492-
return `${leftPad(i, prefixChar, end)} ${markedLine}`;
493-
}).join('\n');
494-
});
495-
}
496-
497512
// List source code
498513
function list(delta = 5) {
499-
return formatSourceContext(delta)
514+
return selectedFrame.list(delta)
500515
.then(print, (error) => {
501516
print('You can\'t list source code right now');
502517
throw error;
@@ -684,7 +699,7 @@ function createRepl(inspector) {
684699
print(`${reason === 'other' ? 'break' : reason} in ${scriptUrl}:${lineNumber + 1}`);
685700

686701
inspector.suspendReplWhile(() =>
687-
Promise.all([formatWatchers(true), formatSourceContext(2)])
702+
Promise.all([formatWatchers(true), selectedFrame.list(2)])
688703
.then(([watcherList, context]) => (watcherList ? `${watcherList}\n${context}` : context))
689704
.then(print));
690705
});

0 commit comments

Comments
 (0)