Collection<LookupElement> (CompletionParameters parameters, PsiElement position) { PsiElement container = BringVariableIntoScopeFix.getContainer(position); if (container == null) return Collections.emptyList(); Map<String, Optional<PsiLocalVariable>> variableMap = EntryStream.ofTree(container, (depth, element) -> depth > 2 ? null : StreamEx.of(element.getChildren())) .values() .select(PsiCodeBlock.class) .flatArray(PsiCodeBlock::getStatements) .select(PsiDeclarationStatement.class) .flatArray(PsiDeclarationStatement::getDeclaredElements) .select(PsiLocalVariable.class) .remove(var -> PsiTreeUtil.isAncestor(var, position, true)) .toMap(PsiLocalVariable::getName, Optional::of, (v1, v2) -> Optional.empty()); PsiResolveHelper helper = JavaPsiFacade.getInstance(parameters.getOriginalFile().getProject()).getResolveHelper(); variableMap.values().removeAll(Collections.singleton(Optional.<PsiLocalVariable>empty())); variableMap.keySet().removeIf(name -> helper.resolveReferencedVariable(name, position) != null); int offset = position.getTextRange().getStartOffset(); variableMap.values().removeIf(v -> v.orElseThrow().getTextRange().getStartOffset() > offset); if (variableMap.isEmpty()) return Collections.emptyList(); return ContainerUtil.map(variableMap.values(), optVar -> { assert optVar.isPresent(); PsiLocalVariable variable = optVar.get(); String place = getPlace(variable); return new VariableLookupItem(variable, JavaBundle.message("completion.inner.scope.tail.text", place)).setPriority(-1); }); }