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

📄 sourcemodule.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

        Tuple3<IModule, String, IToken> o = nature.getAstManager().findOnImportedMods(state.getCopyWithActTok(rep), this);
        
        if(o != null && o.o1 instanceof SourceModule){
            mod =  (SourceModule) o.o1;
            tok = o.o2;
            
        }else if(o != null && o.o1 instanceof CompiledModule){
            //ok, we have to check the compiled module
            tok = o.o2;
            if (tok == null || tok.length() == 0 ){
                return new Definition[]{new Definition(1,1,"",null,null,o.o1)};
            }else{
                return (Definition[]) o.o1.findDefinition(state.getCopyWithActTok(tok), -1, -1, nature, lFindInfo);
            }
        }
        
        //mod == this if we are now checking the globals (or maybe not)...heheheh
        ICompletionState copy = state.getCopy();
        copy.setActivationToken(tok);
        try{
        	state.checkFindDefinitionMemory(mod, tok);
        	findDefinitionsFromModAndTok(nature, toRet, visitor.moduleImported, mod, copy);
        }catch(CompletionRecursionException e){
        	//ignore (will return what we've got so far)
        }
            
        return toRet.toArray(new Definition[0]);
    }

    /**
     * Finds the definitions for some module and a token from that module
     * @throws CompletionRecursionException 
     */
	private void findDefinitionsFromModAndTok(IPythonNature nature, ArrayList<Definition> toRet, String moduleImported, SourceModule mod, ICompletionState state) throws CompletionRecursionException {
        String tok = state.getActivationToken();
		if(tok != null){
        	if(tok.length() > 0){
	            Definition d = mod.findGlobalTokDef(state.getCopyWithActTok(tok), nature);
				if(d != null){
	                toRet.add(d);
	                
	            }else if(moduleImported != null){
	            	//if it was found as some import (and is already stored as a dotted name), we must check for
	            	//multiple representations in the absolute form:
	            	//as a relative import
	            	//as absolute import
	            	getModuleDefinition(nature, toRet, mod, moduleImported);
	            }

        	}else{
        		//we found it, but it is an empty tok (which means that what we found is the actual module).
        		toRet.add(new Definition(1,1,"",null,null,mod)); 
        	}
        }
	}

	private IDefinition getModuleDefinition(IPythonNature nature, ArrayList<Definition> toRet, SourceModule mod, String moduleImported) {
		String rel = AbstractToken.makeRelative(mod.getName(), moduleImported);
		IModule modFound = nature.getAstManager().getModule(rel, nature, false);
		if(modFound == null){
			modFound = nature.getAstManager().getModule(moduleImported, nature, false);
		}
		if(modFound != null){
			//ok, found it
			Definition definition = new Definition(1,1,"", null, null, modFound);
			if(toRet != null){
				toRet.add(definition);
			}
			return definition;
		}
		return null;
	}


    /**
     * @param tok
     * @param nature 
     * @return
     * @throws CompletionRecursionException 
     */
    public Definition findGlobalTokDef(ICompletionState state, IPythonNature nature) throws CompletionRecursionException {
        String tok = state.getActivationToken();
    	String[] headAndTail = FullRepIterable.headAndTail(tok);
    	String firstPart = headAndTail[0];
    	String rep = headAndTail[1];
    	
    	IToken[] tokens = null;
    	if(nature != null){
    		tokens = nature.getAstManager().getCompletionsForModule(this, state.getCopyWithActTok(firstPart), true);
    	}else{
    		tokens = getGlobalTokens();
    	}
        for (IToken token : tokens) {
            boolean sameRep = token.getRepresentation().equals(rep);
            if(sameRep){
                if(token instanceof SourceToken){
                    if(((SourceToken)token).getType() == IToken.TYPE_OBJECT_FOUND_INTERFACE){
                        //just having it extracted from the interface from an object does not mean
                        //that it's actual definition was found
                        continue; 
                    }
                	//ok, we found it
                    SimpleNode a = ((SourceToken)token).getAst();
                    Tuple<Integer, Integer> def = getLineColForDefinition(a);
                    
                    String parentPackage = token.getParentPackage();
                    IModule module = this;
                    if(nature != null){
                    	IModule mod = nature.getAstManager().getModule(parentPackage, nature, true);
                    	if(mod != null){
                    		module = mod;
                    	}
                    }
                    
                    
                    if(module instanceof SourceModule){
                        //this is just to get its scope...
                        SourceModule m = (SourceModule) module;
                        FindScopeVisitor scopeVisitor = new FindScopeVisitor(a.beginLine, a.beginColumn);
                        if (m.ast != null){
                            try {
                                m.ast.accept(scopeVisitor);
                            } catch (Exception e) {
                                PydevPlugin.log(e);
                            }
                        }
                        return new Definition(def.o1, def.o2, rep, a, scopeVisitor.scope, module);
                    }else{
                        //line, col
                        return new Definition(def.o1, def.o2, rep, a, new LocalScope(new FastStack<SimpleNode>()), module);
                    }
                }else if(token instanceof ConcreteToken){
                	//a contrete token represents a module
                	String modName = token.getParentPackage();
                	if(modName.length() > 0){
                		modName+= ".";
                	}
                	modName += token.getRepresentation();
                	IModule module = nature.getAstManager().getModule(modName, nature, true);
                	if(module == null){
                		return null;
                	}else{
                		return new Definition(0+1, 0+1, "", null, null, module); // it is the module itself
                	}
                	
                }else if(token instanceof CompiledToken){
                    String parentPackage = token.getParentPackage();
                    FullRepIterable iterable = new FullRepIterable(parentPackage, true);
                    
                    IModule module = null;
                    for(String modName: iterable){
                        module = nature.getAstManager().getModule(modName, nature, true);
                        if(module != null){
                            break;
                        }
                    }
                    if(module == null){
                        return null;
                    }
                    
                    int length = module.getName().length();
                    String finalRep = "";
                    if(parentPackage.length() > length){
                        finalRep = parentPackage.substring(length + 1)+'.';
                    }
                    finalRep += token.getRepresentation();
                    
                    try {
                        IDefinition[] definitions = module.findDefinition(state.getCopyWithActTok(finalRep), -1, -1, nature, new ArrayList<FindInfo>());
                        if(definitions.length > 0){
                            return (Definition) definitions[0];
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }else{
                	throw new RuntimeException("Unexpected token:"+token.getClass());
                }
            }
        }
        
        return null;
    }
    
    public Tuple<Integer, Integer> getLineColForDefinition(SimpleNode a){
    	int line = a.beginLine;
    	int col = a.beginColumn;
    	
    	if(a instanceof ClassDef){
    		ClassDef c = (ClassDef)a;
    		line = c.name.beginLine;
    		col = c.name.beginColumn;
    		
    	} else if(a instanceof FunctionDef){
    		FunctionDef c = (FunctionDef)a;
    		line = c.name.beginLine;
    		col = c.name.beginColumn;
    	}
    	
    	return new Tuple<Integer, Integer>(line,col);
    }
    
    /**
     * @param line: at 0
     * @param col: at 0
     */
    public IToken[] getLocalTokens(int line, int col, ILocalScope scope){
        try {
            if(scope == null){
    	        FindScopeVisitor scopeVisitor = new FindScopeVisitor(line, col);
    	        if (ast != null){
                    ast.accept(scopeVisitor);
    	        }
                scope = scopeVisitor.scope;
            }
	        
	        return scope.getLocalTokens(line, col, false);
        } catch (Exception e) {
            e.printStackTrace();
            return EMPTY_ITOKEN_ARRAY;
        }
    }

    /**
     * @param line: at 0
     * @param col: at 0
     */
    public ILocalScope getLocalScope(int line, int col) {
        try {
            FindScopeVisitor scopeVisitor = new FindScopeVisitor(line, col);
            if (ast != null){
                ast.accept(scopeVisitor);
            }
            
            return scopeVisitor.scope;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * @return if the file we have is the same file in the cache.
     */
    public boolean isSynched() {
        if(this.file == null && TESTING){
            return true; //when testing we can have a source module without a file
        }
        return this.file.lastModified() == this.lastModified;
    }
    
    public SimpleNode getAst(){
        return ast;
    }

    
    /**
     * 
     */
    public int findAstEnd(SimpleNode node) {
        try {
            int line = node.beginLine;
            int col = node.beginColumn;
	        FindScopeVisitor scopeVisitor = new FindScopeVisitor(line, col);
	        if (ast != null){
                ast.accept(scopeVisitor);
	        }
	        
	        return scopeVisitor.scope.getScopeEndLine();
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * @return
     */
    public int findIfMain() {
        try {
	        FindScopeVisitor scopeVisitor = new FindScopeVisitor(-1,-1);
	        if (ast != null){
                ast.accept(scopeVisitor);
	        }
	        
	        return scopeVisitor.scope.getIfMainLine();
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof SourceModule)) {
            return false;
        }
        SourceModule m = (SourceModule) obj;
        if(file == null || m.file == null){
            if(file != null){
                return false;
            }
            if(m.file != null){
                return false;
            }
            return this.name.equals(m.name);
        }
        
        return REF.getFileAbsolutePath(file).equals(REF.getFileAbsolutePath(m.file)) && this.name.equals(m.name); 
    }

	public void setName(String n) {
		this.name = n;
	}

}

⌨️ 快捷键说明

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