mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
IDEA-125512 switch case doesn't indent properly
This commit is contained in:
+20
-5
@@ -26,6 +26,7 @@ import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.actionSystem.IdeActions;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
|
||||
@@ -39,6 +40,7 @@ import com.intellij.patterns.PsiJavaElementPattern;
|
||||
import com.intellij.patterns.PsiNameValuePairPattern;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.filters.*;
|
||||
import com.intellij.psi.filters.classes.AnnotationTypeFilter;
|
||||
import com.intellij.psi.filters.classes.AssignableFromContextFilter;
|
||||
@@ -52,10 +54,7 @@ import com.intellij.psi.scope.ElementClassFilter;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.PairConsumer;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import com.intellij.util.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -324,7 +323,7 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
}
|
||||
|
||||
if (isSwitchLabel) {
|
||||
result.addElement(TailTypeDecorator.withTail(element, TailType.createSimpleTailType(':')));
|
||||
result.addElement(new IndentingDecorator(TailTypeDecorator.withTail(element, TailType.createSimpleTailType(':'))));
|
||||
}
|
||||
else {
|
||||
final LookupItem item = element.as(LookupItem.CLASS_CONDITION_KEY);
|
||||
@@ -791,4 +790,20 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
result.addElement(TailTypeDecorator.withTail(LookupElementBuilder.create(s), TailType.SEMICOLON));
|
||||
}
|
||||
}
|
||||
|
||||
private static class IndentingDecorator extends LookupElementDecorator<LookupElement> {
|
||||
public IndentingDecorator(LookupElement delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInsert(InsertionContext context) {
|
||||
super.handleInsert(context);
|
||||
Project project = context.getProject();
|
||||
Document document = context.getDocument();
|
||||
int lineStartOffset = DocumentUtil.getLineStartOffset(context.getStartOffset(), document);
|
||||
PsiDocumentManager.getInstance(project).commitDocument(document);
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(context.getFile(), lineStartOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
class Test {
|
||||
void m(E e) {
|
||||
switch (e) {
|
||||
case AA:
|
||||
case B<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum E {
|
||||
AA, BB
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
class Test {
|
||||
void m(E e) {
|
||||
switch (e) {
|
||||
case AA:
|
||||
case BB:<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum E {
|
||||
AA, BB
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ class C {
|
||||
|
||||
public static void main(A a) {
|
||||
switch (a) {
|
||||
case Abc:<caret>
|
||||
case Abc:<caret>
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -1502,4 +1502,6 @@ class Bar {
|
||||
}
|
||||
}'''
|
||||
}
|
||||
|
||||
public void testIndentingForSwitchCase() { doTest() }
|
||||
}
|
||||
|
||||
@@ -86,4 +86,12 @@ public final class DocumentUtil {
|
||||
public static boolean isValidOffset(int offset, @NotNull Document document) {
|
||||
return offset >= 0 && offset <= document.getTextLength();
|
||||
}
|
||||
|
||||
public static int getLineStartOffset(int offset, @NotNull Document document) {
|
||||
if (offset < 0 || offset > document.getTextLength()) {
|
||||
return offset;
|
||||
}
|
||||
int lineNumber = document.getLineNumber(offset);
|
||||
return document.getLineStartOffset(lineNumber);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.DocumentUtil;
|
||||
import org.intellij.lang.annotations.JdkConstants;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -224,7 +225,7 @@ public final class EditorUtil {
|
||||
* @return given text offset that identifies the same position that is pointed by the given visual column
|
||||
*
|
||||
* @deprecated This function can give incorrect results when soft wraps are enabled in editor. It is also slow in case of
|
||||
* long document lines - {@link com.intellij.openapi.editor.Editor#logicalPositionToOffset(com.intellij.openapi.editor.LogicalPosition)}
|
||||
* long document lines - {@link Editor#logicalPositionToOffset(LogicalPosition)}
|
||||
* should be faster when soft wraps are enabled. To be removed in IDEA 16.
|
||||
*/
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
@@ -718,8 +719,8 @@ public final class EditorUtil {
|
||||
* @param end target end coordinate
|
||||
* @return pair of the closest surrounding non-soft-wrapped logical positions for the visual line start and end
|
||||
*
|
||||
* @see #getNotFoldedLineStartOffset(com.intellij.openapi.editor.Editor, int)
|
||||
* @see #getNotFoldedLineEndOffset(com.intellij.openapi.editor.Editor, int)
|
||||
* @see #getNotFoldedLineStartOffset(Editor, int)
|
||||
* @see #getNotFoldedLineEndOffset(Editor, int)
|
||||
*/
|
||||
@SuppressWarnings("AssignmentToForLoopParameter")
|
||||
public static Pair<LogicalPosition, LogicalPosition> calcSurroundingRange(@NotNull Editor editor,
|
||||
@@ -776,7 +777,7 @@ public final class EditorUtil {
|
||||
*/
|
||||
public static int getNotFoldedLineStartOffset(@NotNull Editor editor, int offset) {
|
||||
while(true) {
|
||||
offset = getLineStartOffset(offset, editor.getDocument());
|
||||
offset = DocumentUtil.getLineStartOffset(offset, editor.getDocument());
|
||||
FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(offset - 1);
|
||||
if (foldRegion == null || foldRegion.getStartOffset() >= offset) {
|
||||
break;
|
||||
@@ -801,14 +802,6 @@ public final class EditorUtil {
|
||||
return offset;
|
||||
}
|
||||
|
||||
private static int getLineStartOffset(int offset, Document document) {
|
||||
if (offset > document.getTextLength()) {
|
||||
return offset;
|
||||
}
|
||||
int lineNumber = document.getLineNumber(offset);
|
||||
return document.getLineStartOffset(lineNumber);
|
||||
}
|
||||
|
||||
private static int getLineEndOffset(int offset, Document document) {
|
||||
if (offset >= document.getTextLength()) {
|
||||
return offset;
|
||||
|
||||
Reference in New Issue
Block a user