1 package com
.intellij
.psi
.impl
;
3 import com
.intellij
.lang
.ASTFactory
;
4 import com
.intellij
.lang
.Commenter
;
5 import com
.intellij
.lang
.Language
;
6 import com
.intellij
.lang
.LanguageCommenters
;
7 import com
.intellij
.openapi
.fileTypes
.LanguageFileType
;
8 import com
.intellij
.psi
.*;
9 import com
.intellij
.psi
.impl
.source
.DummyHolderFactory
;
10 import com
.intellij
.psi
.impl
.source
.SourceTreeToPsiMap
;
11 import com
.intellij
.psi
.impl
.source
.tree
.FileElement
;
12 import com
.intellij
.psi
.impl
.source
.tree
.LeafElement
;
13 import com
.intellij
.psi
.impl
.source
.tree
.TreeElement
;
14 import com
.intellij
.util
.IncorrectOperationException
;
15 import org
.jetbrains
.annotations
.NonNls
;
16 import org
.jetbrains
.annotations
.NotNull
;
21 public class PsiParserFacadeImpl
implements PsiParserFacade
{
22 protected final PsiManagerEx myManager
;
24 public PsiParserFacadeImpl(PsiManagerEx manager
) {
29 public PsiElement
createWhiteSpaceFromText(@NotNull @NonNls String text
) throws IncorrectOperationException
{
30 final FileElement holderElement
= DummyHolderFactory
.createHolder(myManager
, null).getTreeElement();
31 final LeafElement newElement
= ASTFactory
.leaf(TokenType
.WHITE_SPACE
, holderElement
.getCharTable().intern(text
));
32 holderElement
.rawAddChildren(newElement
);
33 return newElement
.getPsi();
37 public PsiComment
createLineCommentFromText(@NotNull final LanguageFileType fileType
,
38 @NotNull final String text
) throws IncorrectOperationException
{
39 Commenter commenter
= LanguageCommenters
.INSTANCE
.forLanguage(fileType
.getLanguage());
40 assert commenter
!= null;
41 String prefix
= commenter
.getLineCommentPrefix();
42 assert prefix
!= null;
44 PsiFile aFile
= createDummyFile(prefix
+ text
, fileType
);
45 PsiElement
[] children
= aFile
.getChildren();
46 for (PsiElement aChildren
: children
) {
47 if (aChildren
instanceof PsiComment
) {
48 PsiComment comment
= (PsiComment
)aChildren
;
49 DummyHolderFactory
.createHolder(myManager
, (TreeElement
)SourceTreeToPsiMap
.psiElementToTree(comment
), null);
53 throw new IncorrectOperationException("Incorrect comment \"" + text
+ "\".");
57 public PsiComment
createLineOrBlockCommentFromText(@NotNull Language lang
, @NotNull String text
)
58 throws IncorrectOperationException
{
59 Commenter commenter
= LanguageCommenters
.INSTANCE
.forLanguage(lang
);
60 assert commenter
!= null;
61 String prefix
= commenter
.getLineCommentPrefix();
62 final String blockCommentPrefix
= commenter
.getBlockCommentPrefix();
63 final String blockCommentSuffix
= commenter
.getBlockCommentSuffix();
64 assert prefix
!= null || (blockCommentPrefix
!= null && blockCommentSuffix
!= null);
66 PsiFile aFile
= PsiFileFactory
.getInstance(myManager
.getProject()).createFileFromText("_Dummy_", lang
, prefix
!= null ?
(prefix
+ text
) : (blockCommentPrefix
+ text
+ blockCommentSuffix
));
67 PsiElement
[] children
= aFile
.getChildren();
68 for (PsiElement aChildren
: children
) {
69 if (aChildren
instanceof PsiComment
) {
70 PsiComment comment
= (PsiComment
)aChildren
;
71 DummyHolderFactory
.createHolder(myManager
, (TreeElement
)SourceTreeToPsiMap
.psiElementToTree(comment
), null);
75 throw new IncorrectOperationException("Incorrect comment \"" + text
+ "\".");
78 protected PsiFile
createDummyFile(String text
, final LanguageFileType fileType
) {
79 String ext
= fileType
.getDefaultExtension();
80 @NonNls String fileName
= "_Dummy_." + ext
;
82 return PsiFileFactory
.getInstance(myManager
.getProject()).createFileFromText(fileType
, fileName
, text
, 0, text
.length());