Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(defaultContentLanguage: IIndexEntry) {
this.defaultContentLanguage = defaultContentLanguage;
// currently this uses a trie, which is overkill for this number of items,
// but I'm using it because I do need prefix matching and this library does that.
// Enhance this TrieSearch doesn't provide a way to include our alternative language name arrays
// Enhance allow for matching even if close (e.g. levenshtein distance)
// Configure the trie to match what is in the index json
this.index = new TrieSearch([
"englishName",
"localName",
//"altNames", TrieSearch can't handle array values, so we'll add them by hand below
"iso639_1",
"iso639_3"
]);
// add the primary name and two codes
const index = require("./langindex.json");
this.index.addAll(index);
// now add the alternative names
index.forEach(indexEntry => {
if (indexEntry.altNames && indexEntry.altNames.length > 0) {
indexEntry.altNames.forEach(alternativeName => {
this.index.map(alternativeName, indexEntry);
import TrieSearch from 'trie-search';
import { CompletionItemProvider, TextDocument, Position, CancellationToken, CompletionItem, CompletionContext, ProviderResult, CompletionList, CompletionItemKind, Range } from 'vscode';
interface TrieObject {
key: string;
index: number;
}
export class TextAutocompleteCompletionItemProvider implements CompletionItemProvider {
private words: TrieSearch = new TrieSearch('key');
public constructor(keywords: string[]) {
let i = 0;
/* in the future, this could be extended to add a pri rather than just use the position
* the array
*/
this.words.addAll(keywords.map((value: string) => {
return {
key: value,
index: ++i
};
}));
}
private getKeywordsGivenPartialWord(wordToComplete: string, limit: number) : CompletionItem[] {
import TrieSearch from 'trie-search';
const trie = new TrieSearch('file', {
indexField: 'id',
idFieldOrFunction: 'id',
splitOnRegEx: /[^A-Za-z0-9]/g,
});
onmessage = (message) => {
const data = message.data;
switch (data.type) {
case 'load':
const start = performance.now();
const files = JSON.parse(data.payload).map((file, i) => ({id: i, file: file}));
trie.addAll(files);
const time = (performance.now() - start).toFixed(1);
console.log('Added %s items (%s tokens) to search trie in %s ms.', files.length, trie.size, time);
postMessage({
type: 'status',
'search': async (params) => {
const limit = parseInt(params.limit, 10);
const query = params.query;
const start = performance.now();
let items = trie.get(query, TrieSearch.UNION_REDUCER) || [];
const total = items.length;
if (limit) items = items.slice(0, limit);
const time = (performance.now() - start).toFixed(1);
console.log('Returned %s results in %s ms.', items.length, time);
return {
items: items,
total: total,
};
},
}).pipe(through(value => {
if (typeof value === 'object') {
if (!trie) trie = new TrieSearch(null, trieOptions);
trie.addFromObject(value);
resolve(trie);
return;
}
if (!trie) trie = new TrieSearch('value', trieOptions);
trie.add({value});
resolve(trie);
}));
});
}).pipe(through(value => {
if (typeof value === 'object') {
if (!trie) trie = new TrieSearch(null, trieOptions);
trie.addFromObject(value);
resolve(trie);
return;
}
if (!trie) trie = new TrieSearch('value', trieOptions);
trie.add({value});
resolve(trie);
}));
});
}).pipe(through(value => {
if (typeof value === 'object') {
if (!trie) trie = new TrieSearch(null, trieOptions);
trie.addFromObject(value);
resolve(trie);
return;
}
if (!trie) trie = new TrieSearch('value', trieOptions);
trie.add({value});
resolve(trie);
}));
});
}).pipe(through(value => {
if (typeof value === 'object') {
if (!trie) trie = new TrieSearch(null, trieOptions);
trie.addFromObject(value);
resolve(trie);
return;
}
if (!trie) trie = new TrieSearch('value', trieOptions);
trie.add({value});
resolve(trie);
}));
});
index(recurse = false, trieSearchOptions = {}, trieSearch = undefined) {
trieSearch = trieSearch || new TrieSearch(undefined, Object.assign(trieSearchOptions, {
keepAll: true,
keepAllKey: 'id'
}));
trieSearch.visitedById = trieSearch.visitedById || {};
if (trieSearch.visitedById[this.id]) {
return;
}
trieSearch.visitedById[this.id] = true;
let _add = function(model, trieSearch, prop, obj) {
if (typeof obj === 'object' && obj !== null && obj.indexValue) {
trieSearch.map(obj.indexValue, model);
} else if (obj && obj.toString() !== '') {
function search(query, maxResults) {
let results = trie.get(query, TrieSearch.UNION_REDUCER) || [];
const count = results.length;
if (maxResults) {
results = results.slice(0, maxResults);
}
postMessage({
type: 'results',
payload: {
results: results,
count: count,
}
});
}