Search in sources :

Example 11 with LanguageFileType

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

the class SubstitutedFileType method substituteFileType.

@NotNull
public static FileType substituteFileType(@NotNull VirtualFile file, @NotNull FileType fileType, Project project) {
    if (project == null) {
        return fileType;
    }
    if (fileType instanceof LanguageFileType) {
        final Language language = ((LanguageFileType) fileType).getLanguage();
        final Language substitutedLanguage = LanguageSubstitutors.INSTANCE.substituteLanguage(language, file, project);
        LanguageFileType substFileType = substitutedLanguage.getAssociatedFileType();
        if (!substitutedLanguage.equals(language) && substFileType != null) {
            return new SubstitutedFileType(fileType, substFileType, substitutedLanguage);
        }
    }
    return fileType;
}
Also used : LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) Language(com.intellij.lang.Language) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with LanguageFileType

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

the class MatcherImpl method matchByDownUp.

@NotNull
protected List<MatchResult> matchByDownUp(PsiElement element, final MatchOptions options) {
    final CollectingMatchResultSink sink = new CollectingMatchResultSink();
    final CompiledPattern compiledPattern = prepareMatching(sink, options);
    matchContext.setShouldRecursivelyMatch(false);
    PsiElement targetNode = compiledPattern.getTargetNode();
    PsiElement elementToStartMatching = null;
    if (targetNode == null) {
        targetNode = compiledPattern.getNodes().current();
        if (targetNode != null) {
            compiledPattern.getNodes().advance();
            assert !compiledPattern.getNodes().hasNext();
            compiledPattern.getNodes().rewind();
            element = element.getParent();
            if (element == null) {
                return Collections.emptyList();
            }
            while (element.getClass() != targetNode.getClass()) {
                element = element.getParent();
                if (element == null)
                    return Collections.emptyList();
            }
            elementToStartMatching = element;
        }
    } else {
        final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(element);
        if (profile == null)
            return Collections.emptyList();
        targetNode = profile.extendMatchedByDownUp(targetNode);
        MatchingHandler handler = null;
        while (element.getClass() == targetNode.getClass() || compiledPattern.isTypedVar(targetNode) && compiledPattern.getHandler(targetNode).canMatch(targetNode, element, matchContext)) {
            handler = compiledPattern.getHandler(targetNode);
            handler.setPinnedElement(element);
            elementToStartMatching = element;
            if (handler instanceof TopLevelMatchingHandler)
                break;
            element = element.getParent();
            targetNode = targetNode.getParent();
            if (options.isLooseMatching()) {
                element = profile.updateCurrentNode(element);
                targetNode = profile.updateCurrentNode(targetNode);
            }
        }
        if (!(handler instanceof TopLevelMatchingHandler))
            return Collections.emptyList();
    }
    assert targetNode != null : "Could not match down up when no target node";
    final LanguageFileType fileType = (LanguageFileType) matchContext.getOptions().getFileType();
    match(elementToStartMatching, fileType.getLanguage());
    matchContext.getSink().matchingFinished();
    return sink.getMatches();
}
Also used : CollectingMatchResultSink(com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) TopLevelMatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler) MatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler) TopLevelMatchingHandler(com.intellij.structuralsearch.impl.matcher.handlers.TopLevelMatchingHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with LanguageFileType

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

the class MatcherImplUtil method createSourceTreeFromText.

public static PsiElement[] createSourceTreeFromText(String text, PatternTreeContext context, FileType fileType, String extension, Project project, boolean physical) {
    if (fileType instanceof LanguageFileType) {
        Language language = ((LanguageFileType) fileType).getLanguage();
        StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
        if (profile != null) {
            return profile.createPatternTree(text, context, fileType, null, null, extension, project, physical);
        }
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) Language(com.intellij.lang.Language) StructuralSearchProfile(com.intellij.structuralsearch.StructuralSearchProfile)

Example 14 with LanguageFileType

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

the class ScratchFileActions method doCreateNewScratch.

static void doCreateNewScratch(@NotNull Project project, boolean buffer, @NotNull Language language, @NotNull String text) {
    FeatureUsageTracker.getInstance().triggerFeatureUsed("scratch");
    LanguageFileType fileType = language.getAssociatedFileType();
    String ext = buffer || fileType == null ? "" : fileType.getDefaultExtension();
    String fileName = buffer ? "buffer" + nextBufferIndex() : "scratch";
    ScratchFileService.Option option = buffer ? ScratchFileService.Option.create_if_missing : ScratchFileService.Option.create_new_always;
    VirtualFile f = ScratchRootType.getInstance().createScratchFile(project, PathUtil.makeFileName(fileName, ext), language, text, option);
    if (f != null) {
        FileEditorManager.getInstance(project).openFile(f, true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType)

Example 15 with LanguageFileType

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

the class TypedHandler method getFileType.

private static FileType getFileType(@NotNull PsiFile file, @NotNull Editor editor) {
    FileType fileType = file.getFileType();
    Language language = PsiUtilBase.getLanguageInEditor(editor, file.getProject());
    if (language != null && language != PlainTextLanguage.INSTANCE) {
        LanguageFileType associatedFileType = language.getAssociatedFileType();
        if (associatedFileType != null)
            fileType = associatedFileType;
    }
    return fileType;
}
Also used : LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) PlainTextLanguage(com.intellij.openapi.fileTypes.PlainTextLanguage) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType)

Aggregations

LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)33 Language (com.intellij.lang.Language)20 FileType (com.intellij.openapi.fileTypes.FileType)14 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)8 PsiFile (com.intellij.psi.PsiFile)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 ParserDefinition (com.intellij.lang.ParserDefinition)4 IFileElementType (com.intellij.psi.tree.IFileElementType)4 PsiElement (com.intellij.psi.PsiElement)3 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)3 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)3 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)2 com.intellij.codeInsight.completion (com.intellij.codeInsight.completion)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 LookupElementDecorator (com.intellij.codeInsight.lookup.LookupElementDecorator)1 DocumentContent (com.intellij.diff.contents.DocumentContent)1