Search in sources :

Example 1 with DartUriElement

use of com.jetbrains.lang.dart.psi.DartUriElement 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)

Example 2 with DartUriElement

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

the class DartServerCompletionContributor method getPrefixIfCompletingUri.

@Nullable
private static String getPrefixIfCompletingUri(@NotNull final CompletionParameters parameters) {
    final PsiElement psiElement = parameters.getOriginalPosition();
    final PsiElement parent = psiElement != null ? psiElement.getParent() : null;
    final PsiElement parentParent = parent instanceof DartStringLiteralExpression ? parent.getParent() : null;
    if (parentParent instanceof DartUriElement) {
        final int uriStringOffset = ((DartUriElement) parentParent).getUriStringAndItsRange().second.getStartOffset();
        if (parameters.getOffset() >= parentParent.getTextRange().getStartOffset() + uriStringOffset) {
            return parentParent.getText().substring(uriStringOffset, parameters.getOffset() - parentParent.getTextRange().getStartOffset());
        }
    }
    return null;
}
Also used : DartUriElement(com.jetbrains.lang.dart.psi.DartUriElement) DartStringLiteralExpression(com.jetbrains.lang.dart.psi.DartStringLiteralExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 DartStringLiteralExpression (com.jetbrains.lang.dart.psi.DartStringLiteralExpression)2 DartUriElement (com.jetbrains.lang.dart.psi.DartUriElement)2 TextRange (com.intellij.openapi.util.TextRange)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 Nullable (org.jetbrains.annotations.Nullable)1