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