📄 modulesmanager.java
字号:
//source files have priority over other modules (dlls) -- if both are source, there is no real way to resolve
//this priority, so, let's just add it over.
add = true;
}
}
if(add){
//the previous must be removed (if there was any)
keys.remove(modulesKey);
keys.put(modulesKey, modulesKey);
}
}
}
for (String modName : fromJar) {
final ModulesKey k = new ModulesKey(modName, null);
keys.put(k, k);
}
onChangePythonpath(defaultSelectedInterpreter, keys);
//assign to instance variable
this.setModules(keys);
}
/**
* Subclasses may do more things after the defaults were added to the cache (e.g.: the system modules manager may
* add builtins)
*/
protected void onChangePythonpath(String defaultSelectedInterpreter, SortedMap<ModulesKey, ModulesKey> keys) {
}
/**
* This is the only method that should remove a module.
* No other method should remove them directly.
*
* @param key this is the key that should be removed
*/
protected void doRemoveSingleModule(ModulesKey key) {
synchronized (modulesKeys) {
if(DEBUG_BUILD){
System.out.println("Removing module:"+key+" - "+this.getClass());
}
this.modulesKeys.remove(key);
ModulesManager.cache.remove(key, this);
}
}
/**
* This method that actually removes some keys from the modules.
*
* @param toRem the modules to be removed
*/
protected void removeThem(Collection<ModulesKey> toRem) {
//really remove them here.
for (Iterator<ModulesKey> iter = toRem.iterator(); iter.hasNext();) {
doRemoveSingleModule(iter.next());
}
}
public void removeModules(Collection<ModulesKey> toRem) {
removeThem(toRem);
}
public void addModule(final ModulesKey key) {
doAddSingleModule(key, new EmptyModule(key.name, key.file));
}
/**
* This is the only method that should add / update a module.
* No other method should add it directly (unless it is loading or rebuilding it).
*
* @param key this is the key that should be added
* @param n
*/
public void doAddSingleModule(final ModulesKey key, AbstractModule n) {
if(DEBUG_BUILD){
System.out.println("Adding module:"+key+" - "+this.getClass());
}
synchronized (modulesKeys) {
this.modulesKeys.put(key, key);
ModulesManager.cache.add(key, n, this);
}
}
/**
* @return a set of all module keys
*/
public Set<String> getAllModuleNames() {
Set<String> s = new HashSet<String>();
synchronized (modulesKeys) {
for (ModulesKey key : this.modulesKeys.keySet()) {
s.add(key.name);
}
}
return s;
}
public SortedMap<ModulesKey,ModulesKey> getAllDirectModulesStartingWith(String strStartingWith) {
if(strStartingWith.length() == 0){
return modulesKeys;
}
ModulesKey startingWith = new ModulesKey(strStartingWith, null);
ModulesKey endingWith = new ModulesKey(startingWith+"z", null);
synchronized (modulesKeys) {
//we don't want it to be backed up by the same set (because it may be changed, so, we may get
//a java.util.ConcurrentModificationException on places that use it)
return new TreeMap<ModulesKey, ModulesKey>(modulesKeys.subMap(startingWith, endingWith));
}
}
public SortedMap<ModulesKey,ModulesKey> getAllModulesStartingWith(String strStartingWith) {
return getAllDirectModulesStartingWith(strStartingWith);
}
public ModulesKey[] getOnlyDirectModules() {
synchronized (modulesKeys) {
return (ModulesKey[]) this.modulesKeys.keySet().toArray(new ModulesKey[0]);
}
}
/**
* @return
*/
public int getSize() {
return this.modulesKeys.size();
}
public IModule getModule(String name, IPythonNature nature, boolean dontSearchInit) {
return getModule(true, name, nature, dontSearchInit);
}
/**
* This method returns the module that corresponds to the path passed as a parameter.
*
* @param name the name of the module we're looking for (e.g.: mod1.mod2)
* @param dontSearchInit is used in a negative form because initially it was isLookingForRelative, but
* it actually defines if we should look in __init__ modules too, so, the name matches the old signature.
*
* NOTE: isLookingForRelative description was: when looking for relative imports, we don't check for __init__
* @return the module represented by this name
*/
public IModule getModule(boolean acceptCompiledModule, String name, IPythonNature nature, boolean dontSearchInit) {
AbstractModule n = null;
ModulesKey keyForCacheAccess = new ModulesKey(null, null);
if(!dontSearchInit){
if(n == null){
keyForCacheAccess.name = new StringBuffer(name).append(".__init__").toString();
n = cache.getObj(keyForCacheAccess, this);
if(n != null){
name += ".__init__";
}
}
}
if (n == null) {
keyForCacheAccess.name = name;
n = cache.getObj(keyForCacheAccess, this);
}
if (n instanceof SourceModule) {
//ok, module exists, let's check if it is synched with the filesystem version...
SourceModule s = (SourceModule) n;
if (!s.isSynched()) {
//change it for an empty and proceed as usual.
n = new EmptyModule(s.getName(), s.getFile());
doAddSingleModule(new ModulesKey(s.getName(), s.getFile()), n);
}
}
if (n instanceof EmptyModule) {
EmptyModule e = (EmptyModule) n;
//let's treat os as a special extension, since many things it has are too much
//system dependent, and being so, many of its useful completions are not goten
//e.g. os.path is defined correctly only on runtime.
boolean found = false;
if (!found && e.f != null) {
if(!e.f.exists()){
keyForCacheAccess.name = name;
keyForCacheAccess.file = e.f;
doRemoveSingleModule(keyForCacheAccess);
n = null;
}else{
try {
n = AbstractModule.createModule(name, e.f, nature, -1);
} catch (IOException exc) {
keyForCacheAccess.name = name;
keyForCacheAccess.file = e.f;
doRemoveSingleModule(keyForCacheAccess);
n = null;
}
}
}else{ //ok, it does not have a file associated, so, we treat it as a builtin (this can happen in java jars)
if(acceptCompiledModule){
n = new CompiledModule(name, IToken.TYPE_BUILTIN, nature.getAstManager());
}else{
return null;
}
}
if (n != null) {
doAddSingleModule(new ModulesKey(name, e.f), n);
} else {
System.err.println("The module " + name + " could not be found nor created!");
}
}
if (n instanceof EmptyModule) {
throw new RuntimeException("Should not be an empty module anymore!");
}
return n;
}
protected AbstractModule getBuiltinModule(String name, IPythonNature nature, boolean dontSearchInit) {
//default implementation returns null (only the SystemModulesManager will actually return something here)
return null;
}
/**
* Passes through all the compiled modules in memory and clears its tokens (so that
* we restore them when needed).
*/
public void clearCache(){
ModulesManager.cache.internalCache.clear();
}
/**
* @see org.python.pydev.core.IProjectModulesManager#isInPythonPath(org.eclipse.core.resources.IResource, org.eclipse.core.resources.IProject)
*/
public boolean isInPythonPath(IResource member, IProject container) {
return resolveModule(member, container) != null;
}
/**
* @see org.python.pydev.core.IProjectModulesManager#resolveModule(org.eclipse.core.resources.IResource, org.eclipse.core.resources.IProject)
*/
public String resolveModule(IResource member, IProject container) {
File inOs = member.getRawLocation().toFile();
return resolveModule(REF.getFileAbsolutePath(inOs));
}
protected String getResolveModuleErr(IResource member) {
return "Unable to find the path "+member+" in the project were it\n" +
"is added as a source folder for pydev."+this.getClass();
}
/**
* @param full
* @return
*/
public String resolveModule(String full) {
return pythonPathHelper.resolveModule(full, false);
}
public List<String> getPythonPath(){
return pythonPathHelper.getPythonpath();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -