~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev/src/org/python/pydev/editor/codecompletion/revisited/visitors/Scope.java

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on Jan 20, 2005
 
3
 *
 
4
 * @author Fabio Zadrozny
 
5
 */
 
6
package org.python.pydev.editor.codecompletion.revisited.visitors;
 
7
 
 
8
import java.util.ArrayList;
 
9
import java.util.HashSet;
 
10
import java.util.Iterator;
 
11
import java.util.List;
 
12
import java.util.Set;
 
13
 
 
14
import org.python.pydev.core.IToken;
 
15
import org.python.pydev.core.structure.FastStack;
 
16
import org.python.pydev.editor.codecompletion.PyCodeCompletion;
 
17
import org.python.pydev.editor.codecompletion.revisited.modules.SourceToken;
 
18
import org.python.pydev.parser.jython.SimpleNode;
 
19
import org.python.pydev.parser.jython.ast.ClassDef;
 
20
import org.python.pydev.parser.jython.ast.FunctionDef;
 
21
import org.python.pydev.parser.visitors.NodeUtils;
 
22
 
 
23
/**
 
24
 * @author Fabio Zadrozny
 
25
 */
 
26
public class Scope {
 
27
 
 
28
    public FastStack<SimpleNode> scope = new FastStack<SimpleNode>();
 
29
    
 
30
    public int scopeEndLine = -1;
 
31
 
 
32
    public int ifMainLine = -1;
 
33
    
 
34
    public Scope(FastStack<SimpleNode> scope){
 
35
        this.scope.addAll(scope);
 
36
    }
 
37
    
 
38
    /**
 
39
     * @see java.lang.Object#equals(java.lang.Object)
 
40
     */
 
41
    public boolean equals(Object obj) {
 
42
        if (!(obj instanceof Scope)) {
 
43
            return false;
 
44
        }
 
45
        
 
46
        Scope s = (Scope) obj;
 
47
        
 
48
        if(this.scope.size() != s.scope.size()){
 
49
            return false;
 
50
        }
 
51
        
 
52
        return checkIfScopesMatch(s);
 
53
    }
 
54
    
 
55
    /**
 
56
     * Checks if this scope is an outer scope of the scope passed as a param (s).
 
57
     * Or if it is the same scope. 
 
58
     */
 
59
    public boolean isOuterOrSameScope(Scope s){
 
60
        if(this.scope.size() > s.scope.size()){
 
61
            return false;
 
62
        }
 
63
 
 
64
        return checkIfScopesMatch(s);
 
65
    }
 
66
 
 
67
    /**
 
68
     * @param s the scope we're checking for
 
69
     * @return if the scope passed as a parameter starts with the same scope we have here. It should not be
 
70
     * called if the size of the scope we're checking is bigger than the size of 'this' scope. 
 
71
     */
 
72
    private boolean checkIfScopesMatch(Scope s) {
 
73
        Iterator otIt = s.scope.iterator();
 
74
        
 
75
        for (Iterator iter = this.scope.iterator(); iter.hasNext();) {
 
76
            SimpleNode element = (SimpleNode) iter.next();
 
77
            SimpleNode otElement = (SimpleNode) otIt.next();
 
78
            
 
79
            if(element.beginColumn != otElement.beginColumn)
 
80
                return false;
 
81
            
 
82
            if(element.beginLine != otElement.beginLine)
 
83
                return false;
 
84
            
 
85
            if(! element.getClass().equals(otElement.getClass()))
 
86
                return false;
 
87
            
 
88
            if(! NodeUtils.getFullRepresentationString(element).equals( NodeUtils.getFullRepresentationString(otElement)))
 
89
                return false;
 
90
            
 
91
        }
 
92
        return true;
 
93
    }
 
94
    
 
95
    /**
 
96
     * @return all the local tokens found 
 
97
     */
 
98
    public IToken[] getAllLocalTokens(){
 
99
        return getLocalTokens(Integer.MAX_VALUE, Integer.MAX_VALUE);
 
100
    }
 
101
    
 
102
    
 
103
    /**
 
104
     * @param endLine tokens will only be recognized if its beginLine is higher than this parameter.
 
105
     */
 
106
    public IToken[] getLocalTokens(int endLine, int col){
 
107
        Set<SourceToken> comps = new HashSet<SourceToken>();
 
108
        
 
109
        for (Iterator iter = this.scope.iterator(); iter.hasNext();) {
 
110
            SimpleNode element = (SimpleNode) iter.next();
 
111
            
 
112
            if (element instanceof FunctionDef) {
 
113
                FunctionDef f = (FunctionDef) element;
 
114
                for (int i = 0; i < f.args.args.length; i++) {
 
115
                    String s = NodeUtils.getRepresentationString(f.args.args[i]);
 
116
                    comps.add(new SourceToken(f.args.args[i], s, "", "", "", PyCodeCompletion.TYPE_PARAM));
 
117
                }
 
118
                
 
119
                try {
 
120
                    for (int i = 0; i < f.body.length; i++) {
 
121
                                GlobalModelVisitor visitor = new GlobalModelVisitor(GlobalModelVisitor.GLOBAL_TOKENS, "");
 
122
                        f.body[i].accept(visitor);
 
123
                        List t = visitor.tokens;
 
124
                        for (Iterator iterator = t.iterator(); iterator.hasNext();) {
 
125
                            SourceToken tok = (SourceToken) iterator.next();
 
126
                            
 
127
                            //if it is found here, it is a local type
 
128
                            tok.type = PyCodeCompletion.TYPE_PARAM;
 
129
                            if(tok.getAst().beginLine <= endLine){
 
130
                                comps.add(tok);
 
131
                            }
 
132
                            
 
133
                        }
 
134
                    }
 
135
                } catch (Exception e) {
 
136
                    e.printStackTrace();
 
137
                }
 
138
            }
 
139
        }
 
140
        
 
141
        
 
142
        return (SourceToken[]) comps.toArray(new SourceToken[0]);
 
143
    }
 
144
 
 
145
    /**
 
146
     * @return the modules that are imported in the current (local) scope as tokens
 
147
     */
 
148
    public List<IToken> getLocalImportedModules(int line, int col, String moduleName) {
 
149
        ArrayList<IToken> importedModules = new ArrayList<IToken>();
 
150
        for (Iterator iter = this.scope.iterator(); iter.hasNext();) {
 
151
            SimpleNode element = (SimpleNode) iter.next();
 
152
            
 
153
            if (element instanceof FunctionDef) {
 
154
                FunctionDef f = (FunctionDef) element;
 
155
                for (int i = 0; i < f.body.length; i++) {
 
156
 
 
157
                    IToken[] tokens = GlobalModelVisitor.getTokens(f.body[i], GlobalModelVisitor.ALIAS_MODULES, moduleName);
 
158
                    for (IToken token : tokens) {
 
159
                        importedModules.add(token);
 
160
                    }
 
161
                }
 
162
            }
 
163
        }
 
164
        return importedModules;
 
165
    }
 
166
 
 
167
    /**
 
168
     * @return the first class scope found or null if there is None
 
169
     */
 
170
        public ClassDef getClassDef() {
 
171
                for(SimpleNode node : this.scope){
 
172
                        if(node instanceof ClassDef){
 
173
                                return (ClassDef) node;
 
174
                        }
 
175
                }
 
176
                return null;
 
177
        }
 
178
 
 
179
        /**
 
180
         * @return whether the last element found in this scope is a class definition
 
181
         */
 
182
        public boolean isLastClassDef() {
 
183
                if(this.scope.size() > 0 && this.scope.peek() instanceof ClassDef){
 
184
                        return true;
 
185
                }
 
186
                return false;
 
187
        }
 
188
 
 
189
 
 
190
}
 
191
 
 
192
 
 
193
 
 
194
 
 
195
 
 
196
 
 
197
 
 
198
 
 
199
 
 
200
 
 
201
 
 
202