-
Notifications
You must be signed in to change notification settings - Fork 7.3k
repl: added support for custom completions #8484
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,3 +211,36 @@ testMe.complete(' ', function(error, data) { | |
testMe.complete('toSt', function(error, data) { | ||
assert.deepEqual(data, [['toString'], 'toSt']); | ||
}); | ||
|
||
// To test custom completer function. | ||
var putIn2 = new ArrayStream(); | ||
var testMe2 = repl.start({ | ||
prompt: '', | ||
input : putIn2, | ||
output: putIn2, | ||
completer: function customCompleter(line, cb) { | ||
var completions = 'aaa aa1 aa2 bbb bb1 bb2 bb3 ccc ddd eee'.split(' '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having all of these strings and calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its easier to read but this could be changed . |
||
var hits = completions.filter(function (item) { | ||
return item.indexOf(line) === 0; | ||
}); | ||
// Show all completions if none was found. | ||
cb([hits.length ? hits : completions, line]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you not need to pass an error argument here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean? Can you be more explicit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most callbacks take an error as their first argument. Is that not the case here? |
||
} | ||
}); | ||
|
||
putIn2.run(['.clear']); | ||
|
||
// On empty line should output all the custom completions | ||
// without complete anything. | ||
putIn2.run(['']); | ||
testMe2.complete('', function(error, data) { | ||
assert.deepEqual(data, [ | ||
'aaa aa1 aa2 bbb bb1 bb2 bb3 ccc ddd eee'.split(' '), '' | ||
]); | ||
}); | ||
|
||
// On `a` should output `aaa aa1 aa2` and complete until `aa`. | ||
putIn2.run(['a']); | ||
testMe2.complete('a', function(error, data) { | ||
assert.deepEqual(data, ['aaa aa1 aa2'.split(' '), 'a']); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just pass
line
andcallback
by name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to avoid in the future if anyone adds more parameters to the original function but forgot to put them there. A little silly but removes complexity by having only one source of truth.