mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
auto-import context variable declaration type when completing (IDEA-90157)
This commit is contained in:
+20
-3
@@ -644,7 +644,20 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
if (file instanceof PsiJavaFile) {
|
||||
if (context.getInvocationCount() > 0) {
|
||||
autoImport(file, context.getStartOffset() - 1, context.getEditor());
|
||||
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
|
||||
|
||||
PsiElement leaf = file.findElementAt(context.getStartOffset() - 1);
|
||||
if (leaf != null) leaf = PsiTreeUtil.prevVisibleLeaf(leaf);
|
||||
|
||||
PsiVariable variable = PsiTreeUtil.getParentOfType(leaf, PsiVariable.class);
|
||||
if (variable != null) {
|
||||
PsiTypeElement typeElement = variable.getTypeElement();
|
||||
if (typeElement != null) {
|
||||
PsiType type = typeElement.getType();
|
||||
if (type instanceof PsiClassType && ((PsiClassType)type).resolve() == null) {
|
||||
autoImportReference(file, context.getEditor(), typeElement.getInnermostComponentReferenceElement());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JavaCompletionUtil.initOffsets(file, context.getOffsetMap());
|
||||
@@ -727,7 +740,7 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
return iterator.getTokenType() == JavaTokenType.EQ || iterator.getTokenType() == JavaTokenType.LPARENTH;
|
||||
}
|
||||
|
||||
private static void autoImport(final PsiFile file, int offset, final Editor editor) {
|
||||
private static void autoImport(@NotNull final PsiFile file, int offset, @NotNull final Editor editor) {
|
||||
final CharSequence text = editor.getDocument().getCharsSequence();
|
||||
while (offset > 0 && Character.isJavaIdentifierPart(text.charAt(offset))) offset--;
|
||||
if (offset <= 0) return;
|
||||
@@ -740,7 +753,10 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
while (offset > 0 && Character.isWhitespace(text.charAt(offset))) offset--;
|
||||
if (offset <= 0) return;
|
||||
|
||||
PsiJavaCodeReferenceElement element = extractReference(PsiTreeUtil.findElementOfClassAtOffset(file, offset, PsiExpression.class, false));
|
||||
autoImportReference(file, editor, extractReference(PsiTreeUtil.findElementOfClassAtOffset(file, offset, PsiExpression.class, false)));
|
||||
}
|
||||
|
||||
private static void autoImportReference(@NotNull PsiFile file, @NotNull Editor editor, @Nullable PsiJavaCodeReferenceElement element) {
|
||||
if (element == null) return;
|
||||
|
||||
while (true) {
|
||||
@@ -751,6 +767,7 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
}
|
||||
if (!(element.getParent() instanceof PsiMethodCallExpression) && element.multiResolve(true).length == 0) {
|
||||
new ImportClassFix(element).doFix(editor, false, false);
|
||||
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class Main<T extends Foo> {
|
||||
{
|
||||
List<Foo> list = new <caret>
|
||||
}
|
||||
}
|
||||
+13
@@ -1029,6 +1029,19 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
|
||||
assertStringItems("Bar", "Goo");
|
||||
}
|
||||
|
||||
public void testAutoImportExpectedType() throws Throwable {
|
||||
boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
|
||||
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
|
||||
try {
|
||||
configureByTestName();
|
||||
performAction();
|
||||
myFixture.assertPreferredCompletionItems(1, "List", "ArrayList", "AbstractList");
|
||||
}
|
||||
finally {
|
||||
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
|
||||
}
|
||||
}
|
||||
|
||||
public void testNoWrongSubstitutorFromStats() throws Throwable {
|
||||
doTest();
|
||||
FileDocumentManager.getInstance().saveDocument(myFixture.getEditor().getDocument());
|
||||
|
||||
Reference in New Issue
Block a user