⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sourcemodule.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**
     * @see org.python.pydev.core.IModule#getGlobalTokens(org.python.pydev.core.ICompletionState, org.python.pydev.core.ICodeCompletionASTManager)
     */
    public IToken[] getGlobalTokens(ICompletionState initialState, ICodeCompletionASTManager manager) {
        String activationToken = initialState.getActivationToken();
        int activationTokenLen = activationToken.length();
        String[] actToks = FullRepIterable.dotSplit(activationToken);
        
        String goFor = null;
        if(actToks.length > 0){
            goFor = actToks[0];
        }
        IToken[] t = getTokens(GlobalModelVisitor.GLOBAL_TOKENS, null, goFor);
        
        for (int i = 0; i < t.length; i++) {
            SourceToken token = (SourceToken) t[i];
            String rep = token.getRepresentation();
            
            SimpleNode ast = token.getAst();
            
            if(activationTokenLen > rep.length() && activationToken.startsWith(rep)){
                //we need this thing to work correctly for nested modules...
                //some tests are available at: PythonCompletionTestWithoutBuiltins.testDeepNestedXXX
                
                int iActTok = 0;
                if(actToks[iActTok].equals(rep)){
                    //System.out.println("Now we have to find act..."+activationToken+"(which is a definition of:"+rep+")");
                    try {
                        Definition[] definitions;
                        String value = activationToken;
                        String initialValue=null;
                        while(true){
                            if(value.equals(initialValue)){
                                break;
                            }
                            initialValue = value;
                            if(iActTok > actToks.length){
                                break; //unable to find it
                            }
                            definitions = findDefinition(initialState.getCopyWithActTok(value), token.getLineDefinition(), token.getColDefinition()+1, manager.getNature(), new ArrayList<FindInfo>());
                            if(definitions.length == 1){
                                Definition d = definitions[0];
                                if(d.ast instanceof Assign){
                                    Assign assign = (Assign) d.ast;
                                    value = NodeUtils.getRepresentationString(assign.value);
                                    definitions = findDefinition(initialState.getCopyWithActTok(value), d.line, d.col, manager.getNature(), new ArrayList<FindInfo>());
                                }else if(d.ast instanceof ClassDef){
                                    IToken[] toks = (IToken[]) ((SourceModule)d.module).getClassToks(initialState, manager, d.ast).toArray(EMPTY_ITOKEN_ARRAY);
                                    if(iActTok == actToks.length-1){
                                        return toks;
                                    }
                                    value = d.value;
                                    
                                }else if (d.ast instanceof Name){
                                    ClassDef classDef = (ClassDef) d.scope.getClassDef();
                                    if(classDef != null){
                                    	FindDefinitionModelVisitor visitor = new FindDefinitionModelVisitor(actToks[actToks.length-1], d.line, d.col, d.module);
                                        try {
											classDef.accept(visitor);
										} catch (StopVisitingException e) {
											//expected exception
										}
                                        if(visitor.definitions.size() == 0){
                                        	return EMPTY_ITOKEN_ARRAY;
                                        }
                                        d = visitor.definitions.get(0);
                                        value = d.value;
                                        if(d instanceof AssignDefinition){
                                            return getValueCompletions(initialState, manager, value, d.module);
                                        }
                                    }else{
                                    	if(d.module instanceof SourceModule){
                                    		SourceModule m = (SourceModule) d.module;
                                    		String joined = FullRepIterable.joinFirstParts(actToks);
                                            Definition[] definitions2 = m.findDefinition(initialState.getCopyWithActTok(joined), d.line, d.col, manager.getNature(), null);
                                    		if(definitions2.length == 0){
                                    			return EMPTY_ITOKEN_ARRAY;
                                    		}
                                    		d = definitions2[0];
                                    		value = d.value+"."+actToks[actToks.length-1];
                                    		if(d instanceof AssignDefinition){
                                    			return ((SourceModule)d.module).getValueCompletions(initialState, manager, value, d.module);
                                    		}
                                    	}
                                    }
                                    
                                }else if ((d.ast == null && d.module != null) || d.ast instanceof ImportFrom){
                                    return getValueCompletions(initialState, manager, value, d.module);
                                    
                                }else{
                                    break;
                                }
                            }else{
                                return getValueCompletions(initialState, manager, value, this);
                            }
                            iActTok++;
                        }
                    } catch (CompletionRecursionException e) {
                    } catch (Exception e) {
                        PydevPlugin.log(e);
                    }
                }
            } else if(rep.equals(activationToken)){
                if(ast instanceof ClassDef){
                    initialState.setLookingFor(ICompletionState.LOOKING_FOR_UNBOUND_VARIABLE);
                }
                List<IToken> classToks = getClassToks(initialState, manager, ast);
                if(classToks.size() == 0){
                    if(initialState.getLookingFor() == ICompletionState.LOOKING_FOR_ASSIGN){
                        continue;
                    }
                    //otherwise, return it empty anyway...
                    return EMPTY_ITOKEN_ARRAY;
                }
                return (IToken[]) classToks.toArray(EMPTY_ITOKEN_ARRAY);
            }
        }
        return EMPTY_ITOKEN_ARRAY;
    }

    /**
     * @param initialState
     * @param manager
     * @param value
     * @return
     * @throws CompletionRecursionException 
     */
    private IToken[] getValueCompletions(ICompletionState initialState, ICodeCompletionASTManager manager, String value, IModule module) throws CompletionRecursionException {
        initialState.checkFindMemory(this, value);
        ICompletionState copy = initialState.getCopy();
        copy.setActivationToken(value);
        IToken[] completionsForModule = manager.getCompletionsForModule(module, copy);
        return completionsForModule;
    }

    /**
     * @param initialState
     * @param manager
     * @param ast
     * @return
     */
    public List<IToken> getClassToks(ICompletionState initialState, ICodeCompletionASTManager manager, SimpleNode ast) {
        List<IToken> modToks = GlobalModelVisitor.getTokens(ast, GlobalModelVisitor.INNER_DEFS, name, initialState);//name = moduleName
        
        try {
            //COMPLETION: get the completions for the whole hierarchy if this is a class!!
            ICompletionState state;
            if (ast instanceof ClassDef) {
                ClassDef c = (ClassDef) ast;
                for (int j = 0; j < c.bases.length; j++) {
                    if (c.bases[j] instanceof Name) {
                        Name n = (Name) c.bases[j];
                        String base = n.id;
                        //An error in the programming might result in an error.
                        //
                        //e.g. The case below results in a loop.
                        //
                        //class A(B):
                        //    
                        //    def a(self):
                        //        pass
                        //        
                        //class B(A):
                        //    
                        //    def b(self):
                        //        pass
                        state = initialState.getCopy();
                        state.setActivationToken(base);

                        state.checkMemory(this, base);

                        final IToken[] comps = manager.getCompletionsForModule(this, state);
                        modToks.addAll(Arrays.asList(comps));
                    } else if (c.bases[j] instanceof Attribute) {
                        Attribute attr = (Attribute) c.bases[j];
                        String s = NodeUtils.getFullRepresentationString(attr);

                        state = initialState.getCopy();
                        state.setActivationToken(s);
                        final IToken[] comps = manager.getCompletionsForModule(this, state);
                        modToks.addAll(Arrays.asList(comps));
                    }
                }

            }
        } catch (CompletionRecursionException e) {
            // let's return what we have so far...
        }
        return modToks;
    }

    /**
     * @param line: starts at 1
     * @param col: starts at 1
     */
    @SuppressWarnings("unchecked")
	public Definition[] findDefinition(ICompletionState state, int line, int col, IPythonNature nature, List<FindInfo> lFindInfo) throws Exception{
        String rep = state.getActivationToken();
    	if(lFindInfo == null){
    		lFindInfo = new ArrayList<FindInfo>();
    	}
        //the line passed in starts at 1 and the lines for the visitor start at 0
        ArrayList<Definition> toRet = new ArrayList<Definition>();
        FindInfo info = new FindInfo();
        lFindInfo.add(info);
        
        //first thing is finding its scope
        FindScopeVisitor scopeVisitor = new FindScopeVisitor(line, col);
        if (ast != null){
        	ast.accept(scopeVisitor);
        }
        
        //this visitor checks for assigns for the token
        FindDefinitionModelVisitor visitor = new FindDefinitionModelVisitor(rep, line, col, this);
        if (ast != null){
        	try{
        		ast.accept(visitor);
			} catch (StopVisitingException e) {
				//expected exception
			}
        }
        
        if(visitor.definitions.size() > 0){
        	//ok, it is an assign, so, let's get it

            for (Iterator iter = visitor.definitions.iterator(); iter.hasNext();) {
	            Object next = iter.next();
                if(next instanceof AssignDefinition){
                    AssignDefinition element = (AssignDefinition) next;
    	            if(element.target.startsWith("self") == false){
    		            if(element.scope.isOuterOrSameScope(scopeVisitor.scope) || element.foundAsGlobal){
    		                toRet.add(element);
    		            }
    	            }else{
    		            toRet.add(element);
    	            }
                }else{
                    toRet.add((Definition) next);
                }
	        }
            return (Definition[]) toRet.toArray(new Definition[0]);
        }
        
        
        
        //now, check for locals
        IToken[] localTokens = scopeVisitor.scope.getAllLocalTokens();
        info.localTokens = localTokens;
        for (IToken tok : localTokens) {
        	if(tok.getRepresentation().equals(rep)){
        		return new Definition[]{new Definition(tok, scopeVisitor.scope, this, true)};
        	}
        }
        
        //not found... check as local imports
        List<IToken> localImportedModules = scopeVisitor.scope.getLocalImportedModules(line, col, this.name);
        for (IToken tok : localImportedModules) {
        	if(tok.getRepresentation().equals(rep)){
                Tuple3<IModule, String, IToken> o = nature.getAstManager().findOnImportedMods(new IToken[]{tok}, state.getCopyWithActTok(rep), this.getName());
                if(o != null && o.o1 instanceof SourceModule){
                    ICompletionState copy = state.getCopy();
                    copy.setActivationToken(o.o2);
                    
                    findDefinitionsFromModAndTok(nature, toRet, null, (SourceModule) o.o1, copy);
                }
                if(toRet.size() > 0){
                	return (Definition[]) toRet.toArray(new Definition[0]);
                }
        	}
        }
        	
        
        //ok, not assign nor import, let's check if it is some self (we do not check for only 'self' because that would map to a
        //local (which has already been covered).
        if (rep.startsWith("self.")){
        	//ok, it is some self, now, that is only valid if we are in some class definition
        	ClassDef classDef = (ClassDef) scopeVisitor.scope.getClassDef();
        	if(classDef != null){
        		//ok, we are in a class, so, let's get the self completions
        		String classRep = NodeUtils.getRepresentationString(classDef);
				IToken[] globalTokens = getGlobalTokens(
        				new CompletionState(line-1, col-1, classRep, nature,""), 
        				nature.getAstManager());
				
        		String withoutSelf = rep.substring(5);
        		for (IToken token : globalTokens) {
					if(token.getRepresentation().equals(withoutSelf)){
						String parentPackage = token.getParentPackage();
						IModule module = nature.getAstManager().getModule(parentPackage, nature, true);
                        
						if(token instanceof SourceToken && (module != null || this.name.equals(parentPackage))){
                            if(module == null){
                                module = this;
                            }
                            
			                SimpleNode ast2 = ((SourceToken)token).getAst();
							Tuple<Integer, Integer> def = getLineColForDefinition(ast2);
							FastStack<SimpleNode> stack = new FastStack<SimpleNode>();
                            if(module instanceof SourceModule){
                                stack.push(((SourceModule)module).getAst());
                            }
							stack.push(classDef);
							ILocalScope scope = new LocalScope(stack);
							return new Definition[]{new Definition(def.o1, def.o2, token.getRepresentation(), ast2, scope, module)};
                            
						}else{
							return new Definition[0];
						}
					}
				}
        	}
        }
        
        	
    	//ok, it is not an assign, so, let's search the global tokens (and imports)
        String tok = rep;
        SourceModule mod = this;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -