|
| 1 | +/* ***** BEGIN LICENSE BLOCK ***** |
| 2 | + * Distributed under the BSD license: |
| 3 | + * |
| 4 | + * Copyright (c) 2012, Ajax.org B.V. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of Ajax.org B.V. nor the |
| 15 | + * names of its contributors may be used to endorse or promote products |
| 16 | + * derived from this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY |
| 22 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + * ***** END LICENSE BLOCK ***** */ |
| 30 | + |
1 | 31 | define(function(require, exports, module) { |
2 | 32 |
|
3 | 33 | var ID_REGEX = /[a-zA-Z_0-9\$]/; |
@@ -26,36 +56,71 @@ function retrieveFollowingIdentifier(text, pos, regex) { |
26 | 56 | return buf; |
27 | 57 | } |
28 | 58 |
|
29 | | -function prefixBinarySearch(items, prefix) { |
30 | | - var startIndex = 0; |
31 | | - var stopIndex = items.length - 1; |
32 | | - var middle = Math.floor((stopIndex + startIndex) / 2); |
33 | | - |
34 | | - while (stopIndex > startIndex && middle >= 0 && items[middle].indexOf(prefix) !== 0) { |
35 | | - if (prefix < items[middle]) { |
36 | | - stopIndex = middle - 1; |
37 | | - } |
38 | | - else if (prefix > items[middle]) { |
39 | | - startIndex = middle + 1; |
| 59 | +// filched from jQuery |
| 60 | +function grep( elems, callback, inv ) { |
| 61 | + var retVal, |
| 62 | + ret = [], |
| 63 | + i = 0, |
| 64 | + length = elems.length; |
| 65 | + inv = !!inv; |
| 66 | + |
| 67 | + // Go through the array, only saving the items |
| 68 | + // that pass the validator function |
| 69 | + for ( ; i < length; i++ ) { |
| 70 | + retVal = !!callback( elems[ i ], i ); |
| 71 | + if ( inv !== retVal ) { |
| 72 | + ret.push( elems[ i ] ); |
40 | 73 | } |
41 | | - middle = Math.floor((stopIndex + stopIndex) / 2); |
42 | 74 | } |
| 75 | + |
| 76 | + return ret; |
| 77 | +}; |
| 78 | + |
| 79 | +function sortByScore(items, identDict) { |
43 | 80 |
|
44 | | - // Look back to make sure we haven't skipped any |
45 | | - while (middle > 0 && items[middle-1].indexOf(prefix) === 0) |
46 | | - middle--; |
47 | | - return middle >= 0 ? middle : 0; // ensure we're not returning a negative index |
48 | | -} |
| 81 | + return items.sort(function(a, b) { |
| 82 | + var scoreA = identDict[a], |
| 83 | + scoreB = identDict[b]; |
| 84 | + |
| 85 | + if (a < b) |
| 86 | + return 1; |
| 87 | + else if (a > b) |
| 88 | + return -1; |
| 89 | + else |
| 90 | + return 0; |
| 91 | + }); |
| 92 | +}; |
| 93 | + |
| 94 | +function findCompletions(prefix, identDict, allIdentifiers) { |
| 95 | + var _self = this, |
| 96 | + fuzzyMatcher = function (prefix, item) { |
| 97 | + return ~item.toLowerCase().indexOf(prefix.toLowerCase()); |
| 98 | + }; |
| 99 | + |
| 100 | + var matches = grep(allIdentifiers, function (item) { |
| 101 | + return fuzzyMatcher(prefix, item); |
| 102 | + }); |
| 103 | + |
| 104 | + matches = sortByScore(matches, identDict); |
49 | 105 |
|
50 | | -function findCompletions(prefix, allIdentifiers) { |
51 | | - allIdentifiers.sort(); |
52 | | - var startIdx = prefixBinarySearch(allIdentifiers, prefix); |
53 | | - var matches = []; |
54 | | - for (var i = startIdx; i < allIdentifiers.length && allIdentifiers[i].indexOf(prefix) === 0; i++) |
55 | | - matches.push(allIdentifiers[i]); |
56 | 106 | return matches; |
57 | 107 | } |
58 | 108 |
|
| 109 | +exports.removeDuplicateWords = function(matches) { |
| 110 | + // First, sort |
| 111 | + matches = matches.sort(); |
| 112 | + |
| 113 | + for (var i = 1; i < matches.length; ){ |
| 114 | + if (matches[i - 1] == matches[i]){ |
| 115 | + matches.splice(i, 1); |
| 116 | + } else { |
| 117 | + i++; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return matches; |
| 122 | +}; |
| 123 | + |
59 | 124 | exports.retrievePrecedingIdentifier = retrievePrecedingIdentifier; |
60 | 125 | exports.retrieveFollowingIdentifier = retrieveFollowingIdentifier; |
61 | 126 | exports.findCompletions = findCompletions; |
|
0 commit comments