Merge branch 'master' of https://github.com/ryszard/coffee-script into 1.1-pre

This commit is contained in:
Jeremy Ashkenas
2011-01-15 14:50:41 -05:00
4 changed files with 71 additions and 3 deletions

34
src/autocomplete.coffee Normal file
View 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]

View File

@@ -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()