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());
}
}
}
Aggregations