Search in sources :

Example 1 with XmlProcessingInstruction

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

the class ImportReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    if (element instanceof XmlProcessingInstruction) {
        final ASTNode importNode = element.getNode().findChildByType(XmlTokenType.XML_TAG_CHARACTERS);
        if (importNode != null) {
            final PsiElement importInstr = importNode.getPsi();
            final String instructionTarget = JavaFxPsiUtil.getInstructionTarget("import", (XmlProcessingInstruction) element);
            if (instructionTarget != null && instructionTarget.equals(importInstr.getText())) {
                final PsiReference[] references = FxmlReferencesContributor.CLASS_REFERENCE_PROVIDER.getReferencesByString(instructionTarget, element, importInstr.getStartOffsetInParent());
                if (instructionTarget.endsWith(".*")) {
                    return ArrayUtil.remove(references, references.length - 1);
                } else {
                    return references;
                }
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlProcessingInstruction(com.intellij.psi.xml.XmlProcessingInstruction) ASTNode(com.intellij.lang.ASTNode) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with XmlProcessingInstruction

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

the class JavaFxUnusedImportsInspection method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, final boolean isOnTheFly) {
    if (!JavaFxFileTypeFactory.isFxml(file))
        return null;
    final XmlDocument document = ((XmlFile) file).getDocument();
    if (document == null)
        return null;
    final Set<String> usedNames = new HashSet<>();
    file.accept(new JavaFxImportsOptimizer.JavaFxUsedClassesVisitor() {

        @Override
        protected void appendClassName(String fqn) {
            usedNames.add(fqn);
            final String packageName = StringUtil.getPackageName(fqn);
            if (!StringUtil.isEmpty(packageName)) {
                usedNames.add(packageName);
            }
        }

        @Override
        protected void appendDemandedPackageName(@NotNull String packageName) {
            usedNames.add(packageName);
        }
    });
    final InspectionManager inspectionManager = InspectionManager.getInstance(file.getProject());
    final List<ProblemDescriptor> problems = new ArrayList<>();
    final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(document.getProlog(), XmlProcessingInstruction.class);
    final Map<String, XmlProcessingInstruction> targetProcessingInstructions = new LinkedHashMap<>();
    for (XmlProcessingInstruction instruction : instructions) {
        final String target = JavaFxPsiUtil.getInstructionTarget("import", instruction);
        if (target != null) {
            targetProcessingInstructions.put(target, instruction);
        }
    }
    for (String target : targetProcessingInstructions.keySet()) {
        final XmlProcessingInstruction instruction = targetProcessingInstructions.get(target);
        if (target.endsWith(".*")) {
            if (!usedNames.contains(StringUtil.trimEnd(target, ".*"))) {
                problems.add(inspectionManager.createProblemDescriptor(instruction, "Unused import", new JavaFxOptimizeImportsFix(), ProblemHighlightType.LIKE_UNUSED_SYMBOL, isOnTheFly));
            }
        } else if (!usedNames.contains(target) || targetProcessingInstructions.containsKey(StringUtil.getPackageName(target) + ".*")) {
            problems.add(inspectionManager.createProblemDescriptor(instruction, "Unused import", new JavaFxOptimizeImportsFix(), ProblemHighlightType.LIKE_UNUSED_SYMBOL, isOnTheFly));
        }
    }
    return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) JavaFxImportsOptimizer(org.jetbrains.plugins.javaFX.fxml.codeInsight.JavaFxImportsOptimizer) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlProcessingInstruction(com.intellij.psi.xml.XmlProcessingInstruction) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlProcessingInstruction (com.intellij.psi.xml.XmlProcessingInstruction)2 ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 XmlDocument (com.intellij.psi.xml.XmlDocument)1 XmlFile (com.intellij.psi.xml.XmlFile)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 JavaFxImportsOptimizer (org.jetbrains.plugins.javaFX.fxml.codeInsight.JavaFxImportsOptimizer)1