LookupElement[] (@Nullable LinkedHashSet<String> names, @Nullable PsiNamedElement elementToRename, @Nullable PsiElement nameSuggestionContext, final boolean shouldSelectAll) { if (names == null) { if (elementToRename == null) return LookupElement.EMPTY_ARRAY; names = new LinkedHashSet<>(); final LinkedHashSet<String> finalNames = names; mySuggestedNameInfo = ActionUtil.underModalProgress(elementToRename.getProject(), RefactoringBundle.message("progress.title.collecting.suggested.names"), () -> NameSuggestionProvider.suggestNames(elementToRename, nameSuggestionContext, finalNames)); } final LookupElement[] lookupElements = new LookupElement[names.size()]; final Iterator<String> iterator = names.iterator(); for (int i = 0; i < lookupElements.length; i++) { final String suggestion = iterator.next(); lookupElements[i] = LookupElementBuilder.create(suggestion).withInsertHandler(new InsertHandler<>() { @Override public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement item) { if (shouldSelectAll) return; final Editor topLevelEditor = InjectedLanguageEditorUtil.getTopLevelEditor(context.getEditor()); final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor); if (templateState != null) { final TextRange range = templateState.getCurrentVariableRange(); if (range != null) { topLevelEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), suggestion); } } } }); } return lookupElements; }