Search in sources :

Example 76 with Language

use of com.intellij.lang.Language 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)

Example 77 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class OfflineDescriptorResolveResult method getElementsIntersectingRange.

@NotNull
private static PsiElement[] getElementsIntersectingRange(PsiFile file, final int startOffset, final int endOffset) {
    final FileViewProvider viewProvider = file.getViewProvider();
    final Set<PsiElement> result = new LinkedHashSet<>();
    for (Language language : viewProvider.getLanguages()) {
        final PsiFile psiRoot = viewProvider.getPsi(language);
        if (HighlightingLevelManager.getInstance(file.getProject()).shouldInspect(psiRoot)) {
            result.addAll(CollectHighlightsUtil.getElementsInRange(psiRoot, startOffset, endOffset, true));
        }
    }
    return PsiUtilCore.toPsiElementArray(result);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Language(com.intellij.lang.Language) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class GotoClassAction method gotoActionPerformed.

@Override
public void gotoActionPerformed(@NotNull AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null)
        return;
    FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.class");
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final GotoClassModel2 model = new GotoClassModel2(project);
    showNavigationPopup(e, model, new GotoActionCallback<Language>() {

        @Override
        protected ChooseByNameFilter<Language> createFilter(@NotNull ChooseByNamePopup popup) {
            return new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
        }

        @Override
        public void elementChosen(ChooseByNamePopup popup, Object element) {
            ApplicationManager.getApplication().runReadAction(() -> {
                if (element instanceof PsiElement && ((PsiElement) element).isValid()) {
                    PsiElement psiElement = getElement(((PsiElement) element), popup);
                    psiElement = psiElement.getNavigationElement();
                    VirtualFile file = PsiUtilCore.getVirtualFile(psiElement);
                    if (file != null && popup.getLinePosition() != -1) {
                        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
                        Navigatable n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
                        if (n.canNavigate()) {
                            n.navigate(true);
                            return;
                        }
                    }
                    if (file != null && popup.getMemberPattern() != null) {
                        NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
                        Navigatable member = findMember(popup.getMemberPattern(), psiElement, file);
                        if (member != null) {
                            member.navigate(true);
                        }
                    }
                    NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
                } else {
                    EditSourceUtil.navigate(((NavigationItem) element), true, popup.isOpenInCurrentWindowRequested());
                }
            });
        }
    }, IdeBundle.message("go.to.class.toolwindow.title"), true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Navigatable(com.intellij.pom.Navigatable) Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 79 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class LRUPopupBuilder method changeLanguageWithUndo.

private static void changeLanguageWithUndo(@NotNull Project project, @NotNull final Language t, @NotNull final VirtualFile[] sortedFiles, @NotNull final PerFileMappings<Language> mappings) throws UnexpectedUndoException {
    ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(sortedFiles);
    if (status.hasReadonlyFiles())
        return;
    final Set<VirtualFile> matchedExtensions = ContainerUtil.newLinkedHashSet();
    final Map<VirtualFile, Language> oldMapping = ContainerUtil.newHashMap();
    for (VirtualFile file : sortedFiles) {
        oldMapping.put(file, mappings.getMapping(file));
        if (ScratchUtil.hasMatchingExtension(project, file)) {
            matchedExtensions.add(file);
        }
    }
    BasicUndoableAction action = new BasicUndoableAction(sortedFiles) {

        @Override
        public void undo() throws UnexpectedUndoException {
            for (VirtualFile file : sortedFiles) {
                mappings.setMapping(file, oldMapping.get(file));
            }
        }

        @Override
        public void redo() throws UnexpectedUndoException {
            for (VirtualFile file : sortedFiles) {
                mappings.setMapping(file, t);
            }
        }
    };
    action.redo();
    UndoManager.getInstance(project).undoableActionPerformed(action);
    for (VirtualFile file : matchedExtensions) {
        try {
            ScratchUtil.updateFileExtension(project, file);
        } catch (IOException ignored) {
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Language(com.intellij.lang.Language) IOException(java.io.IOException) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction) ReadonlyStatusHandler(com.intellij.openapi.vfs.ReadonlyStatusHandler)

Example 80 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class RootType method substituteIcon.

@Nullable
public Icon substituteIcon(@NotNull Project project, @NotNull VirtualFile file) {
    Language language = substituteLanguage(project, file);
    FileType fileType = LanguageUtil.getLanguageFileType(language);
    if (fileType == null)
        fileType = ScratchUtil.getFileTypeFromName(file);
    return fileType != null ? fileType.getIcon() : null;
}
Also used : Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Language (com.intellij.lang.Language)292 NotNull (org.jetbrains.annotations.NotNull)58 Nullable (org.jetbrains.annotations.Nullable)49 PsiElement (com.intellij.psi.PsiElement)46 PsiFile (com.intellij.psi.PsiFile)45 FileType (com.intellij.openapi.fileTypes.FileType)31 Project (com.intellij.openapi.project.Project)31 TextRange (com.intellij.openapi.util.TextRange)25 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 FileViewProvider (com.intellij.psi.FileViewProvider)21 HbLanguage (com.dmarcotte.handlebars.HbLanguage)20 JavaLanguage (com.intellij.lang.java.JavaLanguage)16 Editor (com.intellij.openapi.editor.Editor)14 XMLLanguage (com.intellij.lang.xml.XMLLanguage)12 TemplateLanguageFileViewProvider (com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)11 List (java.util.List)11 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)9 JavascriptLanguage (com.intellij.lang.javascript.JavascriptLanguage)9 Document (com.intellij.openapi.editor.Document)9