From b762b79d4b9de05e4320d7f0b566502610ea05eb Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 22 Oct 2014 13:47:50 +0400 Subject: [PATCH] WEB-13675 Do not insert redundant alignment between array elements and properties. This alignment included in ChildAttributes for new properties and array elements leads to spaces being inserted instead of tabs inside indents for corresponding blocks when both SMART_TABS and USE_TAB_CHARACTER options are active. Test data for tests of line movements was updated because it made use of these alignments. --- .../intellij/json/formatter/JsonBlock.java | 14 ++++++------- .../com/intellij/json/JsonEditingTest.java | 21 +++++++++++++++++++ ...dentWithTabsWhenSmartTabEnabled.after.json | 5 +++++ .../IndentWithTabsWhenSmartTabEnabled.json | 4 ++++ .../mover/ArraySelectionMovedDown.json | 7 +++++-- .../ArraySelectionMovedDown_afterDown.json | 7 +++++-- .../mover/ObjectSelectionMovedDown.json | 10 ++++++--- .../ObjectSelectionMovedDown_afterDown.json | 10 ++++++--- 8 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.after.json create mode 100644 json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.json diff --git a/json/src/com/intellij/json/formatter/JsonBlock.java b/json/src/com/intellij/json/formatter/JsonBlock.java index 2dccd347e177..8ffff7f2dc44 100644 --- a/json/src/com/intellij/json/formatter/JsonBlock.java +++ b/json/src/com/intellij/json/formatter/JsonBlock.java @@ -48,8 +48,6 @@ public class JsonBlock implements ASTBlock { // lazy initialized on first call to #getSubBlocks() private List mySubBlocks = null; - private final Alignment myChildAlignment = Alignment.createAlignment(); - private final Alignment myPropertyValueAlignment; private final Wrap myChildWrap; @@ -119,9 +117,8 @@ public class JsonBlock implements ASTBlock { JsonCodeStyleSettings customSettings = getCustomSettings(); if (isContainer() && childNodeType != JsonElementTypes.COMMA && !BRACES.contains(childNodeType)) { - assert myChildWrap != null && myChildAlignment != null; + assert myChildWrap != null; wrap = myChildWrap; - alignment = myChildAlignment; indent = Indent.getNormalIndent(); } // Handle properties alignment @@ -184,7 +181,10 @@ public class JsonBlock implements ASTBlock { @Override public ChildAttributes getChildAttributes(int newChildIndex) { if (isContainer()) { - return new ChildAttributes(Indent.getNormalIndent(), myChildAlignment); + // WEB-13675: For some reason including alignment in child attributes causes + // indents to consist solely of spaces when both USE_TABS and SMART_TAB + // options are enabled. + return new ChildAttributes(Indent.getNormalIndent(), null); } // Will use continuation indent for cases like { "foo" } return new ChildAttributes(null, null); @@ -192,8 +192,8 @@ public class JsonBlock implements ASTBlock { @Override public boolean isIncomplete() { - IElementType nodeType = myNode.getElementType(); - ASTNode lastChildNode = myNode.getLastChildNode(); + final IElementType nodeType = myNode.getElementType(); + final ASTNode lastChildNode = myNode.getLastChildNode(); if (nodeType == JsonElementTypes.OBJECT) { return lastChildNode != null && lastChildNode.getElementType() != JsonElementTypes.R_CURLY; } diff --git a/json/tests/test/com/intellij/json/JsonEditingTest.java b/json/tests/test/com/intellij/json/JsonEditingTest.java index bb6f5088c89c..42421979f490 100644 --- a/json/tests/test/com/intellij/json/JsonEditingTest.java +++ b/json/tests/test/com/intellij/json/JsonEditingTest.java @@ -3,6 +3,9 @@ package com.intellij.json; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.util.Computable; import com.intellij.psi.PsiFile; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import org.jetbrains.annotations.NotNull; /** @@ -37,4 +40,22 @@ public class JsonEditingTest extends JsonTestCase { public void testNormalIndentAfterPropertyWithoutComma() { doTest("\n"); } + + // WEB-13675 + public void testIndentWithTabsWhenSmartTabEnabled() { + CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myFixture.getProject()); + CommonCodeStyleSettings.IndentOptions indentOptions = settings.getCommonSettings(JsonLanguage.INSTANCE).getIndentOptions(); + assertNotNull(indentOptions); + CommonCodeStyleSettings.IndentOptions oldSettings = (CommonCodeStyleSettings.IndentOptions)indentOptions.clone(); + indentOptions.TAB_SIZE = 4; + indentOptions.INDENT_SIZE = 4; + indentOptions.USE_TAB_CHARACTER = true; + indentOptions.SMART_TABS = true; + try { + doTest("\n\"baz\""); + } + finally { + indentOptions.copyFrom(oldSettings); + } + } } diff --git a/json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.after.json b/json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.after.json new file mode 100644 index 000000000000..152513cad497 --- /dev/null +++ b/json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.after.json @@ -0,0 +1,5 @@ +{ + "foo": true, + "bar": false, + "baz" +} \ No newline at end of file diff --git a/json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.json b/json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.json new file mode 100644 index 000000000000..69824365e9e8 --- /dev/null +++ b/json/tests/testData/editing/IndentWithTabsWhenSmartTabEnabled.json @@ -0,0 +1,4 @@ +{ + "foo": true, + "bar": false, +} \ No newline at end of file diff --git a/json/tests/testData/mover/ArraySelectionMovedDown.json b/json/tests/testData/mover/ArraySelectionMovedDown.json index 476b56d42dea..81e4dc2bcd0e 100644 --- a/json/tests/testData/mover/ArraySelectionMovedDown.json +++ b/json/tests/testData/mover/ArraySelectionMovedDown.json @@ -1,5 +1,8 @@ [ - 1, [2], [true, - false], + 1, [2], + [ + true, + false + ], null ] \ No newline at end of file diff --git a/json/tests/testData/mover/ArraySelectionMovedDown_afterDown.json b/json/tests/testData/mover/ArraySelectionMovedDown_afterDown.json index 9db514b06905..1a7a49635d95 100644 --- a/json/tests/testData/mover/ArraySelectionMovedDown_afterDown.json +++ b/json/tests/testData/mover/ArraySelectionMovedDown_afterDown.json @@ -1,5 +1,8 @@ [ null, - 1, [2], [true, - false] + 1, [2], + [ + true, + false + ] ] \ No newline at end of file diff --git a/json/tests/testData/mover/ObjectSelectionMovedDown.json b/json/tests/testData/mover/ObjectSelectionMovedDown.json index 57efbe402072..ed363c1d3d6f 100644 --- a/json/tests/testData/mover/ObjectSelectionMovedDown.json +++ b/json/tests/testData/mover/ObjectSelectionMovedDown.json @@ -1,6 +1,10 @@ { - "foo": false, "bar": 42, - "baz": [1, 2, - 3], + "foo": false, + "bar": 42, + "baz": [ + 1, + 2, + 3 + ], "quux": null } \ No newline at end of file diff --git a/json/tests/testData/mover/ObjectSelectionMovedDown_afterDown.json b/json/tests/testData/mover/ObjectSelectionMovedDown_afterDown.json index 860d3b5d3783..ebf686822340 100644 --- a/json/tests/testData/mover/ObjectSelectionMovedDown_afterDown.json +++ b/json/tests/testData/mover/ObjectSelectionMovedDown_afterDown.json @@ -1,6 +1,10 @@ { + "foo": false, "quux": null, - "foo": false, "bar": 42, - "baz": [1, 2, - 3] + "bar": 42, + "baz": [ + 1, + 2, + 3 + ] } \ No newline at end of file