mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Merge branch 'master' of https://github.com/ryszard/coffee-script into 1.1-pre
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
(function() {
|
||||
var CoffeeScript, error, helpers, readline, repl, run, stdio;
|
||||
var CoffeeScript, autocomplete, error, helpers, readline, repl, run, stdio;
|
||||
CoffeeScript = require('./coffee-script');
|
||||
helpers = require('./helpers');
|
||||
autocomplete = require('./autocomplete');
|
||||
readline = require('readline');
|
||||
stdio = process.openStdin();
|
||||
error = function(err) {
|
||||
@@ -29,7 +30,7 @@
|
||||
return repl.prompt();
|
||||
};
|
||||
process.on('uncaughtException', error);
|
||||
repl = readline.createInterface(stdio);
|
||||
repl = readline.createInterface(stdio, autocomplete.complete);
|
||||
repl.setPrompt('coffee> ');
|
||||
stdio.on('data', function(buffer) {
|
||||
return repl.write(buffer);
|
||||
|
||||
34
src/autocomplete.coffee
Normal file
34
src/autocomplete.coffee
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
{RESERVED} = require './coffee-script'
|
||||
Script = process.binding('evals').Script
|
||||
|
||||
# Return elements of candidates for which `prefix` is a prefix.
|
||||
get_completions = (prefix, candidates) ->
|
||||
(el for el in candidates when el.indexOf(prefix) == 0)
|
||||
|
||||
get_property_names = (o) ->
|
||||
try
|
||||
Object.getOwnPropertyNames(o)
|
||||
catch error
|
||||
(k for k of o)
|
||||
|
||||
complete_attribute = (text) ->
|
||||
match = /\s*([\w\.]+)(?:\.(\w*))$/.exec(text)
|
||||
if match?
|
||||
[ob, prefix] = [match[1], match[2]]
|
||||
try
|
||||
val = Script.runInThisContext ob
|
||||
catch error
|
||||
return [[], text]
|
||||
completions = get_completions prefix, get_property_names val
|
||||
[completions, prefix]
|
||||
|
||||
complete_variable = (text) ->
|
||||
free = /\W*(\w*)$/i.exec(text)?[1]
|
||||
if free?
|
||||
completions = get_completions free, RESERVED.concat(get_property_names Script.runInThisContext 'this')
|
||||
[completions, free]
|
||||
|
||||
# Returns a list of completions and the completed text
|
||||
exports.complete = (text) ->
|
||||
complete_attribute(text) or complete_variable(text) or [[], text]
|
||||
@@ -7,6 +7,7 @@
|
||||
# Require the **coffee-script** module to get access to the compiler.
|
||||
CoffeeScript = require './coffee-script'
|
||||
helpers = require './helpers'
|
||||
autocomplete = require './autocomplete'
|
||||
readline = require 'readline'
|
||||
|
||||
# Start by opening up **stdio**.
|
||||
@@ -34,7 +35,7 @@ run = (buffer) ->
|
||||
process.on 'uncaughtException', error
|
||||
|
||||
# Create the REPL by listening to **stdin**.
|
||||
repl = readline.createInterface stdio
|
||||
repl = readline.createInterface stdio, autocomplete.complete
|
||||
repl.setPrompt 'coffee> '
|
||||
stdio.on 'data', (buffer) -> repl.write buffer
|
||||
repl.on 'close', -> stdio.destroy()
|
||||
|
||||
32
test/test_autocomplete.coffee
Normal file
32
test/test_autocomplete.coffee
Normal file
@@ -0,0 +1,32 @@
|
||||
return unless require?
|
||||
|
||||
complete = require './../lib/autocomplete'
|
||||
|
||||
eq_set = (left, right) ->
|
||||
left = left.slice(0)
|
||||
right = right.slice(0)
|
||||
left.sort()
|
||||
right.sort()
|
||||
eq left.join(' '), right.join(' ')
|
||||
|
||||
# JavaScript keywords
|
||||
[completions, completed] = complete.complete "c"
|
||||
ok completions instanceof Array
|
||||
should_be = ["case", "catch", "class", "clearInterval", "clearTimeout", "console", "const", "continue"]
|
||||
eq_set should_be, completions
|
||||
|
||||
[completions, completed] = complete.complete 'E'
|
||||
eq_set completions, ['EvalError', 'Error']
|
||||
|
||||
[completions, completed] = complete.complete "Math.c"
|
||||
eq_set completions, ["cos", "ceil"]
|
||||
|
||||
# I don't know how to make this testable :(
|
||||
# a = {baba: 1, babo: 2}
|
||||
|
||||
# [completions, completed] = complete.complete "a.bab"
|
||||
# eq_set completions, ["baba", "babo"]
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user