Search in sources :

Example 1 with DartNewExpression

use of com.jetbrains.lang.dart.psi.DartNewExpression in project intellij-plugins by JetBrains.

the class DartServerCompletionContributor method beforeCompletion.

@Override
public void beforeCompletion(@NotNull final CompletionInitializationContext context) {
    final PsiElement psiElement = context.getFile().findElementAt(context.getStartOffset());
    final PsiElement parent = psiElement != null ? psiElement.getParent() : null;
    if (parent instanceof DartStringLiteralExpression) {
        final PsiElement parentParent = parent.getParent();
        if (parentParent instanceof DartUriElement) {
            final Pair<String, TextRange> uriAndRange = ((DartUriElement) parentParent).getUriStringAndItsRange();
            context.setReplacementOffset(parentParent.getTextRange().getStartOffset() + uriAndRange.second.getEndOffset());
        } else {
            // If replacement context is not set explicitly then com.intellij.codeInsight.completion.CompletionProgressIndicator#duringCompletion
            // implementation looks for the reference at caret and on Tab replaces the whole reference.
            // angular_analyzer_plugin provides angular-specific completion inside Dart string literals. Without the following hack Tab replaces
            // too much useful text. This hack is not ideal though as it may leave a piece of tail not replaced.
            // TODO: use replacementLength received from the server
            context.setReplacementOffset(context.getReplacementOffset());
        }
    } else {
        PsiReference reference = context.getFile().findReferenceAt(context.getStartOffset());
        if (reference instanceof PsiMultiReference && ((PsiMultiReference) reference).getReferences().length > 0) {
            // to ensure that references are sorted by range
            reference.getRangeInElement();
            reference = ((PsiMultiReference) reference).getReferences()[0];
        }
        if (reference instanceof DartNewExpression) {
            // historically DartNewExpression is a reference; it can appear here only in situation like new Foo(o.<caret>);
            // without the following hack closing paren is replaced on Tab. We won't get here if at least one symbol after dot typed.
            context.setReplacementOffset(context.getStartOffset());
        }
    }
}
Also used : DartUriElement(com.jetbrains.lang.dart.psi.DartUriElement) PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) TextRange(com.intellij.openapi.util.TextRange) DartStringLiteralExpression(com.jetbrains.lang.dart.psi.DartStringLiteralExpression) PsiElement(com.intellij.psi.PsiElement) DartNewExpression(com.jetbrains.lang.dart.psi.DartNewExpression)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)1 DartNewExpression (com.jetbrains.lang.dart.psi.DartNewExpression)1 DartStringLiteralExpression (com.jetbrains.lang.dart.psi.DartStringLiteralExpression)1 DartUriElement (com.jetbrains.lang.dart.psi.DartUriElement)1