Search in sources :

Example 6 with StrutsManager

use of com.intellij.struts2.dom.struts.model.StrutsManager in project intellij-plugins by JetBrains.

the class GotoRelatedActionProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull final PsiElement psiElement) {
    PsiFile psiFile = psiElement.getContainingFile();
    if (psiFile == null)
        return Collections.emptyList();
    final PsiFile containingFile = psiFile.getOriginalFile();
    final String filename = containingFile.getName();
    final String extension = FileUtilRt.getExtension(filename);
    if (!SUPPORTED_EXTENSIONS.contains(extension)) {
        return Collections.emptyList();
    }
    final StrutsManager strutsManager = StrutsManager.getInstance(psiElement.getProject());
    final StrutsModel strutsModel = strutsManager.getCombinedModel(psiElement);
    if (strutsModel == null) {
        return Collections.emptyList();
    }
    final List<PsiFile> allFiles = containingFile.getViewProvider().getAllFiles();
    final Set<Action> actions = new HashSet<>();
    final List<GotoRelatedItem> items = new ArrayList<>();
    strutsModel.processActions(action -> {
        for (final Result result : action.getResults()) {
            final PathReference pathReference = result.getValue();
            if (pathReference == null) {
                continue;
            }
            final String path = pathReference.getPath();
            if (!path.endsWith(filename)) {
                continue;
            }
            final PsiElement resolve = pathReference.resolve();
            if (ContainerUtil.find(allFiles, resolve) == null) {
                continue;
            }
            if (!actions.contains(action)) {
                items.add(new DomGotoRelatedItem(action));
                actions.add(action);
            }
            final PsiClass actionClass = action.searchActionClass();
            if (actionClass == null) {
                continue;
            }
            final PsiMethod actionMethod = action.searchActionMethod();
            items.add(new GotoRelatedItem(actionMethod != null ? actionMethod : actionClass));
        }
        return true;
    });
    return items;
}
Also used : PathReference(com.intellij.openapi.paths.PathReference) Action(com.intellij.struts2.dom.struts.action.Action) PsiMethod(com.intellij.psi.PsiMethod) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) PsiClass(com.intellij.psi.PsiClass) Result(com.intellij.struts2.dom.struts.action.Result) DomGotoRelatedItem(com.intellij.codeInsight.navigation.DomGotoRelatedItem) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) PsiFile(com.intellij.psi.PsiFile) DomGotoRelatedItem(com.intellij.codeInsight.navigation.DomGotoRelatedItem) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with StrutsManager

use of com.intellij.struts2.dom.struts.model.StrutsManager in project intellij-plugins by JetBrains.

the class StrutsConstantManagerImpl method getStrutsModel.

/**
   * Determines best matching StrutsModel.
   *
   * @param psiFile Context file.
   * @return {@code null} if no model could be determined.
   */
@Nullable
private static StrutsModel getStrutsModel(final PsiFile psiFile) {
    final StrutsManager strutsManager = StrutsManager.getInstance(psiFile.getProject());
    final StrutsModel model;
    if (psiFile instanceof XmlFile && strutsManager.isStruts2ConfigFile((XmlFile) psiFile)) {
        model = strutsManager.getModelByFile((XmlFile) psiFile);
    } else {
        model = strutsManager.getCombinedModel(psiFile);
    }
    return model;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with StrutsManager

use of com.intellij.struts2.dom.struts.model.StrutsManager in project intellij-plugins by JetBrains.

the class Struts2ProblemFileHighlightFilter method value.

@Override
public boolean value(final VirtualFile virtualFile) {
    if (virtualFile.getFileType() != StdFileTypes.XML) {
        return false;
    }
    final Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
    if (module == null) {
        return false;
    }
    if (StrutsFacet.getInstance(module) == null) {
        return false;
    }
    final boolean isStrutsXml = ReadAction.compute(() -> {
        final StrutsManager strutsManager = StrutsManager.getInstance(project);
        final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        return psiFile instanceof XmlFile && strutsManager.isStruts2ConfigFile((XmlFile) psiFile) && strutsManager.getModelByFile((XmlFile) psiFile) != null;
    });
    if (isStrutsXml) {
        return true;
    }
    return ReadAction.compute(() -> {
        final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        return psiFile instanceof XmlFile && ValidatorManager.getInstance(project).isValidatorsFile((XmlFile) psiFile);
    });
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 9 with StrutsManager

use of com.intellij.struts2.dom.struts.model.StrutsManager in project intellij-plugins by JetBrains.

the class ActionAnnotatorBase method collectNavigationMarkers.

@Override
protected void collectNavigationMarkers(@NotNull final PsiElement element, final Collection<? super RelatedItemLineMarkerInfo> lineMarkerInfos) {
    if (!(element instanceof PsiIdentifier))
        return;
    final PsiClass clazz = getActionPsiClass(element.getParent());
    if (clazz == null || clazz.getNameIdentifier() != element) {
        return;
    }
    // do not run on non-public, abstract classes or interfaces
    if (clazz.isInterface() || clazz.isAnnotationType() || !clazz.hasModifierProperty(PsiModifier.PUBLIC) || clazz.hasModifierProperty(PsiModifier.ABSTRACT)) {
        return;
    }
    // short exit if Struts Facet not present
    final Module module = ModuleUtilCore.findModuleForPsiElement(clazz);
    if (module == null || StrutsFacet.getInstance(module) == null) {
        return;
    }
    final StrutsManager strutsManager = StrutsManager.getInstance(element.getProject());
    final StrutsModel strutsModel = strutsManager.getCombinedModel(module);
    if (strutsModel == null) {
        return;
    }
    installValidationTargets(element, lineMarkerInfos, clazz);
    final List<Action> actions = strutsModel.findActionsByClass(clazz);
    if (actions.isEmpty()) {
        return;
    }
    installActionTargets(element, lineMarkerInfos, actions);
    installActionMethods(lineMarkerInfos, clazz, actions);
}
Also used : Action(com.intellij.struts2.dom.struts.action.Action) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) Module(com.intellij.openapi.module.Module)

Example 10 with StrutsManager

use of com.intellij.struts2.dom.struts.model.StrutsManager in project intellij-plugins by JetBrains.

the class ActionPropertyReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull final PsiElement psiElement, @NotNull final ProcessingContext processingContext) {
    if (TaglibUtil.isDynamicExpression(((XmlAttributeValue) psiElement).getValue())) {
        return PsiReference.EMPTY_ARRAY;
    }
    final XmlTag tag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
    assert tag != null;
    final XmlTag actionTag = findEnclosingTag(tag, tag.getNamespacePrefix());
    if (actionTag == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final String actionName = Comparing.equal(actionTag.getLocalName(), "action") ? actionTag.getAttributeValue("name") : actionTag.getAttributeValue("action");
    if (actionName == null || TaglibUtil.isDynamicExpression(actionName)) {
        return PsiReference.EMPTY_ARRAY;
    }
    final StrutsManager strutsManager = StrutsManager.getInstance(psiElement.getProject());
    final StrutsModel strutsModel = strutsManager.getCombinedModel(psiElement);
    if (strutsModel == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final List<Action> actions = strutsModel.findActionsByName(actionName, actionTag.getAttributeValue("namespace"));
    if (actions.size() != 1) {
        return PsiReference.EMPTY_ARRAY;
    }
    final Action action = actions.get(0);
    return new BeanPropertyPathReferenceSet(psiElement, action.searchActionClass(), supportsReadOnlyProperties) {

        // TODO CTOR creates references eagerly, so we have to subclass here
        @Override
        public boolean isSoft() {
            return false;
        }
    }.getPsiReferences();
}
Also used : Action(com.intellij.struts2.dom.struts.action.Action) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) BeanPropertyPathReferenceSet(com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StrutsManager (com.intellij.struts2.dom.struts.model.StrutsManager)10 StrutsModel (com.intellij.struts2.dom.struts.model.StrutsModel)7 PsiFile (com.intellij.psi.PsiFile)5 XmlFile (com.intellij.psi.xml.XmlFile)5 NotNull (org.jetbrains.annotations.NotNull)5 Module (com.intellij.openapi.module.Module)4 Action (com.intellij.struts2.dom.struts.action.Action)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiElement (com.intellij.psi.PsiElement)2 PsiManager (com.intellij.psi.PsiManager)2 StrutsFileSet (com.intellij.struts2.facet.ui.StrutsFileSet)2 Nullable (org.jetbrains.annotations.Nullable)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 BaseIntentionAction (com.intellij.codeInsight.intention.impl.BaseIntentionAction)1 DomGotoRelatedItem (com.intellij.codeInsight.navigation.DomGotoRelatedItem)1 Annotation (com.intellij.lang.annotation.Annotation)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 Editor (com.intellij.openapi.editor.Editor)1 PathReference (com.intellij.openapi.paths.PathReference)1 Project (com.intellij.openapi.project.Project)1