Skip to content

Commit

Permalink
fix(completions): don't set filterText after all (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Feb 14, 2023
1 parent 5bcc2a1 commit 4c5d295
Showing 1 changed file with 2 additions and 39 deletions.
41 changes: 2 additions & 39 deletions src/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ export function asCompletionItem(
item.detail = Previewer.plainWithLinks(sourceDisplay, filePathConverter);
}

const { line, optionalReplacementRange, isMemberCompletion, dotAccessorContext } = completionContext;
const { optionalReplacementRange, isMemberCompletion, dotAccessorContext } = completionContext;
let range = getRangeFromReplacementSpan(replacementSpan, optionalReplacementRange, position, document, features);
let { insertText } = entry;
item.filterText = getFilterText(entry, optionalReplacementRange, line, insertText);

if (isMemberCompletion && dotAccessorContext && !entry.isSnippet) {
item.filterText = dotAccessorContext.text + (insertText || item.label);
if (!range) {
if (features.completionInsertReplaceSupport && optionalReplacementRange) {
range = {
Expand All @@ -104,7 +102,7 @@ export function asCompletionItem(
} else {
range = { replace: dotAccessorContext.range };
}
insertText = item.filterText;
insertText = dotAccessorContext.text + (insertText || item.label);
}
}

Expand Down Expand Up @@ -171,41 +169,6 @@ function getRangeFromReplacementSpan(
}
}

function getFilterText(entry: ts.server.protocol.CompletionEntry, wordRange: lsp.Range | undefined, line: string, insertText: string | undefined): string | undefined {
// Handle private field completions
if (entry.name.startsWith('#')) {
const wordStart = wordRange ? line.charAt(wordRange.start.character) : undefined;
if (insertText) {
if (insertText.startsWith('this.#')) {
return wordStart === '#' ? insertText : insertText.replace(/&this\.#/, '');
} else {
return wordStart;
}
} else {
return wordStart === '#' ? undefined : entry.name.replace(/^#/, '');
}
}

// For `this.` completions, generally don't set the filter text since we don't want them to be overly prioritized. #74164
if (insertText?.startsWith('this.')) {
return undefined;
}

// Handle the case:
// ```
// const xyz = { 'ab c': 1 };
// xyz.ab|
// ```
// In which case we want to insert a bracket accessor but should use `.abc` as the filter text instead of
// the bracketed insert text.
if (insertText?.startsWith('[')) {
return insertText.replace(/^\[['"](.+)[['"]\]$/, '.$1');
}

// In all other cases, fallback to using the insertText
return insertText;
}

function ensureRangeIsOnSingleLine(range: lsp.Range, document: LspDocument): lsp.Range {
if (range.start.line !== range.end.line) {
return lsp.Range.create(range.start, document.getLineEnd(range.start.line));
Expand Down

0 comments on commit 4c5d295

Please sign in to comment.