Search in sources :

Example 61 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class InconsistentPropertiesEndsInspectionProvider method check.

@Override
public void check(BidirectionalMap<PropertiesFile, PropertiesFile> parents, List<PropertiesFile> files, Map<PropertiesFile, Set<String>> keysUpToParent, Map<PropertiesFile, Map<String, String>> propertiesFilesNamesMaps, InspectionManager manager, RefManager refManager, ProblemDescriptionsProcessor processor) {
    for (PropertiesFile file : files) {
        final Set<String> filePropertyKeys = new THashSet<>(propertiesFilesNamesMaps.get(file).keySet());
        PropertiesFile parent = parents.get(file);
        while (parent != null) {
            final Collection<String> commonKeys = ContainerUtil.intersection(propertiesFilesNamesMaps.get(parent).keySet(), filePropertyKeys);
            for (final String commonKey : commonKeys) {
                final IProperty property = file.findPropertyByKey(commonKey);
                assert property != null;
                final String propertyValue = property.getValue();
                if (StringUtil.isEmptyOrSpaces(propertyValue)) {
                    continue;
                }
                final char lastChar = propertyValue.charAt(propertyValue.length() - 1);
                if (!PROPERTY_VALUE_END_CHECK_SYMBOLS.contains(lastChar)) {
                    continue;
                }
                final String parentPropertyValue = propertiesFilesNamesMaps.get(parent).get(commonKey);
                if (parentPropertyValue == null) {
                    continue;
                }
                final char parentLastChar = parentPropertyValue.charAt(parentPropertyValue.length() - 1);
                if (lastChar != parentLastChar) {
                    final String message;
                    if (PROPERTY_VALUE_END_CHECK_SYMBOLS.contains(parentLastChar)) {
                        message = InspectionsBundle.message("inconsistent.bundle.property.inconsistent.end.parent.end.from.check.symbols", lastChar, parentLastChar, parent.getName());
                    } else {
                        message = InspectionsBundle.message("inconsistent.bundle.property.inconsistent.end", lastChar);
                    }
                    final PsiElement propertyPsiElement = property.getPsiElement();
                    processor.addProblemElement(refManager.getReference(file.getContainingFile()), manager.createProblemDescriptor(propertyPsiElement, message, true, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                }
            }
            filePropertyKeys.removeAll(commonKeys);
            parent = parents.get(parent);
        }
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) THashSet(gnu.trove.THashSet) PsiElement(com.intellij.psi.PsiElement)

Example 62 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class MavenFilteredPropertyPsiReference method doResolve.

@Override
protected PsiElement doResolve() {
    PsiElement result = super.doResolve();
    if (result != null)
        return result;
    for (String each : myMavenProject.getFilterPropertiesFiles()) {
        VirtualFile file = LocalFileSystem.getInstance().findFileByPath(each);
        if (file == null)
            continue;
        IProperty property = MavenDomUtil.findProperty(myProject, file, myText);
        if (property != null)
            return property.getPsiElement();
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IProperty(com.intellij.lang.properties.IProperty) PsiElement(com.intellij.psi.PsiElement)

Example 63 with IProperty

use of com.intellij.lang.properties.IProperty 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)

Example 64 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class GotoPropertyDeclarationsProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull DataContext context) {
    final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
    if (!(editor instanceof ResourceBundleEditor)) {
        return Collections.emptyList();
    }
    final ResourceBundleEditor resourceBundleEditor = (ResourceBundleEditor) editor;
    final Collection<ResourceBundleEditorViewElement> elements = resourceBundleEditor.getSelectedElements();
    if (elements.size() != 1) {
        return Collections.emptyList();
    }
    final IProperty[] properties = ContainerUtil.getFirstItem(elements).getProperties();
    if (properties == null || properties.length != 1 || !(properties[0] instanceof Property)) {
        return Collections.emptyList();
    }
    final IProperty property = properties[0];
    final String propertyKey = property.getKey();
    final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(property.getPsiElement().getContainingFile());
    assert file != null;
    final ResourceBundle resourceBundle = file.getResourceBundle();
    return ContainerUtil.mapNotNull(resourceBundle.getPropertiesFiles(), (NullableFunction<PropertiesFile, GotoRelatedItem>) f -> {
        final IProperty foundProperty = f.findPropertyByKey(propertyKey);
        return foundProperty == null ? null : new GotoRelatedItem(foundProperty.getPsiElement(), "Property Declarations");
    });
}
Also used : Property(com.intellij.lang.properties.psi.Property) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) DataContext(com.intellij.openapi.actionSystem.DataContext) Collection(java.util.Collection) NullableFunction(com.intellij.util.NullableFunction) ContainerUtil(com.intellij.util.containers.ContainerUtil) FileEditor(com.intellij.openapi.fileEditor.FileEditor) GotoRelatedProvider(com.intellij.navigation.GotoRelatedProvider) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PlatformDataKeys(com.intellij.openapi.actionSystem.PlatformDataKeys) PropertiesImplUtil(com.intellij.lang.properties.PropertiesImplUtil) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) ResourceBundle(com.intellij.lang.properties.ResourceBundle) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) FileEditor(com.intellij.openapi.fileEditor.FileEditor) IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class PropertiesPsiCompletionUtil method getPropertiesKeys.

static Set<Object> getPropertiesKeys(final PropertyReferenceBase propertyReference) {
    final Set<Object> variants = new THashSet<>(new TObjectHashingStrategy<Object>() {

        public int computeHashCode(final Object object) {
            if (object instanceof IProperty) {
                final String key = ((IProperty) object).getKey();
                return key == null ? 0 : key.hashCode();
            } else {
                return 0;
            }
        }

        public boolean equals(final Object o1, final Object o2) {
            return o1 instanceof IProperty && o2 instanceof IProperty && Comparing.equal(((IProperty) o1).getKey(), ((IProperty) o2).getKey(), true);
        }
    });
    List<PropertiesFile> propertiesFileList = propertyReference.getPropertiesFiles();
    if (propertiesFileList == null) {
        PropertiesReferenceManager.getInstance(propertyReference.getElement().getProject()).processAllPropertiesFiles(new PropertiesFileProcessor() {

            @Override
            public boolean process(String baseName, PropertiesFile propertiesFile) {
                addVariantsFromFile(propertyReference, propertiesFile, variants);
                return true;
            }
        });
    } else {
        for (PropertiesFile propFile : propertiesFileList) {
            addVariantsFromFile(propertyReference, propFile, variants);
        }
    }
    return variants;
}
Also used : PropertiesFileProcessor(com.intellij.lang.properties.PropertiesFileProcessor) IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) THashSet(gnu.trove.THashSet)

Aggregations

IProperty (com.intellij.lang.properties.IProperty)75 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)45 PsiElement (com.intellij.psi.PsiElement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 PsiFile (com.intellij.psi.PsiFile)13 NotNull (org.jetbrains.annotations.NotNull)13 Property (com.intellij.lang.properties.psi.Property)11 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)7 Nullable (org.jetbrains.annotations.Nullable)7 ResourceBundle (com.intellij.lang.properties.ResourceBundle)6 THashSet (gnu.trove.THashSet)6 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)4 Module (com.intellij.openapi.module.Module)4 Collections (java.util.Collections)4 StructureViewTreeElement (com.intellij.ide.structureView.StructureViewTreeElement)3 PropertiesImplUtil (com.intellij.lang.properties.PropertiesImplUtil)3 TreeElement (com.intellij.ide.util.treeView.smartTree.TreeElement)2 XmlProperty (com.intellij.lang.properties.xml.XmlProperty)2 Logger (com.intellij.openapi.diagnostic.Logger)2