Search in sources :

Example 51 with FileType

use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.

the class TestDataNavigationHandler method showNavigationPopup.

private static void showNavigationPopup(final Project project, final List<String> fileNames, final RelativePoint point) {
    List<String> listPaths = new ArrayList<>(fileNames);
    final String CREATE_MISSING_OPTION = "Create Missing Files";
    if (fileNames.size() == 2) {
        VirtualFile file1 = LocalFileSystem.getInstance().refreshAndFindFileByPath(fileNames.get(0));
        VirtualFile file2 = LocalFileSystem.getInstance().refreshAndFindFileByPath(fileNames.get(1));
        if (file1 == null || file2 == null) {
            listPaths.add(CREATE_MISSING_OPTION);
        }
    }
    final JList list = new JBList(ArrayUtil.toStringArray(listPaths));
    list.setCellRenderer(new ColoredListCellRenderer() {

        @Override
        protected void customizeCellRenderer(@NotNull JList list, Object value, int index, boolean selected, boolean hasFocus) {
            String path = (String) value;
            String fileName = PathUtil.getFileName(path);
            if (!fileName.equals(CREATE_MISSING_OPTION)) {
                final FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName);
                setIcon(fileType.getIcon());
            }
            append(String.format("%s (%s)", fileName, PathUtil.getParentPath(path)));
        }
    });
    PopupChooserBuilder builder = new PopupChooserBuilder(list);
    builder.setItemChoosenCallback(() -> {
        final int[] indices = list.getSelectedIndices();
        if (ArrayUtil.indexOf(indices, fileNames.size()) >= 0) {
            createMissingFiles(project, fileNames);
        } else {
            for (int index : indices) {
                openFileByIndex(project, fileNames, index);
            }
        }
    }).createPopup().show(point);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) RelativePoint(com.intellij.ui.awt.RelativePoint) FileType(com.intellij.openapi.fileTypes.FileType) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder)

Example 52 with FileType

use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.

the class NewGroovyClassAction method buildDialog.

@Override
protected void buildDialog(Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {
    builder.setTitle(GroovyBundle.message("newclass.dlg.title")).addKind("Class", JetgroovyIcons.Groovy.Class, GroovyTemplates.GROOVY_CLASS).addKind("Interface", JetgroovyIcons.Groovy.Interface, GroovyTemplates.GROOVY_INTERFACE);
    if (GroovyConfigUtils.getInstance().isVersionAtLeast(directory, GroovyConfigUtils.GROOVY2_3, true)) {
        builder.addKind("Trait", JetgroovyIcons.Groovy.Trait, GroovyTemplates.GROOVY_TRAIT);
    }
    builder.addKind("Enum", JetgroovyIcons.Groovy.Enum, GroovyTemplates.GROOVY_ENUM).addKind("Annotation", JetgroovyIcons.Groovy.AnnotationType, GroovyTemplates.GROOVY_ANNOTATION);
    for (FileTemplate template : FileTemplateManager.getInstance(project).getAllTemplates()) {
        FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension());
        if (fileType.equals(GroovyFileType.GROOVY_FILE_TYPE) && JavaDirectoryService.getInstance().getPackage(directory) != null) {
            builder.addKind(template.getName(), JetgroovyIcons.Groovy.Class, template.getName());
        }
    }
}
Also used : GroovyFileType(org.jetbrains.plugins.groovy.GroovyFileType) FileType(com.intellij.openapi.fileTypes.FileType) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 53 with FileType

use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.

the class BeforeAfterActionMetaData method retrieveURLs.

@NotNull
private static TextDescriptor[] retrieveURLs(@NotNull URL descriptionDirectory, @NotNull String prefix, @NotNull String suffix) throws MalformedURLException {
    List<TextDescriptor> urls = new ArrayList<>();
    final FileType[] fileTypes = FileTypeManager.getInstance().getRegisteredFileTypes();
    for (FileType fileType : fileTypes) {
        final String[] extensions = FileTypeManager.getInstance().getAssociatedExtensions(fileType);
        for (String extension : extensions) {
            for (int i = 0; ; i++) {
                URL url = new URL(descriptionDirectory.toExternalForm() + "/" + prefix + "." + extension + (i == 0 ? "" : Integer.toString(i)) + suffix);
                try {
                    InputStream inputStream = url.openStream();
                    inputStream.close();
                    urls.add(new ResourceTextDescriptor(url));
                } catch (IOException ioe) {
                    break;
                }
            }
        }
    }
    if (urls.isEmpty()) {
        String[] children;
        Exception cause = null;
        try {
            URI uri = descriptionDirectory.toURI();
            children = uri.isOpaque() ? null : ObjectUtils.notNull(new File(uri).list(), ArrayUtil.EMPTY_STRING_ARRAY);
        } catch (URISyntaxException | IllegalArgumentException e) {
            cause = e;
            children = null;
        }
        LOG.error("URLs not found for available file types and prefix: '" + prefix + "', suffix: '" + suffix + "';" + " in directory: '" + descriptionDirectory + "'" + (children == null ? "" : "; directory contents: " + Arrays.asList(children)), cause);
        return EMPTY_EXAMPLE;
    }
    return urls.toArray(new TextDescriptor[urls.size()]);
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileType(com.intellij.openapi.fileTypes.FileType) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with FileType

use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.

the class IntentionDescriptionPanel method showUsages.

private static void showUsages(final JPanel panel, final TitledSeparator separator, final List<IntentionUsagePanel> usagePanels, @Nullable final TextDescriptor[] exampleUsages) throws IOException {
    GridBagConstraints gb = null;
    boolean reuse = exampleUsages != null && panel.getComponents().length == exampleUsages.length;
    if (!reuse) {
        disposeUsagePanels(usagePanels);
        panel.setLayout(new GridBagLayout());
        panel.removeAll();
        gb = new GridBagConstraints();
        gb.anchor = GridBagConstraints.NORTHWEST;
        gb.fill = GridBagConstraints.BOTH;
        gb.gridheight = GridBagConstraints.REMAINDER;
        gb.gridwidth = 1;
        gb.gridx = 0;
        gb.gridy = 0;
        gb.insets = new Insets(0, 0, 0, 0);
        gb.ipadx = 5;
        gb.ipady = 5;
        gb.weightx = 1;
        gb.weighty = 1;
    }
    if (exampleUsages != null) {
        for (int i = 0; i < exampleUsages.length; i++) {
            final TextDescriptor exampleUsage = exampleUsages[i];
            final String name = exampleUsage.getFileName();
            final FileTypeManagerEx fileTypeManager = FileTypeManagerEx.getInstanceEx();
            final String extension = fileTypeManager.getExtension(name);
            final FileType fileType = fileTypeManager.getFileTypeByExtension(extension);
            IntentionUsagePanel usagePanel;
            if (reuse) {
                usagePanel = (IntentionUsagePanel) panel.getComponent(i);
            } else {
                usagePanel = new IntentionUsagePanel();
                usagePanels.add(usagePanel);
            }
            usagePanel.reset(exampleUsage.getText(), fileType);
            if (!reuse) {
                if (i == exampleUsages.length) {
                    gb.gridwidth = GridBagConstraints.REMAINDER;
                }
                panel.add(usagePanel, gb);
                gb.gridx++;
            }
        }
    }
    panel.revalidate();
    panel.repaint();
}
Also used : FileTypeManagerEx(com.intellij.openapi.fileTypes.ex.FileTypeManagerEx) FileType(com.intellij.openapi.fileTypes.FileType) HintHint(com.intellij.ui.HintHint)

Example 55 with FileType

use of com.intellij.openapi.fileTypes.FileType in project intellij-community by JetBrains.

the class FrameworkDetectionIndex method getIndexer.

@NotNull
@Override
public DataIndexer<Integer, Void, FileContent> getIndexer() {
    final MultiMap<FileType, Pair<ElementPattern<FileContent>, Integer>> detectors = new MultiMap<>();
    FrameworkDetectorRegistry registry = FrameworkDetectorRegistry.getInstance();
    for (FrameworkDetector detector : FrameworkDetector.EP_NAME.getExtensions()) {
        detectors.putValue(detector.getFileType(), Pair.create(detector.createSuitableFilePattern(), registry.getDetectorId(detector)));
    }
    return new DataIndexer<Integer, Void, FileContent>() {

        @NotNull
        @Override
        public Map<Integer, Void> map(@NotNull FileContent inputData) {
            final FileType fileType = inputData.getFileType();
            if (!detectors.containsKey(fileType)) {
                return Collections.emptyMap();
            }
            Map<Integer, Void> result = null;
            for (Pair<ElementPattern<FileContent>, Integer> pair : detectors.get(fileType)) {
                if (pair.getFirst().accepts(inputData)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(inputData.getFile() + " accepted by detector " + pair.getSecond());
                    }
                    if (result == null) {
                        result = new HashMap<>();
                    }
                    myDispatcher.getMulticaster().fileUpdated(inputData.getFile(), pair.getSecond());
                    result.put(pair.getSecond(), null);
                }
            }
            return result != null ? result : Collections.<Integer, Void>emptyMap();
        }
    };
}
Also used : FrameworkDetector(com.intellij.framework.detection.FrameworkDetector) ElementPattern(com.intellij.patterns.ElementPattern) NotNull(org.jetbrains.annotations.NotNull) MultiMap(com.intellij.util.containers.MultiMap) FileType(com.intellij.openapi.fileTypes.FileType) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FileType (com.intellij.openapi.fileTypes.FileType)198 VirtualFile (com.intellij.openapi.vfs.VirtualFile)60 NotNull (org.jetbrains.annotations.NotNull)38 Language (com.intellij.lang.Language)31 PsiFile (com.intellij.psi.PsiFile)31 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)30 Nullable (org.jetbrains.annotations.Nullable)28 Project (com.intellij.openapi.project.Project)27 Document (com.intellij.openapi.editor.Document)20 IncorrectOperationException (com.intellij.util.IncorrectOperationException)13 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)11 Editor (com.intellij.openapi.editor.Editor)10 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)10 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)10 PlainTextFileType (com.intellij.openapi.fileTypes.PlainTextFileType)9 UnknownFileType (com.intellij.openapi.fileTypes.UnknownFileType)9 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)8 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)8 PsiElement (com.intellij.psi.PsiElement)8