EA-61477 (assert: SharedImplUtil.findFileElement)
[idea/community.git] / platform / structuralsearch / source / com / intellij / structuralsearch / impl / matcher / MatcherImplUtil.java
1 package com.intellij.structuralsearch.impl.matcher;
2
3 import com.intellij.lang.Language;
4 import com.intellij.openapi.fileTypes.FileType;
5 import com.intellij.openapi.fileTypes.LanguageFileType;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.psi.PsiElement;
8 import com.intellij.structuralsearch.MatchOptions;
9 import com.intellij.structuralsearch.StructuralSearchProfile;
10 import com.intellij.structuralsearch.StructuralSearchUtil;
11 import com.intellij.structuralsearch.impl.matcher.compiler.PatternCompiler;
12 import com.intellij.util.IncorrectOperationException;
13
14 /**
15  * Created by IntelliJ IDEA.
16  * User: Maxim.Mossienko
17  * Date: Mar 19, 2004
18  * Time: 6:56:25 PM
19  * To change this template use File | Settings | File Templates.
20  */
21 public class MatcherImplUtil {
22
23   public static void transform(MatchOptions options) {
24     if (options.hasVariableConstraints()) return;
25     PatternCompiler.transformOldPattern(options);
26   }
27
28   public static PsiElement[] createTreeFromText(String text, PatternTreeContext context, FileType fileType, Project project)
29     throws IncorrectOperationException {
30     return createTreeFromText(text, context, fileType, null, null, project, false);
31   }
32
33   public static PsiElement[] createSourceTreeFromText(String text,
34                                                       PatternTreeContext context,
35                                                       FileType fileType,
36                                                       String extension,
37                                                       Project project,
38                                                       boolean physical) {
39     if (fileType instanceof LanguageFileType) {
40       Language language = ((LanguageFileType)fileType).getLanguage();
41       StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
42       if (profile != null) {
43         return profile.createPatternTree(text, context, fileType, null, null, extension, project, physical);
44       }
45     }
46     return PsiElement.EMPTY_ARRAY;
47   }
48
49   public static PsiElement[] createTreeFromText(String text,
50                                                 PatternTreeContext context,
51                                                 FileType fileType,
52                                                 Language language,
53                                                 String contextName,
54                                                 Project project,
55                                                 boolean physical) throws IncorrectOperationException {
56     if (language == null && fileType instanceof LanguageFileType) {
57       language = ((LanguageFileType)fileType).getLanguage();
58     }
59     if (language != null) {
60       StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
61       if (profile != null) {
62         return profile.createPatternTree(text, context, fileType, language, contextName, null, project, physical);
63       }
64     }
65     return PsiElement.EMPTY_ARRAY;
66   }
67 }