Search in sources :

Example 21 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.

the class XPathAnnotator method checkFunctionCall.

private static void checkFunctionCall(AnnotationHolder holder, XPathFunctionCall call, @NotNull ContextProvider contextProvider) {
    final ASTNode node = call.getNode().findChildByType(XPathTokenTypes.FUNCTION_NAME);
    if (node == null) {
        return;
    }
    final QName name = contextProvider.getQName(call);
    final XPathFunction function = call.resolve();
    final Function functionDecl = function != null ? function.getDeclaration() : null;
    if (functionDecl == null) {
        final PrefixedNameImpl qName = (PrefixedNameImpl) call.getQName();
        // need special check for extension functions
        if (call.getQName().getPrefix() != null && contextProvider.getFunctionContext().allowsExtensions()) {
            final PsiReference[] references = call.getReferences();
            if (references.length > 1 && references[1].resolve() == null) {
                final Annotation ann = holder.createErrorAnnotation(qName.getPrefixNode(), "Extension namespace prefix '" + qName.getPrefix() + "' has not been declared");
                ann.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
            } else if (name != null) {
                final String extNS = name.getNamespaceURI();
                if (!StringUtil.isEmpty(extNS)) {
                    final Set<Pair<QName, Integer>> pairs = contextProvider.getFunctionContext().getFunctions().keySet();
                    for (Pair<QName, Integer> pair : pairs) {
                        // extension namespace is known
                        final String uri = pair.first.getNamespaceURI();
                        if (uri != null && uri.equals(extNS)) {
                            holder.createWarningAnnotation(node, "Unknown function '" + name + "'");
                        }
                    }
                }
            }
        } else {
            if (name != null) {
                holder.createWarningAnnotation(node, "Unknown function '" + name + "'");
            } else if (qName.getPrefixNode() != null) {
                final Annotation ann = holder.createErrorAnnotation(qName.getPrefixNode(), "Extension namespace prefix '" + qName.getPrefix() + "' has not been declared");
                ann.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
            }
        }
    } else {
        final XPathExpression[] arguments = call.getArgumentList();
        for (int i = 0; i < arguments.length; i++) {
            checkArgument(holder, arguments[i], i, functionDecl.getParameters());
        }
        if (arguments.length < functionDecl.getMinArity()) {
            if (functionDecl.getMinArity() == 1) {
                holder.createErrorAnnotation(node, "Missing argument for function '" + name + "'");
            } else {
                final Parameter last = functionDecl.getParameters()[functionDecl.getParameters().length - 1];
                final String atLeast = last.kind == Parameter.Kind.OPTIONAL || last.kind == Parameter.Kind.VARARG ? "at least " : "";
                holder.createErrorAnnotation(node, "Function '" + name + "' requires " + atLeast + functionDecl.getMinArity() + " arguments");
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) QName(javax.xml.namespace.QName) PsiReference(com.intellij.psi.PsiReference) Annotation(com.intellij.lang.annotation.Annotation) Function(org.intellij.lang.xpath.context.functions.Function) PrefixedNameImpl(org.intellij.lang.xpath.psi.impl.PrefixedNameImpl) ASTNode(com.intellij.lang.ASTNode) Parameter(org.intellij.lang.xpath.context.functions.Parameter) Pair(com.intellij.openapi.util.Pair)

Example 22 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.

the class DeleteUnusedParameterFix method deleteElement.

protected void deleteElement(@NotNull XsltParameter obj) throws IncorrectOperationException {
    final XsltTemplate template = XsltCodeInsightUtil.getTemplate(obj.getTag(), false);
    if (template == null || template.getMatchExpression() == null) {
        final SearchScope searchScope = obj.getResolveScope();
        for (PsiReference reference : ReferencesSearch.search(obj, searchScope, false)) {
            final XmlTag t = PsiTreeUtil.getContextOfType(reference.getElement(), XmlTag.class, true);
            if (t != null && XsltSupport.XSLT_NS.equals(t.getNamespace())) {
                assert "with-param".equals(t.getLocalName());
                t.delete();
            }
        }
    }
    super.deleteElement(obj);
}
Also used : SearchScope(com.intellij.psi.search.SearchScope) PsiReference(com.intellij.psi.PsiReference) XsltTemplate(org.intellij.lang.xpath.xslt.psi.XsltTemplate) XmlTag(com.intellij.psi.xml.XmlTag)

Example 23 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.

the class XmlEncodingReferenceProvider method extractFromContentAttribute.

public static PsiReference[] extractFromContentAttribute(final XmlAttributeValue value) {
    String text = value.getValue();
    int start = text.indexOf(CHARSET_PREFIX);
    if (start != -1) {
        start += CHARSET_PREFIX.length();
        int end = text.indexOf(';', start);
        if (end == -1)
            end = text.length();
        String charsetName = text.substring(start, end);
        TextRange textRange = new TextRange(start, end).shiftRight(xmlAttributeValueRange(value).getStartOffset());
        return new PsiReference[] { new XmlEncodingReference(value, charsetName, textRange, 0) };
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange)

Example 24 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.

the class ExtractClosureFromClosureProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    final GrVariable var = (GrVariable) myHelper.getToSearchFor();
    if (var != null) {
        final List<UsageInfo> result = new ArrayList<>();
        for (PsiReference ref : ReferencesSearch.search(var)) {
            final PsiElement element = ref.getElement();
            if (element.getLanguage() != GroovyLanguage.INSTANCE) {
                result.add(new OtherLanguageUsageInfo(ref));
                continue;
            }
            final GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
            if (call == null)
                continue;
            result.add(new ExternalUsageInfo(element));
        }
        return result.toArray(new UsageInfo[result.size()]);
    }
    return UsageInfo.EMPTY_ARRAY;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with PsiReference

use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.

the class GroovyInlineLocalHandler method createSettings.

/**
   * Returns Settings object for referenced definition in case of local variable
   */
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
    final String localName = variable.getName();
    final Project project = variable.getProject();
    GrExpression initializer = null;
    Instruction writeInstr = null;
    Instruction[] flow = null;
    //search for initializer to inline
    if (invokedOnReference) {
        LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
        final PsiReference ref = TargetElementUtil.findReference(editor);
        LOG.assertTrue(ref != null);
        PsiElement cur = ref.getElement();
        if (cur instanceof GrReferenceExpression) {
            GrControlFlowOwner controlFlowOwner;
            do {
                controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
                if (controlFlowOwner == null)
                    break;
                flow = controlFlowOwner.getControlFlow();
                final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
                final PsiElement finalCur = cur;
                Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
                LOG.assertTrue(instruction != null);
                final BitSet prev = writes.get(instruction.num());
                if (prev.cardinality() == 1) {
                    writeInstr = flow[prev.nextSetBit(0)];
                    final PsiElement element = writeInstr.getElement();
                    if (element instanceof GrVariable) {
                        initializer = ((GrVariable) element).getInitializerGroovy();
                    } else if (element instanceof GrReferenceExpression) {
                        initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
                    }
                }
                PsiElement old_cur = cur;
                if (controlFlowOwner instanceof GrClosableBlock) {
                    cur = controlFlowOwner;
                } else {
                    PsiElement parent = controlFlowOwner.getParent();
                    if (parent instanceof GrMember)
                        cur = ((GrMember) parent).getContainingClass();
                }
                if (cur == old_cur)
                    break;
            } while (initializer == null);
        }
    } else {
        flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
        initializer = variable.getInitializerGroovy();
        writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
    }
    if (initializer == null || writeInstr == null) {
        String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
        CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
        return null;
    }
    int writeInstructionNumber = writeInstr.num();
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
    RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
    if (dialog.showAndGet()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) TypeInferenceHelper(org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Logger(com.intellij.openapi.diagnostic.Logger) HelpID(com.intellij.refactoring.HelpID) InlineActionHandler(com.intellij.lang.refactoring.InlineActionHandler) PsiReference(com.intellij.psi.PsiReference) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) BitSet(java.util.BitSet) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) BitSet(java.util.BitSet) PsiReference(com.intellij.psi.PsiReference) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiReference (com.intellij.psi.PsiReference)564 PsiElement (com.intellij.psi.PsiElement)327 NotNull (org.jetbrains.annotations.NotNull)97 Nullable (org.jetbrains.annotations.Nullable)55 TextRange (com.intellij.openapi.util.TextRange)54 PsiFile (com.intellij.psi.PsiFile)52 ArrayList (java.util.ArrayList)46 Test (org.junit.Test)40 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)36 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)32 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)25 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)23 XmlTag (com.intellij.psi.xml.XmlTag)22 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)20 PsiClass (com.intellij.psi.PsiClass)17 XmlAttribute (com.intellij.psi.xml.XmlAttribute)17 LinkedList (java.util.LinkedList)17 LookupElement (com.intellij.codeInsight.lookup.LookupElement)16 Project (com.intellij.openapi.project.Project)16