notnull
[fedora-idea.git] / platform / lang-api / src / com / intellij / psi / PsiRecursiveElementWalkingVisitor.java
blob8df03c6b609e806522dc924d22c5129cadd90dab
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.
16 package com.intellij.psi;
18 import com.intellij.openapi.progress.ProgressManager;
19 import org.jetbrains.annotations.NotNull;
21 import java.util.List;
23 /**
24 * Represents a PSI element visitor which recursively visits the children of the element
25 * on which the visit was started.
27 public abstract class PsiRecursiveElementWalkingVisitor extends PsiElementVisitor {
28 private final boolean myVisitAllFileRoots;
29 private final PsiWalkingState myWalkingState = new PsiWalkingState(this){
30 public void elementFinished(@NotNull PsiElement element) {
31 PsiRecursiveElementWalkingVisitor.this.elementFinished(element);
35 protected PsiRecursiveElementWalkingVisitor() {
36 this(false);
39 protected PsiRecursiveElementWalkingVisitor(boolean visitAllFileRoots) {
40 myVisitAllFileRoots = visitAllFileRoots;
43 public void visitElement(final PsiElement element) {
44 ProgressManager.checkCanceled();
46 myWalkingState.elementStarted(element);
49 protected void elementFinished(PsiElement element) {
53 @Override
54 public void visitFile(final PsiFile file) {
55 if (myVisitAllFileRoots) {
56 final FileViewProvider viewProvider = file.getViewProvider();
57 final List<PsiFile> allFiles = viewProvider.getAllFiles();
58 if (allFiles.size() > 1) {
59 if (file == viewProvider.getPsi(viewProvider.getBaseLanguage())) {
60 for (PsiFile lFile : allFiles) {
61 lFile.acceptChildren(this);
63 return;
68 super.visitFile(file);