2
* Created on Jan 20, 2005
4
* @author Fabio Zadrozny
6
package org.python.pydev.editor.codecompletion.revisited.visitors;
8
import java.util.ArrayList;
9
import java.util.HashSet;
10
import java.util.Iterator;
11
import java.util.List;
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;
24
* @author Fabio Zadrozny
28
public FastStack<SimpleNode> scope = new FastStack<SimpleNode>();
30
public int scopeEndLine = -1;
32
public int ifMainLine = -1;
34
public Scope(FastStack<SimpleNode> scope){
35
this.scope.addAll(scope);
39
* @see java.lang.Object#equals(java.lang.Object)
41
public boolean equals(Object obj) {
42
if (!(obj instanceof Scope)) {
46
Scope s = (Scope) obj;
48
if(this.scope.size() != s.scope.size()){
52
return checkIfScopesMatch(s);
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.
59
public boolean isOuterOrSameScope(Scope s){
60
if(this.scope.size() > s.scope.size()){
64
return checkIfScopesMatch(s);
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.
72
private boolean checkIfScopesMatch(Scope s) {
73
Iterator otIt = s.scope.iterator();
75
for (Iterator iter = this.scope.iterator(); iter.hasNext();) {
76
SimpleNode element = (SimpleNode) iter.next();
77
SimpleNode otElement = (SimpleNode) otIt.next();
79
if(element.beginColumn != otElement.beginColumn)
82
if(element.beginLine != otElement.beginLine)
85
if(! element.getClass().equals(otElement.getClass()))
88
if(! NodeUtils.getFullRepresentationString(element).equals( NodeUtils.getFullRepresentationString(otElement)))
96
* @return all the local tokens found
98
public IToken[] getAllLocalTokens(){
99
return getLocalTokens(Integer.MAX_VALUE, Integer.MAX_VALUE);
104
* @param endLine tokens will only be recognized if its beginLine is higher than this parameter.
106
public IToken[] getLocalTokens(int endLine, int col){
107
Set<SourceToken> comps = new HashSet<SourceToken>();
109
for (Iterator iter = this.scope.iterator(); iter.hasNext();) {
110
SimpleNode element = (SimpleNode) iter.next();
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));
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();
127
//if it is found here, it is a local type
128
tok.type = PyCodeCompletion.TYPE_PARAM;
129
if(tok.getAst().beginLine <= endLine){
135
} catch (Exception e) {
142
return (SourceToken[]) comps.toArray(new SourceToken[0]);
146
* @return the modules that are imported in the current (local) scope as tokens
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();
153
if (element instanceof FunctionDef) {
154
FunctionDef f = (FunctionDef) element;
155
for (int i = 0; i < f.body.length; i++) {
157
IToken[] tokens = GlobalModelVisitor.getTokens(f.body[i], GlobalModelVisitor.ALIAS_MODULES, moduleName);
158
for (IToken token : tokens) {
159
importedModules.add(token);
164
return importedModules;
168
* @return the first class scope found or null if there is None
170
public ClassDef getClassDef() {
171
for(SimpleNode node : this.scope){
172
if(node instanceof ClassDef){
173
return (ClassDef) node;
180
* @return whether the last element found in this scope is a class definition
182
public boolean isLastClassDef() {
183
if(this.scope.size() > 0 && this.scope.peek() instanceof ClassDef){