Skip to content

Commit 51fdc49

Browse files
committed
debugger: run last command on presssing enter
Fixes: #2895
1 parent 6222e5b commit 51fdc49

5 files changed

Lines changed: 67 additions & 5 deletions

File tree

doc/api/debugger.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ The `repl` command allows code to be evaluated remotely. The `next` command
8484
steps over to the next line. Type `help` to see what other commands are
8585
available.
8686

87+
Pressing `enter` without typing a command will repeat the previous debugger
88+
command.
89+
8790
## Watchers
8891

8992
It is possible to watch expression and variable values while debugging. On

lib/_debugger.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@ var helpMessage = 'Commands: ' + commands.map(function(group) {
674674
return group.join(', ');
675675
}).join(',\n');
676676

677+
// Previous command received. Initialize to empty command.
678+
var lastCommand = '\n';
679+
677680

678681
function SourceUnderline(sourceText, position, repl) {
679682
if (!sourceText) return '';
@@ -945,10 +948,10 @@ Interface.prototype.requireConnection = function() {
945948
Interface.prototype.controlEval = function(code, context, filename, callback) {
946949
try {
947950
// Repeat last command if empty line are going to be evaluated
948-
if (this.repl.rli.history && this.repl.rli.history.length > 0) {
949-
if (code === '\n') {
950-
code = this.repl.rli.history[0] + '\n';
951-
}
951+
if (code === '\n') {
952+
code = lastCommand;
953+
} else {
954+
lastCommand = code;
952955
}
953956

954957
// exec process.title => exec("process.title");

lib/repl.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,10 @@ function REPLServer(prompt,
421421
}
422422
}
423423

424-
if (cmd || self.bufferedCommand) {
424+
// self.context.setBreakpoint exists we are in the debugger.
425+
// If we are in the debugger, then we should process empty lines as they are
426+
// a shortcut for "repeat the previous debugging command".
427+
if (cmd || self.bufferedCommand || self.context.setBreakpoint) {
425428
var evalCmd = self.bufferedCommand + cmd;
426429
if (/^\s*\{/.test(evalCmd) && /\}\s*$/.test(evalCmd)) {
427430
// It's confusing for `{ a : 1 }` to be interpreted as a block
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var a = 1;
2+
3+
var b = 2;
4+
5+
var c = 3;
6+
7+
b = c;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const path = require('path');
3+
const spawn = require('child_process').spawn;
4+
const assert = require('assert');
5+
6+
const common = require('../common');
7+
8+
const fixture = path.join(
9+
common.fixturesDir,
10+
'debugger-repeat-last.js'
11+
);
12+
13+
const args = [
14+
'debug',
15+
fixture
16+
];
17+
18+
const proc = spawn(process.execPath, args, { stdio: 'pipe' });
19+
proc.stdout.setEncoding('utf8');
20+
21+
var stdout = '';
22+
23+
var sentCommand = false;
24+
var sentEmpty = false;
25+
var sentExit = false;
26+
27+
proc.stdout.on('data', (data) => {
28+
stdout += data;
29+
if (!sentCommand && stdout.includes('> 1')) {
30+
setImmediate(() => {proc.stdin.write('n\n');});
31+
return sentCommand = true;
32+
}
33+
if (!sentEmpty && stdout.includes('> 3')) {
34+
setImmediate(() => {proc.stdin.write('\n');});
35+
return sentEmpty = true;
36+
}
37+
if (!sentExit && sentCommand && sentEmpty) {
38+
setTimeout(() => {proc.stdin.write('\n\n\n.exit\n\n\n');}, 1);
39+
return sentExit = true;
40+
}
41+
});
42+
43+
process.on('exit', (exitCode) => {
44+
assert.strictEqual(exitCode, 0);
45+
console.log(stdout);
46+
});

0 commit comments

Comments
 (0)