joyent/node — pull request #2063 by seebees (2011-11-10 01:22:44+00:00)
Title: .load, .save and local scope tab completion

Details below. My tests pass but I need to make sure 'make test-all' is clean. Before I do all that I wanted to make sure someone besides me thinks this is cool.

The major down side as I see it is that I'm storing every line executed in a given REPL on that REPL... But how much memory would that use anyway ;) On the up-side, local variable tab-completion and file saving!

If no one likes it, feel free to close it. You won't hurt my feelings.

REPLServer.prototype.resetContext:
Reset the line cache

REPLServer.prototype.memory (don't know if I like that name, called from finish)
pushes what cmd's have been executed against it into this.lines
pushes the "tab depth" for bufferedCommands, in this.lines.level

REPLServer.prototype.displayPrompt:
Uses "tab depth" from this.lines.level to adjust the prompt to visually denote this depth e.g.
> asdf = function () {
… var inner = {
….. one:1

REPLServer.prototype.complete:
Now notices if there is a bufferedCommand and attempts determine locally scoped variables by removing any functions from this.lines and evaling these lines in a nested REPL e.g.
> asdf = function () {
… var inner = { one: 1};
… inn\t
will complete to 'inner' and inner.o\t will complete to 'inner.one'
If the nested REPL still has a bufferedCommand it will falls back to the default.

ArrayStream is a helper class for the nested REPL to get commands pushed to it.
new REPLServer('', new ArrayStream());

Finally added two new REPL commands .save and .load, each takes 1 parameter, a file and attempts to save or load the file to or from the REPL respectively.