From 22409f8a835e13ae1281deb01e61ca94076cae6c Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Tue, 7 Oct 2014 15:17:19 +0400 Subject: [PATCH] IDEA-128832 Code completion works incorrectly in injected fragment with multiple carets --- .../completion/CodeCompletionHandlerBase.java | 1 + .../tree/injected/InjectedLanguageUtil.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java index 09e627a1dc24..507578a566f2 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java @@ -653,6 +653,7 @@ public class CodeCompletionHandlerBase { hostEditor.getCaretModel().runForEachCaret(new CaretAction() { @Override public void perform(Caret caret) { + PsiDocumentManager.getInstance(hostFile.getProject()).commitDocument(hostEditor.getDocument()); PsiFile targetFile = InjectedLanguageUtil.findInjectedPsiNoCommit(hostFile, caret.getOffset()); Editor targetEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(hostEditor, targetFile); int targetCaretOffset = targetEditor.getCaretModel().getOffset(); diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/tree/injected/InjectedLanguageUtil.java b/platform/lang-impl/src/com/intellij/psi/impl/source/tree/injected/InjectedLanguageUtil.java index 647ace567d5d..5ed96453c85e 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/tree/injected/InjectedLanguageUtil.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/tree/injected/InjectedLanguageUtil.java @@ -160,6 +160,9 @@ public class InjectedLanguageUtil { return true; } + /** + * Invocation of this method on uncommitted file can lead to unexpected results, including throwing an exception! + */ public static Editor getEditorForInjectedLanguageNoCommit(@Nullable Editor editor, @Nullable PsiFile file) { if (editor == null || file == null || editor instanceof EditorWindow) return editor; @@ -167,6 +170,9 @@ public class InjectedLanguageUtil { return getEditorForInjectedLanguageNoCommit(editor, file, offset); } + /** + * Invocation of this method on uncommitted file can lead to unexpected results, including throwing an exception! + */ public static Editor getEditorForInjectedLanguageNoCommit(@Nullable Editor editor, @Nullable Caret caret, @Nullable PsiFile file) { if (editor == null || file == null || editor instanceof EditorWindow || caret == null) return editor; @@ -174,6 +180,9 @@ public class InjectedLanguageUtil { return getInjectedEditorForInjectedFile(editor, caret, injectedFile); } + /** + * Invocation of this method on uncommitted file can lead to unexpected results, including throwing an exception! + */ public static Caret getCaretForInjectedLanguageNoCommit(@Nullable Caret caret, @Nullable PsiFile file) { if (caret == null || file == null || caret instanceof InjectedCaret) return caret; @@ -216,6 +225,9 @@ public class InjectedLanguageUtil { return null; } + /** + * Invocation of this method on uncommitted file can lead to unexpected results, including throwing an exception! + */ public static Editor getEditorForInjectedLanguageNoCommit(@Nullable Editor editor, @Nullable PsiFile file, final int offset) { if (editor == null || file == null || editor instanceof EditorWindow) return editor; PsiFile injectedFile = findInjectedPsiNoCommit(file, offset); @@ -251,12 +263,18 @@ public class InjectedLanguageUtil { return EditorWindowImpl.create(documentWindow, (EditorImpl)hostEditor, injectedFile); } + /** + * Invocation of this method on uncommitted host can lead to unexpected results, including throwing an exception! + */ @Nullable public static PsiFile findInjectedPsiNoCommit(@NotNull PsiFile host, int offset) { PsiElement injected = findInjectedElementNoCommit(host, offset); return injected == null ? null : injected.getContainingFile(); } + /** + * Invocation of this method on uncommitted file can lead to unexpected results, including throwing an exception! + */ // consider injected elements public static PsiElement findElementAtNoCommit(@NotNull PsiFile file, int offset) { FileViewProvider viewProvider = file.getViewProvider(); @@ -342,6 +360,9 @@ public class InjectedLanguageUtil { return registrar; } + /** + * Invocation of this method on uncommitted hostFile can lead to unexpected results, including throwing an exception! + */ public static PsiElement findInjectedElementNoCommit(@NotNull PsiFile hostFile, final int offset) { if (hostFile instanceof PsiCompiledElement) return null; Project project = hostFile.getProject();