Provided way to reformat context around selected ranges, in order to make code look consistent. During "Wrapping Blocks" stage we collect all alignments from blocks which will be reformatted, during processing stage if we encounter read only block with such an alignment we drop it's read only status, so it could be realigned also.

This commit is contained in:
Yaroslav Lepenkin
2015-05-22 15:12:07 +03:00
parent aee90b9a0f
commit bbd7a31690
10 changed files with 214 additions and 21 deletions
@@ -35,6 +35,7 @@ import com.intellij.psi.codeStyle.*;
import com.intellij.psi.codeStyle.autodetect.DetectableIndentOptionsProvider;
import com.intellij.testFramework.LightIdeaTestCase;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.LineReader;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -83,7 +84,7 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
return result.toString();
}
protected enum Action {REFORMAT, INDENT}
protected enum Action {REFORMAT, INDENT, REFORMAT_WITH_CONTEXT}
public static JavaCodeStyleSettings getJavaSettings() {
return getSettings().getRootSettings().getCustomSettings(JavaCodeStyleSettings.class);
@@ -107,6 +108,13 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
CodeStyleManager.getInstance(getProject()).adjustLineIndent(psiFile, startOffset);
}
});
ACTIONS.put(Action.REFORMAT_WITH_CONTEXT, new TestFormatAction() {
@Override
public void run(PsiFile psiFile, int startOffset, int endOffset) {
List<TextRange> ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset));
CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges);
}
});
}
private static final String BASE_PATH = JavaTestUtil.getJavaTestDataPath() + "/psi/formatter/java";
@@ -16,6 +16,7 @@
package com.intellij.psi.formatter.java;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.util.IncorrectOperationException;
@@ -593,4 +594,67 @@ public class JavaFormatterAlignmentTest extends AbstractJavaFormatterTest {
" String superString = \"\";\n" +
"}");
}
public void test_Shift_All_AlignedParameters() {
myLineRange = new TextRange(2, 2);
getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
doTextTest(
Action.REFORMAT_WITH_CONTEXT,
"public class Test {\n" +
" \n" +
" public void fooooo(String foo,\n" +
" String booo,\n" +
" String kakadoo) {\n" +
"\n" +
" }\n" +
"\n" +
"}",
"public class Test {\n" +
"\n" +
" public void fooooo(String foo,\n" +
" String booo,\n" +
" String kakadoo) {\n" +
"\n" +
" }\n" +
"\n" +
"}"
);
}
public void test_Align_UnselectedField_IfNeeded() {
myLineRange = new TextRange(2, 2);
getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = true;
doTextTest(
Action.REFORMAT_WITH_CONTEXT,
"public class Test {\n" +
" public int i = 1;\n" +
" public String iiiiiiiiii = 2;\n" +
"}",
"public class Test {\n" +
" public int i = 1;\n" +
" public String iiiiiiiiii = 2;\n" +
"}"
);
}
public void test_Align_UnselectedVariable_IfNeeded() {
myLineRange = new TextRange(3, 3);
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doTextTest(
Action.REFORMAT_WITH_CONTEXT,
"public class Test {\n" +
" public void test() {\n" +
" int s = 2;\n" +
" String sssss = 3;\n" +
" }\n" +
"}",
"public class Test {\n" +
" public void test() {\n" +
" int s = 2;\n" +
" String sssss = 3;\n" +
" }\n" +
"}"
);
}
}