EnterHandler refactoring: indent logic moved to getLineIndent()

This commit is contained in:
Rustam Vishnyakov
2016-04-22 14:12:41 +03:00
parent 1c14cfe8af
commit 574333dc71
3 changed files with 29 additions and 10 deletions
@@ -19,6 +19,7 @@ package com.intellij.codeInsight.editorActions;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegate;
import com.intellij.codeStyle.CodeStyleFacade;
import com.intellij.ide.DataManager;
import com.intellij.lang.*;
import com.intellij.lang.documentation.CodeDocumentationProvider;
@@ -423,7 +424,7 @@ public class EnterHandler extends BaseEnterHandler {
if (codeInsightSettings.SMART_INDENT_ON_ENTER || myForceIndent || commentContext.docStart || commentContext.docAsterisk
|| commentContext.slashSlash)
{
myOffset = adjustLineIndent();
myOffset = adjustLineIndent(myDocument.getCharsSequence());
if (commentContext.docAsterisk && !StringUtil.isEmpty(indentInsideJavadoc) && myOffset < myDocument.getTextLength()) {
myDocument.insertString(myOffset + 1, indentInsideJavadoc);
@@ -471,10 +472,14 @@ public class EnterHandler extends BaseEnterHandler {
}
}
private int adjustLineIndent() {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
return codeStyleManager.adjustLineIndent(myFile, myOffset);
private int adjustLineIndent(CharSequence docChars) {
int indentStart = CharArrayUtil.shiftBackwardUntil(docChars, myOffset - 1, "\n") + 1;
int indentEnd = CharArrayUtil.shiftForward(docChars, indentStart, " \t");
String newIndent = CodeStyleFacade.getInstance(getProject()).getLineIndent(myDocument, myOffset);
if (newIndent == null) return myOffset;
int delta = newIndent.length() - (indentEnd - indentStart);
myDocument.replaceString(indentStart, indentEnd, newIndent);
return myOffset + delta;
}
private void generateJavadoc(CodeDocumentationAwareCommenter commenter) throws IncorrectOperationException {
@@ -557,16 +557,28 @@ public class FormatterImpl extends FormatterEx
final FormatProcessor processor = buildProcessorAndWrapBlocks(
documentModel, block, settings, indentOptions, new FormatTextRanges(affectedRange, true), offset
);
final LeafBlockWrapper blockAfterOffset = processor.getBlockRangesMap().getBlockAtOrAfter(offset);
if (blockAfterOffset != null && !blockAfterOffset.contains(offset)) {
final WhiteSpace whiteSpace = blockAfterOffset.getWhiteSpace();
WhiteSpace whiteSpace = getWhiteSpaceAtOffset(offset, processor);
if (whiteSpace != null) {
final IndentInfo indent = calcIndent(offset, documentModel, processor, whiteSpace);
return indent.generateNewWhiteSpace(indentOptions);
}
return null;
}
@Nullable
private static WhiteSpace getWhiteSpaceAtOffset(int offset,
@NotNull FormatProcessor formatProcessor) {
final LeafBlockWrapper blockAfterOffset = formatProcessor.getBlockRangesMap().getBlockAtOrAfter(offset);
if (blockAfterOffset != null) {
if (!blockAfterOffset.contains(offset)) return blockAfterOffset.getWhiteSpace();
}
else {
if (offset >= formatProcessor.getLastWhiteSpace().getStartOffset()) {
return formatProcessor.getLastWhiteSpace();
}
}
return null;
}
private static IndentInfo calcIndent(int offset, FormattingDocumentModel documentModel, FormatProcessor processor, WhiteSpace whiteSpace) {
processor.setAllWhiteSpacesAreReadOnly();
@@ -25,6 +25,7 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import org.jetbrains.annotations.NotNull;
@@ -50,6 +51,7 @@ public class CodeStyleFacadeImpl extends CodeStyleFacade {
@Nullable
public String getLineIndent(@NotNull final Document document, int offset) {
if (myProject == null) return null;
PsiDocumentManager.getInstance(myProject).commitDocument(document);
return CodeStyleManager.getInstance(myProject).getLineIndent(document, offset);
}