Search in sources :

Example 1 with MavenDomProjectModel

use of org.jetbrains.idea.maven.dom.model.MavenDomProjectModel in project intellij-community by JetBrains.

the class MavenJUnitPatcher method patchJavaParameters.

private static void patchJavaParameters(@NotNull Module module, @NotNull JavaParameters javaParameters, @NotNull MavenProject mavenProject, @NotNull String plugin, @NotNull Element config) {
    MavenDomProjectModel domModel = MavenDomUtil.getMavenDomProjectModel(module.getProject(), mavenProject.getFile());
    MavenTestRunningSettings testRunningSettings = MavenProjectSettings.getInstance(module.getProject()).getTestRunningSettings();
    List<String> paths = MavenJDOMUtil.findChildrenValuesByPath(config, "additionalClasspathElements", "additionalClasspathElement");
    if (paths.size() > 0) {
        for (String path : paths) {
            javaParameters.getClassPath().add(resolvePluginProperties(plugin, path, domModel));
        }
    }
    if (testRunningSettings.isPassSystemProperties() && isEnabled(plugin, "systemPropertyVariables")) {
        Element systemPropertyVariables = config.getChild("systemPropertyVariables");
        if (systemPropertyVariables != null) {
            for (Element element : systemPropertyVariables.getChildren()) {
                String propertyName = element.getName();
                if (!javaParameters.getVMParametersList().hasProperty(propertyName)) {
                    String value = resolvePluginProperties(plugin, element.getValue(), domModel);
                    value = resolveVmProperties(javaParameters.getVMParametersList(), value);
                    if (isResolved(plugin, value)) {
                        javaParameters.getVMParametersList().addProperty(propertyName, value);
                    }
                }
            }
        }
    }
    if (testRunningSettings.isPassEnvironmentVariables() && isEnabled(plugin, "environmentVariables")) {
        Element environmentVariables = config.getChild("environmentVariables");
        if (environmentVariables != null) {
            for (Element element : environmentVariables.getChildren()) {
                String variableName = element.getName();
                if (!javaParameters.getEnv().containsKey(variableName)) {
                    String value = resolvePluginProperties(plugin, element.getValue(), domModel);
                    value = resolveVmProperties(javaParameters.getVMParametersList(), value);
                    if (isResolved(plugin, value)) {
                        javaParameters.addEnv(variableName, value);
                    }
                }
            }
        }
    }
    if (testRunningSettings.isPassArgLine() && isEnabled(plugin, "argLine")) {
        Element argLine = config.getChild("argLine");
        if (argLine != null) {
            String value = resolvePluginProperties(plugin, argLine.getTextTrim(), domModel);
            value = resolveVmProperties(javaParameters.getVMParametersList(), value);
            if (StringUtil.isNotEmpty(value) && isResolved(plugin, value)) {
                javaParameters.getVMParametersList().addParametersString(value);
            }
        }
    }
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenTestRunningSettings(org.jetbrains.idea.maven.project.MavenTestRunningSettings) Element(org.jdom.Element)

Example 2 with MavenDomProjectModel

use of org.jetbrains.idea.maven.dom.model.MavenDomProjectModel in project intellij-community by JetBrains.

the class MavenNavigationUtil method createNavigatableForDependency.

@Nullable
public static Navigatable createNavigatableForDependency(final Project project, final VirtualFile file, final MavenArtifact artifact) {
    return new NavigatableAdapter() {

        public void navigate(boolean requestFocus) {
            if (!file.isValid())
                return;
            MavenDomProjectModel projectModel = MavenDomUtil.getMavenDomProjectModel(project, file);
            if (projectModel == null)
                return;
            MavenDomDependency dependency = findDependency(projectModel, artifact.getGroupId(), artifact.getArtifactId());
            if (dependency == null)
                return;
            XmlTag artifactId = dependency.getArtifactId().getXmlTag();
            if (artifactId == null)
                return;
            navigate(project, artifactId.getContainingFile().getVirtualFile(), artifactId.getTextOffset() + artifactId.getName().length() + 2, requestFocus);
        }
    };
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) NavigatableAdapter(com.intellij.pom.NavigatableAdapter) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with MavenDomProjectModel

use of org.jetbrains.idea.maven.dom.model.MavenDomProjectModel in project intellij-community by JetBrains.

the class MavenModulePsiReference method getVariants.

@NotNull
public Object[] getVariants() {
    List<DomFileElement<MavenDomProjectModel>> files = MavenDomUtil.collectProjectModels(getProject());
    List<Object> result = new ArrayList<>();
    for (DomFileElement<MavenDomProjectModel> eachDomFile : files) {
        VirtualFile eachVFile = eachDomFile.getOriginalFile().getVirtualFile();
        if (Comparing.equal(eachVFile, myVirtualFile))
            continue;
        PsiFile psiFile = eachDomFile.getFile();
        String modulePath = calcRelativeModulePath(myVirtualFile, eachVFile);
        result.add(LookupElementBuilder.create(psiFile, modulePath).withPresentableText(modulePath));
    }
    return result.toArray();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) DomFileElement(com.intellij.util.xml.DomFileElement) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with MavenDomProjectModel

use of org.jetbrains.idea.maven.dom.model.MavenDomProjectModel in project intellij-community by JetBrains.

the class MavenPathReferenceConverter method createReferences.

public static PsiReference[] createReferences(final DomElement genericDomValue, PsiElement element, @NotNull final Condition<PsiFileSystemItem> fileFilter, boolean isAbsolutePath) {
    ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
    TextRange range = manipulator.getRangeInElement(element);
    String text = range.substring(element.getText());
    FileReferenceSet set = new FileReferenceSet(text, element, range.getStartOffset(), null, SystemInfo.isFileSystemCaseSensitive, false) {

        private MavenDomProjectModel model;

        @Override
        protected Condition<PsiFileSystemItem> getReferenceCompletionFilter() {
            return fileFilter;
        }

        @Override
        protected boolean isSoft() {
            return true;
        }

        @Override
        public FileReference createFileReference(TextRange range, int index, String text) {
            return new FileReference(this, range, index, text) {

                @Override
                protected void innerResolveInContext(@NotNull String text, @NotNull PsiFileSystemItem context, Collection<ResolveResult> result, boolean caseSensitive) {
                    if (model == null) {
                        DomElement rootElement = DomUtil.getFileElement(genericDomValue).getRootElement();
                        if (rootElement instanceof MavenDomProjectModel) {
                            model = (MavenDomProjectModel) rootElement;
                        }
                    }
                    String resolvedText = model == null ? text : MavenPropertyResolver.resolve(text, model);
                    if (resolvedText.equals(text)) {
                        if (getIndex() == 0 && resolvedText.length() == 2 && resolvedText.charAt(1) == ':') {
                            // it's root on windows, e.g. "C:"
                            VirtualFile file = LocalFileSystem.getInstance().findFileByPath(resolvedText + '/');
                            if (file != null) {
                                PsiDirectory psiDirectory = context.getManager().findDirectory(file);
                                if (psiDirectory != null) {
                                    result.add(new PsiElementResolveResult(psiDirectory));
                                }
                            }
                        } else {
                            super.innerResolveInContext(resolvedText, context, result, caseSensitive);
                        }
                    } else {
                        VirtualFile contextFile = context.getVirtualFile();
                        if (contextFile == null)
                            return;
                        VirtualFile file = null;
                        if (getIndex() == 0) {
                            file = LocalFileSystem.getInstance().findFileByPath(resolvedText);
                        }
                        if (file == null) {
                            file = LocalFileSystem.getInstance().findFileByPath(contextFile.getPath() + '/' + resolvedText);
                        }
                        if (file != null) {
                            PsiFileSystemItem res = file.isDirectory() ? context.getManager().findDirectory(file) : context.getManager().findFile(file);
                            if (res != null) {
                                result.add(new PsiElementResolveResult(res));
                            }
                        }
                    }
                }
            };
        }
    };
    if (isAbsolutePath) {
        set.addCustomization(FileReferenceSet.DEFAULT_PATH_EVALUATOR_OPTION, file -> {
            VirtualFile virtualFile = file.getVirtualFile();
            if (virtualFile == null) {
                return FileReferenceSet.ABSOLUTE_TOP_LEVEL.fun(file);
            }
            virtualFile = VfsUtil.getRootFile(virtualFile);
            PsiDirectory root = file.getManager().findDirectory(virtualFile);
            if (root == null) {
                return FileReferenceSet.ABSOLUTE_TOP_LEVEL.fun(file);
            }
            return Collections.<PsiFileSystemItem>singletonList(root);
        });
    }
    return set.getAllReferences();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) DomElement(com.intellij.util.xml.DomElement) Collection(java.util.Collection) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)

Example 5 with MavenDomProjectModel

use of org.jetbrains.idea.maven.dom.model.MavenDomProjectModel in project intellij-community by JetBrains.

the class MavenPropertyPsiReference method resolveModelProperty.

@Nullable
private PsiElement resolveModelProperty(@NotNull MavenDomProjectModel projectDom, @NotNull final String path, @NotNull final Set<DomElement> recursionGuard) {
    if (!recursionGuard.add(projectDom))
        return null;
    String pathWithProjectPrefix = "project." + path;
    if (!MavenModelClassesProperties.isPathValid(MavenModelClassesProperties.MAVEN_PROJECT_CLASS, path) && !MavenModelClassesProperties.isPathValid(MavenModelClassesProperties.MAVEN_MODEL_CLASS, path)) {
        if (!schemaHasProperty(MavenSchemaProvider.MAVEN_PROJECT_SCHEMA_URL, pathWithProjectPrefix))
            return null;
    }
    PsiElement result = MavenDomUtil.findTag(projectDom, pathWithProjectPrefix);
    if (result != null)
        return result;
    if (pathWithProjectPrefix.equals("project.groupId") || pathWithProjectPrefix.equals("project.version")) {
        return MavenDomUtil.findTag(projectDom, "project.parent." + path);
    }
    result = new MavenDomProjectProcessorUtils.DomParentProjectFileProcessor<PsiElement>(myProjectsManager) {

        protected PsiElement doProcessParent(VirtualFile parentFile) {
            MavenDomProjectModel parentProjectDom = MavenDomUtil.getMavenDomProjectModel(myProject, parentFile);
            if (parentProjectDom == null)
                return null;
            return resolveModelProperty(parentProjectDom, path, recursionGuard);
        }
    }.process(projectDom);
    if (result != null)
        return result;
    return myElement;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

MavenDomProjectModel (org.jetbrains.idea.maven.dom.model.MavenDomProjectModel)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 NotNull (org.jetbrains.annotations.NotNull)5 MavenDomDependency (org.jetbrains.idea.maven.dom.model.MavenDomDependency)5 Nullable (org.jetbrains.annotations.Nullable)4 PsiFile (com.intellij.psi.PsiFile)3 XmlTag (com.intellij.psi.xml.XmlTag)3 DomElement (com.intellij.util.xml.DomElement)3 MavenProject (org.jetbrains.idea.maven.project.MavenProject)3 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 MavenId (org.jetbrains.idea.maven.model.MavenId)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 IProperty (com.intellij.lang.properties.IProperty)1 Project (com.intellij.openapi.project.Project)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1