From 25d7ad6d56360bf3e94abea7eaf523d12fede3be Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Mon, 21 Nov 2016 17:31:23 +0300 Subject: [PATCH] Cleanup->greener code --- .../psi/codeStyle/CodeStyleManager.java | 12 ++++--- .../source/codeStyle/CodeFormatterFacade.java | 6 ++-- .../codeStyle/CodeStyleManagerImpl.java | 33 +++++++------------ .../codeStyle/CodeStyleManagerRunnable.java | 8 +++-- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java b/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java index 8f8ac006c52e..a72aae5e986f 100644 --- a/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java +++ b/platform/core-api/src/com/intellij/psi/codeStyle/CodeStyleManager.java @@ -76,7 +76,7 @@ public abstract class CodeStyleManager { * @return the element in the PSI tree after the reformat operation corresponding to the * original element. * @throws IncorrectOperationException if the file to reformat is read-only. - * @see #reformatText(com.intellij.psi.PsiFile, int, int) + * @see #reformatText(PsiFile, int, int) */ @NotNull public abstract PsiElement reformat(@NotNull PsiElement element) throws IncorrectOperationException; @@ -90,7 +90,7 @@ public abstract class CodeStyleManager { * @return the element in the PSI tree after the reformat operation corresponding to the * original element. * @throws IncorrectOperationException if the file to reformat is read-only. - * @see #reformatText(com.intellij.psi.PsiFile, int, int) + * @see #reformatText(PsiFile, int, int) */ @NotNull public abstract PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException; @@ -104,7 +104,7 @@ public abstract class CodeStyleManager { * @return the element in the PSI tree after the reformat operation corresponding to the * original element. * @throws IncorrectOperationException if the file to reformat is read-only. - * @see #reformatText(com.intellij.psi.PsiFile, int, int) + * @see #reformatText(PsiFile, int, int) */ public abstract PsiElement reformatRange(@NotNull PsiElement element, int startOffset, int endOffset) throws IncorrectOperationException; @@ -120,7 +120,7 @@ public abstract class CodeStyleManager { * @return the element in the PSI tree after the reformat operation corresponding to the * original element. * @throws IncorrectOperationException if the file to reformat is read-only. - * @see #reformatText(com.intellij.psi.PsiFile, int, int) + * @see #reformatText(PsiFile, int, int) */ public abstract PsiElement reformatRange(@NotNull PsiElement element, int startOffset, @@ -139,7 +139,7 @@ public abstract class CodeStyleManager { /** * Re-formats a ranges of text in the specified file. This method works faster than - * {@link #reformatRange(com.intellij.psi.PsiElement, int, int)} but invalidates the + * {@link #reformatRange(PsiElement, int, int)} but invalidates the * PSI structure for the file. * * @param file the file to reformat @@ -269,8 +269,10 @@ public abstract class CodeStyleManager { * * @param r the operation to run. */ + @SuppressWarnings("LambdaUnfriendlyMethodOverload") public abstract void performActionWithFormatterDisabled(Runnable r); + @SuppressWarnings("LambdaUnfriendlyMethodOverload") public abstract void performActionWithFormatterDisabled(ThrowableRunnable r) throws T; public abstract T performActionWithFormatterDisabled(Computable r); diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java index dfc61597e86e..5452b3656edc 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeFormatterFacade.java @@ -387,12 +387,12 @@ public class CodeFormatterFacade { } } } - return result == null ? Collections.emptySet() : result; + return result == null ? Collections.emptySet() : result; } /** - * Inspects all lines of the given document and wraps all of them that exceed {@link CodeStyleSettings#getRightMargin(com.intellij.lang.Language)} + * Inspects all lines of the given document and wraps all of them that exceed {@link CodeStyleSettings#getRightMargin(Language)} * right margin}. *

* I.e. the algorithm is to do the following for every line: @@ -400,7 +400,7 @@ public class CodeFormatterFacade { *

    * 
    *
  1. - * Check if the line exceeds {@link CodeStyleSettings#getRightMargin(com.intellij.lang.Language)} right margin}. Go to the next line in the case of + * Check if the line exceeds {@link CodeStyleSettings#getRightMargin(Language)} right margin}. Go to the next line in the case of * negative answer; *
  2. *
  3. Determine line wrap position;
  4. diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java index dc8451b630af..dbb7dc9b0666 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerImpl.java @@ -60,13 +60,7 @@ import java.util.concurrent.TimeUnit; public class CodeStyleManagerImpl extends CodeStyleManager { private static final Logger LOG = Logger.getInstance(CodeStyleManagerImpl.class); private static final ThreadLocal SEQUENTIAL_PROCESSING_ALLOWED - = new ThreadLocal() - { - @Override - protected ProcessingUnderProgressInfo initialValue() { - return new ProcessingUnderProgressInfo(); - } - }; + = ThreadLocal.withInitial(() -> new ProcessingUnderProgressInfo()); private final ThreadLocal myCurrentFormattingMode = ThreadLocal.withInitial(() -> FormattingMode.REFORMAT); @@ -101,8 +95,8 @@ public class CodeStyleManagerImpl extends CodeStyleManager { return element; } - ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(element); - final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(new CodeFormatterFacade(getSettings(), element.getLanguage()).processElement(treeElement)); + ASTNode treeElement = element.getNode(); + final PsiElement formatted = new CodeFormatterFacade(getSettings(), element.getLanguage()).processElement(treeElement).getPsi(); if (!canChangeWhiteSpacesOnly) { return postProcessElement(formatted); } @@ -278,7 +272,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { } } - private PsiElement reformatRangeImpl(final PsiElement element, + private PsiElement reformatRangeImpl(final @NotNull PsiElement element, final int startOffset, final int endOffset, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { @@ -289,9 +283,9 @@ public class CodeStyleManagerImpl extends CodeStyleManager { return element; } - ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(element); + ASTNode treeElement = element.getNode(); final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(), element.getLanguage()); - final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(codeFormatter.processRange(treeElement, startOffset, endOffset)); + final PsiElement formatted = codeFormatter.processRange(treeElement, startOffset, endOffset).getPsi(); return canChangeWhiteSpacesOnly ? formatted : postProcessElement(formatted); } @@ -579,9 +573,9 @@ public class CodeStyleManagerImpl extends CodeStyleManager { @Override public Indent getIndent(String text, FileType fileType) { int indent = IndentHelperImpl.getIndent(myProject, fileType, text, true); - int indenLevel = indent / IndentHelperImpl.INDENT_FACTOR; - int spaceCount = indent - indenLevel * IndentHelperImpl.INDENT_FACTOR; - return new IndentImpl(getSettings(), indenLevel, spaceCount, fileType); + int indentLevel = indent / IndentHelperImpl.INDENT_FACTOR; + int spaceCount = indent - indentLevel * IndentHelperImpl.INDENT_FACTOR; + return new IndentImpl(getSettings(), indentLevel, spaceCount, fileType); } @Override @@ -821,7 +815,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager { myDocument.replaceString(lineToInsertStartOffset, caretLineOffset, myCaretIndentToRestore); } - private boolean rangeHasWhiteSpaceSymbolsOnly(CharSequence text, int lineStartOffset, int lineEndOffset) { + private static boolean rangeHasWhiteSpaceSymbolsOnly(CharSequence text, int lineStartOffset, int lineEndOffset) { for (int i = lineStartOffset; i < lineEndOffset; i++) { char c = text.charAt(i); if (c != ' ' && c != '\t' && c != '\n') { @@ -862,9 +856,8 @@ public class CodeStyleManagerImpl extends CodeStyleManager { } } - private TextRange postProcessEnabledRanges(@NotNull final PsiFile file, @NotNull TextRange range, CodeStyleSettings settings) { - TextRange result = TextRange.create(range.getStartOffset(), range.getEndOffset()); - List enabledRanges = myTagHandler.getEnabledRanges(file.getNode(), result); + private void postProcessEnabledRanges(@NotNull final PsiFile file, @NotNull TextRange range, CodeStyleSettings settings) { + List enabledRanges = myTagHandler.getEnabledRanges(file.getNode(), range); int delta = 0; for (TextRange enabledRange : enabledRanges) { enabledRange = enabledRange.shiftRight(delta); @@ -873,8 +866,6 @@ public class CodeStyleManagerImpl extends CodeStyleManager { delta += processedRange.getLength() - enabledRange.getLength(); } } - result = result.grown(delta); - return result; } @Override diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java index d84a034a9be2..8544891a9cf6 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleManagerRunnable.java @@ -161,9 +161,11 @@ abstract class CodeStyleManagerRunnable { } final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file); - final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset); - if (textRange != null) { - return textRange; + if (builder != null) { + final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset); + if (textRange != null) { + return textRange; + } } final TextRange elementRange = elementAtOffset.getTextRange();