/*
 * Copyright 2015 Marek Kaput
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.rustidea.psi.impl;

import com.intellij.lang.DefaultASTFactoryImpl;
import com.intellij.lang.LanguageParserDefinitions;
import com.intellij.lang.ParserDefinition;
import com.intellij.psi.impl.source.tree.LeafElement;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.rustidea.RustLanguage;
import org.rustidea.psi.types.IRustIdentifierType;
import org.rustidea.psi.types.IRustKeywordType;
import org.rustidea.psi.types.IRustTokenType;
import org.rustidea.psi.types.RustType;

public class RustASTFactory extends DefaultASTFactoryImpl {
    @NotNull
    @Override
    public LeafElement createLeaf(@NotNull IElementType type, CharSequence text) {
        final ParserDefinition pd = LanguageParserDefinitions.INSTANCE.forLanguage(RustLanguage.INSTANCE);

        if (pd.getCommentTokens().contains(type)) return createComment(type, text);
        if (type instanceof IRustIdentifierType) return createIdentifier(type, text);
        if (type instanceof IRustKeywordType) return new RustKeywordImpl(type, text);
        if (type instanceof IRustTokenType) return new RustTokenImpl(type, text);

        return super.createLeaf(type, text);
    }

    @NotNull
    public LeafElement createIdentifier(@NotNull IElementType type, CharSequence text) {
        if (type == RustType.LIFETIME) return new RustLifetimeImpl(type, text);
        return new RustIdentifierImpl(type, text);
    }
}

@@ -619,16 +619,53 @@
 .impl;%0A%0A
+import com.intellij.lang.ASTFactory;%0A
 import c
@@ -698,20 +698,16 @@
 TFactory
-Impl
 ;%0Aimport
@@ -795,16 +795,71 @@
 nition;%0A
+import com.intellij.openapi.components.ServiceManager;%0A
 import c
@@ -989,16 +989,59 @@
 otNull;%0A
+import org.jetbrains.annotations.Nullable;%0A
 import o
@@ -1292,44 +1292,279 @@
 nds 
-DefaultASTFactoryImpl %7B%0A    @NotNull
+ASTFactory %7B%0A    private static final ParserDefinition rustParserDef = LanguageParserDefinitions.INSTANCE.forLanguage(RustLanguage.INSTANCE);%0A    private static final DefaultASTFactory defaultASTFactory = ServiceManager.getService(DefaultASTFactory.class);%0A%0A    @Nullable
 %0A   
@@ -1609,32 +1609,38 @@
 teLeaf(@NotNull 
+final 
 IElementType typ
@@ -1633,32 +1633,38 @@
 lementType type,
+ final
  CharSequence te
@@ -1681,165 +1681,100 @@
     
-final ParserDefinition pd = LanguageParserDefinitions.INSTANCE.forLanguage(RustLanguage.INSTANCE);%0A%0A        if (pd.getCommentTokens().contains(type)) return 
+if (rustParserDef.getCommentTokens().contains(type)) %7B%0A            return defaultASTFactory.
 crea
@@ -1788,32 +1788,42 @@
 nt(type, text);%0A
+        %7D%0A
         if (type
@@ -2080,36 +2080,12 @@
 urn 
-super.createLeaf(type, text)
+null
 ;%0A  
@@ -2151,16 +2151,22 @@
 NotNull 
+final 
 IElement
@@ -2175,16 +2175,22 @@
 pe type,
+ final
  CharSeq

Use DI instead of inheritance in RustASTFactory