IDEA-79841 Correct indentation after inplace/extract refactoring

This commit is contained in:
Denis.Zhdanov
2012-01-13 12:06:14 +04:00
parent 906736fe4f
commit 4b00d00d7f
2 changed files with 91 additions and 2 deletions
@@ -0,0 +1,81 @@
/*
* Copyright 2000-2012 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.refactoring;
import com.intellij.psi.*;
import com.intellij.refactoring.extractMethod.PrepareFailedException;
import com.intellij.refactoring.inline.InlineMethodProcessor;
import com.intellij.testFramework.LightCodeInsightTestCase;
import java.io.IOException;
/**
* Is assumed to contain tests that include more than one refactoring into the processing.
*
* @author Denis Zhdanov
* @since 1/12/12 2:35 PM
*/
public class SequentialRefactoringTest extends LightCodeInsightTestCase {
public void testFormattingAfterInlineExtractMethod() throws IOException, PrepareFailedException {
String text =
"public class BrokenAlignment {\n" +
"\n" +
" public Object test() {\n" +
" if (System.currentTimeMillis() > 1) {\n" +
" if (System.currentTimeMillis() > 2) {\n" +
" getData();\n" +
" }\n" +
" }\n" +
" return \"hey\";\n" +
" }\n" +
"\n" +
" private void getData() {\n" +
" String[] args = new String[]{};\n" +
" String result = \"data: \";\n" +
" int i = 0;\n" +
" while (i < args.length) {\n" +
" result += args[i];\n" +
" if (i % 2 == 0) {\n" +
" result += \", it's even!\";\n" +
" } else {\n" +
" System.out.println(\"It's odd :(\");\n" +
" break;\n" +
" }\n" +
" }\n" +
" int k = 1;\n" +
" }\n" +
"\n" +
"}";
configureFromFileText("test.java", text);
// Perform inline.
final PsiClass clazz = ((PsiClassOwner)myFile).getClasses()[0];
final PsiMethod[] methods = clazz.findMethodsByName("getData", false);
final PsiReferenceExpression ref = (PsiReferenceExpression)myFile.findReferenceAt(text.indexOf("getData") + 1);
final InlineMethodProcessor processor = new InlineMethodProcessor(getProject(), methods[0], ref, myEditor, false);
processor.run();
// Perform extract.
final String currentText = myEditor.getDocument().getText();
int start = currentText.indexOf("String[] args");
int end = currentText.indexOf("\n", currentText.indexOf("int k"));
myEditor.getSelectionModel().setSelection(start, end);
ExtractMethodTest.performExtractMethod(true, true, myEditor, myFile, getProject());
checkResultByText(text.replace("getData", "newMethod"));
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -154,7 +154,7 @@ public class PostprocessReformattingAspect implements PomModelAspect {
}
}
private void atomic(Runnable r) {
private static void atomic(Runnable r) {
ProgressManager.getInstance().executeNonCancelableSection(r);
}
@@ -484,6 +484,9 @@ public class PostprocessReformattingAspect implements PomModelAspect {
final TreeSet<PostprocessFormattingTask> rangesToProcess) {
final Set<ASTNode> nodesToProcess = new HashSet<ASTNode>(astNodes);
final Document document = provider.getDocument();
if (document == null) {
return;
}
for (final ASTNode node : astNodes) {
nodesToProcess.remove(node);
final FileElement fileElement = TreeUtil.getFileElement((TreeElement)node);
@@ -507,6 +510,7 @@ public class PostprocessReformattingAspect implements PomModelAspect {
if (!currentNodeGenerated && inGeneratedContext) {
if (element.getElementType() == TokenType.WHITE_SPACE) return false;
final int oldIndent = CodeEditUtil.getOldIndentation(element);
CodeEditUtil.setOldIndentation(element, -1);
LOG.assertTrue(oldIndent >= 0, "for not generated items old indentation must be defined: element " + element);
rangesToProcess.add(new ReindentTask(document.createRangeMarker(element.getTextRange()), oldIndent));
inGeneratedContext = false;
@@ -574,8 +578,10 @@ public class PostprocessReformattingAspect implements PomModelAspect {
}
}
@SuppressWarnings("StatementWithEmptyBody")
private static int getNewIndent(final PsiFile psiFile, final int firstWhitespace) {
final Document document = psiFile.getViewProvider().getDocument();
assert document != null;
final int startOffset = document.getLineStartOffset(document.getLineNumber(firstWhitespace));
int endOffset = startOffset;
final CharSequence charsSequence = document.getCharsSequence();
@@ -592,6 +598,7 @@ public class PostprocessReformattingAspect implements PomModelAspect {
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(myPsiManager.getProject());
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myPsiManager.getProject());
final Document document = viewProvider.getDocument();
assert document != null;
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(styleSettings);
documentManager.commitDocument(document);
@@ -688,6 +695,7 @@ public class PostprocessReformattingAspect implements PomModelAspect {
@Override
public void execute(FileViewProvider viewProvider) {
final Document document = viewProvider.getDocument();
assert document != null;
final PsiFile psiFile = viewProvider.getPsi(viewProvider.getBaseLanguage());
for (Pair<Integer, RangeMarker> integerRangeMarkerPair : myRangesToReindent) {
RangeMarker marker = integerRangeMarkerPair.second;