resource bundles: customization of key/value delimiters in code style (IDEA-137241).

This commit is contained in:
Dmitry Batkovich
2015-03-05 14:59:10 +03:00
parent 3bd1610ec9
commit af7424eff9
18 changed files with 455 additions and 9 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports on properties in which key/value delimiter doesn't corresponds to code style settings.
</body>
</html>
@@ -15,6 +15,7 @@
<orderEntry type="module" module-name="indexing-api" />
<orderEntry type="module" module-name="xml-psi-api" />
<orderEntry type="module" module-name="projectModel-api" />
<orderEntry type="module" module-name="lang-impl" />
<orderEntry type="module" module-name="lang-api" />
</component>
</module>
</module>
@@ -66,3 +66,4 @@ dissociate.resource.bundle.quick.fix.name=Dissociate Resource Bundle
dissociate.resource.bundle.quick.fix.options.label=Additional language codes\:
dissociate.resource.bundle.quick.fix.options.input.text=Inter language code
dissociate.resource.bundle.quick.fix.options.input.title=Additional Locale Languages
wrong.property.key.value.delimiter.inspection.display.name=Property key/value delimiter doesn't correspond to code style
@@ -18,12 +18,14 @@ package com.intellij.lang.properties.psi;
import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesFileType;
import com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettings;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.UserDataCache;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFileFactory;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -42,14 +44,20 @@ public class PropertiesElementFactory {
@NotNull
public static IProperty createProperty(@NotNull Project project, @NonNls @NotNull String name, @NonNls @NotNull String value) {
String text = getPropertyText(name, value);
String text = getPropertyText(name, value, null, project);
final PropertiesFile dummyFile = createPropertiesFile(project, text);
return dummyFile.getProperties().get(0);
}
@NotNull
public static String getPropertyText(@NonNls @NotNull String name, @NonNls @NotNull String value) {
return escape(name) + "=" + escapeValue(value);
public static String getPropertyText(@NonNls @NotNull String name,
@NonNls @NotNull String value,
@NonNls @Nullable Character delimiter,
@Nullable Project project) {
if (delimiter == null) {
delimiter = project == null ? PropertiesCodeStyleSettings.DEFAULT_KEY_VALUE_DELIMITER : PropertiesCodeStyleSettings.getInstance(project).KEY_VALUE_DELIMITER;
}
return escape(name) + String.valueOf(delimiter) + escapeValue(value);
}
@NotNull
@@ -0,0 +1,39 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.properties.psi.codeStyle;
import com.intellij.lang.properties.PropertiesLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;
/**
* @author Dmitry Batkovich
*/
public class PropertiesCodeStyleSettings extends CustomCodeStyleSettings {
public final static char DEFAULT_KEY_VALUE_DELIMITER = '=';
public PropertiesCodeStyleSettings(CodeStyleSettings container) {
super(PropertiesLanguage.INSTANCE.getID(), container);
}
public static PropertiesCodeStyleSettings getInstance(final Project project) {
return CodeStyleSettingsManager.getSettings(project).getCustomSettings(PropertiesCodeStyleSettings.class);
}
public char KEY_VALUE_DELIMITER = DEFAULT_KEY_VALUE_DELIMITER;
}
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettingsPanel">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="10" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e8f52" class="com.intellij.ui.components.JBLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Key/value delimiter:"/>
</properties>
</component>
<vspacer id="3237e">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="35c5d" class="com.intellij.openapi.ui.ComboBox" binding="myDelimiterCombo">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<hspacer id="6d3f9">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</form>
@@ -0,0 +1,111 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.properties.psi.codeStyle;
import com.intellij.application.options.CodeStyleAbstractPanel;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
/**
* @author Dmitry Batkovich
*/
public class PropertiesCodeStyleSettingsPanel extends CodeStyleAbstractPanel {
private final static String WHITESPACE_ELEMENT = "Whitespace symbol";
private ComboBox myDelimiterCombo;
private JPanel myPanel;
public PropertiesCodeStyleSettingsPanel(CodeStyleSettings settings) {
super(settings);
final DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(':');
model.addElement('=');
model.addElement(WHITESPACE_ELEMENT);
myDelimiterCombo.setModel(model);
selectChar(settings.getCustomSettings(PropertiesCodeStyleSettings.class));
}
private void selectChar(PropertiesCodeStyleSettings settings) {
myDelimiterCombo.setSelectedItem(settings.KEY_VALUE_DELIMITER == ' ' ? WHITESPACE_ELEMENT : settings.KEY_VALUE_DELIMITER);
}
private char getSelectedChar() {
final Object item = myDelimiterCombo.getModel().getSelectedItem();
if (item instanceof Character) {
return (Character)item;
}
assert item == WHITESPACE_ELEMENT;
return ' ';
}
private void createUIComponents() {
}
@Override
protected int getRightMargin() {
return 0;
}
@Nullable
@Override
protected EditorHighlighter createHighlighter(EditorColorsScheme scheme) {
return null;
}
@NotNull
@Override
protected FileType getFileType() {
return StdFileTypes.PROPERTIES;
}
@Nullable
@Override
protected String getPreviewText() {
return null;
}
@Override
public void apply(CodeStyleSettings settings) throws ConfigurationException {
final PropertiesCodeStyleSettings propertiesCodeStyleSettings = settings.getCustomSettings(PropertiesCodeStyleSettings.class);
propertiesCodeStyleSettings.KEY_VALUE_DELIMITER = getSelectedChar();
}
@Override
public boolean isModified(CodeStyleSettings settings) {
final PropertiesCodeStyleSettings propertiesCodeStyleSettings = settings.getCustomSettings(PropertiesCodeStyleSettings.class);
return propertiesCodeStyleSettings.KEY_VALUE_DELIMITER != getSelectedChar();
}
@Nullable
@Override
public JComponent getPanel() {
return myPanel;
}
@Override
protected void resetImpl(CodeStyleSettings settings) {
selectChar(settings.getCustomSettings(PropertiesCodeStyleSettings.class));
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.properties.psi.codeStyle;
import com.intellij.application.options.CodeStyleAbstractConfigurable;
import com.intellij.application.options.CodeStyleAbstractPanel;
import com.intellij.lang.properties.PropertiesLanguage;
import com.intellij.openapi.options.Configurable;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsProvider;
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Dmitry Batkovich
*/
public class PropertiesCodeStyleSettingsProvider extends CodeStyleSettingsProvider {
@NotNull
@Override
public Configurable createSettingsPage(CodeStyleSettings settings, CodeStyleSettings originalSettings) {
return new CodeStyleAbstractConfigurable(settings, originalSettings, "Properties Files") {
@Nullable
@Override
public String getHelpTopic() {
return "reference.settingsdialog.codestyle.properties";
}
@Override
protected CodeStyleAbstractPanel createPanel(CodeStyleSettings settings) {
return new PropertiesCodeStyleSettingsPanel(settings);
}
};
}
@Nullable
@Override
public CustomCodeStyleSettings createCustomSettings(CodeStyleSettings settings) {
return new PropertiesCodeStyleSettings(settings);
}
@Nullable
@Override
public String getConfigurableDisplayName() {
return PropertiesLanguage.INSTANCE.getDisplayName();
}
}
@@ -16,6 +16,7 @@
<orderEntry type="module" module-name="indexing-impl" />
<orderEntry type="module" module-name="structure-view-impl" />
<orderEntry type="module" module-name="analysis-impl" />
<orderEntry type="module" module-name="lang-impl" />
</component>
</module>
@@ -0,0 +1,78 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.lang.properties.PropertiesBundle;
import com.intellij.lang.properties.PropertySuppressableInspectionBase;
import com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettings;
import com.intellij.lang.properties.psi.impl.PropertiesFileImpl;
import com.intellij.lang.properties.psi.impl.PropertyImpl;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
/**
* @author Dmitry Batkovich
*/
public class WrongPropertyKeyValueDelimiterInspection extends PropertySuppressableInspectionBase {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (!(holder.getFile() instanceof PropertiesFileImpl)) {
return PsiElementVisitor.EMPTY_VISITOR;
}
final PropertiesCodeStyleSettings codeStyleSettings = PropertiesCodeStyleSettings.getInstance(holder.getProject());
final char codeStyleKeyValueDelimiter = codeStyleSettings.KEY_VALUE_DELIMITER;
return new PsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof PropertyImpl) {
final Character delimiter = ((PropertyImpl)element).getKeyValueDelimiter();
if (delimiter != null && !delimiter.equals(codeStyleKeyValueDelimiter)) {
holder.registerProblem(element, PropertiesBundle.message("wrong.property.key.value.delimiter.inspection.display.name"), new ReplaceKeyValueDelimiterQuickFix(element));
}
}
}
};
}
private static final class ReplaceKeyValueDelimiterQuickFix extends LocalQuickFixOnPsiElement implements HighPriorityAction {
public ReplaceKeyValueDelimiterQuickFix(@NotNull PsiElement element) {
super(element);
}
@NotNull
@Override
public String getText() {
return getFamilyName();
}
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement element, @NotNull PsiElement endElement) {
((PropertyImpl) element).replaceKeyValueDelimiterWithDefault();
}
@NotNull
@Override
public String getFamilyName() {
return "Replace Property Key/Value Delimiter According Code Style";
}
}
}
@@ -22,6 +22,7 @@ import com.intellij.lang.properties.ResourceBundle;
import com.intellij.lang.properties.psi.PropertiesElementFactory;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.psi.PropertiesList;
import com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettings;
import com.intellij.lang.properties.psi.impl.PropertiesFileImpl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
@@ -127,12 +128,12 @@ public class AlphaUnsortedPropertiesFileInspection extends LocalInspectionTool {
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.getValue();
rawText.append(PropertiesElementFactory.getPropertyText(property.getName(), value != null ? value : ""));
rawText.append(PropertiesElementFactory.getPropertyText(property.getName(), value != null ? value : "", delimiter, null));
if (i != properties.size() - 1) {
rawText.append("\n");
}
@@ -99,6 +99,16 @@ public class PropertiesFileImpl extends PsiFileBase implements PropertiesFile {
}
}
public Character findFirstKeyValueDelimiter() {
for (IProperty property : myProperties) {
final Character separator = ((PropertyImpl)property).getKeyValueDelimiter();
if (separator != null) {
return separator;
}
}
return null;
}
@Override
public IProperty findPropertyByKey(@NotNull String key) {
ensurePropertiesLoaded();
@@ -441,4 +441,24 @@ public class PropertyImpl extends PropertiesStubElementImpl<PropertyStub> implem
public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() {
return new PropertyImplEscaper(this);
}
@Nullable
public Character getKeyValueDelimiter() {
final PsiElement delimiter = findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR);
if (delimiter == null) {
return null;
}
final String separatorText = delimiter.getText();
LOG.assertTrue(separatorText.length() == 1);
return separatorText.charAt(0);
}
public void replaceKeyValueDelimiterWithDefault() {
PropertyImpl property = (PropertyImpl)PropertiesElementFactory.createProperty(getProject(), "yyy", "xxx");
final ASTNode oldDelimiter = getNode().findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR);
LOG.assertTrue(oldDelimiter != null);
final ASTNode newDelimiter = property.getNode().findChildByType(PropertiesTokenTypes.KEY_VALUE_SEPARATOR);
LOG.assertTrue(newDelimiter != null);
getNode().replaceChild(oldDelimiter, newDelimiter);
}
}
@@ -81,6 +81,10 @@
key="alpha.unsorted.properties.file.inspection.display.name" groupKey="properties.files.inspection.group.display.name"
enabledByDefault="false" level="INFO"
implementationClass="com.intellij.codeInspection.unsorted.AlphaUnsortedPropertiesFileInspection"/>
<localInspection language="Properties" shortName="WrongPropertyKeyValueDelimiter" bundle="messages.PropertiesBundle"
key="wrong.property.key.value.delimiter.inspection.display.name" groupKey="properties.files.inspection.group.display.name"
enabledByDefault="false" level="WEAK WARNING"
implementationClass="com.intellij.codeInspection.WrongPropertyKeyValueDelimiterInspection"/>
<idIndexer filetype="Properties" implementationClass="com.intellij.psi.impl.cache.impl.idCache.PropertiesIdIndexer"/>
<todoIndexer filetype="Properties" implementationClass="com.intellij.psi.impl.cache.impl.idCache.PropertiesTodoIndexer"/>
@@ -99,6 +103,8 @@
<lang.foldingBuilder language="Properties" implementationClass="com.intellij.lang.properties.editor.PropertiesFoldingBuilder"/>
<gotoRelatedProvider implementation="com.intellij.lang.properties.editor.GotoResourceBundleLocalizationsProvider"/>
<gotoRelatedProvider implementation="com.intellij.lang.properties.editor.GotoPropertyDeclarationsProvider"/>
<codeStyleSettingsProvider implementation="com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettingsProvider"/>
</extensions>
<project-components>
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test1.properties</file>
<line>2</line>
<description>Property key/value delimiter doesn't correspond to code style</description>
</problem>
<problem>
<file>Test1.properties</file>
<line>3</line>
<description>Property key/value delimiter doesn't correspond to code style</description>
</problem>
</problems>
@@ -0,0 +1,4 @@
a=1
c:123
z 123123
zxc=zxc
@@ -18,6 +18,8 @@ package com.intellij.lang.properties;
import com.intellij.lang.properties.psi.PropertiesElementFactory;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.psi.Property;
import com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettings;
import com.intellij.lang.properties.psi.impl.PropertyImpl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.testFramework.LightPlatformTestCase;
@@ -97,8 +99,9 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase
PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "xxx=yyy\nxxx2=tyrt\nxxx3=ttt\n\n");
final Property property = (Property)propertiesFile.findPropertyByKey("xxx2");
WriteCommandAction.runWriteCommandAction(null, new Runnable(){public void run() {
property.delete();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
public void run() {
property.delete();
}
});
@@ -177,4 +180,15 @@ public class PropertiesFileTest extends LightPlatformCodeInsightFixtureTestCase
assertEquals(" e=f", properties.get(2).getUnescapedKey());
assertEquals("\u1234\\uxyzt", properties.get(3).getUnescapedKey());
}
public void testNonDefaultKeyValueDelimiter() {
final PropertiesCodeStyleSettings codeStyleSettings = PropertiesCodeStyleSettings.getInstance(getProject());
codeStyleSettings.KEY_VALUE_DELIMITER = ':';
final PropertyImpl property = (PropertyImpl)PropertiesElementFactory.createProperty(getProject(), "xxx", "yyy");
final Character delimiter = property.getKeyValueDelimiter();
assertNotNull(delimiter);
assertEquals(':', (char)delimiter);
assertEquals("xxx:yyy", property.getPsiElement().getText());
codeStyleSettings.KEY_VALUE_DELIMITER = PropertiesCodeStyleSettings.DEFAULT_KEY_VALUE_DELIMITER;
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.properties;
import com.intellij.codeInspection.WrongPropertyKeyValueDelimiterInspection;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.testFramework.InspectionTestCase;
import org.jetbrains.annotations.NotNull;
/**
* @author Dmitry Batkovich
*/
public class WrongPropertyKeyValueDelimiterInspectionTest extends InspectionTestCase {
@NotNull
@Override
protected String getTestDataPath() {
return PluginPathManager.getPluginHomePath("properties") + "/testData";
}
public void testSimple() throws Exception {
doTest("wrongPropertyKeyDelimiter/", new WrongPropertyKeyValueDelimiterInspection());
}
}