From 012f5dfa1215fe2aafd85606ede8f32d0f9cadcb Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Thu, 5 Nov 2015 18:28:22 +0300 Subject: [PATCH] (IDEA-147380) Change indent size and continuation indent size only if tab usage detected and use tabs was switched off --- .../autodetect/DetectIndentAndTypeTest.java | 24 +++++++++++++++++++ .../autodetect/IndentOptionsDetectorImpl.java | 10 ++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/DetectIndentAndTypeTest.java b/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/DetectIndentAndTypeTest.java index 7e0d14563ce3..cf85f1cd0fef 100644 --- a/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/DetectIndentAndTypeTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/DetectIndentAndTypeTest.java @@ -17,11 +17,14 @@ package com.intellij.psi.codeStyle.autodetect; import com.intellij.ide.highlighter.JavaFileType; import com.intellij.lang.java.JavaLanguage; +import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + public class DetectIndentAndTypeTest extends LightPlatformCodeInsightFixtureTestCase { private CodeStyleSettings mySettings; @@ -146,4 +149,25 @@ public class DetectIndentAndTypeTest extends LightPlatformCodeInsightFixtureTest "\t}\n" + "}\n"); } + + public void testDoNotIndentOptions_WhenTabsDetected_AndUseTabsWasSetByDefault() { + CommonCodeStyleSettings common = mySettings.getCommonSettings(JavaLanguage.INSTANCE); + CommonCodeStyleSettings.IndentOptions indentOptions = common.getIndentOptions(); + + assert indentOptions != null; + + indentOptions.USE_TAB_CHARACTER = true; + + indentOptions.TAB_SIZE = 8; + indentOptions.INDENT_SIZE = 4; + indentOptions.CONTINUATION_INDENT_SIZE = 8; + + myFixture.configureByText(JavaFileType.INSTANCE, myText); + PsiFile file = myFixture.getFile(); + CommonCodeStyleSettings.IndentOptions options = mySettings.getIndentOptionsByFile(file); + + assertThat(options.INDENT_SIZE).isEqualTo(4); + assertThat(indentOptions.CONTINUATION_INDENT_SIZE).isEqualTo(8); + } + } diff --git a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/IndentOptionsDetectorImpl.java b/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/IndentOptionsDetectorImpl.java index 23e5e98fdb3f..5a5541b7e6a0 100644 --- a/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/IndentOptionsDetectorImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/codeStyle/autodetect/IndentOptionsDetectorImpl.java @@ -76,11 +76,6 @@ public class IndentOptionsDetectorImpl implements IndentOptionsDetector { private void adjustIndentOptions(@NotNull IndentOptions indentOptions, @NotNull IndentUsageStatistics stats) { if (isTabsUsed(stats)) { setUseTabs(indentOptions, true); - int continuationRatio = indentOptions.INDENT_SIZE == 0 ? 1 - : indentOptions.CONTINUATION_INDENT_SIZE / indentOptions.INDENT_SIZE; - - indentOptions.INDENT_SIZE = indentOptions.TAB_SIZE; - indentOptions.CONTINUATION_INDENT_SIZE = indentOptions.TAB_SIZE * continuationRatio; } else if (isSpacesUsed(stats)) { setUseTabs(indentOptions, false); @@ -108,6 +103,11 @@ public class IndentOptionsDetectorImpl implements IndentOptionsDetector { private void setUseTabs(@NotNull IndentOptions indentOptions, boolean useTabs) { if (indentOptions.USE_TAB_CHARACTER != useTabs) { indentOptions.USE_TAB_CHARACTER = useTabs; + int continuationRatio = indentOptions.INDENT_SIZE == 0 ? 1 + : indentOptions.CONTINUATION_INDENT_SIZE / indentOptions.INDENT_SIZE; + + indentOptions.INDENT_SIZE = indentOptions.TAB_SIZE; + indentOptions.CONTINUATION_INDENT_SIZE = indentOptions.TAB_SIZE * continuationRatio; LOG.debug("Tab usage set to " + useTabs + " for file " + myFile); } }