Skip to content

Commit

Permalink
💄 use asyn/await
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 6, 2019
1 parent 503c36d commit 3b647d9
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions src/vs/editor/contrib/suggest/wordDistance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,53 @@ export abstract class WordDistance {
distance() { return 0; }
};

static create(service: IEditorWorkerService, editor: ICodeEditor): Promise<WordDistance> {
static async create(service: IEditorWorkerService, editor: ICodeEditor): Promise<WordDistance> {

if (!editor.getConfiguration().contribInfo.suggest.localityBonus) {
return Promise.resolve(WordDistance.None);
return WordDistance.None;
}

if (!editor.hasModel()) {
return Promise.resolve(WordDistance.None);
return WordDistance.None;
}

const model = editor.getModel();
const position = editor.getPosition();

if (!service.canComputeWordRanges(model.uri)) {
return Promise.resolve(WordDistance.None);
return WordDistance.None;
}

return new BracketSelectionRangeProvider().provideSelectionRanges(model, [position]).then(ranges => {
if (!ranges || ranges.length === 0 || ranges[0].length === 0) {
return WordDistance.None;
}
return service.computeWordRanges(model.uri, ranges[0][0].range).then(wordRanges => {
return new class extends WordDistance {
distance(anchor: IPosition, suggestion: CompletionItem) {
if (!wordRanges || !position.equals(editor.getPosition())) {
return 0;
}
if (suggestion.kind === CompletionItemKind.Keyword) {
return 2 << 20;
}
let word = suggestion.label;
let wordLines = wordRanges[word];
if (isFalsyOrEmpty(wordLines)) {
return 2 << 20;
}
let idx = binarySearch(wordLines, Range.fromPositions(anchor), Range.compareRangesUsingStarts);
let bestWordRange = idx >= 0 ? wordLines[idx] : wordLines[Math.max(0, ~idx - 1)];
let blockDistance = ranges.length;
for (const range of ranges[0]) {
if (!Range.containsRange(range.range, bestWordRange)) {
break;
}
blockDistance -= 1;
}
return blockDistance;
const ranges = await new BracketSelectionRangeProvider().provideSelectionRanges(model, [position]);
if (!ranges || ranges.length === 0 || ranges[0].length === 0) {
return WordDistance.None;
}
const wordRanges = await service.computeWordRanges(model.uri, ranges[0][0].range);
return new class extends WordDistance {
distance(anchor: IPosition, suggestion: CompletionItem) {
if (!wordRanges || !position.equals(editor.getPosition())) {
return 0;
}
if (suggestion.kind === CompletionItemKind.Keyword) {
return 2 << 20;
}
let word = suggestion.label;
let wordLines = wordRanges[word];
if (isFalsyOrEmpty(wordLines)) {
return 2 << 20;
}
let idx = binarySearch(wordLines, Range.fromPositions(anchor), Range.compareRangesUsingStarts);
let bestWordRange = idx >= 0 ? wordLines[idx] : wordLines[Math.max(0, ~idx - 1)];
let blockDistance = ranges.length;
for (const range of ranges[0]) {
if (!Range.containsRange(range.range, bestWordRange)) {
break;
}
};
});
});
blockDistance -= 1;
}
return blockDistance;
}
};
}

abstract distance(anchor: IPosition, suggestion: CompletionItem): number;
Expand Down

0 comments on commit 3b647d9

Please sign in to comment.