Search in sources :

Example 11 with MavenDomProjectModel

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

the class IntroducePropertyDialog method createCenterPanel.

protected JComponent createCenterPanel() {
    myFieldNamePanel.setLayout(new BorderLayout());
    myNameField = new NameSuggestionsField(myProject);
    myNameChangedListener = new NameSuggestionsField.DataChanged() {

        public void dataChanged() {
            updateOkStatus();
        }
    };
    myNameField.addDataChangedListener(myNameChangedListener);
    myNameField.setSuggestions(getSuggestions());
    myFieldNamePanel.add(myNameField, BorderLayout.CENTER);
    List<MavenDomProjectModel> projects = getProjects();
    ComboBoxUtil.setModel(myMavenProjectsComboBox, new DefaultComboBoxModel(), projects, model -> {
        String projectName = model.getName().getStringValue();
        MavenProject mavenProject = MavenDomUtil.findProject(model);
        if (mavenProject != null) {
            projectName = mavenProject.getDisplayName();
        }
        if (StringUtil.isEmptyOrSpaces(projectName)) {
            projectName = "pom.xml";
        }
        return Pair.create(projectName, model);
    });
    myMavenProjectsComboBox.setSelectedItem(myMavenDomProjectModel);
    return myMainPanel;
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenProject(org.jetbrains.idea.maven.project.MavenProject) NameSuggestionsField(com.intellij.refactoring.ui.NameSuggestionsField)

Example 12 with MavenDomProjectModel

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

the class MavenDependencyCompletionAndResolutionTest method testChooseFileIntentionForSystemDependency.

public void testChooseFileIntentionForSystemDependency() throws Throwable {
    createProjectPom("<groupId>test</groupId>" + "<artifactId>project</artifactId>" + "<version>1</version>" + "<dependencies>" + "  <dependency><caret>" + "    <groupId>xxx</groupId>" + "    <artifactId>xxx</artifactId>" + "    <version>xxx</version>" + "    <scope>system</system>" + "  </dependency>" + "</dependencies>");
    IntentionAction action = getIntentionAtCaret("Choose File");
    assertNotNull(action);
    String libPath = myIndicesFixture.getRepositoryHelper().getTestDataPath("local1/junit/junit/4.0/junit-4.0.jar");
    final VirtualFile libFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(libPath);
    ((ChooseFileIntentionAction) ((IntentionActionWrapper) action).getDelegate()).setFileChooser(() -> new VirtualFile[] { libFile });
    XmlCodeStyleSettings xmlSettings = CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings().getCustomSettings(XmlCodeStyleSettings.class);
    int prevValue = xmlSettings.XML_TEXT_WRAP;
    try {
        // prevent file path from wrapping.
        xmlSettings.XML_TEXT_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
        myFixture.launchAction(action);
    } finally {
        xmlSettings.XML_TEXT_WRAP = prevValue;
        ((ChooseFileIntentionAction) ((IntentionActionWrapper) action).getDelegate()).setFileChooser(null);
    }
    MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, myProjectPom);
    MavenDomDependency dep = model.getDependencies().getDependencies().get(0);
    assertEquals(findPsiFile(libFile), dep.getSystemPath().getValue());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) ChooseFileIntentionAction(org.jetbrains.idea.maven.dom.intentions.ChooseFileIntentionAction) ChooseFileIntentionAction(org.jetbrains.idea.maven.dom.intentions.ChooseFileIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) XmlCodeStyleSettings(com.intellij.psi.formatter.xml.XmlCodeStyleSettings) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Example 13 with MavenDomProjectModel

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

the class MavenResourceCompilerConfigurationGenerator method generateManifest.

private static void generateManifest(@NotNull MavenProject mavenProject, @NotNull Module module, @NotNull MavenModuleResourceConfiguration resourceConfig) {
    if (mavenProject.isAggregator())
        return;
    if (Boolean.valueOf(IDEA_MAVEN_DISABLE_MANIFEST)) {
        resourceConfig.manifest = null;
        return;
    }
    try {
        String jdkVersion = null;
        Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
        if (sdk != null && (jdkVersion = sdk.getVersionString()) != null) {
            final int quoteIndex = jdkVersion.indexOf('"');
            if (quoteIndex != -1) {
                jdkVersion = jdkVersion.substring(quoteIndex + 1, jdkVersion.length() - 1);
            }
        }
        Manifest manifest = new ManifestBuilder(mavenProject).withJdkVersion(jdkVersion).build();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            manifest.write(outputStream);
            MavenDomProjectModel domModel = MavenDomUtil.getMavenDomProjectModel(module.getProject(), mavenProject.getFile());
            if (domModel != null) {
                final String resolvedText = MavenPropertyResolver.resolve(outputStream.toString(CharsetToolkit.UTF8), domModel);
                resourceConfig.manifest = Base64.getEncoder().encodeToString(resolvedText.getBytes(StandardCharsets.UTF_8));
            }
        } finally {
            StreamUtil.closeStream(outputStream);
        }
        resourceConfig.classpath = ManifestBuilder.getClasspath(mavenProject);
    } catch (ManifestBuilder.ManifestBuilderException e) {
        LOG.warn("Unable to generate artifact manifest", e);
    } catch (Exception e) {
        LOG.warn("Unable to save generated artifact manifest", e);
    }
}
Also used : ManifestBuilder(org.jetbrains.idea.maven.utils.ManifestBuilder) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) Sdk(com.intellij.openapi.projectRoots.Sdk) Manifest(java.util.jar.Manifest)

Example 14 with MavenDomProjectModel

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

the class IntroducePropertyDialog method getSuggestions.

private String[] getSuggestions(int level) {
    Collection<String> result = new THashSet<>();
    String value = mySelectedString.trim();
    boolean addUnqualifiedForm = true;
    XmlTag parent = PsiTreeUtil.getParentOfType(myContext, XmlTag.class, false);
    DomElement domParent = DomUtil.getDomElement(parent);
    if (domParent != null) {
        DomElement domSuperParent = domParent.getParent();
        DomFileElement<DomElement> domFile = DomUtil.getFileElement(domParent);
        if (domSuperParent != null && domFile != null && domFile.getRootElement() == domSuperParent) {
            value = domSuperParent.getXmlElementName();
            addUnqualifiedForm = false;
        } else {
            MavenDomShortArtifactCoordinates coordinates = DomUtil.getParentOfType(domParent, MavenDomShortArtifactCoordinates.class, false);
            if (coordinates != null && !(coordinates instanceof MavenDomProjectModel) && domParent != coordinates.getArtifactId()) {
                String artifactId = coordinates.getArtifactId().getStringValue();
                if (!StringUtil.isEmptyOrSpaces(artifactId)) {
                    value = artifactId;
                    addUnqualifiedForm = false;
                }
            }
        }
    }
    while (true) {
        String newValue = value.replaceAll("  ", " ");
        if (newValue.equals(value))
            break;
        value = newValue;
    }
    value = value.replaceAll(" ", ".");
    List<String> parts = StringUtil.split(value, ".");
    String shortValue = parts.get(parts.size() - 1);
    if (addUnqualifiedForm) {
        result.add(value);
        result.add(shortValue);
    }
    String suffix = "";
    while (parent != null && level != 0) {
        suffix = parent.getName() + suffix;
        result.add(suffix);
        result.add(value + "." + suffix);
        result.add(shortValue + "." + suffix);
        suffix = "." + suffix;
        parent = parent.getParentTag();
        level--;
    }
    result = new ArrayList<>(result);
    Collections.sort((List) result, CodeStyleSettingsManager.getSettings(myProject).PREFER_LONGER_NAMES ? StringLenComparator.getDescendingInstance() : StringLenComparator.getInstance());
    return ArrayUtil.toStringArray(result);
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) DomElement(com.intellij.util.xml.DomElement) MavenDomShortArtifactCoordinates(org.jetbrains.idea.maven.dom.model.MavenDomShortArtifactCoordinates) THashSet(gnu.trove.THashSet) XmlTag(com.intellij.psi.xml.XmlTag)

Example 15 with MavenDomProjectModel

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

the class MavenPropertyPsiReference method doResolve.

// See org.apache.maven.project.interpolation.AbstractStringBasedModelInterpolator.createValueSources()
@Nullable
protected PsiElement doResolve() {
    boolean hasPrefix = false;
    String unprefixed = myText;
    if (myText.startsWith("pom.")) {
        unprefixed = myText.substring("pom.".length());
        hasPrefix = true;
    } else if (myText.startsWith("project.")) {
        unprefixed = myText.substring("project.".length());
        hasPrefix = true;
    }
    MavenProject mavenProject = myMavenProject;
    while (unprefixed.startsWith("parent.")) {
        if (unprefixed.equals("parent.groupId") || unprefixed.equals("parent.artifactId") || unprefixed.equals("parent.version") || unprefixed.equals("parent.relativePath")) {
            break;
        }
        MavenId parentId = mavenProject.getParentId();
        if (parentId == null)
            return null;
        mavenProject = myProjectsManager.findProject(parentId);
        if (mavenProject == null)
            return null;
        unprefixed = unprefixed.substring("parent.".length());
    }
    if (unprefixed.equals("basedir") || (hasPrefix && mavenProject == myMavenProject && unprefixed.equals("baseUri"))) {
        return getBaseDir(mavenProject);
    }
    if (myText.equals(TIMESTAMP_PROP)) {
        return myElement;
    }
    if (hasPrefix) {
        MavenDomProjectModel domProjectModel = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
        if (domProjectModel != null) {
            PsiElement res = resolveModelProperty(domProjectModel, unprefixed, new HashSet<>());
            if (res != null) {
                return res;
            }
        }
    }
    // todo resolve properties from config.
    MavenRunnerSettings runnerSettings = MavenRunner.getInstance(myProject).getSettings();
    if (runnerSettings.getMavenProperties().containsKey(myText) || runnerSettings.getVmOptions().contains("-D" + myText + '=')) {
        return myElement;
    }
    if (MavenUtil.getPropertiesFromMavenOpts().containsKey(myText)) {
        return myElement;
    }
    MavenDomProfile profile = DomUtil.findDomElement(myElement, MavenDomProfile.class);
    if (profile != null) {
        PsiElement result = MavenDomProjectProcessorUtils.findProperty(profile.getProperties(), myText);
        if (result != null)
            return result;
    }
    MavenDomConfiguration pluginCfg = DomUtil.findDomElement(myElement, MavenDomConfiguration.class);
    if (pluginCfg != null) {
        boolean notFound = MavenPluginDescriptor.processDescriptors(descriptor -> {
            if (descriptor.properties != null) {
                for (MavenPluginDescriptor.ModelProperty property : descriptor.properties) {
                    if (property.insideConfigurationOnly && property.name.equals(myText)) {
                        return false;
                    }
                }
            }
            return true;
        }, pluginCfg);
        if (!notFound) {
            return myElement;
        }
    }
    if (myProjectDom != null) {
        PsiElement result = MavenDomProjectProcessorUtils.searchProperty(myText, myProjectDom, myProject);
        if (result != null)
            return result;
    }
    if ("java.home".equals(myText)) {
        PsiElement element = resolveToCustomSystemProperty("java.home", MavenUtil.getModuleJreHome(myProjectsManager, mavenProject));
        if (element != null) {
            return element;
        }
    }
    if ("java.version".equals(myText)) {
        PsiElement element = resolveToCustomSystemProperty("java.version", MavenUtil.getModuleJavaVersion(myProjectsManager, mavenProject));
        if (element != null) {
            return element;
        }
    }
    MavenPropertiesVirtualFileSystem mavenPropertiesVirtualFileSystem = MavenPropertiesVirtualFileSystem.getInstance();
    IProperty property = mavenPropertiesVirtualFileSystem.findSystemProperty(myProject, myText);
    if (property != null)
        return property.getPsiElement();
    if (myText.startsWith("env.")) {
        property = mavenPropertiesVirtualFileSystem.findEnvProperty(myProject, myText.substring("env.".length()));
        if (property != null)
            return property.getPsiElement();
    }
    String textWithEnv = "env." + myText;
    property = mavenPropertiesVirtualFileSystem.findSystemProperty(myProject, textWithEnv);
    if (property != null)
        return property.getPsiElement();
    property = mavenPropertiesVirtualFileSystem.findEnvProperty(myProject, textWithEnv);
    if (property != null)
        return property.getPsiElement();
    if (!hasPrefix) {
        MavenDomProjectModel domProjectModel = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
        if (domProjectModel != null) {
            PsiElement res = resolveModelProperty(domProjectModel, unprefixed, new HashSet<>());
            if (res != null) {
                return res;
            }
        }
    }
    if (mavenProject.getProperties().containsKey(myText)) {
        return myElement;
    }
    if (myText.startsWith("settings.")) {
        return resolveSettingsModelProperty();
    }
    return null;
}
Also used : MavenRunnerSettings(org.jetbrains.idea.maven.execution.MavenRunnerSettings) MavenPropertiesVirtualFileSystem(org.jetbrains.idea.maven.vfs.MavenPropertiesVirtualFileSystem) MavenDomProfile(org.jetbrains.idea.maven.dom.model.MavenDomProfile) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenDomConfiguration(org.jetbrains.idea.maven.dom.model.MavenDomConfiguration) MavenProject(org.jetbrains.idea.maven.project.MavenProject) IProperty(com.intellij.lang.properties.IProperty) MavenPluginDescriptor(org.jetbrains.idea.maven.plugins.api.MavenPluginDescriptor) 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