1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | function hintList(cursor, token, list) {
|
4 | var hints = filterAndSortList(list, normalizeText(token.string));
|
5 | if (!hints) {
|
6 | return;
|
7 | }
|
8 | var tokenStart = token.type !== null && /"|\w/.test(token.string[0])
|
9 | ? token.start
|
10 | : token.end;
|
11 | return {
|
12 | list: hints,
|
13 | from: { line: cursor.line, ch: tokenStart },
|
14 | to: { line: cursor.line, ch: token.end },
|
15 | };
|
16 | }
|
17 | exports.default = hintList;
|
18 | function filterAndSortList(list, text) {
|
19 | if (!text) {
|
20 | return filterNonEmpty(list, function (entry) { return !entry.isDeprecated; });
|
21 | }
|
22 | var byProximity = list.map(function (entry) { return ({
|
23 | proximity: getProximity(normalizeText(entry.text), text),
|
24 | entry: entry,
|
25 | }); });
|
26 | var conciseMatches = filterNonEmpty(filterNonEmpty(byProximity, function (pair) { return pair.proximity <= 2; }), function (pair) { return !pair.entry.isDeprecated; });
|
27 | var sortedMatches = conciseMatches.sort(function (a, b) {
|
28 | return (a.entry.isDeprecated ? 1 : 0) - (b.entry.isDeprecated ? 1 : 0) ||
|
29 | a.proximity - b.proximity ||
|
30 | a.entry.text.length - b.entry.text.length;
|
31 | });
|
32 | return sortedMatches.map(function (pair) { return pair.entry; });
|
33 | }
|
34 | function filterNonEmpty(array, predicate) {
|
35 | var filtered = array.filter(predicate);
|
36 | return filtered.length === 0 ? array : filtered;
|
37 | }
|
38 | function normalizeText(text) {
|
39 | return text.toLowerCase().replace(/\W/g, '');
|
40 | }
|
41 | function getProximity(suggestion, text) {
|
42 | var proximity = lexicalDistance(text, suggestion);
|
43 | if (suggestion.length > text.length) {
|
44 | proximity -= suggestion.length - text.length - 1;
|
45 | proximity += suggestion.indexOf(text) === 0 ? 0 : 0.5;
|
46 | }
|
47 | return proximity;
|
48 | }
|
49 | function lexicalDistance(a, b) {
|
50 | var i;
|
51 | var j;
|
52 | var d = [];
|
53 | var aLength = a.length;
|
54 | var bLength = b.length;
|
55 | for (i = 0; i <= aLength; i++) {
|
56 | d[i] = [i];
|
57 | }
|
58 | for (j = 1; j <= bLength; j++) {
|
59 | d[0][j] = j;
|
60 | }
|
61 | for (i = 1; i <= aLength; i++) {
|
62 | for (j = 1; j <= bLength; j++) {
|
63 | var cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
64 | d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
65 | if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
66 | d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
|
67 | }
|
68 | }
|
69 | }
|
70 | return d[aLength][bLength];
|
71 | }
|
72 |
|
\ | No newline at end of file |