public void insertOrUpdateTranslation( String key, String value, final PropertiesFile propertiesFile) throws IncorrectOperationException { final IProperty property = propertiesFile.findPropertyByKey(key); if (property != null) { final String oldValue = property.getValue(); if (!Comparing.equal(oldValue, value)) { property.setValue(value); myCodeStyleManager.reformat(property.getPsiElement()); } return; } if (myOrdered) { if (myAlphaSorted) { myCodeStyleManager.reformat(propertiesFile.addProperty(key, value).getPsiElement()); return; } final Pair<IProperty, Integer> propertyAndPosition = findExistedPrevSiblingProperty(key, propertiesFile); myCodeStyleManager.reformat( propertiesFile .addPropertyAfter( key, value, propertyAndPosition == null ? null : (Property) propertyAndPosition.getFirst()) .getPsiElement()); } else { insertPropertyLast(key, value, propertiesFile); } }
@Nullable public static PsiElement findPropertyValue( @NotNull Project project, @NotNull VirtualFile file, @NotNull String propName) { IProperty prop = findProperty(project, file, propName); return prop == null ? null : prop.getPsiElement().getFirstChild().getNextSibling().getNextSibling(); }
@Override public String getApplicationName(Module module) { final VirtualFile appProperties = getApplicationPropertiesFile(module); if (appProperties != null) { final PsiFile file = PsiManager.getInstance(module.getProject()).findFile(appProperties); if (file instanceof PropertiesFile) { final IProperty property = ((PropertiesFile) file).findPropertyByKey("application.name"); return property != null ? property.getValue() : super.getApplicationName(module); } } return super.getApplicationName(module); }
protected static void collectPropertiesFileVariants( @Nullable PropertiesFile file, @Nullable String prefix, List<Object> result) { if (file == null) return; for (IProperty each : file.getProperties()) { String name = each.getKey(); if (name != null) { if (prefix != null) name = prefix + name; result.add(createLookupElement(each, name, PlatformIcons.PROPERTY_ICON)); } } }
public void prepareRenaming( final PsiElement element, final String newName, final Map<PsiElement, String> allRenames) { final Project project = element.getProject(); ResourceBundle resourceBundle = ((IProperty) element).getPropertiesFile().getResourceBundle(); final Map<PsiElement, String> allRenamesCopy = new LinkedHashMap<PsiElement, String>(allRenames); allRenames.clear(); for (final Map.Entry<PsiElement, String> e : allRenamesCopy.entrySet()) { final IProperty property = (IProperty) e.getKey(); final List<IProperty> properties = PropertiesUtil.findAllProperties(resourceBundle, property.getUnescapedKey()); for (final IProperty toRename : properties) { allRenames.put(toRename.getPsiElement(), e.getValue()); } } }
@NotNull public static String getPropertyEditorValue(@Nullable final IProperty property) { if (property == null) { return ""; } else { String rawValue = property.getValue(); return rawValue == null ? "" : PropertiesResourceBundleUtil.fromPropertyValueToValueEditor(rawValue); } }
@Override public void findCollisions( PsiElement element, final String newName, Map<? extends PsiElement, String> allRenames, List<UsageInfo> result) { for (final Map.Entry<? extends PsiElement, String> e : allRenames.entrySet()) { for (IProperty property : ((PropertiesFile) e.getKey().getContainingFile()).getProperties()) { if (Comparing.strEqual(e.getValue(), property.getKey())) { result.add( new UnresolvableCollisionUsageInfo(property.getPsiElement(), e.getKey()) { @Override public String getDescription() { return "New property name \'" + e.getValue() + "\' hides existing property"; } }); } } } }
private static void sortPropertiesFile(final PropertiesFile file) { final List<IProperty> properties = new ArrayList<IProperty>(file.getProperties()); Collections.sort( properties, new Comparator<IProperty>() { @Override public int compare(IProperty p1, IProperty p2) { return Comparing.compare(p1.getKey(), p2.getKey()); } }); final char delimiter = PropertiesCodeStyleSettings.getInstance(file.getProject()).KEY_VALUE_DELIMITER; final StringBuilder rawText = new StringBuilder(); for (int i = 0; i < properties.size(); i++) { IProperty property = properties.get(i); final String value = property.getUnescapedValue(); rawText.append( PropertiesElementFactory.getPropertyText( property.getKey(), value != null ? value : "", delimiter, null)); if (i != properties.size() - 1) { rawText.append("\n"); } } final PropertiesFile fakeFile = PropertiesElementFactory.createPropertiesFile(file.getProject(), rawText.toString()); final PropertiesList propertiesList = PsiTreeUtil.findChildOfType(file.getContainingFile(), PropertiesList.class); LOG.assertTrue(propertiesList != null); final PropertiesList fakePropertiesList = PsiTreeUtil.findChildOfType(fakeFile.getContainingFile(), PropertiesList.class); LOG.assertTrue(fakePropertiesList != null); propertiesList.replace(fakePropertiesList); }
public void deletePropertyIfExist(String key, PropertiesFile file) { final IProperty property = file.findPropertyByKey(key); if (property != null && myKeysOrder != null) { boolean keyExistInOtherPropertiesFiles = false; for (PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles()) { if (!propertiesFile.equals(file) && propertiesFile.findPropertyByKey(key) != null) { keyExistInOtherPropertiesFiles = true; break; } } if (!keyExistInOtherPropertiesFiles) { myKeysOrder.remove(key); } } if (property != null) { PsiElement anElement = property.getPsiElement(); if (anElement instanceof PomTargetPsiElement) { final PomTarget xmlProperty = ((PomTargetPsiElement) anElement).getTarget(); LOG.assertTrue(xmlProperty instanceof XmlProperty); anElement = ((XmlProperty) xmlProperty).getNavigationElement(); } anElement.delete(); } }
@Nullable private String getSelectedPropertyName() { final IProperty selectedProperty = getSelectedProperty(); return selectedProperty == null ? null : selectedProperty.getName(); }
// 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<DomElement>()); 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; } if (myProjectDom != null) { PsiElement result = MavenDomProjectProcessorUtils.searchProperty(myText, myProjectDom, myProject); if (result != null) return result; } 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<DomElement>()); if (res != null) { return res; } } } if (myText.startsWith("settings.")) { return resolveSettingsModelProperty(); } return null; }