From 945952cc07dfbb861ead1ad5dac0242b0bf9cd4a Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Fri, 18 Mar 2005 19:24:50 +0300 Subject: [PATCH] (no message) --- .../editorActions/JoinLinesHandler.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/source/com/intellij/codeInsight/editorActions/JoinLinesHandler.java b/source/com/intellij/codeInsight/editorActions/JoinLinesHandler.java index 97f7d0e45a90..fcf240874c69 100644 --- a/source/com/intellij/codeInsight/editorActions/JoinLinesHandler.java +++ b/source/com/intellij/codeInsight/editorActions/JoinLinesHandler.java @@ -23,6 +23,7 @@ import com.intellij.openapi.editor.ex.DocumentEx; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.javadoc.PsiDocComment; import com.intellij.psi.search.LocalSearchScope; @@ -203,15 +204,25 @@ public class JoinLinesHandler extends EditorWriteActionHandler { }); } - private int tryUnwrapBlockStatement(PsiElement elementAtStartLineEnd, PsiElement elementAtNextLineStart) { + private static int tryUnwrapBlockStatement(PsiElement elementAtStartLineEnd, PsiElement elementAtNextLineStart) { if (elementAtStartLineEnd == null || elementAtNextLineStart == null) return -1; - if (!CodeStyleSettingsManager.getSettings(elementAtStartLineEnd.getProject()).KEEP_CONTROL_STATEMENT_IN_ONE_LINE) return -1; + final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(elementAtStartLineEnd.getProject()); if (!(elementAtStartLineEnd instanceof PsiJavaToken) || ((PsiJavaToken)elementAtStartLineEnd).getTokenType() != JavaTokenType.LBRACE) { return -1; } final PsiElement codeBlock = elementAtStartLineEnd.getParent(); if (!(codeBlock instanceof PsiCodeBlock)) return -1; if (!(codeBlock.getParent() instanceof PsiBlockStatement)) return -1; + final PsiElement parentStatement = codeBlock.getParent().getParent(); + + if (!(parentStatement instanceof PsiIfStatement && codeStyleSettings.IF_BRACE_FORCE != CodeStyleSettings.FORCE_BRACES_ALWAYS || + parentStatement instanceof PsiWhileStatement && codeStyleSettings.WHILE_BRACE_FORCE != CodeStyleSettings.FORCE_BRACES_ALWAYS || + (parentStatement instanceof PsiForStatement || parentStatement instanceof PsiForeachStatement) && + codeStyleSettings.FOR_BRACE_FORCE != CodeStyleSettings.FORCE_BRACES_ALWAYS || + parentStatement instanceof PsiDoWhileStatement && + codeStyleSettings.DOWHILE_BRACE_FORCE != CodeStyleSettings.FORCE_BRACES_ALWAYS)) { + return -1; + } PsiElement foundStatement = null; for (PsiElement element = elementAtStartLineEnd.getNextSibling(); element != null; element = element.getNextSibling()) { if (element instanceof PsiWhiteSpace) continue; @@ -231,7 +242,7 @@ public class JoinLinesHandler extends EditorWriteActionHandler { } return -1; } - private int tryJoinDeclaration(PsiElement elementAtStartLineEnd, PsiElement elementAtNextLineStart) { + private static int tryJoinDeclaration(PsiElement elementAtStartLineEnd, PsiElement elementAtNextLineStart) { if (elementAtStartLineEnd == null || elementAtNextLineStart == null) return -1; // first line. @@ -317,32 +328,27 @@ public class JoinLinesHandler extends EditorWriteActionHandler { PsiExpressionStatement statement = (PsiExpressionStatement) assignment.getParent(); - PsiDeclarationStatement newDecl = null; int startOffset = decl.getTextRange().getStartOffset(); try { - newDecl = factory.createVariableDeclarationStatement( + PsiDeclarationStatement newDecl = factory.createVariableDeclarationStatement( var.getName(), var.getType(), initializerExpression ); - PsiVariable newVar = ((PsiVariable) newDecl.getDeclaredElements()[0]); + PsiVariable newVar = (PsiVariable)newDecl.getDeclaredElements()[0]; if (var.getModifierList().getText().length() > 0) { newVar.getModifierList().setModifierProperty(PsiModifier.FINAL, true); } newVar.getModifierList().replace(var.getModifierList()); decl.replace(newDecl); statement.delete(); + return startOffset + newDecl.getTextRange().getEndOffset() - newDecl.getTextRange().getStartOffset(); } catch (IncorrectOperationException e) { LOG.error(e); + return -1; } - - if (newDecl != null) { - return startOffset + newDecl.getTextRange().getEndOffset() - newDecl.getTextRange().getStartOffset(); - } - - return -1; } - private int tryJoinStringLiteral(Document doc, PsiFile psiFile, int offsetNear) { + private static int tryJoinStringLiteral(Document doc, PsiFile psiFile, int offsetNear) { CharSequence text = doc.getCharsSequence(); int state = 0; int startQuoteOffset = -1;