summaryrefslogtreecommitdiff
path: root/platform/core-api/src/com/intellij/psi/stubs/DefaultStubBuilder.java
blob: b4290e83651abf85c6a2d4b244949e3741795787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 com.intellij.psi.stubs;

import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.StubBasedPsiElement;
import com.intellij.psi.StubBuilder;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.Stack;
import org.jetbrains.annotations.NotNull;

/**
 * @author max
 */
public class DefaultStubBuilder implements StubBuilder {
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.stubs.DefaultStubBuilder");

  @Override
  public StubElement buildStubTree(@NotNull PsiFile file) {
    return buildStubTreeFor(file, createStubForFile(file));
  }

  @NotNull
  protected StubElement createStubForFile(@NotNull PsiFile file) {
    @SuppressWarnings("unchecked") PsiFileStubImpl stub = new PsiFileStubImpl(file);
    return stub;
  }

  @NotNull
  private StubElement buildStubTreeFor(@NotNull PsiElement root, @NotNull StubElement parentStub) {
    Stack<StubElement> parentStubs = new Stack<StubElement>();
    Stack<PsiElement> parentElements = new Stack<PsiElement>();
    parentElements.push(root);
    parentStubs.push(parentStub);

    while (!parentElements.isEmpty()) {
      StubElement stub = parentStubs.pop();
      PsiElement elt = parentElements.pop();

      if (elt instanceof StubBasedPsiElement) {
        final IStubElementType type = ((StubBasedPsiElement)elt).getElementType();

        if (type.shouldCreateStub(elt.getNode())) {
          @SuppressWarnings("unchecked") StubElement s = type.createStub(elt, stub);
          stub = s;
        }
      }
      else {
        final ASTNode node = elt.getNode();
        final IElementType type = node == null? null : node.getElementType();
        if (type instanceof IStubElementType && ((IStubElementType)type).shouldCreateStub(node)) {
          LOG.error("Non-StubBasedPsiElement requests stub creation. Stub type: " + type + ", PSI: " + elt);
        }
      }

      for (PsiElement child = elt.getLastChild(); child != null; child = child.getPrevSibling()) {
        if (!skipChildProcessingWhenBuildingStubs(elt, child)) {
          parentStubs.push(stub);
          parentElements.push(child);
        }
      }
    }
    return parentStub;
  }

  /**
   * Note to implementers: always keep in sync with {@linkplain #skipChildProcessingWhenBuildingStubs(ASTNode, ASTNode)}.
   */
  protected boolean skipChildProcessingWhenBuildingStubs(@NotNull PsiElement parent, @NotNull PsiElement element) {
    return false;
  }

  @NotNull
  protected StubElement buildStubTreeFor(@NotNull ASTNode root, @NotNull StubElement parentStub) {
    Stack<StubElement> parentStubs = new Stack<StubElement>();
    Stack<ASTNode> parentNodes = new Stack<ASTNode>();
    parentNodes.push(root);
    parentStubs.push(parentStub);

    while (!parentStubs.isEmpty()) {
      StubElement stub = parentStubs.pop();
      ASTNode node = parentNodes.pop();
      IElementType nodeType = node.getElementType();

      if (nodeType instanceof IStubElementType) {
        final IStubElementType type = (IStubElementType)nodeType;

        if (type.shouldCreateStub(node)) {
          PsiElement element = node.getPsi();
          if (!(element instanceof StubBasedPsiElement)) {
            LOG.error("Non-StubBasedPsiElement requests stub creation. Stub type: " + type + ", PSI: " + element);
          }
          @SuppressWarnings("unchecked") StubElement s = type.createStub(element, stub);
          stub = s;
          LOG.assertTrue(stub != null, element);
        }
      }

      for (ASTNode childNode = node.getLastChildNode(); childNode != null; childNode = childNode.getTreePrev()) {
        if (!skipChildProcessingWhenBuildingStubs(node, childNode)) {
          parentNodes.push(childNode);
          parentStubs.push(stub);
        }
      }
    }

    return parentStub;
  }

  /**
   * Note to implementers: always keep in sync with {@linkplain #skipChildProcessingWhenBuildingStubs(PsiElement, PsiElement)}.
   */
  @Override
  public boolean skipChildProcessingWhenBuildingStubs(@NotNull ASTNode parent, @NotNull ASTNode node) {
    return false;
  }
}