Search in sources :

Example 1 with ExpressionTypeProvider

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());
    }
}
Also used : Language(com.intellij.lang.Language) DumbService(com.intellij.openapi.project.DumbService) JBIterable(com.intellij.util.containers.JBIterable) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Set(java.util.Set) TextRange(com.intellij.openapi.util.TextRange) LanguageExpressionTypes(com.intellij.lang.LanguageExpressionTypes) ContainerUtil(com.intellij.util.containers.ContainerUtil) Editor(com.intellij.openapi.editor.Editor) ExpressionTypeProvider(com.intellij.lang.ExpressionTypeProvider) Pass(com.intellij.openapi.util.Pass) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) Function(com.intellij.util.Function) Map(java.util.Map) CodeInsightActionHandler(com.intellij.codeInsight.CodeInsightActionHandler) PsiElement(com.intellij.psi.PsiElement) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) ObjectUtils(com.intellij.util.ObjectUtils) IntroduceTargetChooser(com.intellij.refactoring.IntroduceTargetChooser) NotNull(org.jetbrains.annotations.NotNull) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ExpressionTypeProvider(com.intellij.lang.ExpressionTypeProvider) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) Language(com.intellij.lang.Language) Pass(com.intellij.openapi.util.Pass) PsiElement(com.intellij.psi.PsiElement)

Aggregations

CodeInsightActionHandler (com.intellij.codeInsight.CodeInsightActionHandler)1 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)1 ExpressionTypeProvider (com.intellij.lang.ExpressionTypeProvider)1 Language (com.intellij.lang.Language)1 LanguageExpressionTypes (com.intellij.lang.LanguageExpressionTypes)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Editor (com.intellij.openapi.editor.Editor)1 EditorUtil (com.intellij.openapi.editor.ex.util.EditorUtil)1 DumbService (com.intellij.openapi.project.DumbService)1 Project (com.intellij.openapi.project.Project)1 Pass (com.intellij.openapi.util.Pass)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiUtilCore (com.intellij.psi.util.PsiUtilCore)1 IntroduceTargetChooser (com.intellij.refactoring.IntroduceTargetChooser)1 Function (com.intellij.util.Function)1 ObjectUtils (com.intellij.util.ObjectUtils)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1