Cleanup->greener code

This commit is contained in:
Rustam Vishnyakov
2016-11-21 17:32:13 +03:00
parent 243b69c123
commit 25d7ad6d56
4 changed files with 27 additions and 32 deletions
@@ -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 <T extends Throwable> void performActionWithFormatterDisabled(ThrowableRunnable<T> r) throws T;
public abstract <T> T performActionWithFormatterDisabled(Computable<T> r);
@@ -387,12 +387,12 @@ public class CodeFormatterFacade {
}
}
}
return result == null ? Collections.<PsiLanguageInjectionHost>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}.
* <p/>
* I.e. the algorithm is to do the following for every line:
@@ -400,7 +400,7 @@ public class CodeFormatterFacade {
* <pre>
* <ol>
* <li>
* 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;
* </li>
* <li>Determine line wrap position; </li>
@@ -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<ProcessingUnderProgressInfo> SEQUENTIAL_PROCESSING_ALLOWED
= new ThreadLocal<ProcessingUnderProgressInfo>()
{
@Override
protected ProcessingUnderProgressInfo initialValue() {
return new ProcessingUnderProgressInfo();
}
};
= ThreadLocal.withInitial(() -> new ProcessingUnderProgressInfo());
private final ThreadLocal<FormattingMode> 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<TextRange> enabledRanges = myTagHandler.getEnabledRanges(file.getNode(), result);
private void postProcessEnabledRanges(@NotNull final PsiFile file, @NotNull TextRange range, CodeStyleSettings settings) {
List<TextRange> 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
@@ -161,9 +161,11 @@ abstract class CodeStyleManagerRunnable<T> {
}
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();