diff --git a/lib/coffee-script/repl.js b/lib/coffee-script/repl.js index 36f0a65a..9513b483 100644 --- a/lib/coffee-script/repl.js +++ b/lib/coffee-script/repl.js @@ -31,6 +31,60 @@ return stdout.write((err.stack || err.toString()) + '\n'); }; + ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/; + + SIMPLEVAR = /(\w+)$/i; + + autocomplete = function(text) { + return completeAttribute(text) || completeVariable(text) || [[], text]; + }; + + completeAttribute = function(text) { + var all, completions, match, obj, prefix, val; + if (match = text.match(ACCESSOR)) { + all = match[0], obj = match[1], prefix = match[2]; + try { + val = Script.runInThisContext(obj); + } catch (error) { + return; + } + completions = getCompletions(prefix, Object.getOwnPropertyNames(Object(val))); + return [completions, prefix]; + } + }; + + completeVariable = function(text) { + var completions, free, keywords, possibilities, r, vars, _ref; + free = (_ref = text.match(SIMPLEVAR)) != null ? _ref[1] : void 0; + if (text === "") free = ""; + if (free != null) { + vars = Script.runInThisContext('Object.getOwnPropertyNames(Object(this))'); + keywords = (function() { + var _i, _len, _ref2, _results; + _ref2 = CoffeeScript.RESERVED; + _results = []; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + r = _ref2[_i]; + if (r.slice(0, 2) !== '__') _results.push(r); + } + return _results; + })(); + possibilities = vars.concat(keywords); + completions = getCompletions(free, possibilities); + return [completions, free]; + } + }; + + getCompletions = function(prefix, candidates) { + var el, _i, _len, _results; + _results = []; + for (_i = 0, _len = candidates.length; _i < _len; _i++) { + el = candidates[_i]; + if (el.indexOf(prefix) === 0) _results.push(el); + } + return _results; + }; + if (readline.createInterface.length < 3) { repl = readline.createInterface(stdin, autocomplete); stdin.on('data', function(buffer) { @@ -128,58 +182,4 @@ repl.prompt(); - ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/; - - SIMPLEVAR = /(\w+)$/i; - - autocomplete = function(text) { - return completeAttribute(text) || completeVariable(text) || [[], text]; - }; - - completeAttribute = function(text) { - var all, completions, match, obj, prefix, val; - if (match = text.match(ACCESSOR)) { - all = match[0], obj = match[1], prefix = match[2]; - try { - val = Script.runInThisContext(obj); - } catch (error) { - return; - } - completions = getCompletions(prefix, Object.getOwnPropertyNames(Object(val))); - return [completions, prefix]; - } - }; - - completeVariable = function(text) { - var completions, free, keywords, possibilities, r, vars, _ref; - free = (_ref = text.match(SIMPLEVAR)) != null ? _ref[1] : void 0; - if (text === "") free = ""; - if (free != null) { - vars = Script.runInThisContext('Object.getOwnPropertyNames(Object(this))'); - keywords = (function() { - var _i, _len, _ref2, _results; - _ref2 = CoffeeScript.RESERVED; - _results = []; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - r = _ref2[_i]; - if (r.slice(0, 2) !== '__') _results.push(r); - } - return _results; - })(); - possibilities = vars.concat(keywords); - completions = getCompletions(free, possibilities); - return [completions, free]; - } - }; - - getCompletions = function(prefix, candidates) { - var el, _i, _len, _results; - _results = []; - for (_i = 0, _len = candidates.length; _i < _len; _i++) { - el = candidates[_i]; - if (el.indexOf(prefix) === 0) _results.push(el); - } - return _results; - }; - }).call(this); diff --git a/src/repl.coffee b/src/repl.coffee index 52f7f052..d5d5bcdb 100644 --- a/src/repl.coffee +++ b/src/repl.coffee @@ -29,6 +29,42 @@ unless process.platform is 'win32' error = (err) -> stdout.write (err.stack or err.toString()) + '\n' +## Autocompletion + +# Regexes to match complete-able bits of text. +ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/ +SIMPLEVAR = /(\w+)$/i + +# Returns a list of completions, and the completed text. +autocomplete = (text) -> + completeAttribute(text) or completeVariable(text) or [[], text] + +# Attempt to autocomplete a chained dotted attribute: `one.two.three`. +completeAttribute = (text) -> + if match = text.match ACCESSOR + [all, obj, prefix] = match + try + val = Script.runInThisContext obj + catch error + return + completions = getCompletions prefix, Object.getOwnPropertyNames Object val + [completions, prefix] + +# Attempt to autocomplete an in-scope free variable: `one`. +completeVariable = (text) -> + free = text.match(SIMPLEVAR)?[1] + free = "" if text is "" + if free? + vars = Script.runInThisContext 'Object.getOwnPropertyNames(Object(this))' + keywords = (r for r in CoffeeScript.RESERVED when r[..1] isnt '__') + possibilities = vars.concat keywords + completions = getCompletions free, possibilities + [completions, free] + +# Return elements of candidates for which `prefix` is a prefix. +getCompletions = (prefix, candidates) -> + (el for el in candidates when el.indexOf(prefix) is 0) + # Create the REPL by listening to **stdin**. if readline.createInterface.length < 3 repl = readline.createInterface stdin, autocomplete @@ -119,39 +155,3 @@ repl.on 'line', (buffer) -> repl.setPrompt REPL_PROMPT repl.prompt() - -## Autocompletion - -# Regexes to match complete-able bits of text. -ACCESSOR = /\s*([\w\.]+)(?:\.(\w*))$/ -SIMPLEVAR = /(\w+)$/i - -# Returns a list of completions, and the completed text. -autocomplete = (text) -> - completeAttribute(text) or completeVariable(text) or [[], text] - -# Attempt to autocomplete a chained dotted attribute: `one.two.three`. -completeAttribute = (text) -> - if match = text.match ACCESSOR - [all, obj, prefix] = match - try - val = Script.runInThisContext obj - catch error - return - completions = getCompletions prefix, Object.getOwnPropertyNames Object val - [completions, prefix] - -# Attempt to autocomplete an in-scope free variable: `one`. -completeVariable = (text) -> - free = text.match(SIMPLEVAR)?[1] - free = "" if text is "" - if free? - vars = Script.runInThisContext 'Object.getOwnPropertyNames(Object(this))' - keywords = (r for r in CoffeeScript.RESERVED when r[..1] isnt '__') - possibilities = vars.concat keywords - completions = getCompletions free, possibilities - [completions, free] - -# Return elements of candidates for which `prefix` is a prefix. -getCompletions = (prefix, candidates) -> - (el for el in candidates when el.indexOf(prefix) is 0)