WEB-14167 Represent property alignment types as integers, not enums

This commit is contained in:
Mikhail Golubev
2014-11-18 18:32:59 +03:00
parent 0af43f358e
commit 401c7635e5
5 changed files with 37 additions and 20 deletions
@@ -12,7 +12,6 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.TokenType;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
@@ -23,8 +22,8 @@ import java.util.List;
import static com.intellij.json.JsonElementTypes.*;
import static com.intellij.json.JsonParserDefinition.*;
import static com.intellij.json.formatter.JsonCodeStyleSettings.PropertyAlignment.ALIGN_ON_COLON;
import static com.intellij.json.formatter.JsonCodeStyleSettings.PropertyAlignment.ALIGN_ON_VALUE;
import static com.intellij.json.formatter.JsonCodeStyleSettings.ALIGN_PROPERTY_ON_COLON;
import static com.intellij.json.formatter.JsonCodeStyleSettings.ALIGN_PROPERTY_ON_VALUE;
import static com.intellij.json.psi.JsonPsiUtil.hasElementType;
/**
@@ -120,7 +119,7 @@ public class JsonBlock implements ASTBlock {
indent = Indent.getNormalIndent();
}
else if (hasElementType(childNode, JSON_OPEN_BRACES)) {
if (JsonPsiUtil.isPropertyValue(myPsiElement) && customSettings.PROPERTY_ALIGNMENT == ALIGN_ON_VALUE) {
if (JsonPsiUtil.isPropertyValue(myPsiElement) && customSettings.PROPERTY_ALIGNMENT == ALIGN_PROPERTY_ON_VALUE) {
// WEB-13587 Align compound values on opening brace/bracket, not the whole block
assert myParent != null && myParent.myParent != null && myParent.myParent.myPropertyValueAlignment != null;
alignment = myParent.myParent.myPropertyValueAlignment;
@@ -130,10 +129,10 @@ public class JsonBlock implements ASTBlock {
// Handle properties alignment
else if (hasElementType(myNode, PROPERTY) ) {
assert myParent != null && myParent.myPropertyValueAlignment != null;
if (hasElementType(childNode, COLON) && customSettings.PROPERTY_ALIGNMENT == ALIGN_ON_COLON) {
if (hasElementType(childNode, COLON) && customSettings.PROPERTY_ALIGNMENT == ALIGN_PROPERTY_ON_COLON) {
alignment = myParent.myPropertyValueAlignment;
}
else if (JsonPsiUtil.isPropertyValue(childNode.getPsi()) && customSettings.PROPERTY_ALIGNMENT == ALIGN_ON_VALUE) {
else if (JsonPsiUtil.isPropertyValue(childNode.getPsi()) && customSettings.PROPERTY_ALIGNMENT == ALIGN_PROPERTY_ON_VALUE) {
if (!hasElementType(childNode, JSON_CONTAINERS)) {
alignment = myParent.myPropertyValueAlignment;
}
@@ -89,12 +89,12 @@ public class JsonCodeStylePanel extends CodeStyleAbstractPanel {
@Override
public void apply(CodeStyleSettings settings) throws ConfigurationException {
getCustomSettings(settings).PROPERTY_ALIGNMENT = getSelectedAlignmentType();
getCustomSettings(settings).PROPERTY_ALIGNMENT = getSelectedAlignmentType().getId();
}
@Override
public boolean isModified(CodeStyleSettings settings) {
return getCustomSettings(settings).PROPERTY_ALIGNMENT != getSelectedAlignmentType();
return getCustomSettings(settings).PROPERTY_ALIGNMENT != getSelectedAlignmentType().getId();
}
@Nullable
@@ -106,7 +106,7 @@ public class JsonCodeStylePanel extends CodeStyleAbstractPanel {
@Override
protected void resetImpl(CodeStyleSettings settings) {
for (int i = 0; i < myPropertiesAlignmentCombo.getItemCount(); i++) {
if (myPropertiesAlignmentCombo.getItemAt(i) == getCustomSettings(settings).PROPERTY_ALIGNMENT) {
if (((PropertyAlignment)myPropertiesAlignmentCombo.getItemAt(i)).getId() == getCustomSettings(settings).PROPERTY_ALIGNMENT) {
myPropertiesAlignmentCombo.setSelectedIndex(i);
break;
}
@@ -12,10 +12,22 @@ import org.jetbrains.annotations.NotNull;
*/
public class JsonCodeStyleSettings extends CustomCodeStyleSettings {
public static int DO_NOT_ALIGN_PROPERTY = PropertyAlignment.DO_NOT_ALIGN.getId();
public static int ALIGN_PROPERTY_ON_VALUE = PropertyAlignment.ALIGN_ON_VALUE.getId();
public static int ALIGN_PROPERTY_ON_COLON = PropertyAlignment.ALIGN_ON_COLON.getId();
public boolean SPACE_AFTER_COLON = true;
public boolean SPACE_BEFORE_COLON = false;
public PropertyAlignment PROPERTY_ALIGNMENT = PropertyAlignment.DO_NOT_ALIGN;
// TODO: check whether it's possible to migrate CustomCodeStyleSettings to newer com.intellij.util.xmlb.XmlSerializer
/**
* Contains value of {@link com.intellij.json.formatter.JsonCodeStyleSettings.PropertyAlignment#getId()}
*
* @see #DO_NOT_ALIGN_PROPERTY
* @see #ALIGN_PROPERTY_ON_VALUE
* @see #ALIGN_PROPERTY_ON_COLON
*/
public int PROPERTY_ALIGNMENT = PropertyAlignment.DO_NOT_ALIGN.getId();
public int OBJECT_WRAPPING = CommonCodeStyleSettings.WRAP_ALWAYS;
// This was default policy for array elements wrapping in JavaScript's JSON.
@@ -27,19 +39,25 @@ public class JsonCodeStyleSettings extends CustomCodeStyleSettings {
}
public enum PropertyAlignment {
DO_NOT_ALIGN(JsonBundle.message("msg.align.properties.none")),
ALIGN_ON_VALUE(JsonBundle.message("msg.align.properties.on.value")),
ALIGN_ON_COLON(JsonBundle.message("msg.align.properties.on.colon"));
DO_NOT_ALIGN(JsonBundle.message("msg.align.properties.none"), 0),
ALIGN_ON_VALUE(JsonBundle.message("msg.align.properties.on.value"), 1),
ALIGN_ON_COLON(JsonBundle.message("msg.align.properties.on.colon"), 2);
private final String myDescription;
private final int myId;
PropertyAlignment(@NotNull String description) {
PropertyAlignment(@NotNull String description, int id) {
myDescription = description;
myId = id;
}
@NotNull
public String getDescription() {
return myDescription;
}
public int getId() {
return myId;
}
}
}
@@ -42,8 +42,8 @@ public class JsonEditingTest extends JsonTestCase {
// WEB-13675
public void testIndentWithTabsWhenSmartTabEnabled() {
CommonCodeStyleSettings.IndentOptions indentOptions = getIndentOptions();
CommonCodeStyleSettings.IndentOptions oldSettings = (CommonCodeStyleSettings.IndentOptions)indentOptions.clone();
final CommonCodeStyleSettings.IndentOptions indentOptions = getIndentOptions();
final CommonCodeStyleSettings.IndentOptions oldSettings = (CommonCodeStyleSettings.IndentOptions)indentOptions.clone();
indentOptions.TAB_SIZE = 4;
indentOptions.INDENT_SIZE = 4;
indentOptions.USE_TAB_CHARACTER = true;
@@ -72,9 +72,9 @@ public class JsonEditingTest extends JsonTestCase {
final JsonCodeStyleSettings settings = getCustomCodeStyleSettings();
final CommonCodeStyleSettings.IndentOptions indentOptions = getIndentOptions();
JsonCodeStyleSettings.PropertyAlignment oldPropertyAlignment = settings.PROPERTY_ALIGNMENT;
int oldIndentSize = indentOptions.INDENT_SIZE;
settings.PROPERTY_ALIGNMENT = JsonCodeStyleSettings.PropertyAlignment.ALIGN_ON_VALUE;
final int oldPropertyAlignment = settings.PROPERTY_ALIGNMENT;
final int oldIndentSize = indentOptions.INDENT_SIZE;
settings.PROPERTY_ALIGNMENT = JsonCodeStyleSettings.ALIGN_PROPERTY_ON_VALUE;
indentOptions.INDENT_SIZE = 4;
try {
doTest("\n");
@@ -84,7 +84,7 @@ public class JsonFormattingTest extends FormatterTestCase {
withPreservedSettings(new ThrowableRunnable<Exception>() {
@Override
public void run() throws Exception {
getCustomSettings().PROPERTY_ALIGNMENT = alignmentType;
getCustomSettings().PROPERTY_ALIGNMENT = alignmentType.getId();
doTest();
}
});