diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java index 05531ded0f16..acc8d8112096 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java @@ -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 { diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java index 2fd4d92f865c..e12d601549b5 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java @@ -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(); diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java index 914d225f802b..6aadf95a1ae2 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java @@ -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); }