Search in sources :

Example 1 with DartStringLiteralExpression

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

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

the class DartCharFilter method acceptChar.

@Nullable
@Override
public Result acceptChar(final char c, final int prefixLength, @NotNull final Lookup lookup) {
    if (!lookup.isCompletion())
        return null;
    final PsiElement element = lookup.getPsiElement();
    final PsiElement parent = element == null ? null : element.getParent();
    if (parent instanceof DartStringLiteralExpression) {
        return Result.ADD_TO_PREFIX;
    }
    return null;
}
Also used : DartStringLiteralExpression(com.jetbrains.lang.dart.psi.DartStringLiteralExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DartStringLiteralExpression

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

Example 4 with DartStringLiteralExpression

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

the class DartServerCompletionContributor method getPrefixForSpecialCases.

/**
   * Handles completion provided by angular_analyzer_plugin in HTML files and inside string literals;
   * our PSI doesn't allow top calculate prefix in such cases
   */
@Nullable
private static String getPrefixForSpecialCases(@NotNull final CompletionParameters parameters, final int replacementOffset) {
    final PsiElement psiElement = parameters.getOriginalPosition();
    if (psiElement == null)
        return null;
    final PsiElement parent = psiElement.getParent();
    final Language language = psiElement.getContainingFile().getLanguage();
    if (parent instanceof DartStringLiteralExpression || language.isKindOf(XMLLanguage.INSTANCE)) {
        return getPrefixUsingServerData(parameters, replacementOffset);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) DartLanguage(com.jetbrains.lang.dart.DartLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) DartStringLiteralExpression(com.jetbrains.lang.dart.psi.DartStringLiteralExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)4 DartStringLiteralExpression (com.jetbrains.lang.dart.psi.DartStringLiteralExpression)4 Nullable (org.jetbrains.annotations.Nullable)3 DartUriElement (com.jetbrains.lang.dart.psi.DartUriElement)2 Language (com.intellij.lang.Language)1 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)1 XMLLanguage (com.intellij.lang.xml.XMLLanguage)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)1 DartLanguage (com.jetbrains.lang.dart.DartLanguage)1 DartNewExpression (com.jetbrains.lang.dart.psi.DartNewExpression)1