mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
IDEA-101570 Auto-Completing 'try' and 'finally' keywords does not honor next line brace placement
This commit is contained in:
@@ -118,24 +118,10 @@ public class TailTypes {
|
||||
return styleSettings.SPACE_WITHIN_IF_PARENTHESES;
|
||||
}
|
||||
};
|
||||
public static final TailType FINALLY_LBRACE = new BracesTailType() {
|
||||
@Override
|
||||
protected boolean isSpaceBeforeLBrace(final CommonCodeStyleSettings styleSettings, final Editor editor, final int tailOffset) {
|
||||
return styleSettings.SPACE_BEFORE_FINALLY_LBRACE;
|
||||
}
|
||||
};
|
||||
public static final TailType TRY_LBRACE = new BracesTailType() {
|
||||
@Override
|
||||
protected boolean isSpaceBeforeLBrace(CommonCodeStyleSettings styleSettings, Editor editor, int tailOffset) {
|
||||
return styleSettings.SPACE_BEFORE_TRY_LBRACE;
|
||||
}
|
||||
};
|
||||
public static final TailType DO_LBRACE = new BracesTailType() {
|
||||
@Override
|
||||
protected boolean isSpaceBeforeLBrace(CommonCodeStyleSettings styleSettings, Editor editor, int tailOffset) {
|
||||
return styleSettings.SPACE_BEFORE_DO_LBRACE;
|
||||
}
|
||||
};
|
||||
private static final TailType BRACES = new BracesTailType();
|
||||
public static final TailType FINALLY_LBRACE = BRACES;
|
||||
public static final TailType TRY_LBRACE = BRACES;
|
||||
public static final TailType DO_LBRACE = BRACES;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -23,22 +23,31 @@ import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.actionSystem.IdeActions;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public abstract class BracesTailType extends TailType {
|
||||
|
||||
protected abstract boolean isSpaceBeforeLBrace(CommonCodeStyleSettings styleSettings, Editor editor, final int tailOffset);
|
||||
public class BracesTailType extends TailType {
|
||||
|
||||
@Override
|
||||
public int processTail(final Editor editor, int tailOffset) {
|
||||
final CommonCodeStyleSettings styleSettings = getLocalCodeStyleSettings(editor, tailOffset);
|
||||
if (isSpaceBeforeLBrace(styleSettings, editor, tailOffset)) {
|
||||
tailOffset = insertChar(editor, tailOffset, ' ');
|
||||
int startOffset = tailOffset;
|
||||
|
||||
CharSequence seq = editor.getDocument().getCharsSequence();
|
||||
int nextNonWs = CharArrayUtil.shiftForward(seq, tailOffset, " \t");
|
||||
if (nextNonWs < seq.length() && seq.charAt(nextNonWs) == '{') {
|
||||
tailOffset = nextNonWs + 1;
|
||||
} else {
|
||||
tailOffset = insertChar(editor, startOffset, '{');
|
||||
}
|
||||
tailOffset = insertChar(editor, tailOffset, '{');
|
||||
|
||||
tailOffset = reformatBrace(editor, tailOffset, startOffset);
|
||||
|
||||
if (EnterAfterUnmatchedBraceHandler.isAfterUnmatchedLBrace(editor, tailOffset, getFileType(editor))) {
|
||||
new EnterHandler(EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER))
|
||||
.executeWriteAction(editor, DataManager.getInstance().getDataContext(editor.getContentComponent()));
|
||||
@@ -47,4 +56,16 @@ public abstract class BracesTailType extends TailType {
|
||||
return tailOffset;
|
||||
}
|
||||
|
||||
private static int reformatBrace(Editor editor, int tailOffset, int startOffset) {
|
||||
Project project = editor.getProject();
|
||||
if (project != null) {
|
||||
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
|
||||
if (psiFile != null) {
|
||||
editor.getCaretModel().moveToOffset(tailOffset);
|
||||
CodeStyleManager.getInstance(project).reformatText(psiFile, startOffset, tailOffset);
|
||||
tailOffset = editor.getCaretModel().getOffset();
|
||||
}
|
||||
}
|
||||
return tailOffset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
{
|
||||
try<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
{
|
||||
try
|
||||
{
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
{
|
||||
do<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
{
|
||||
do {
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1331,5 +1331,15 @@ class XInternalError {}
|
||||
checkResult()
|
||||
}
|
||||
|
||||
public void testBraceOnNextLine() {
|
||||
codeStyleSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE
|
||||
doTest()
|
||||
}
|
||||
|
||||
public void testDoForceBraces() {
|
||||
codeStyleSettings.DOWHILE_BRACE_FORCE = CommonCodeStyleSettings.FORCE_BRACES_ALWAYS
|
||||
doTest('\n')
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user