Sort completion items in CompletionResult

This commit is contained in:
Akash Yadav
2022-05-14 22:14:51 +05:30
parent 3f02700633
commit b2061a1012
2 changed files with 5 additions and 24 deletions
@@ -51,21 +51,7 @@ import io.github.rosemoe.sora.util.MyCharacter;
* @author Akash Yadav
*/
public class CommonCompletionProvider {
private static final Comparator<CompletionItem> RESULT_SORTER =
(p1, p2) -> {
if (p1 == null && p2 == null) {
return 0;
} else if (p1 == null) {
return -1;
} else if (p2 == null) {
return 1;
}
String s1 = p1.getSortText() == null ? p1.getLabel() : p1.getSortText();
String s2 = p2.getSortText() == null ? p2.getLabel() : p2.getSortText();
return s1.compareTo(s2);
};
private static final ILogger LOG = ILogger.newInstance("CommonCompletionProvider");
private final ILanguageServer server;
private CompletableFuture<CompletionResult> future;
@@ -130,7 +116,7 @@ public class CommonCompletionProvider {
});
try {
return finalizeResults(future.get().getItems());
return future.get().getItems();
} catch (Throwable e) {
// Do not log if completion was interrupted or cancelled
if (!(e instanceof InterruptedException || e instanceof CompletionCancelledException)) {
@@ -140,11 +126,4 @@ public class CommonCompletionProvider {
return Collections.emptyList();
}
}
@NonNull
@Contract("_ -> param1")
private List<CompletionItem> finalizeResults(@NonNull List<CompletionItem> items) {
items.sort(RESULT_SORTER);
return items;
}
}
@@ -44,7 +44,9 @@ data class CompletionParams(var position: Position, var file: Path) {
}
}
data class CompletionResult(var isIncomplete: Boolean, var items: List<CompletionItem>) {
open class CompletionResult(var isIncomplete: Boolean, items: List<CompletionItem>) {
var items = items.subList(0, MAX_ITEMS).toMutableList().apply { sort() }
companion object {
const val MAX_ITEMS = 50