IDEA-128832 Code completion works incorrectly in injected fragment with multiple carets

This commit is contained in:
Dmitry Batrak
2014-10-07 15:18:55 +04:00
parent ff828b58ec
commit 22409f8a83
2 changed files with 22 additions and 0 deletions
@@ -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();
@@ -160,6 +160,9 @@ public class InjectedLanguageUtil {
return true;
}
/**
* Invocation of this method on uncommitted <code>file</code> 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 <code>file</code> 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 <code>file</code> 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 <code>file</code> 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 <code>host</code> 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 <code>file</code> 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 <code>hostFile</code> 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();