Search in sources :

Example 66 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoImportPackageQuickFix method doAutoImportOrShowHint.

public boolean doAutoImportOrShowHint(@NotNull Editor editor, boolean showHint) {
    PsiElement element = getStartElement();
    if (element == null || !element.isValid())
        return false;
    PsiReference reference = getReference(element);
    if (reference == null || reference.resolve() != null)
        return false;
    List<String> packagesToImport = getImportPathVariantsToImport(element);
    if (packagesToImport.isEmpty()) {
        return false;
    }
    PsiFile file = element.getContainingFile();
    String firstPackageToImport = getFirstItem(packagesToImport);
    // autoimport on trying to fix
    if (packagesToImport.size() == 1) {
        if (GoCodeInsightSettings.getInstance().isAddUnambiguousImportsOnTheFly() && !LaterInvocator.isInModalContext() && (ApplicationManager.getApplication().isUnitTestMode() || DaemonListeners.canChangeFileSilently(file))) {
            CommandProcessor.getInstance().runUndoTransparentAction(() -> perform(file, firstPackageToImport));
            return true;
        }
    }
    // show hint on failed autoimport
    if (showHint) {
        if (ApplicationManager.getApplication().isUnitTestMode())
            return false;
        if (HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true))
            return false;
        if (!GoCodeInsightSettings.getInstance().isShowImportPopup())
            return false;
        TextRange referenceRange = reference.getRangeInElement().shiftRight(element.getTextRange().getStartOffset());
        HintManager.getInstance().showQuestionHint(editor, ShowAutoImportPass.getMessage(packagesToImport.size() > 1, getFirstItem(packagesToImport)), referenceRange.getStartOffset(), referenceRange.getEndOffset(), () -> {
            if (file.isValid() && !editor.isDisposed()) {
                perform(packagesToImport, file, editor);
            }
            return true;
        });
        return true;
    }
    return false;
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 67 with PsiElement

use of com.intellij.psi.PsiElement 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 68 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiElement position = parameters.getPosition();
    PsiFile file = parameters.getOriginalFile();
    ASTNode node = position.getNode();
    if (file instanceof GoFile && position.getParent() instanceof GoPackageClause && node.getElementType() == GoTypes.IDENTIFIER) {
        boolean isTestFile = GoTestFinder.isTestFile(file);
        PsiDirectory directory = file.getParent();
        String currentPackageName = ((GoFile) file).getPackageName();
        Collection<String> packagesInDirectory = GoPackageUtil.getAllPackagesInDirectory(directory, null, true);
        for (String packageName : packagesInDirectory) {
            if (!packageName.equals(currentPackageName)) {
                result.addElement(packageLookup(packageName, GoCompletionUtil.PACKAGE_PRIORITY - 1));
            }
            if (isTestFile) {
                result.addElement(packageLookup(packageName + GoConstants.TEST_SUFFIX, GoCompletionUtil.PACKAGE_PRIORITY));
            }
        }
        if (directory != null && ContainerUtil.filter(directory.getFiles(), Conditions.instanceOf(GoFile.class)).size() == 1) {
            String packageFromDirectory = GoPsiImplUtil.getLocalPackageName(directory.getName());
            if (!packageFromDirectory.isEmpty()) {
                result.addElement(packageLookup(packageFromDirectory, GoCompletionUtil.PACKAGE_PRIORITY - 1));
            }
        }
        result.addElement(packageLookup(GoConstants.MAIN, GoCompletionUtil.PACKAGE_PRIORITY - 2));
    }
    super.fillCompletionVariants(parameters, result);
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 69 with PsiElement

use of com.intellij.psi.PsiElement in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoKeywordCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    super.fillCompletionVariants(parameters, result);
    PsiElement position = parameters.getPosition();
    if (insideGoOrDeferStatements().accepts(position) || anonymousFunction().accepts(position)) {
        InsertHandler<LookupElement> insertHandler = GoKeywordCompletionProvider.createTemplateBasedInsertHandler("go_lang_anonymous_func");
        result.addElement(GoKeywordCompletionProvider.createKeywordLookupElement("func", CONTEXT_KEYWORD_PRIORITY, insertHandler));
    }
    if (referenceExpression().accepts(position)) {
        InsertHandler<LookupElement> insertHandler = GoKeywordCompletionProvider.createTemplateBasedInsertHandler("go_lang_anonymous_struct");
        result.addElement(GoKeywordCompletionProvider.createKeywordLookupElement("struct", CONTEXT_KEYWORD_PRIORITY, insertHandler));
    }
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement)

Example 70 with PsiElement

use of com.intellij.psi.PsiElement in project idea-handlebars by dmarcotte.

the class HbBlockMismatchAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof HbOpenBlockMustache) {
        HbOpenBlockMustache openBlockMustache = (HbOpenBlockMustache) element;
        HbMustacheName openBlockMustacheName = openBlockMustache.getBlockMustacheName();
        HbCloseBlockMustache closeBlockMustache = openBlockMustache.getPairedElement();
        if (closeBlockMustache != null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (openBlockMustacheName == null || closeBlockMustacheName == null) {
                return;
            }
            String openBlockName = openBlockMustacheName.getName();
            String closeBlockName = closeBlockMustacheName.getName();
            if (!openBlockName.equals(closeBlockName)) {
                Annotation openBlockAnnotation = holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.open.block", openBlockName, closeBlockName));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                Annotation closeBlockAnnotation = holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.close.block", openBlockName, closeBlockName));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
            }
        } else {
            holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.end.block", openBlockMustache.getName()));
        }
    }
    if (element instanceof HbCloseBlockMustache) {
        HbCloseBlockMustache closeBlockMustache = (HbCloseBlockMustache) element;
        PsiElement openBlockElement = closeBlockMustache.getPairedElement();
        if (openBlockElement == null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (closeBlockMustacheName == null) {
                return;
            }
            holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.start.block", closeBlockMustache.getName()));
        }
    }
}
Also used : HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) HbMustacheName(com.dmarcotte.handlebars.psi.HbMustacheName) HbCloseBlockMustache(com.dmarcotte.handlebars.psi.HbCloseBlockMustache) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3198 Nullable (org.jetbrains.annotations.Nullable)493 PsiFile (com.intellij.psi.PsiFile)474 NotNull (org.jetbrains.annotations.NotNull)442 TextRange (com.intellij.openapi.util.TextRange)239 PsiReference (com.intellij.psi.PsiReference)227 Project (com.intellij.openapi.project.Project)222 VirtualFile (com.intellij.openapi.vfs.VirtualFile)210 ArrayList (java.util.ArrayList)195 ASTNode (com.intellij.lang.ASTNode)142 XmlTag (com.intellij.psi.xml.XmlTag)134 PsiClass (com.intellij.psi.PsiClass)115 Editor (com.intellij.openapi.editor.Editor)112 Document (com.intellij.openapi.editor.Document)109 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)85 PsiDirectory (com.intellij.psi.PsiDirectory)80 IElementType (com.intellij.psi.tree.IElementType)78 Module (com.intellij.openapi.module.Module)77 PsiMethod (com.intellij.psi.PsiMethod)73 UsageInfo (com.intellij.usageView.UsageInfo)70