Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
debugger: run last command on presssing enter
Fixes: #2895
  • Loading branch information
Trott committed Apr 9, 2016
commit 51fdc49ed782e4d18754d9777a05822839d19c98
3 changes: 3 additions & 0 deletions doc/api/debugger.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ The `repl` command allows code to be evaluated remotely. The `next` command
steps over to the next line. Type `help` to see what other commands are
available.

Pressing `enter` without typing a command will repeat the previous debugger
command.

## Watchers

It is possible to watch expression and variable values while debugging. On
Expand Down
11 changes: 7 additions & 4 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,9 @@ var helpMessage = 'Commands: ' + commands.map(function(group) {
return group.join(', ');
}).join(',\n');

// Previous command received. Initialize to empty command.
var lastCommand = '\n';


function SourceUnderline(sourceText, position, repl) {
if (!sourceText) return '';
Expand Down Expand Up @@ -945,10 +948,10 @@ Interface.prototype.requireConnection = function() {
Interface.prototype.controlEval = function(code, context, filename, callback) {
try {
// Repeat last command if empty line are going to be evaluated
if (this.repl.rli.history && this.repl.rli.history.length > 0) {
if (code === '\n') {
code = this.repl.rli.history[0] + '\n';
}
if (code === '\n') {
code = lastCommand;
} else {
lastCommand = code;
}

// exec process.title => exec("process.title");
Expand Down
5 changes: 4 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ function REPLServer(prompt,
}
}

if (cmd || self.bufferedCommand) {
// self.context.setBreakpoint exists we are in the debugger.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'If'? I'm a bit uneasy about this check, it's not very robust. Doing setBreakPoint = 1 in the normal REPL will make this code think it's being called from the debugger.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis You're right, of course. I think have a better solution that I've just pushed. PTAL.

// If we are in the debugger, then we should process empty lines as they are
// a shortcut for "repeat the previous debugging command".
if (cmd || self.bufferedCommand || self.context.setBreakpoint) {
var evalCmd = self.bufferedCommand + cmd;
if (/^\s*\{/.test(evalCmd) && /\}\s*$/.test(evalCmd)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/debugger-repeat-last.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var a = 1;

var b = 2;

var c = 3;

b = c;
46 changes: 46 additions & 0 deletions test/parallel/test-debugger-repeat-last.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;
const assert = require('assert');

const common = require('../common');

const fixture = path.join(
common.fixturesDir,
'debugger-repeat-last.js'
);

const args = [
'debug',
fixture
];

const proc = spawn(process.execPath, args, { stdio: 'pipe' });
proc.stdout.setEncoding('utf8');

var stdout = '';

var sentCommand = false;
var sentEmpty = false;
var sentExit = false;

proc.stdout.on('data', (data) => {
stdout += data;
if (!sentCommand && stdout.includes('> 1')) {
setImmediate(() => {proc.stdin.write('n\n');});
return sentCommand = true;
}
if (!sentEmpty && stdout.includes('> 3')) {
setImmediate(() => {proc.stdin.write('\n');});
return sentEmpty = true;
}
if (!sentExit && sentCommand && sentEmpty) {
setTimeout(() => {proc.stdin.write('\n\n\n.exit\n\n\n');}, 1);
return sentExit = true;
}
});

process.on('exit', (exitCode) => {
assert.strictEqual(exitCode, 0);
console.log(stdout);
});