Search in sources :

Example 56 with PsiElement

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

the class GoUnresolvedFixBase method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (editor == null) {
        LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile()));
        return;
    }
    PsiElement reference = PsiTreeUtil.getNonStrictParentOfType(startElement, GoReferenceExpressionBase.class);
    PsiElement anchor = reference != null ? findAnchor(reference) : null;
    if (anchor == null) {
        LOG.error("Cannot find anchor for " + myWhat + " (GoUnresolvedFixBase), offset: " + editor.getCaretModel().getOffset(), AttachmentFactory.createAttachment(file.getVirtualFile()));
        return;
    }
    Template template = TemplateSettings.getInstance().getTemplateById(myTemplateId);
    if (template == null) {
        LOG.error("Cannot find anchor for " + myWhat + " (GoUnresolvedFixBase), offset: " + editor.getCaretModel().getOffset(), AttachmentFactory.createAttachment(file.getVirtualFile()));
        return;
    }
    int start = anchor.getTextRange().getStartOffset();
    editor.getCaretModel().moveToOffset(start);
    template.setToReformat(true);
    TemplateManager.getInstance(project).startTemplate(editor, template, true, ContainerUtil.stringMap("NAME", myName), null);
}
Also used : LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) Template(com.intellij.codeInsight.template.Template)

Example 57 with PsiElement

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

the class GoUnusedFunctionInspection method buildGoVisitor.

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
    return new GoVisitor() {

        @Override
        public void visitFunctionDeclaration(@NotNull GoFunctionDeclaration o) {
            if (o.isBlank())
                return;
            GoFile file = o.getContainingFile();
            String name = o.getName();
            if (!canRun(name))
                return;
            if (GoConstants.MAIN.equals(file.getPackageName()) && GoConstants.MAIN.equals(name))
                return;
            if (GoConstants.INIT.equals(name))
                return;
            if (GoTestFinder.isTestFile(file) && GoTestFunctionType.fromName(name) != null)
                return;
            if (ReferencesSearch.search(o, o.getUseScope()).findFirst() == null) {
                PsiElement id = o.getIdentifier();
                TextRange range = TextRange.from(id.getStartOffsetInParent(), id.getTextLength());
                holder.registerProblem(o, "Unused function <code>#ref</code> #loc", ProblemHighlightType.LIKE_UNUSED_SYMBOL, range, new GoDeleteQuickFix("Delete function", GoFunctionDeclaration.class), new GoRenameToBlankQuickFix(o));
            }
        }
    };
}
Also used : GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) GoFile(com.goide.psi.GoFile) GoRenameToBlankQuickFix(com.goide.quickfix.GoRenameToBlankQuickFix) TextRange(com.intellij.openapi.util.TextRange) GoDeleteQuickFix(com.goide.quickfix.GoDeleteQuickFix) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 58 with PsiElement

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

the class GoMoveToStructInitializationIntention method isUninitializedFieldReferenceExpression.

@Contract("null, _-> false")
private static boolean isUninitializedFieldReferenceExpression(@Nullable GoReferenceExpression fieldReferenceExpression, @NotNull GoCompositeLit structLiteral) {
    if (fieldReferenceExpression == null)
        return false;
    GoLiteralValue literalValue = structLiteral.getLiteralValue();
    PsiElement resolve = fieldReferenceExpression.resolve();
    return literalValue != null && isFieldDefinition(resolve) && !exists(literalValue.getElementList(), element -> isFieldInitialization(element, resolve));
}
Also used : PsiElement(com.intellij.psi.PsiElement) Contract(org.jetbrains.annotations.Contract)

Example 59 with PsiElement

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

the class GoMoveToStructInitializationIntention method getStructLiteral.

@Nullable
private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fieldReferenceExpression, @NotNull GoAssignmentStatement structAssignment) {
    GoVarDefinition varDefinition = ObjectUtils.tryCast(resolveQualifier(fieldReferenceExpression), GoVarDefinition.class);
    PsiElement field = fieldReferenceExpression.resolve();
    if (varDefinition == null || !isFieldDefinition(field) || !hasStructTypeWithField(varDefinition, (GoNamedElement) field)) {
        return null;
    }
    GoExpression structReferenceExpression = find(structAssignment.getLeftHandExprList().getExpressionList(), expression -> isResolvedTo(expression, varDefinition));
    if (structReferenceExpression == null)
        return null;
    GoExpression compositeLit = GoPsiImplUtil.getRightExpression(structAssignment, structReferenceExpression);
    return ObjectUtils.tryCast(compositeLit, GoCompositeLit.class);
}
Also used : PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 60 with PsiElement

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

the class GoMoveToStructInitializationIntention method getUninitializedSingleFieldReferences.

@NotNull
private static List<GoReferenceExpression> getUninitializedSingleFieldReferences(@NotNull GoAssignmentStatement assignment, @NotNull GoReferenceExpression fieldReferenceExpression, @NotNull GoCompositeLit compositeLit) {
    PsiElement resolve = resolveQualifier(fieldReferenceExpression);
    List<GoReferenceExpression> uninitializedFieldReferencesByQualifier = filter(getUninitializedFieldReferenceExpressions(assignment, compositeLit), e -> isResolvedTo(e.getQualifier(), resolve));
    MultiMap<PsiElement, GoReferenceExpression> resolved = groupBy(uninitializedFieldReferencesByQualifier, GoReferenceExpression::resolve);
    return map(filter(resolved.entrySet(), set -> set.getValue().size() == 1), set -> getFirstItem(set.getValue()));
}
Also used : BaseElementAtCaretIntentionAction(com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction) IncorrectOperationException(com.intellij.util.IncorrectOperationException) com.goide.psi(com.goide.psi) GoPsiImplUtil(com.goide.psi.impl.GoPsiImplUtil) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Contract(org.jetbrains.annotations.Contract) List(java.util.List) Nls(org.jetbrains.annotations.Nls) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) ObjectUtils(com.intellij.util.ObjectUtils) GoElementFactory(com.goide.psi.impl.GoElementFactory) NotNull(org.jetbrains.annotations.NotNull) MultiMap(com.intellij.util.containers.MultiMap) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

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