From 8ef855248fb24b97cc8bd708a3a3e24d3f3dcc1d Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 7 Mar 2013 21:25:28 +0100 Subject: [PATCH] CodeCompletionHandlerBase: extract some methods --- .../completion/CodeCompletionHandlerBase.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) 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 3cadaf0a5f0c..b2a683c691bf 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CodeCompletionHandlerBase.java @@ -505,14 +505,9 @@ public class CodeCompletionHandlerBase { } }); final PsiFile hostFile = InjectedLanguageUtil.getTopLevelFile(fileCopy[0]); - final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(hostFile.getProject()); final Editor hostEditor = InjectedLanguageUtil.getTopLevelEditor(initContext.getEditor()); - final OffsetMap hostMap = new OffsetMap(hostEditor.getDocument()); - final OffsetMap original = initContext.getOffsetMap(); - for (final OffsetKey key : original.getAllOffsets()) { - hostMap.addOffset(key, injectedLanguageManager.injectedToHost(fileCopy[0], original.getOffset(key))); - } + final OffsetMap hostMap = translateOffsetMapToHost(initContext, fileCopy[0], hostFile, hostEditor); final Document document = fileCopy[0].getViewProvider().getDocument(); assert document != null : "no document"; @@ -563,6 +558,19 @@ public class CodeCompletionHandlerBase { } } + private OffsetMap translateOffsetMapToHost(CompletionInitializationContext initContext, + PsiFile context, + PsiFile hostFile, + Editor hostEditor) { + final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(hostFile.getProject()); + final OffsetMap hostMap = new OffsetMap(hostEditor.getDocument()); + final OffsetMap original = initContext.getOffsetMap(); + for (final OffsetKey key : original.getAllOffsets()) { + hostMap.addOffset(key, injectedLanguageManager.injectedToHost(context, original.getOffset(key))); + } + return hostMap; + } + private static CompletionContext createCompletionContext(PsiFile hostFile, int hostStartOffset, Editor hostEditor, @@ -583,11 +591,8 @@ public class CodeCompletionHandlerBase { EditorWindow injectedEditor = (EditorWindow)InjectedLanguageUtil .getEditorForInjectedLanguageNoCommit(hostEditor, hostFile, hostStartOffset); assert injected == injectedEditor.getInjectedFile(); - final OffsetMap map = new OffsetMap(injectedEditor.getDocument()); - for (final OffsetKey key : hostMap.getAllOffsets()) { - map.addOffset(key, injectedEditor.logicalPositionToOffset(injectedEditor.hostToInjected(hostEditor.offsetToLogicalPosition(hostMap.getOffset(key))))); - } - context = new CompletionContext(hostFile.getProject(), injectedEditor, injected, map); + context = new CompletionContext(hostFile.getProject(), injectedEditor, injected, + translateOffsetMapToInjected(hostEditor, hostMap, injectedEditor)); assert hostStartOffset == injectedLanguageManager.injectedToHost(injected, context.getStartOffset()) : "inconsistent injected offset translation"; } else { context = new CompletionContext(hostFile.getProject(), hostEditor, hostFile, hostMap); @@ -599,6 +604,14 @@ public class CodeCompletionHandlerBase { return context; } + private static OffsetMap translateOffsetMapToInjected(Editor hostEditor, OffsetMap hostMap, EditorWindow injectedEditor) { + final OffsetMap map = new OffsetMap(injectedEditor.getDocument()); + for (final OffsetKey key : hostMap.getAllOffsets()) { + map.addOffset(key, injectedEditor.logicalPositionToOffset(injectedEditor.hostToInjected(hostEditor.offsetToLogicalPosition(hostMap.getOffset(key))))); + } + return map; + } + private boolean isAutocompleteCommonPrefixOnInvocation() { return invokedExplicitly && CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX; }