Search in sources :

Example 41 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-plugins by JetBrains.

the class DartServerCompletionContributor method getPrefixUsingServerData.

@Nullable
private static String getPrefixUsingServerData(@NotNull final CompletionParameters parameters, final int replacementOffset) {
    PsiElement element = parameters.getOriginalPosition();
    if (element == null)
        return null;
    final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(element.getProject());
    final PsiFile injectedContext = parameters.getOriginalFile();
    final int completionOffset = manager.injectedToHost(injectedContext, parameters.getOffset());
    final TextRange range = manager.injectedToHost(injectedContext, element.getTextRange());
    // shouldn't happen
    if (completionOffset < range.getStartOffset() || completionOffset > range.getEndOffset())
        return null;
    // shouldn't happen
    if (replacementOffset > completionOffset)
        return null;
    while (element != null) {
        final int elementStartOffset = manager.injectedToHost(injectedContext, element.getTextRange().getStartOffset());
        if (elementStartOffset <= replacementOffset) {
            // that's good, we can use this element to calculate prefix
            break;
        }
        element = element.getParent();
    }
    if (element != null) {
        final int startOffset = manager.injectedToHost(injectedContext, element.getTextRange().getStartOffset());
        return element.getText().substring(replacementOffset - startOffset, completionOffset - startOffset);
    }
    return null;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)41 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)12 PsiFile (com.intellij.psi.PsiFile)11 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)10 Pair (com.intellij.openapi.util.Pair)7 Nullable (org.jetbrains.annotations.Nullable)7 Document (com.intellij.openapi.editor.Document)5 Project (com.intellij.openapi.project.Project)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)4 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 HighlightInfoHolder (com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder)1 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1