List<LookupElement> (@Nullable final Character charToTypeAfterCompletion) { final CaretModel caretModel = myEditor.getCaretModel(); final List<Caret> carets = caretModel.getAllCarets(); final List<Integer> originalOffsets = new ArrayList<>(carets.size()); for (final Caret caret : carets) { originalOffsets.add(caret.getOffset()); } caretModel.removeSecondaryCarets(); // We do it in reverse order because completions would affect offsets // i.e.: when you complete "spa" to "spam", next caret offset increased by 1 Collections.reverse(originalOffsets); final List<LookupElement> result = new ArrayList<>(); for (final int originalOffset : originalOffsets) { caretModel.moveToOffset(originalOffset); final LookupElement[] lookupElements = completeBasic(); if (charToTypeAfterCompletion != null) { type(charToTypeAfterCompletion); } if (lookupElements != null) { result.addAll(Arrays.asList(lookupElements)); } } return result; }