diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeRearrangeWholeFile_after.java b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeRearrangeWholeFile_after.java new file mode 100644 index 000000000000..325157e34237 --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeRearrangeWholeFile_after.java @@ -0,0 +1,15 @@ +import java.util.List; + +public class Test { + + public long newRun; + public int ab; + public int awe; + private int c; + + public void run() { + List strings = null; + } + + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeRearrangeWholeFile_before.java b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeRearrangeWholeFile_before.java new file mode 100644 index 000000000000..40f2bdf8a1cd --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeRearrangeWholeFile_before.java @@ -0,0 +1,21 @@ +import java.util.HashMap; +import java.util.Set; +import java.util.List; + +public class Test { + + public long newRun; + + public void run() { + List strings = null; + } + + public int ab; + private int c; + + + public int awe; + + + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeWholeFile_after.java b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeWholeFile_after.java new file mode 100644 index 000000000000..1a0c071acd66 --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeWholeFile_after.java @@ -0,0 +1,10 @@ +import java.util.List; + +public class Test { + + + public void run() { + List strings = null; + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeWholeFile_before.java b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeWholeFile_before.java new file mode 100644 index 000000000000..26f115f2588b --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatOptimizeWholeFile_before.java @@ -0,0 +1,12 @@ +import java.util.HashMap; +import java.util.Set; +import java.util.List; + +public class Test { + + + public void run() { + List strings = null; + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatRearrangeSelection_after.java b/java/java-tests/testData/actions/reformatFileInEditor/formatRearrangeSelection_after.java new file mode 100644 index 000000000000..fb22c27cefc8 --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatRearrangeSelection_after.java @@ -0,0 +1,9 @@ +class Test { + + public int a = 3; + private int b = 3; + + +public void run () {} +int aero = 12; +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatRearrangeSelection_before.java b/java/java-tests/testData/actions/reformatFileInEditor/formatRearrangeSelection_before.java new file mode 100644 index 000000000000..0f1201c936ea --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatRearrangeSelection_before.java @@ -0,0 +1,9 @@ +class Test { + + private int b = 3; +public int a = 3; + + +public void run () {} +int aero = 12; +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatSelection_after.java b/java/java-tests/testData/actions/reformatFileInEditor/formatSelection_after.java new file mode 100644 index 000000000000..357b4779dc09 --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatSelection_after.java @@ -0,0 +1,15 @@ +public class Test { + + int a = 3; + + int c = 12; + + public void run() { + + int arr = 12; + long test = 1; + + } + + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatSelection_before.java b/java/java-tests/testData/actions/reformatFileInEditor/formatSelection_before.java new file mode 100644 index 000000000000..c28ea3ea10db --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatSelection_before.java @@ -0,0 +1,15 @@ +public class Test { + + int a = 3; + + int c = 12; + + public void run() { + + int arr = 12; +long test =1; + + } + + +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatWholeFile_after.java b/java/java-tests/testData/actions/reformatFileInEditor/formatWholeFile_after.java new file mode 100644 index 000000000000..837621dd5421 --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatWholeFile_after.java @@ -0,0 +1,8 @@ +public class Test { + + int a = 3; + + + void run() { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/actions/reformatFileInEditor/formatWholeFile_before.java b/java/java-tests/testData/actions/reformatFileInEditor/formatWholeFile_before.java new file mode 100644 index 000000000000..d20b5115dd61 --- /dev/null +++ b/java/java-tests/testData/actions/reformatFileInEditor/formatWholeFile_before.java @@ -0,0 +1,9 @@ +public class Test { + + int a = 3; + + +void run() +{ +} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/actions/ReformatCodeActionInEditorTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/actions/ReformatCodeActionInEditorTest.java new file mode 100644 index 000000000000..184405d8ddf3 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/actions/ReformatCodeActionInEditorTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2000-2014 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.codeInsight.actions; + +import com.intellij.JavaTestUtil; +import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class ReformatCodeActionInEditorTest extends LightCodeInsightFixtureTestCase { + + @Override + protected String getTestDataPath() { + return JavaTestUtil.getJavaTestDataPath() + "/actions/reformatFileInEditor/"; + } + + public void doTest(@NotNull ReformatFilesOptions options) { + setOptions(options); + myFixture.configureByFile(getTestDataPath() + getTestName(true) + "_before.java"); + + final String actionId = IdeActions.ACTION_EDITOR_REFORMAT; + AnAction action = ActionManager.getInstance().getAction(actionId); + + AnActionEvent event = createEventFor(action, getProject(), myFixture.getEditor()); + + action.actionPerformed(event); + myFixture.checkResultByFile(getTestName(true) + "_after.java"); + } + + protected AnActionEvent createEventFor(@NotNull AnAction action, @NotNull final Project project, @NotNull final Editor editor) { + return new AnActionEvent(null, new DataContext() { + @Nullable + @Override + public Object getData(@NonNls String dataId) { + if (CommonDataKeys.PROJECT.is(dataId)) return project; + if (CommonDataKeys.EDITOR.is(dataId)) return editor; + return null; + } + }, "", action.getTemplatePresentation(), ActionManager.getInstance(), 0); + } + + protected void setOptions(ReformatFilesOptions options) { + ReformatCodeAction.setTestOptions(options); + } + + public void testFormatWholeFile() { + doTest(new MockReformatFileSettings().setProcessWholeFile(true)); + } + + public void testFormatOptimizeWholeFile() { + doTest(new MockReformatFileSettings().setProcessWholeFile(true).setOptimizeImports(true)); + } + + public void testFormatOptimizeRearrangeWholeFile() { + doTest(new MockReformatFileSettings().setProcessWholeFile(true).setOptimizeImports(true).setRearrange(true)); + } + + public void testFormatSelection() { + doTest(new MockReformatFileSettings().setProcessWholeFile(false)); + } + + public void testFormatRearrangeSelection() { + doTest(new MockReformatFileSettings().setProcessWholeFile(false).setRearrange(true)); + } + + + //todo add tests on vcs changes + +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/RearrangeCodeProcessor.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/RearrangeCodeProcessor.java index b34fcb858108..4dbdefc6c8ec 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/RearrangeCodeProcessor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/RearrangeCodeProcessor.java @@ -45,6 +45,11 @@ public class RearrangeCodeProcessor extends AbstractLayoutCodeProcessor { public RearrangeCodeProcessor(@NotNull AbstractLayoutCodeProcessor previousProcessor) { super(previousProcessor, COMMAND_NAME, PROGRESS_TEXT); } + + public RearrangeCodeProcessor(@NotNull AbstractLayoutCodeProcessor previousProcessor, @NotNull SelectionModel selectionModel) { + super(previousProcessor, COMMAND_NAME, PROGRESS_TEXT); + mySelectionModel = selectionModel; + } public RearrangeCodeProcessor(@NotNull Project project, @NotNull PsiFile file, diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeAction.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeAction.java index bcbd0fd6738d..d21ec363fd55 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeAction.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeAction.java @@ -192,8 +192,13 @@ public class ReformatCodeAction extends AnAction implements DumbAware { processor = new ReformatCodeProcessor(project, file, range, !processSelectedText && processChangedTextOnly); } - if (rearrangeEntries && editor != null) { - processor = new RearrangeCodeProcessor(processor); + if (rearrangeEntries) { + if (processSelectedText && editor != null) { + processor = new RearrangeCodeProcessor(processor, editor.getSelectionModel()); + } + else { + processor = new RearrangeCodeProcessor(processor); + } } processor.run();