Skip to content

Commit

Permalink
Patches #124 Problems with slashes in autocomplete options
Browse files Browse the repository at this point in the history
Fixes autocomplete issue when using slashes.

When a partial match was made after the user entered a command with a slash, the autocomplete function
would ignore the matched prefix.

When a autocomplete was used with a slash the autcomplete would fail when post prefix was an empty string

Tests added
  • Loading branch information
Owain Llewellyn committed Aug 31, 2016
1 parent 21c3669 commit 9d393df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
33 changes: 13 additions & 20 deletions dist/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,26 @@ var autocomplete = {
return prefix + matches[0] + space;
} else if (matches.length === 0) {
return undefined;
} else if (strX.length === 0) {
return matches;
}
var shortestMatchLength = _.reduce(matches, function (acc, value) {
return value.length < acc ? value.length : acc;
}, 99999);

// starting at position str.length, if all matches share the same char in this
// position, increment position. When we get to a position where not all matches
// match, this is the longest string we can autocomplete
var longestMatchLength = shortestMatchLength;
var firstMatch = matches[0];
for (var pos = str.length; pos < shortestMatchLength; pos++) {
var allMatchesHaveSameChar = _.every(matches, function (m) {
return m[pos] === firstMatch[pos];
});
if (!allMatchesHaveSameChar) {
longestMatchLength = pos;
break;

var longestMatchLength = matches.reduce(function (previous, current) {
for (var i = 0; i < current.length; i++) {
if (previous[i] && current[i] !== previous[i]) {
return current.substr(0, i);
}
}
}
return previous;
}).length;

// couldn't resolve any further, return all matches
if (longestMatchLength === str.length) {
if (longestMatchLength === strX.length) {
return matches;
}

// return the longest matching portion
return matches[0].substr(0, longestMatchLength);
// return the longest matching portion along with the prefix
return prefix + matches[0].substr(0, longestMatchLength);
}
};

Expand Down
36 changes: 15 additions & 21 deletions lib/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,27 @@ var autocomplete = {
return prefix + matches[0] + space;
} else if (matches.length === 0) {
return undefined;
} else if (strX.length === 0) {
return matches;
}
var shortestMatchLength = _.reduce(matches, function (acc, value) {
return value.length < acc ? value.length : acc;
}, 99999);

// starting at position str.length, if all matches share the same char in this
// position, increment position. When we get to a position where not all matches
// match, this is the longest string we can autocomplete
var longestMatchLength = shortestMatchLength;
var firstMatch = matches[0];
for (var pos = str.length; pos < shortestMatchLength; pos++) {
var allMatchesHaveSameChar = _.every(matches, function (m) {
return m[pos] === firstMatch[pos];
});
if (!allMatchesHaveSameChar) {
longestMatchLength = pos;
break;
}
}

var longestMatchLength = matches
.reduce(function (previous, current) {
for (var i = 0; i < current.length; i++) {
if (previous[i] && current[i] !== previous[i]) {
return current.substr(0, i);
}
}
return previous;
}).length;

// couldn't resolve any further, return all matches
if (longestMatchLength === str.length) {
if (longestMatchLength === strX.length) {
return matches;
}

// return the longest matching portion
return matches[0].substr(0, longestMatchLength);
// return the longest matching portion along with the prefix
return prefix + matches[0].substr(0, longestMatchLength);
}
};

Expand Down
15 changes: 15 additions & 0 deletions test/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,19 @@ describe('session._autocomplete', function () {
var result = vorpal.session._autocomplete('d', ['def', 'xyz']);
assert.equal(result, 'def ');
});


it('should return the prefix along with the partial match when supplied with a prefix input', function() {
var result = vorpal.session._autocomplete('foo/de', ['dally','definitive', 'definitop', 'bob']);
assert.equal(result, "foo/definit");
});

it("should return a list of matches when supplied with a prefix but no value post prefix", function() {
var result = vorpal.session._autocomplete('foo/', ['dally','definitive', 'definitop', 'bob']);
assert.equal(result.length, 4);
assert.equal(result[0], "bob");
assert.equal(result[1], "dally");
assert.equal(result[2], "definitive");
assert.equal(result[3], "definitop");
});
});

0 comments on commit 9d393df

Please sign in to comment.