Search in sources :

Example 1 with ImportClassFix

use of com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix in project intellij-community by JetBrains.

the class JavaCompletionContributor method autoImportReference.

private static void autoImportReference(@NotNull PsiFile file, @NotNull Editor editor, @Nullable PsiJavaCodeReferenceElement element) {
    if (element == null)
        return;
    while (true) {
        final PsiJavaCodeReferenceElement qualifier = extractReference(element.getQualifier());
        if (qualifier == null)
            break;
        element = qualifier;
    }
    if (!(element.getParent() instanceof PsiMethodCallExpression) && element.multiResolve(true).length == 0) {
        new ImportClassFix(element).doFix(editor, false, false);
        PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
    }
}
Also used : ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix)

Example 2 with ImportClassFix

use of com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix in project intellij-community by JetBrains.

the class ImportHelperTest method testAutoImportCaretLocation2.

public void testAutoImportCaretLocation2() throws Throwable {
    boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
    try {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
        configureByText(StdFileTypes.JAVA, "class X { <caret>ArrayList c = new ArrayList(); }");
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).flushCurrentCommandMerger();
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).clearUndoRedoQueueInTests(getFile().getVirtualFile());
        type(" ");
        backspace();
        assertEquals(2, highlightErrors().size());
        UIUtil.dispatchAllInvocationEvents();
        int offset = myEditor.getCaretModel().getOffset();
        PsiReference ref = myFile.findReferenceAt(offset);
        assertTrue(ref instanceof PsiJavaCodeReferenceElement);
        ImportClassFixBase.Result result = new ImportClassFix((PsiJavaCodeReferenceElement) ref).doFix(getEditor(), true, false);
        assertEquals(ImportClassFixBase.Result.CLASS_AUTO_IMPORTED, result);
        UIUtil.dispatchAllInvocationEvents();
        assertEmpty(highlightErrors());
    } finally {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
    }
}
Also used : UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) ImportClassFixBase(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase) ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix)

Example 3 with ImportClassFix

use of com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix in project intellij-community by JetBrains.

the class ImportHelperTest method testAutoImportSkipsClassReferenceInMethodPosition.

public void testAutoImportSkipsClassReferenceInMethodPosition() throws Throwable {
    @NonNls String text = "package x; import java.util.HashMap; class S { HashMap<String,String> f(){ return  Hash<caret>Map <String, String >();} }  ";
    configureByText(StdFileTypes.JAVA, text);
    boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
    CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
    DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);
    try {
        List<HighlightInfo> errs = highlightErrors();
        assertTrue(errs.size() > 1);
        PsiJavaFile javaFile = (PsiJavaFile) getFile();
        assertEquals(1, javaFile.getImportList().getAllImportStatements().length);
        PsiReference ref = javaFile.findReferenceAt(getEditor().getCaretModel().getOffset());
        ImportClassFix fix = new ImportClassFix((PsiJavaCodeReferenceElement) ref);
        assertFalse(fix.isAvailable(getProject(), getEditor(), getFile()));
    } finally {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 4 with ImportClassFix

use of com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix in project intellij-community by JetBrains.

the class ImportHelperTest method testAutoImportWorksWhenITypeSpaceAfterClassName.

public void testAutoImportWorksWhenITypeSpaceAfterClassName() throws Throwable {
    @NonNls String text = "class S { ArrayList<caret> }";
    configureByText(StdFileTypes.JAVA, text);
    boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
    CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
    DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);
    try {
        doHighlighting();
        //caret is too close
        assertEmpty(((PsiJavaFile) getFile()).getImportList().getAllImportStatements());
        type(" ");
        PsiJavaCodeReferenceElement element = (PsiJavaCodeReferenceElement) getFile().findReferenceAt(getEditor().getCaretModel().getOffset() - 2);
        ImportClassFix fix = new ImportClassFix(element);
        ImportClassFixBase.Result result = fix.doFix(getEditor(), false, false);
        assertEquals(ImportClassFixBase.Result.CLASS_AUTO_IMPORTED, result);
        assertNotSame(0, ((PsiJavaFile) getFile()).getImportList().getAllImportStatements().length);
    } finally {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) ImportClassFixBase(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase) ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix)

Example 5 with ImportClassFix

use of com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix in project intellij-community by JetBrains.

the class JavaReferenceImporter method autoImportReferenceAtCursor.

public static boolean autoImportReferenceAtCursor(@NotNull Editor editor, @NotNull PsiFile file, final boolean allowCaretNearRef) {
    if (!file.getViewProvider().getLanguages().contains(JavaLanguage.INSTANCE)) {
        return false;
    }
    int caretOffset = editor.getCaretModel().getOffset();
    Document document = editor.getDocument();
    int lineNumber = document.getLineNumber(caretOffset);
    int startOffset = document.getLineStartOffset(lineNumber);
    int endOffset = document.getLineEndOffset(lineNumber);
    List<PsiElement> elements = CollectHighlightsUtil.getElementsInRange(file, startOffset, endOffset);
    for (PsiElement element : elements) {
        if (element instanceof PsiJavaCodeReferenceElement) {
            PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement) element;
            if (ref.multiResolve(true).length == 0) {
                new ImportClassFix(ref).doFix(editor, false, allowCaretNearRef);
                return true;
            }
        }
    }
    return false;
}
Also used : ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix) Document(com.intellij.openapi.editor.Document)

Aggregations

ImportClassFix (com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix)7 ImportClassFixBase (com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase)3 NonNls (org.jetbrains.annotations.NonNls)3 UndoManagerImpl (com.intellij.openapi.command.impl.UndoManagerImpl)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 Document (com.intellij.openapi.editor.Document)1