completion
Advanced tools
+2
-0
| # completion changelog | ||
| 0.2.0 - Added `line-info` to collect useful information for completers | ||
| 0.1.0 - Initial release |
| // Load in dependencies | ||
| var lineInfo = require('line-info'); | ||
| var _ = require('underscore.string'); | ||
@@ -11,6 +12,5 @@ | ||
| complete: function (params, cb) { | ||
| // Fragment words | ||
| var line = params.line; | ||
| var linePartialLeft = line.slice(0, params.cursor); | ||
| var partialLeftWords = linePartialLeft.split(/\s+/g); | ||
| // Collect info | ||
| var info = lineInfo(params); | ||
| var partialLeftWords = info.words.partialLeft; | ||
@@ -36,3 +36,3 @@ // TODO: Prepare much more practical text information as noted in README | ||
| if (layer !== undefined) { | ||
| var rightmostPartialLeftWord = partialLeftWords[lenMinusOne]; | ||
| var partialLeftWord = info.word.partialLeft; | ||
| // If it is `null`, treat it as a terminal command and callback with it | ||
@@ -45,3 +45,3 @@ // ['npm', 'publish', ''] on {npm: {publish: null}} -> [] (nothing to complete) | ||
| } else if (typeof layer === 'function') { | ||
| return layer(params, cb); | ||
| return layer(info, cb); | ||
| // Otherwise, if it is an objet, find matching commands | ||
@@ -52,3 +52,3 @@ // ['git', 'che'] on {git: {checkout: getGitBranches}} -> ['checkout'] | ||
| var matchingCmds = cmds.filter(function (cmd) { | ||
| return _.startsWith(cmd, rightmostPartialLeftWord); | ||
| return _.startsWith(cmd, partialLeftWord); | ||
| }); | ||
@@ -55,0 +55,0 @@ matchingCmds.sort(); |
+3
-2
| { | ||
| "name": "completion", | ||
| "description": "Completion library for words, commands, and sentences", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "homepage": "https://github.com/twolfson/completion", | ||
@@ -32,3 +32,4 @@ "author": { | ||
| "dependencies": { | ||
| "underscore.string": "~2.3.3" | ||
| "underscore.string": "~2.3.3", | ||
| "line-info": "~0.2.0" | ||
| }, | ||
@@ -35,0 +36,0 @@ "devDependencies": { |
+13
-10
@@ -27,6 +27,6 @@ # completion [](https://travis-ci.org/twolfson/completion) | ||
| git: { | ||
| checkout: function (params, cb) { | ||
| checkout: function (info, cb) { | ||
| // For `git checkout dev/|` | ||
| // params.line = 'git checkout dev' | ||
| // params.cursor = 17 | ||
| // info.words.value = ['git', 'checkout', 'dev/'] | ||
| // info.word.partialLeft = 'dev/' | ||
| getGitBranches(function (err, allBranches) { | ||
@@ -37,4 +37,6 @@ if (err) { | ||
| var partialLeftWord = info.word.partialLeft; | ||
| var branches = allBranches.filter(function (branch) { | ||
| return params.line.match(branch); | ||
| // 'chec' === 'chec' (from 'checkout') | ||
| return partialLeftWord === branch.substr(0, partialLeftWord.length); | ||
| }); | ||
@@ -88,6 +90,5 @@ cb(null, branches); | ||
| - A function that will callback with potential matches | ||
| - The function should be error-first; have a signature of `function (params, cb)` | ||
| - params `Object` - A container for information | ||
| - line `String` - Original string input to `completion.complete` | ||
| - cursor `Number` - Index within `line` of the cursor | ||
| - The function should be error-first; have a signature of `function (info, cb)` | ||
| - info `Object` - Collection of distilled information | ||
| - The format will be the returned value from [twolfson/line-info][] | ||
| - cb `Function` - Error-first callback function to run with matches | ||
@@ -98,2 +99,4 @@ - `cb` has a signature of `function (err, results)` | ||
| [twolfson/line-info]: https://github.com/twolfson/line-info#lineinfoparams | ||
| ### `completion.complete(params, cb)` | ||
@@ -115,3 +118,3 @@ Get potential completion matches | ||
| // `git checkout master` | ||
| checkout: function (params, cb) { | ||
| checkout: function (info, cb) { | ||
| // Get git branches and find matches | ||
@@ -123,3 +126,3 @@ }, | ||
| // `git remote rm origin` | ||
| rm: function (params, cb) { | ||
| rm: function (info, cb) { | ||
| // Get git branches and find matches | ||
@@ -126,0 +129,0 @@ } |
16947
1.3%162
1.89%2
100%+ Added
+ Added