more general replace() implementation
[fedora-idea.git] / platform / lang-impl / src / com / intellij / lang / ASTFactory.java
blob6d335835fb69264307a4ada94f85027b15a7388b
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author max
20 package com.intellij.lang;
22 import com.intellij.lexer.Lexer;
23 import com.intellij.lexer.LexerUtil;
24 import com.intellij.psi.TokenType;
25 import com.intellij.psi.impl.source.CharTableImpl;
26 import com.intellij.psi.impl.source.CodeFragmentElement;
27 import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
28 import com.intellij.psi.impl.source.tree.*;
29 import com.intellij.psi.tree.*;
30 import com.intellij.util.CharTable;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
34 public abstract class ASTFactory {
35 public static final DefaultFactory DEFAULT = new DefaultFactory();
36 private static final CharTable WHITSPACES = new CharTableImpl();
38 @Nullable
39 public abstract CompositeElement createComposite(IElementType type);
41 @Nullable
42 public LazyParseableElement createLazy(ILazyParseableElementType type, CharSequence text) {
43 if (type instanceof IFileElementType) {
44 return new FileElement(type, text);
47 return new LazyParseableElement(type, text);
50 @Nullable
51 public abstract LeafElement createLeaf(IElementType type, CharSequence text);
53 @NotNull
54 public static LazyParseableElement lazy(ILazyParseableElementType type, CharSequence text) {
55 ASTNode node = type.createNode(text);
56 if (node != null) return (LazyParseableElement)node;
58 if (type == TokenType.CODE_FRAGMENT) {
59 return new CodeFragmentElement(null);
62 LazyParseableElement psi = factory(type).createLazy(type, text);
63 return psi != null ? psi : DEFAULT.createLazy(type, text);
66 @Deprecated
67 @NotNull
68 public static LeafElement leaf(IElementType type, CharSequence fileText, int start, int end, CharTable table) {
69 return leaf(type, table.intern(fileText, start, end));
72 @NotNull
73 public static LeafElement leaf(IElementType type, CharSequence text) {
74 if (type == TokenType.WHITE_SPACE) {
75 return new PsiWhiteSpaceImpl(text);
78 if (type instanceof ILeafElementType) {
79 return (LeafElement)((ILeafElementType)type).createLeafNode(text);
82 final LeafElement customLeaf = factory(type).createLeaf(type, text);
83 return customLeaf != null ? customLeaf : DEFAULT.createLeaf(type, text);
86 private static ASTFactory factory(IElementType type) {
87 return LanguageASTFactory.INSTANCE.forLanguage(type.getLanguage());
90 public static LeafElement whitespace(CharSequence text) {
91 PsiWhiteSpaceImpl w = new PsiWhiteSpaceImpl(WHITSPACES.intern(text));
92 CodeEditUtil.setNodeGenerated(w, true);
93 return w;
96 public static LeafElement leaf(IElementType type, CharSequence text, CharTable table) {
97 return leaf(type, table.intern(text));
100 public static LeafElement leaf(final Lexer lexer, final CharTable charTable) {
101 return leaf(lexer.getTokenType(), LexerUtil.internToken(lexer, charTable));
104 @NotNull
105 public static CompositeElement composite(IElementType type) {
106 if (type instanceof ICompositeElementType) {
107 return (CompositeElement)((ICompositeElementType)type).createCompositeNode();
110 if (type == TokenType.CODE_FRAGMENT) {
111 return new CodeFragmentElement(null);
114 final CompositeElement customComposite = factory(type).createComposite(type);
115 return customComposite != null ? customComposite : DEFAULT.createComposite(type);
118 private static class DefaultFactory extends ASTFactory {
119 @Override
120 @NotNull
121 public CompositeElement createComposite(IElementType type) {
122 if (type instanceof IFileElementType) {
123 return new FileElement(type, null);
126 return new CompositeElement(type);
129 @Override
130 @NotNull
131 public LeafElement createLeaf(IElementType type, CharSequence text) {
132 final Language lang = type.getLanguage();
133 final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
134 if (parserDefinition != null) {
135 if (parserDefinition.getCommentTokens().contains(type)) {
136 return new PsiCommentImpl(type, text);
139 return new LeafPsiElement(type, text);