From d945f6dde28a2ec0f0bbe170b3b90b8d915354bb Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 15 May 2018 17:21:29 +0300 Subject: [PATCH] cleanup --- .../folding/impl/ClosureFolding.java | 61 +++++++++---------- .../xpath/xslt/impl/XsltStuffProvider.java | 1 + 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/ClosureFolding.java b/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/ClosureFolding.java index 3581e2b6a32c..4b3abb9a5c15 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/ClosureFolding.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/folding/impl/ClosureFolding.java @@ -16,11 +16,13 @@ package com.intellij.codeInsight.folding.impl; import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase; +import com.intellij.codeInsight.folding.JavaCodeFoldingSettings; import com.intellij.codeInsight.generation.OverrideImplementExploreUtil; import com.intellij.lang.folding.NamedFoldingDescriptor; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.FoldingGroup; import com.intellij.openapi.project.IndexNotReadyException; +import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.util.PsiUtil; @@ -30,6 +32,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -45,12 +48,12 @@ class ClosureFolding { private final boolean myQuick; private ClosureFolding(@NotNull PsiAnonymousClass anonymousClass, - @NotNull PsiNewExpression newExpression, - boolean quick, - @Nullable PsiClass baseClass, - @NotNull JavaFoldingBuilderBase builder, - @NotNull PsiMethod method, - @NotNull PsiCodeBlock methodBody) { + @NotNull PsiNewExpression newExpression, + boolean quick, + @Nullable PsiClass baseClass, + @NotNull JavaFoldingBuilderBase builder, + @NotNull PsiMethod method, + @NotNull PsiCodeBlock methodBody) { myAnonymousClass = anonymousClass; myNewExpression = newExpression; myQuick = quick; @@ -61,7 +64,7 @@ class ClosureFolding { } @Nullable - List process(Document document) { + List process(@NotNull Document document) { PsiJavaToken lbrace = methodBody.getLBrace(); PsiJavaToken rbrace = methodBody.getRBrace(); PsiElement classRBrace = myAnonymousClass.getRBrace(); @@ -82,15 +85,15 @@ class ClosureFolding { return createDescriptors(classRBrace, rangeStart, rangeEnd, header, "}"); } - private static int trimStartSpaces(CharSequence seq, int rangeStart) { + private static int trimStartSpaces(@NotNull CharSequence seq, int rangeStart) { return CharArrayUtil.shiftForward(seq, rangeStart, " \n\t"); } - private static int trimTailSpaces(CharSequence seq, int rangeEnd) { + private static int trimTailSpaces(@NotNull CharSequence seq, int rangeEnd) { return CharArrayUtil.shiftBackward(seq, rangeEnd - 1, " \n\t") + 1; } - private static int getContentRangeEnd(Document document, PsiJavaToken rbrace, PsiElement classRBrace) { + private static int getContentRangeEnd(@NotNull Document document, @NotNull PsiJavaToken rbrace, @NotNull PsiElement classRBrace) { CharSequence seq = document.getCharsSequence(); int rangeEnd = rbrace.getTextRange().getStartOffset(); @@ -104,7 +107,7 @@ class ClosureFolding { return rangeEnd; } - private boolean showSingleLineFolding(Document document, String contents, String header) { + private boolean showSingleLineFolding(@NotNull Document document, @NotNull String contents, @NotNull String header) { return contents.indexOf('\n') < 0 && myBuilder.fitsRightMargin(myAnonymousClass, document, getClosureStartOffset(), getClosureEndOffset(), header.length() + contents.length() + 5); } @@ -118,24 +121,25 @@ class ClosureFolding { } @Nullable - private List createDescriptors(PsiElement classRBrace, + private List createDescriptors(@NotNull PsiElement classRBrace, int rangeStart, int rangeEnd, - String header, - String footer) { + @NotNull String header, + @NotNull String footer) { if (rangeStart >= rangeEnd) return null; FoldingGroup group = FoldingGroup.newGroup("lambda"); List foldElements = new ArrayList<>(); - foldElements.add(new NamedFoldingDescriptor(myNewExpression, getClosureStartOffset(), rangeStart, group, header)); + foldElements.add(new NamedFoldingDescriptor(myNewExpression.getNode(), new TextRange(getClosureStartOffset(), rangeStart), group, header, JavaCodeFoldingSettings.getInstance().isCollapseLambdas(), Collections + .emptySet())); if (rangeEnd + 1 < getClosureEndOffset()) { - foldElements.add(new NamedFoldingDescriptor(classRBrace, rangeEnd, getClosureEndOffset(), group, footer)); + foldElements.add(new NamedFoldingDescriptor(classRBrace.getNode(), new TextRange(rangeEnd, getClosureEndOffset()), group, footer, JavaCodeFoldingSettings.getInstance().isCollapseLambdas(), Collections.emptySet())); } return foldElements; } @Nullable - private static String getClosureContents(int rangeStart, int rangeEnd, CharSequence seq) { + private static String getClosureContents(int rangeStart, int rangeEnd, @NotNull CharSequence seq) { int firstLineStart = CharArrayUtil.shiftForward(seq, rangeStart, " \t"); if (firstLineStart < seq.length() - 1 && seq.charAt(firstLineStart) == '\n') firstLineStart++; @@ -145,6 +149,7 @@ class ClosureFolding { return seq.subSequence(firstLineStart, lastLineEnd).toString(); } + @NotNull private String getFoldingHeader() { String methodName = shouldShowMethodName() ? myMethod.getName() : ""; String type = myQuick ? "" : getOptionalLambdaType(); @@ -153,7 +158,7 @@ class ClosureFolding { } @Nullable - static ClosureFolding prepare(PsiAnonymousClass anonymousClass, boolean quick, JavaFoldingBuilderBase builder) { + static ClosureFolding prepare(@NotNull PsiAnonymousClass anonymousClass, boolean quick, @NotNull JavaFoldingBuilderBase builder) { PsiElement parent = anonymousClass.getParent(); if (parent instanceof PsiNewExpression && hasNoArguments((PsiNewExpression)parent)) { PsiClass baseClass = quick ? null : anonymousClass.getBaseClassType().resolve(); @@ -168,20 +173,16 @@ class ClosureFolding { return null; } - private static boolean hasNoArguments(PsiNewExpression expression) { + private static boolean hasNoArguments(@NotNull PsiNewExpression expression) { PsiExpressionList argumentList = expression.getArgumentList(); return argumentList != null && argumentList.isEmpty(); } private static boolean hasOnlyOneLambdaMethod(@NotNull PsiAnonymousClass anonymousClass, boolean checkResolve) { PsiField[] fields = anonymousClass.getFields(); - if (fields.length != 0) { - if (fields.length == 1 && HighlightUtilBase.SERIAL_VERSION_UID_FIELD_NAME.equals(fields[0].getName()) && - fields[0].hasModifierProperty(PsiModifier.STATIC)) { - //ok - } else { - return false; - } + if (fields.length != 0 && (fields.length != 1 || !HighlightUtilBase.SERIAL_VERSION_UID_FIELD_NAME.equals(fields[0].getName()) || + !fields[0].hasModifierProperty(PsiModifier.STATIC))) { + return false; } if (anonymousClass.getInitializers().length != 0 || anonymousClass.getInnerClasses().length != 0 || @@ -208,13 +209,10 @@ class ClosureFolding { static boolean seemsLikeLambda(@Nullable PsiClass baseClass, @NotNull PsiElement context) { if (baseClass == null || !PsiUtil.hasDefaultConstructor(baseClass, true)) return false; - if (PsiUtil.isLanguageLevel8OrHigher(context) && LambdaUtil.isFunctionalClass(baseClass)) { - return false; - } - - return true; + return !PsiUtil.isLanguageLevel8OrHigher(context) || !LambdaUtil.isFunctionalClass(baseClass); } + @NotNull private String getOptionalLambdaType() { if (myBuilder.shouldShowExplicitLambdaType(myAnonymousClass, myNewExpression)) { String baseClassName = ObjectUtils.assertNotNull(myAnonymousClass.getBaseClassType().resolve()).getName(); @@ -241,5 +239,4 @@ class ClosureFolding { return true; } } - } diff --git a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/impl/XsltStuffProvider.java b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/impl/XsltStuffProvider.java index 9e936e83466a..e92271f1b715 100644 --- a/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/impl/XsltStuffProvider.java +++ b/plugins/xpath/xpath-lang/src/org/intellij/lang/xpath/xslt/impl/XsltStuffProvider.java @@ -97,6 +97,7 @@ public class XsltStuffProvider implements UsageGroupingRuleProvider { return "Template (" + sb.toString() + ")"; } + @Override @Nullable public FileStatus getFileStatus() { return null;