use of com.intellij.psi.PsiRecursiveElementVisitor in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoRefactoringUtil method getOccurrences.
@NotNull
public static List<PsiElement> getOccurrences(@NotNull PsiElement pattern, @Nullable PsiElement context) {
if (context == null)
return Collections.emptyList();
List<PsiElement> occurrences = ContainerUtil.newArrayList();
PsiRecursiveElementVisitor visitor = new PsiRecursiveElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (PsiEquivalenceUtil.areElementsEquivalent(element, pattern)) {
occurrences.add(element);
return;
}
super.visitElement(element);
}
};
context.acceptChildren(visitor);
return occurrences;
}
use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-community by JetBrains.
the class XsltCodeInsightUtil method findVariableInsertionPoint.
public static XmlTag findVariableInsertionPoint(final XmlTag currentUsageTag, PsiElement usageBlock, final String referenceName, XmlTag... moreUsages) {
// sort tags by document order
final Set<XmlTag> usages = new TreeSet<>(POSITION_COMPARATOR);
usages.add(currentUsageTag);
ContainerUtil.addAll(usages, moreUsages);
// collect all other possible unresolved references with the same name in the current template
usageBlock.accept(new PsiRecursiveElementVisitor() {
public void visitElement(PsiElement element) {
if (element instanceof XPathVariableReference) {
visitXPathVariableReference(((XPathVariableReference) element));
} else {
super.visitElement(element);
}
}
private void visitXPathVariableReference(XPathVariableReference reference) {
if (referenceName.equals(reference.getReferencedName())) {
if (reference.resolve() == null) {
usages.add(PsiTreeUtil.getContextOfType(reference, XmlTag.class, true));
}
}
}
public void visitXmlAttribute(XmlAttribute attribute) {
if (XsltSupport.isXPathAttribute(attribute)) {
final PsiFile[] xpathFiles = XsltSupport.getFiles(attribute);
for (PsiFile xpathFile : xpathFiles) {
xpathFile.accept(this);
}
}
}
});
final Iterator<XmlTag> it = usages.iterator();
final XmlTag firstUsage = it.next();
// find broadest scope to create the variable in
XmlTag tag = firstUsage;
while (it.hasNext()) {
XmlTag xmlTag = it.next();
final PsiElement t = PsiTreeUtil.findCommonParent(tag, xmlTag);
if (t instanceof XmlTag) {
tag = (XmlTag) t;
} else {
break;
}
}
// find the actual tag to create the variable before
final XmlTag[] subTags = tag.getSubTags();
for (XmlTag xmlTag : subTags) {
if (xmlTag.getTextOffset() > firstUsage.getTextOffset())
break;
tag = xmlTag;
}
final XmlTag parentTag = tag.getParentTag();
if (parentTag == null)
return tag;
final String parentName = parentTag.getLocalName();
if ("apply-templates".equals(parentName) || "call-template".equals(parentName) || "when".equals(parentName) || "choose".equals(parentName)) {
if ("when".equals(parentName))
tag = tag.getParentTag();
assert tag != null;
tag = tag.getParentTag();
}
assert tag != null;
return tag;
}
use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-community by JetBrains.
the class AdvancedDtdOptions method prepareNamespaceMap.
public static Map<String, ?> prepareNamespaceMap(Project project, VirtualFile firstFile) {
final PsiFile file = PsiManager.getInstance(project).findFile(firstFile);
if (file == null) {
return Collections.emptyMap();
}
final HashMap<String, Object> map = new LinkedHashMap<>();
file.accept(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof XmlElementDecl) {
final String s = ((XmlElementDecl) element).getName();
if (s != null) {
final String[] parts = s.split(":");
if (parts.length > 1) {
map.put(XMLNS + ":" + parts[0], null);
}
}
} else if (element instanceof XmlAttributeDecl) {
final String s = ((XmlAttributeDecl) element).getName();
if (s != null) {
final String[] parts = s.split(":");
if (parts.length > 1) {
map.put(XMLNS + ":" + parts[0], null);
}
}
}
super.visitElement(element);
}
});
return map;
}
use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-community by JetBrains.
the class GroovyFormattingModelBuilder method createModel.
@Override
@NotNull
public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) {
ASTNode node = element.getNode();
assert node != null;
PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(GroovyLanguage.INSTANCE);
assert containingFile != null : element.getContainingFile();
ASTNode astNode = containingFile.getNode();
assert astNode != null;
CommonCodeStyleSettings groovySettings = settings.getCommonSettings(GroovyLanguage.INSTANCE);
GroovyCodeStyleSettings customSettings = settings.getCustomSettings(GroovyCodeStyleSettings.class);
final AlignmentProvider alignments = new AlignmentProvider();
if (customSettings.USE_FLYING_GEESE_BRACES) {
element.accept(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (GeeseUtil.isClosureRBrace(element)) {
GeeseUtil.calculateRBraceAlignment(element, alignments);
} else {
super.visitElement(element);
}
}
});
}
final GroovyBlock block = new GroovyBlock(astNode, Indent.getAbsoluteNoneIndent(), null, new FormattingContext(groovySettings, alignments, customSettings, false));
return new GroovyFormattingModel(containingFile, block, FormattingDocumentModelImpl.createOn(containingFile));
}
use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-elixir by KronicDeth.
the class ModuleAttribute method annotate.
/*
* Public Instance Methods
*/
/**
* Annotates the specified PSI element.
* It is guaranteed to be executed in non-reentrant fashion.
* I.e there will be no call of this method for this instance before previous call get completed.
* Multiple instances of the annotator might exist simultaneously, though.
*
* @param element to annotate.
* @param holder the container which receives annotations created by the plugin.
*/
@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
element.accept(new PsiRecursiveElementVisitor() {
/*
*
* Instance Methods
*
*/
/*
* Public Instance Methods
*/
@Override
public void visitElement(@NotNull final PsiElement element) {
if (element instanceof AtNonNumericOperation) {
visitMaybeUsage((AtNonNumericOperation) element);
} else if (element instanceof AtUnqualifiedNoParenthesesCall) {
visitDeclaration((AtUnqualifiedNoParenthesesCall) element);
}
}
/*
* Private Instance Methods
*/
private void visitDeclaration(@NotNull final AtUnqualifiedNoParenthesesCall atUnqualifiedNoParenthesesCall) {
ElixirAtIdentifier atIdentifier = atUnqualifiedNoParenthesesCall.getAtIdentifier();
TextRange textRange = atIdentifier.getTextRange();
String identifier = identifierName(atIdentifier);
if (isCallbackName(identifier)) {
highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
highlightCallback(atUnqualifiedNoParenthesesCall, holder);
} else if (isDocumentationName(identifier)) {
highlight(textRange, holder, ElixirSyntaxHighlighter.DOCUMENTATION_MODULE_ATTRIBUTE);
highlightDocumentationText(atUnqualifiedNoParenthesesCall, holder);
} else if (isTypeName(identifier)) {
highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
highlightType(atUnqualifiedNoParenthesesCall, holder);
} else if (isSpecificationName(identifier)) {
highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
highlightSpecification(atUnqualifiedNoParenthesesCall, holder);
} else {
highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
}
}
private void visitMaybeUsage(@NotNull final AtNonNumericOperation element) {
PsiElement operand = element.operand();
if (operand != null && !(operand instanceof ElixirAccessExpression)) {
visitUsage(element);
}
}
private void visitUsage(@NotNull final AtNonNumericOperation atNonNumericOperation) {
highlight(atNonNumericOperation.getTextRange(), holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
if (!isNonReferencing(atNonNumericOperation) && atNonNumericOperation.getReference().resolve() == null) {
holder.createErrorAnnotation(atNonNumericOperation, "Unresolved module attribute");
}
}
});
}
Aggregations