diff --git a/java/java-impl/src/com/intellij/javadoc/JavadocHelper.java b/java/java-impl/src/com/intellij/javadoc/JavadocHelper.java index 88cda8ed89ef..3db349967b4f 100644 --- a/java/java-impl/src/com/intellij/javadoc/JavadocHelper.java +++ b/java/java-impl/src/com/intellij/javadoc/JavadocHelper.java @@ -137,6 +137,7 @@ public class JavadocHelper { @NotNull public Pair> parse(@NotNull PsiFile psiFile, @NotNull Editor editor, int offset) { List result = new ArrayList(); + PsiDocumentManager.getInstance(psiFile.getProject()).commitDocument(editor.getDocument()); final PsiElement elementAtCaret = psiFile.findElementAt(offset); if (elementAtCaret == null) { return EMPTY; 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 7e581148ce0e..05531ded0f16 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java @@ -170,7 +170,6 @@ public class EnterHandler extends BaseEnterHandler { ); action.setForceIndent(forceIndent); action.run(); - documentManager.commitDocument(document); for (EnterHandlerDelegate delegate : delegates) { if (delegate.postProcessEnter(file, editor, dataContext) == EnterHandlerDelegate.Result.Stop) { break; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java index 253e34455365..5effbe204652 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/enter/EnterHandlerDelegate.java @@ -16,10 +16,10 @@ package com.intellij.codeInsight.editorActions.enter; -import com.intellij.openapi.extensions.ExtensionPointName; -import com.intellij.openapi.editor.actionSystem.EditorActionHandler; -import com.intellij.openapi.editor.Editor; import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.actionSystem.EditorActionHandler; +import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.util.Ref; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; @@ -39,5 +39,22 @@ public interface EnterHandlerDelegate { @NotNull final Ref caretAdvance, @NotNull final DataContext dataContext, @Nullable final EditorActionHandler originalHandler); + /** + * Called at the end of Enter handling after line feed insertion and indentation adjustment. + *

+ * Important Note: A document associated with the editor has modifications which are not reflected yet in the PSI file. If any + * operations with PSI are needed including a search for PSI elements, the document must be committed first to update the PSI. + * For example: + *

+   *   PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument);
+   * 
+ * + * @param file The PSI file associated with the document. + * @param editor The document. + * @param dataContext The data context passed to the Enter handler. + * @return One of {@link Result} values. + * @see DataContext + * @see com.intellij.psi.PsiDocumentManager + */ Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext); } diff --git a/python/src/com/jetbrains/python/editor/PythonEnterHandler.java b/python/src/com/jetbrains/python/editor/PythonEnterHandler.java index be74c2f12ea4..0656177f2fe9 100644 --- a/python/src/com/jetbrains/python/editor/PythonEnterHandler.java +++ b/python/src/com/jetbrains/python/editor/PythonEnterHandler.java @@ -369,6 +369,7 @@ public class PythonEnterHandler extends EnterHandlerDelegateAdapter { private static void addGoogleDocStringSectionIndent(@NotNull PsiFile file, @NotNull Editor editor, int offset) { final Document document = editor.getDocument(); + PsiDocumentManager.getInstance(file.getProject()).commitDocument(document); final PsiElement element = file.findElementAt(offset); if (element != null) { // Insert additional indentation after section header in Google code style docstrings