extracted common code
[fedora-idea.git] / lang-api / src / com / intellij / psi / PsiRecursiveElementWalkingVisitor.java
blob54e6db50a2a80b4b28e5e89a961526c95c64e1fe
1 /*
2 * Copyright 2000-2007 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.
16 package com.intellij.psi;
18 import java.util.List;
20 /**
21 * Represents a PSI element visitor which recursively visits the children of the element
22 * on which the visit was started.
24 public abstract class PsiRecursiveElementWalkingVisitor extends PsiElementVisitor {
25 private final boolean myVisitAllFileRoots;
26 private final PsiWalkingState myWalkingState = new PsiWalkingState(this){
27 public void elementFinished(PsiElement element) {
28 PsiRecursiveElementWalkingVisitor.this.elementFinished(element);
32 protected PsiRecursiveElementWalkingVisitor() {
33 this(false);
36 protected PsiRecursiveElementWalkingVisitor(boolean visitAllFileRoots) {
37 myVisitAllFileRoots = visitAllFileRoots;
40 public void visitElement(final PsiElement element) {
41 myWalkingState.elementStarted(element);
44 protected void elementFinished(PsiElement element) {
48 @Override
49 public void visitFile(final PsiFile file) {
50 if (myVisitAllFileRoots) {
51 final FileViewProvider viewProvider = file.getViewProvider();
52 final List<PsiFile> allFiles = viewProvider.getAllFiles();
53 if (allFiles.size() > 1) {
54 if (file == viewProvider.getPsi(viewProvider.getBaseLanguage())) {
55 for (PsiFile lFile : allFiles) {
56 lFile.acceptChildren(this);
58 return;
63 super.visitFile(file);