From 8d7040e88ea42584cc1b002cd3fe7766da461e99 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Mon, 1 Sep 2014 19:08:01 +0400 Subject: [PATCH] Creating indent by CodeStyleManager.adjustLineIndent when surrounding with try-finally (IDEA-128859) --- .../JavaWithTryFinallySurrounder.java | 16 ++++++---------- .../java/SurroundWithTryFinallyUsingIndents.java | 5 +++++ ...SurroundWithTryFinallyUsingIndents_after.java | 9 +++++++++ .../surroundWith/JavaSurroundWithTest.java | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents.java create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java index e757ddeacc17..28732a65390b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryFinallySurrounder.java @@ -19,10 +19,8 @@ import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.EditorModificationUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.util.IncorrectOperationException; @@ -65,16 +63,14 @@ class JavaWithTryFinallySurrounder extends JavaStatementsSurrounder{ if (finallyBlock == null) { return null; } - int offset = finallyBlock.getTextRange().getStartOffset() + 2; - editor.getCaretModel().moveToOffset(offset); - final Document document = editor.getDocument(); + Document document = editor.getDocument(); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document); + TextRange finallyBlockRange = finallyBlock.getTextRange(); + int newLineOffset = finallyBlockRange.getStartOffset() + 2; + editor.getCaretModel().moveToOffset(newLineOffset); editor.getSelectionModel().removeSelection(); - final PsiStatement[] tryBlockStatements = tryBlock.getStatements(); - LOG.assertTrue(tryBlockStatements.length > 0, tryBlock.getText()); - final PsiStatement firstTryStmt = tryBlockStatements[0]; - final int indent = firstTryStmt.getTextOffset() - document.getLineStartOffset(document.getLineNumber(firstTryStmt.getTextOffset())); - EditorModificationUtil.insertStringAtCaret(editor, StringUtil.repeat(" ", indent), false, true); + CodeStyleManager.getInstance(project).adjustLineIndent(document, newLineOffset); + PsiDocumentManager.getInstance(project).commitDocument(document); return new TextRange(editor.getCaretModel().getOffset(), editor.getCaretModel().getOffset()); } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents.java new file mode 100644 index 000000000000..c491a29b3380 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents.java @@ -0,0 +1,5 @@ +class Test { + void test() { + int a = 2; + } +} diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents_after.java new file mode 100644 index 000000000000..a3887076e98f --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryFinallyUsingIndents_after.java @@ -0,0 +1,9 @@ +class Test { + void test() { + try { + int a = 2; + } finally { + + } + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java index 60be917bde7c..8bd53fdabfbf 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java @@ -17,6 +17,7 @@ package com.intellij.codeInsight.generation.surroundWith; import com.intellij.codeInsight.template.impl.TemplateManagerImpl; import com.intellij.codeInsight.template.impl.TemplateState; +import com.intellij.ide.highlighter.JavaFileType; import com.intellij.lang.LanguageSurrounders; import com.intellij.lang.java.JavaLanguage; import com.intellij.lang.surroundWith.SurroundDescriptor; @@ -24,6 +25,7 @@ import com.intellij.lang.surroundWith.Surrounder; import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; +import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.testFramework.LightCodeInsightTestCase; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; @@ -156,6 +158,18 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { doTest(getTestName(false), new JavaWithIfElseExpressionSurrounder()); } + public void testSurroundWithTryFinallyUsingIndents() { + CommonCodeStyleSettings.IndentOptions indentOptions = getCurrentCodeStyleSettings().getIndentOptions(JavaFileType.INSTANCE); + boolean oldUseTabs = indentOptions.USE_TAB_CHARACTER; + try { + indentOptions.USE_TAB_CHARACTER = true; + doTest(getTestName(false), new JavaWithTryFinallySurrounder()); + } + finally { + indentOptions.USE_TAB_CHARACTER = oldUseTabs; + } + } + private void doTest(@NotNull String fileName, final Surrounder surrounder) { configureByFile(BASE_PATH + fileName + ".java");