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");
}
}
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations