Skip to content

Commit

Permalink
perf: Support for LSP CompletionList#ItemDefaults
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jul 28, 2023
1 parent 5352b81 commit 8ba367a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,30 @@ public void fillCompletionVariants(@NotNull CompletionParameters parameters, @No

private void addCompletionItems(PsiFile file, Editor editor, CompletionPrefix completionPrefix, Either<List<CompletionItem>,
CompletionList> completion, LanguageServer languageServer, @NotNull CompletionResultSet result) {
List<CompletionItem> items = completion.isLeft() ? completion.getLeft() : completion.getRight().getItems();
CompletionItemDefaults itemDefaults = null;
List<CompletionItem> items = null;
if (completion.isLeft()) {
items = completion.getLeft();
} else {
CompletionList completionList = completion.getRight();
itemDefaults = completionList.getItemDefaults();
items = completionList.getItems();
}
for (var item : items) {
if (StringUtils.isBlank(item.getLabel())) {
// Invalid completion Item, ignore it
continue;
}
// Create lookup item
var lookupItem = createLookupItem(file, editor, completionPrefix.getCompletionOffset(), item, languageServer);
var lookupItem = createLookupItem(file, editor, completionPrefix.getCompletionOffset(), item, itemDefaults, languageServer);
// Group it by using completion item kind
var groupedLookupItem = PrioritizedLookupElement.withGrouping(lookupItem, item.getKind().getValue());
// Compute the prefix
String prefix = completionPrefix.getPrefixFor(lookupItem.getTextEditRange(), item);
if (prefix != null) {
// Add the IJ completion item (lookup item) by using the computed prefix
result.withPrefixMatcher(prefix)
.caseInsensitive() // set case insentitive to search Java class which starts with upper case
.caseInsensitive() // set case insensitive to search Java class which starts with upper case
.addElement(groupedLookupItem);
} else {
// Should happens rarely, only when text edit is for multi-lines or if completion is triggered outside the text edit range.
Expand All @@ -116,10 +124,32 @@ private void addCompletionItems(PsiFile file, Editor editor, CompletionPrefix co

private static LSPCompletionProposal createLookupItem(PsiFile file, Editor editor, int offset,
CompletionItem item,
LanguageServer languageServer) {
CompletionItemDefaults itemDefaults, LanguageServer languageServer) {
// Update text edit range with item defaults if needed
updateWithItemDefaults(item, itemDefaults);
return new LSPCompletionProposal(file, editor, offset, item, languageServer);
}

private static void updateWithItemDefaults(CompletionItem item, CompletionItemDefaults itemDefaults) {
if (itemDefaults == null) {
return;
}
String itemText = item.getTextEditText();
if (itemDefaults.getEditRange() != null && itemText != null) {
if (itemDefaults.getEditRange().isLeft()) {
Range defaultRange = itemDefaults.getEditRange().getLeft();
if (defaultRange != null) {
item.setTextEdit(Either.forLeft(new TextEdit(defaultRange, itemText)));
}
} else {
InsertReplaceRange defaultInsertReplaceRange = itemDefaults.getEditRange().getRight();
if (defaultInsertReplaceRange != null) {
item.setTextEdit(Either.forRight(new InsertReplaceEdit(itemText, defaultInsertReplaceRange.getInsert(), defaultInsertReplaceRange.getReplace())));
}
}
}
}


private static LookupElement createErrorProposal(int offset, Exception ex) {
return LookupElementBuilder.create("Error while computing completion", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
public class LSPCompletionProposal extends LookupElement {
private static final Logger LOGGER = LoggerFactory.getLogger(LSPCompletionProposal.class);

protected final CompletionItem item;
protected final int initialOffset;
private final CompletionItem item;
private final int initialOffset;
private final PsiFile file;
protected int currentOffset;
protected int bestOffset;
protected final Editor editor;
protected final LanguageServer languageServer;
private int currentOffset;
private int bestOffset;
private final Editor editor;
private final LanguageServer languageServer;

public LSPCompletionProposal(PsiFile file, Editor editor, int offset, CompletionItem item, LanguageServer languageServer) {
this.file = file;
Expand Down

0 comments on commit 8ba367a

Please sign in to comment.