📄 pythonmodelprovider.java
字号:
debug("After", modification);
}
}
/**
* Given a Path from the 1st child of the project, will try to create that path in the python model.
* @param project the project
* @param found a stack so that the last element added is the leaf of the path we want to discover
*/
private void tryCreateModelFromProject(IProject project, FastStack<Object> found) {
PythonNature nature = PythonNature.getPythonNature(project);
if(nature== null){
return;//if the python nature is not available, we won't have any python elements here
}
Set<String> sourcePathSet = new HashSet<String>();
try {
sourcePathSet = nature.getPythonPathNature().getProjectSourcePathSet();
} catch (CoreException e) {
PydevPlugin.log(e);
}
Object currentParent = project;
PythonSourceFolder pythonSourceFolder = null;
for(Iterator<Object> it = found.topDownIterator();it.hasNext();){
Object child = it.next();
if(child instanceof IFolder){
if(pythonSourceFolder == null){
pythonSourceFolder = tryWrapSourceFolder(currentParent, (IFolder) child, sourcePathSet);
if(pythonSourceFolder != null){
currentParent = pythonSourceFolder;
}else if(child instanceof IContainer){
currentParent = (IContainer) child;
}
//just go on (if we found the source folder or not, because if we found, that's ok, and if
//we didn't, then the children will not be in the python model anyway)
continue;
}
}
if(pythonSourceFolder != null){
IWrappedResource r = doWrap(currentParent, pythonSourceFolder, child);
if(r != null){
child = r;
}
}
currentParent = child;
}
}
/**
* Actually wraps some resource into a wrapped resource.
*
* @param parent this is the parent
* it may be null -- in the case of a remove
* it may be a wrapped resource (if it is in the python model)
* it may be a resource (if it is a source folder)
*
*
* @param pythonSourceFolder this is the python source folder for the resource (it may be null if the resource itself is a source folder
* or if it is actually a resource that has already been removed)
* @param currentChildren those are the children that should be wrapped
* @param isAdd whether this is an add operation or not
* @return
*/
@SuppressWarnings("unchecked")
protected boolean wrapChildren(Object parent, PythonSourceFolder pythonSourceFolder, Set currentChildren, boolean isAdd) {
LinkedHashSet convertedChildren = new LinkedHashSet();
for (Iterator childrenItr = currentChildren.iterator(); childrenItr.hasNext();) {
Object child = childrenItr.next();
Object existing = getResourceInPythonModel((IResource) child, true);
if(existing == null){
if(isAdd){
//add
IWrappedResource w = doWrap(parent, pythonSourceFolder, child);
if(w != null){ //if it is null, it is not below a python source folder
childrenItr.remove();
convertedChildren.add(w);
}
}else{
continue; //it has already been removed
}
}else{ //existing != null
childrenItr.remove();
convertedChildren.add(existing);
if(!isAdd){
//also remove it from the model
IWrappedResource wrapped = (IWrappedResource) existing;
wrapped.getSourceFolder().removeChild((IResource) child);
}
}
}
//if we did have some wrapping... go on and add them to the out list (and return true)
if (!convertedChildren.isEmpty()) {
currentChildren.addAll(convertedChildren);
return true;
}
//nothing happened, so, just say it
return false;
}
/**
* This method tries to wrap a given resource as a wrapped resource (if possible)
*
* @param parent the parent of the wrapped resource
* @param pythonSourceFolder the source folder that contains this resource
* @param child the object that should be wrapped
* @return the object as an object from the python model
*/
@SuppressWarnings("unchecked")
protected IWrappedResource doWrap(Object parent, PythonSourceFolder pythonSourceFolder, Object child) {
if (child instanceof IProject){
//do nothing (because a project is never going to be an IWrappedResource)
if(pythonSourceFolder == null && parent != null){
PythonSourceFolder f = doWrapPossibleSourceFolder(parent, (IProject)child);
if(f != null){
return f;
}
}
}else if(child instanceof IFolder){
IFolder folder = (IFolder) child;
//it may be a PythonSourceFolder
if(pythonSourceFolder == null && parent != null){
PythonSourceFolder f = doWrapPossibleSourceFolder(parent, folder);
if(f != null){
return f;
}
}
if(pythonSourceFolder != null){
return new PythonFolder((IWrappedResource) parent, folder, pythonSourceFolder);
}
}else if(child instanceof IFile){
if(pythonSourceFolder != null){
//if the python source folder is null, that means that this is a file that is not actually below a source folder -- so, don't wrap it
return new PythonFile((IWrappedResource) parent, (IFile) child, pythonSourceFolder);
}
}else if (child instanceof IResource){
if(pythonSourceFolder != null){
return new PythonResource((IWrappedResource) parent, (IResource) child, pythonSourceFolder);
}
}else{
throw new RuntimeException("Unexpected class:"+child.getClass());
}
return null;
}
/**
* Try to wrap a folder or project as a source folder...
*/
private PythonSourceFolder doWrapPossibleSourceFolder(Object parent, IContainer container) {
try {
IProject project;
if(!(container instanceof IProject)){
project = ((IContainer)parent).getProject();
}else{
project = (IProject) container;
}
PythonNature nature = PythonNature.getPythonNature(project);
if(nature!= null){
//check for source folder
Set<String> sourcePathSet = nature.getPythonPathNature().getProjectSourcePathSet();
PythonSourceFolder newSourceFolder = tryWrapSourceFolder(parent, container, sourcePathSet);
if(newSourceFolder != null){
return newSourceFolder;
}
}
} catch (CoreException e) {
throw new RuntimeException(e);
}
return null;
}
/**
* This method checks if the given folder can be wrapped as a source-folder, and if that's possible, creates and returns
* it
* @return a created source folder or null if it couldn't be created.
*/
private PythonSourceFolder tryWrapSourceFolder(Object parent, IContainer container, Set<String> sourcePathSet) {
IPath fullPath = container.getFullPath();
if(sourcePathSet.contains(fullPath.toString())){
PythonSourceFolder sourceFolder;
if(container instanceof IFolder){
sourceFolder = new PythonSourceFolder(parent, (IFolder)container);
}else if(container instanceof IProject){
sourceFolder = new PythonProjectSourceFolder(parent, (IProject)container);
}else{
throw new RuntimeException("Shouldn't get here: "+container.getClass());
}
//System.out.println("Created source folder: "+ret[i]+" - "+folder.getProject()+" - "+folder.getProjectRelativePath());
Set<PythonSourceFolder> sourceFolders = getProjectSourceFolders(container.getProject());
sourceFolders.add((PythonSourceFolder) sourceFolder);
return sourceFolder;
}
return null;
}
/**
* Converts elements to the python model -- but only creates it if it's parent is found in the python model
*/
@SuppressWarnings("unchecked")
protected boolean convertToPythonElementsUpdateOrRefresh(Set currentChildren) {
LinkedHashSet convertedChildren = new LinkedHashSet();
for (Iterator childrenItr = currentChildren.iterator(); childrenItr.hasNext();) {
Object child = childrenItr.next();
if(child instanceof IResource && !(child instanceof IWrappedResource)){
IResource res = (IResource) child;
Object resourceInPythonModel = getResourceInPythonModel(res, true);
if(resourceInPythonModel != null){
//if it is in the python model, just go on
childrenItr.remove();
convertedChildren.add(resourceInPythonModel);
}else{
//now, if it's not but its parent is, go on and create it
Object pythonParent = getResourceInPythonModel(res.getParent(), true);
if(pythonParent instanceof IWrappedResource){
IWrappedResource parent = (IWrappedResource) pythonParent;
if (res instanceof IProject){
//do nothing (because a project is never going to be an IWrappedResource)
throw new RuntimeException("Shouldn't be here...");
}else if(res instanceof IFolder){
childrenItr.remove();
convertedChildren.add(new PythonFolder(parent, (IFolder) res, parent.getSourceFolder()));
}else if(res instanceof IFile){
childrenItr.remove();
convertedChildren.add(new PythonFile(parent, (IFile) res, parent.getSourceFolder()));
}else if (child instanceof IResource){
childrenItr.remove();
convertedChildren.add(new PythonResource(parent, (IResource) child, parent.getSourceFolder()));
}
}
}
}
}
if (!convertedChildren.isEmpty()) {
currentChildren.addAll(convertedChildren);
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -