notnull
[fedora-idea.git] / platform / lang-impl / src / com / intellij / psi / impl / source / tree / RecursiveTreeElementWalkingVisitor.java
blobc69f77318e8c3993ea7b807be16bf293c6d90642
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.
17 package com.intellij.psi.impl.source.tree;
19 import com.intellij.lang.ASTNode;
20 import com.intellij.psi.WalkingState;
21 import org.jetbrains.annotations.NotNull;
23 public abstract class RecursiveTreeElementWalkingVisitor extends TreeElementVisitor{
24 private final boolean myDoTransform;
26 protected RecursiveTreeElementWalkingVisitor() {
27 this(true);
30 protected RecursiveTreeElementWalkingVisitor(boolean doTransform) {
31 myDoTransform = doTransform;
34 private static class ASTTreeGuide implements WalkingState.TreeGuide<ASTNode> {
35 public ASTNode getNextSibling(@NotNull ASTNode element) {
36 return element.getTreeNext();
39 public ASTNode getPrevSibling(@NotNull ASTNode element) {
40 return element.getTreePrev();
43 public ASTNode getFirstChild(@NotNull ASTNode element) {
44 return element.getFirstChildNode();
47 public ASTNode getParent(@NotNull ASTNode element) {
48 return element.getTreeParent();
51 private static final ASTTreeGuide instance = new ASTTreeGuide();
54 private final WalkingState<ASTNode> myWalkingState = new WalkingState<ASTNode>(ASTTreeGuide.instance) {
55 @Override
56 public void elementFinished(@NotNull ASTNode element) {
60 @Override
61 public void visit(@NotNull ASTNode element) {
62 ((TreeElement)element).acceptTree(RecursiveTreeElementWalkingVisitor.this);
66 @Override
67 public void visitLeaf(LeafElement leaf) {
68 visitNode(leaf);
71 @Override
72 public void visitComposite(CompositeElement composite) {
73 visitNode(composite);
76 protected void visitNode(TreeElement element){
77 if (myDoTransform || !TreeUtil.isCollapsedChameleon(element)) {
78 myWalkingState.elementStarted(element);