Search in sources :

Example 86 with FileType

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

the class FileReferenceCompletionImpl method getFileReferenceCompletionVariants.

@NotNull
@Override
public Object[] getFileReferenceCompletionVariants(final FileReference reference) {
    final String s = reference.getText();
    if (s != null && s.equals("/")) {
        return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
    final CommonProcessors.CollectUniquesProcessor<PsiFileSystemItem> collector = new CommonProcessors.CollectUniquesProcessor<>();
    final PsiElementProcessor<PsiFileSystemItem> processor = new PsiElementProcessor<PsiFileSystemItem>() {

        @Override
        public boolean execute(@NotNull PsiFileSystemItem fileSystemItem) {
            return new FilteringProcessor<>(reference.getFileReferenceSet().getReferenceCompletionFilter(), collector).process(FileReference.getOriginalFile(fileSystemItem));
        }
    };
    List<Object> additionalItems = ContainerUtil.newArrayList();
    for (PsiFileSystemItem context : reference.getContexts()) {
        for (final PsiElement child : context.getChildren()) {
            if (child instanceof PsiFileSystemItem) {
                processor.execute((PsiFileSystemItem) child);
            }
        }
        if (context instanceof FileReferenceResolver) {
            additionalItems.addAll(((FileReferenceResolver) context).getVariants(reference));
        }
    }
    final FileType[] types = reference.getFileReferenceSet().getSuitableFileTypes();
    final THashSet<PsiElement> set = new THashSet<>(collector.getResults(), VARIANTS_HASHING_STRATEGY);
    final PsiElement[] candidates = PsiUtilCore.toPsiElementArray(set);
    final Object[] variants = new Object[candidates.length + additionalItems.size()];
    for (int i = 0; i < candidates.length; i++) {
        PsiElement candidate = candidates[i];
        Object item = reference.createLookupItem(candidate);
        if (item == null) {
            item = FileInfoManager.getFileLookupItem(candidate);
        }
        if (candidate instanceof PsiFile && item instanceof LookupElement && types.length > 0 && ArrayUtil.contains(((PsiFile) candidate).getFileType(), types)) {
            item = PrioritizedLookupElement.withPriority((LookupElement) item, Double.MAX_VALUE);
        }
        variants[i] = item;
    }
    for (int i = 0; i < additionalItems.size(); i++) {
        variants[i + candidates.length] = additionalItems.get(i);
    }
    if (!reference.getFileReferenceSet().isUrlEncoded()) {
        return variants;
    }
    List<Object> encodedVariants = new ArrayList<>(variants.length + additionalItems.size());
    for (int i = 0; i < candidates.length; i++) {
        final PsiElement element = candidates[i];
        if (element instanceof PsiNamedElement) {
            final PsiNamedElement psiElement = (PsiNamedElement) element;
            String name = psiElement.getName();
            final String encoded = reference.encode(name, psiElement);
            if (encoded == null)
                continue;
            if (!encoded.equals(name)) {
                final Icon icon = psiElement.getIcon(Iconable.ICON_FLAG_READ_STATUS | Iconable.ICON_FLAG_VISIBILITY);
                LookupElementBuilder item = FileInfoManager.getFileLookupItem(candidates[i], encoded, icon);
                encodedVariants.add(item.withTailText(" (" + name + ")"));
            } else {
                encodedVariants.add(variants[i]);
            }
        }
    }
    encodedVariants.addAll(additionalItems);
    return ArrayUtil.toObjectArray(encodedVariants);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiFile(com.intellij.psi.PsiFile) CommonProcessors(com.intellij.util.CommonProcessors) PsiElement(com.intellij.psi.PsiElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) THashSet(gnu.trove.THashSet) FileType(com.intellij.openapi.fileTypes.FileType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with FileType

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

the class PsiCommentManipulator method handleContentChange.

@Override
public PsiComment handleContentChange(@NotNull PsiComment psiComment, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    String oldText = psiComment.getText();
    String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());
    FileType type = psiComment.getContainingFile().getFileType();
    PsiFile fromText = PsiFileFactory.getInstance(psiComment.getProject()).createFileFromText("__." + type.getDefaultExtension(), type, newText);
    PsiComment newElement = PsiTreeUtil.getParentOfType(fromText.findElementAt(0), psiComment.getClass(), false);
    assert newElement != null : type + " " + type.getDefaultExtension() + " " + newText;
    return (PsiComment) psiComment.replace(newElement);
}
Also used : PsiComment(com.intellij.psi.PsiComment) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile)

Example 88 with FileType

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

the class StubVersionMap method registerStamp.

private void registerStamp(FileType fileTypeByName, long stamp) {
    fileTypeToVersion.put(fileTypeByName, stamp);
    FileType previousType = versionToFileType.put(stamp, fileTypeByName);
    if (previousType != null) {
        assert false;
    }
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType)

Example 89 with FileType

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

the class IndexConfiguration method registerIndex.

<K, V> void registerIndex(ID<K, V> name, @NotNull UpdatableIndex<K, V, FileContent> index, FileBasedIndex.InputFilter inputFilter, int version, @Nullable Collection<FileType> associatedFileTypes) {
    assert !myFreezed;
    synchronized (myIndices) {
        myIndexIdToVersionMap.put(name, version);
        if (associatedFileTypes != null && !associatedFileTypes.isEmpty()) {
            for (FileType fileType : associatedFileTypes) {
                List<ID<?, ?>> ids = myFileType2IndicesWithFileTypeInfoMap.get(fileType);
                if (ids == null)
                    myFileType2IndicesWithFileTypeInfoMap.put(fileType, ids = new ArrayList<>(5));
                ids.add(name);
            }
        } else {
            myIndicesWithoutFileTypeInfo.add(name);
        }
        Pair<UpdatableIndex<?, ?, FileContent>, FileBasedIndex.InputFilter> old = myIndices.put(name, new Pair<>(index, inputFilter));
        if (old != null) {
            throw new IllegalStateException("Index " + old.first + " already registered for the name '" + name + "'");
        }
    }
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType)

Example 90 with FileType

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

the class LanguageTextField method createDocument.

public static Document createDocument(String value, @Nullable Language language, Project project, @NotNull SimpleDocumentCreator documentCreator) {
    if (language != null) {
        final PsiFileFactory factory = PsiFileFactory.getInstance(project);
        final FileType fileType = language.getAssociatedFileType();
        assert fileType != null;
        final long stamp = LocalTimeCounter.currentTime();
        final PsiFile psiFile = factory.createFileFromText("Dummy." + fileType.getDefaultExtension(), fileType, value, stamp, true, false);
        documentCreator.customizePsiFile(psiFile);
        final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
        assert document != null;
        return document;
    } else {
        return EditorFactory.getInstance().createDocument(value);
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document)

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