Search in sources :

Example 1 with GoImportPackageQuickFix

use of com.goide.codeInsight.imports.GoImportPackageQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoAutoImportInsertHandler method autoImport.

private static void autoImport(@NotNull InsertionContext context, @NotNull GoNamedElement element) {
    PsiFile file = context.getFile();
    boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(ModuleUtilCore.findModuleForPsiElement(file));
    String importPath = element.getContainingFile().getImportPath(vendoringEnabled);
    if (StringUtil.isEmpty(importPath))
        return;
    PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
    PsiReference reference = file.findReferenceAt(context.getStartOffset());
    if (reference != null) {
        PsiElement referenceElement = reference.getElement();
        GoImportPackageQuickFix fix = new GoImportPackageQuickFix(referenceElement, importPath);
        fix.invoke(context.getProject(), file, context.getEditor(), referenceElement, referenceElement);
    }
}
Also used : GoImportPackageQuickFix(com.goide.codeInsight.imports.GoImportPackageQuickFix) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 2 with GoImportPackageQuickFix

use of com.goide.codeInsight.imports.GoImportPackageQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoReferenceImporter method autoImportReferenceAtCursor.

@Override
public boolean autoImportReferenceAtCursor(@NotNull Editor editor, @NotNull PsiFile file) {
    if (!file.getViewProvider().getLanguages().contains(GoLanguage.INSTANCE) || !DaemonListeners.canChangeFileSilently(file)) {
        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 GoCompositeElement) {
            for (PsiReference reference : element.getReferences()) {
                GoImportPackageQuickFix fix = new GoImportPackageQuickFix(reference);
                if (fix.doAutoImportOrShowHint(editor, false)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : GoImportPackageQuickFix(com.goide.codeInsight.imports.GoImportPackageQuickFix) GoCompositeElement(com.goide.psi.GoCompositeElement) PsiReference(com.intellij.psi.PsiReference) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GoImportPackageQuickFix (com.goide.codeInsight.imports.GoImportPackageQuickFix)2 PsiElement (com.intellij.psi.PsiElement)2 PsiReference (com.intellij.psi.PsiReference)2 GoCompositeElement (com.goide.psi.GoCompositeElement)1 Document (com.intellij.openapi.editor.Document)1 PsiFile (com.intellij.psi.PsiFile)1