use of com.intellij.lang.ExpressionTypeProvider in project intellij-community by JetBrains.
the class ShowExpressionTypeHandler method invoke.
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
ApplicationManager.getApplication().assertIsDispatchThread();
Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
final Set<ExpressionTypeProvider> handlers = getHandlers(project, language, file.getViewProvider().getBaseLanguage());
if (handlers.isEmpty())
return;
boolean exactRange = false;
TextRange range = EditorUtil.getSelectionInAnyMode(editor);
final Map<PsiElement, ExpressionTypeProvider> map = ContainerUtil.newLinkedHashMap();
int offset = TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset());
for (int i = 0; i < 3 && map.isEmpty() && offset > i; i++) {
PsiElement elementAt = file.findElementAt(offset - i);
if (elementAt == null)
continue;
for (ExpressionTypeProvider handler : handlers) {
for (PsiElement element : ((ExpressionTypeProvider<? extends PsiElement>) handler).getExpressionsAt(elementAt)) {
TextRange r = element.getTextRange();
if (exactRange && !r.equals(range) || !r.contains(range))
continue;
if (!exactRange)
exactRange = r.equals(range);
map.put(element, handler);
}
}
}
Pass<PsiElement> callback = new Pass<PsiElement>() {
@Override
public void pass(@NotNull PsiElement expression) {
//noinspection unchecked
ExpressionTypeProvider<PsiElement> provider = ObjectUtils.assertNotNull(map.get(expression));
final String informationHint = provider.getInformationHint(expression);
TextRange range = expression.getTextRange();
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
ApplicationManager.getApplication().invokeLater(() -> HintManager.getInstance().showInformationHint(editor, informationHint));
}
};
if (map.isEmpty()) {
ApplicationManager.getApplication().invokeLater(() -> {
String errorHint = ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(handlers)).getErrorHint();
HintManager.getInstance().showErrorHint(editor, errorHint);
});
} else if (map.size() == 1) {
callback.pass(ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(map.keySet())));
} else {
IntroduceTargetChooser.showChooser(editor, ContainerUtil.newArrayList(map.keySet()), callback, expression -> expression.getText());
}
}
Aggregations