mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
finish multiline support for REPL rewrite
This commit is contained in:
@@ -12,17 +12,17 @@
|
||||
|
||||
replDefaults = {
|
||||
prompt: 'coffee> ',
|
||||
"eval": function(code, context, file, cb) {
|
||||
"eval": function(input, context, filename, cb) {
|
||||
var js;
|
||||
try {
|
||||
if (/^\(\s+\)$/.test(code)) {
|
||||
if (/^\(\s+\)$/.test(input)) {
|
||||
return cb(null);
|
||||
}
|
||||
code = code.replace(/子/mg, '\n');
|
||||
code = CoffeeScript.compile(code, {
|
||||
filename: file,
|
||||
js = CoffeeScript.compile(input, {
|
||||
filename: filename,
|
||||
bare: true
|
||||
});
|
||||
return cb(null, vm.runInContext(code, context, file));
|
||||
return cb(null, vm.runInContext(js, context, filename));
|
||||
} catch (err) {
|
||||
return cb(err);
|
||||
}
|
||||
@@ -34,36 +34,46 @@
|
||||
rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;
|
||||
multiline = {
|
||||
enabled: false,
|
||||
prompt: new Array(repl.prompt.length).join('.') + ' ',
|
||||
initialPrompt: '------> ',
|
||||
prompt: '......> ',
|
||||
buffer: ''
|
||||
};
|
||||
nodeLineListener = rli.listeners('line')[0];
|
||||
rli.removeListener('line', nodeLineListener);
|
||||
rli.on('line', function(cmd) {
|
||||
if (multiline.enabled === true) {
|
||||
if (multiline.enabled) {
|
||||
multiline.buffer += "" + cmd + "\n";
|
||||
return rli.prompt(true);
|
||||
rli.setPrompt(multiline.prompt);
|
||||
rli.prompt(true);
|
||||
} else {
|
||||
return nodeLineListener(cmd);
|
||||
nodeLineListener(cmd);
|
||||
}
|
||||
});
|
||||
return inputStream.on('keypress', function(char, key) {
|
||||
if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
|
||||
return;
|
||||
}
|
||||
multiline.enabled = !multiline.enabled;
|
||||
if (multiline.enabled === false) {
|
||||
if (multiline.enabled) {
|
||||
if (!multiline.buffer.match(/\n/)) {
|
||||
multiline.enabled = !multiline.enabled;
|
||||
rli.setPrompt(repl.prompt);
|
||||
rli.prompt(true);
|
||||
return;
|
||||
}
|
||||
multiline.buffer = multiline.buffer.replace(/\n/mg, '子');
|
||||
if (!rli.line.match(/^\s*$/)) {
|
||||
return;
|
||||
}
|
||||
multiline.enabled = !multiline.enabled;
|
||||
rli.line = '';
|
||||
rli.cursor = 0;
|
||||
rli.output.cursorTo(0);
|
||||
rli.output.clearLine(1);
|
||||
rli.emit('line', multiline.buffer);
|
||||
return multiline.buffer = '';
|
||||
multiline.buffer = '';
|
||||
} else {
|
||||
rli.setPrompt(multiline.prompt);
|
||||
return rli.prompt(true);
|
||||
multiline.enabled = !multiline.enabled;
|
||||
rli.setPrompt(multiline.initialPrompt);
|
||||
rli.prompt(true);
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -76,6 +86,9 @@
|
||||
}
|
||||
opts = merge(replDefaults, opts);
|
||||
repl = nodeREPL.start(opts);
|
||||
repl.on('exit', function() {
|
||||
return repl.outputStream.write('\n');
|
||||
});
|
||||
addMultilineHandler(repl);
|
||||
return repl;
|
||||
}
|
||||
|
||||
@@ -5,54 +5,67 @@ CoffeeScript = require './coffee-script'
|
||||
|
||||
replDefaults =
|
||||
prompt: 'coffee> ',
|
||||
eval: (code, context, file, cb) ->
|
||||
eval: (input, context, filename, cb) ->
|
||||
try
|
||||
return cb(null) if /^\(\s+\)$/.test code # Empty command
|
||||
code = code.replace /子/mg, '\n' # Temporary hack, see TODO below
|
||||
code = CoffeeScript.compile(code, {filename: file, bare: true})
|
||||
cb(null, vm.runInContext(code, context, file))
|
||||
return cb null if /^\(\s+\)$/.test input # Empty command
|
||||
# TODO: pass in-scope vars and avoid accidentally shadowing them by omitting those declarations
|
||||
js = CoffeeScript.compile input, {filename, bare: yes}
|
||||
cb null, vm.runInContext js, context, filename
|
||||
catch err
|
||||
cb(err)
|
||||
cb err
|
||||
|
||||
# TODO: how to test?
|
||||
addMultilineHandler = (repl) ->
|
||||
{rli, inputStream, outputStream} = repl
|
||||
|
||||
multiline =
|
||||
multiline =
|
||||
enabled: off
|
||||
prompt: new Array(repl.prompt.length).join('.') + ' '
|
||||
initialPrompt: '------> '
|
||||
prompt: '......> '
|
||||
buffer: ''
|
||||
|
||||
# Proxy node's line listener
|
||||
nodeLineListener = rli.listeners('line')[0]
|
||||
rli.removeListener 'line', nodeLineListener
|
||||
rli.on 'line', (cmd) ->
|
||||
if multiline.enabled is on
|
||||
if multiline.enabled
|
||||
multiline.buffer += "#{cmd}\n"
|
||||
rli.setPrompt multiline.prompt
|
||||
rli.prompt true
|
||||
else
|
||||
nodeLineListener(cmd)
|
||||
return
|
||||
|
||||
# Handle Ctrl-v
|
||||
inputStream.on 'keypress', (char, key) ->
|
||||
return unless key and key.ctrl and not key.meta and not key.shift and key.name is 'v'
|
||||
multiline.enabled = !multiline.enabled
|
||||
if multiline.enabled is off
|
||||
if multiline.enabled
|
||||
# allow arbitrarily switching between modes any time before multiple lines are entered
|
||||
unless multiline.buffer.match /\n/
|
||||
multiline.enabled = not multiline.enabled
|
||||
rli.setPrompt repl.prompt
|
||||
rli.prompt true
|
||||
return
|
||||
# TODO: how to encode line breaks so the node repl will pass the complete multiline to our eval?
|
||||
multiline.buffer = multiline.buffer.replace /\n/mg, '子'
|
||||
# no-op unless the current line is empty
|
||||
return unless rli.line.match /^\s*$/
|
||||
# eval, print, loop
|
||||
multiline.enabled = not multiline.enabled
|
||||
rli.line = ''
|
||||
rli.cursor = 0
|
||||
rli.output.cursorTo 0
|
||||
rli.output.clearLine 1
|
||||
rli.emit 'line', multiline.buffer
|
||||
multiline.buffer = ''
|
||||
else
|
||||
rli.setPrompt multiline.prompt
|
||||
multiline.enabled = not multiline.enabled
|
||||
rli.setPrompt multiline.initialPrompt
|
||||
rli.prompt true
|
||||
return
|
||||
|
||||
module.exports =
|
||||
start: (opts = {}) ->
|
||||
opts = merge(replDefaults, opts)
|
||||
opts = merge replDefaults, opts
|
||||
repl = nodeREPL.start opts
|
||||
addMultilineHandler(repl)
|
||||
repl
|
||||
repl.on 'exit', -> repl.outputStream.write '\n'
|
||||
addMultilineHandler repl
|
||||
repl
|
||||
|
||||
Reference in New Issue
Block a user