📄 pythonbasemodelprovider.java
字号:
if(parentElement instanceof IResource){
project = ((IResource)parentElement).getProject();
}
//we can only get the nature if the project is open
if(project != null && project.isOpen()){
nature = PythonNature.getPythonNature(project);
}
//replace folders -> source folders (we should only get here on a path that's not below a source folder)
Object[] childrenToReturn = super.getChildren(parentElement);
//if we don't have a python nature in this project, there is no way we can have a PythonSourceFolder
Object[] ret = new Object[childrenToReturn.length];
for (int i=0; i < childrenToReturn.length; i++) {
PythonNature localNature = nature;
IProject localProject = project;
//now, first we have to try to get it (because it might already be created)
Object child = childrenToReturn[i];
if(!(child instanceof IResource)){
continue;
}
child = getResourceInPythonModel((IResource) child);
ret[i] = child;
//if it is a folder (that is not already a PythonSourceFolder, it might be that we have to create a PythonSourceFolder)
if (child instanceof IContainer && !(child instanceof PythonSourceFolder)) {
IContainer container = (IContainer) child;
try {
//check if it is a source folder (and if it is, create it)
if(localNature == null){
if(container instanceof IProject){
localProject = (IProject) container;
if(localProject.isOpen() == false){
continue;
}else{
localNature = PythonNature.getPythonNature(localProject);
}
}else{
continue;
}
}
//if it's a python project, the nature can't be null
if(localNature == null){
continue;
}
Set<String> sourcePathSet = localNature.getPythonPathNature().getProjectSourcePathSet();
IPath fullPath = container.getFullPath();
if(sourcePathSet.contains(fullPath.toString())){
if(container instanceof IFolder){
ret[i] = new PythonSourceFolder(parentElement, (IFolder)container);
}else if(container instanceof IProject){
ret[i] = new PythonProjectSourceFolder(parentElement, (IProject)container);
}else{
throw new RuntimeException("Should not get here.");
}
Set<PythonSourceFolder> sourceFolders = getProjectSourceFolders(localProject);
sourceFolders.add((PythonSourceFolder) ret[i]);
}
} catch (CoreException e) {
throw new RuntimeException(e);
}
}
}
return ret;
}
/**
* @param wrappedResourceParent: this is the parent that is an IWrappedResource (which means
* that children will also be IWrappedResources)
*
* @return the children (an array of IWrappedResources)
*/
private Object[] getChildrenForIWrappedResource(IWrappedResource wrappedResourceParent) {
//------------------------------------------------------------------- get python nature
PythonNature nature = null;
Object[] childrenToReturn = null;
Object obj = wrappedResourceParent.getActualObject();
IProject project = null;
if(obj instanceof IResource){
IResource resource = (IResource) obj;
project = resource.getProject();
if(project != null && project.isOpen()){
nature = PythonNature.getPythonNature(project);
}
}
//------------------------------------------------------------------- treat python nodes
if (wrappedResourceParent instanceof PythonNode) {
PythonNode node = (PythonNode) wrappedResourceParent;
childrenToReturn = getChildrenFromParsedItem(wrappedResourceParent, node.entry, node.pythonFile);
//------------------------------------- treat python files (add the classes/methods,etc)
}else if (wrappedResourceParent instanceof PythonFile) {
// if it's a file, we want to show the classes and methods
PythonFile file = (PythonFile) wrappedResourceParent;
if (PythonPathHelper.isValidSourceFile(file.getActualObject())) {
if (nature != null) {
ICodeCompletionASTManager astManager = nature.getAstManager();
//the nature may still not be completely restored...
if(astManager != null){
IModulesManager modulesManager = astManager.getModulesManager();
if (modulesManager instanceof ProjectModulesManager) {
ProjectModulesManager projectModulesManager = (ProjectModulesManager) modulesManager;
String moduleName = projectModulesManager.resolveModuleInDirectManager(file.getActualObject(), project);
if (moduleName != null) {
IModule module = projectModulesManager.getModuleInDirectManager(moduleName, nature, true);
if(module == null){
//ok, something strange happened... it shouldn't be null... maybe empty, but not null at this point
//so, if it exists, let's try to create it...
//TODO: This should be moved to somewhere else.
File f = new File(PydevPlugin.getIResourceOSString(file.getActualObject()));
if(f.exists()){
projectModulesManager.addModule(new ModulesKey(moduleName, f));
module = projectModulesManager.getModuleInDirectManager(moduleName, nature, true);
}
}
if (module instanceof SourceModule) {
SourceModule sourceModule = (SourceModule) module;
OutlineCreatorVisitor visitor = OutlineCreatorVisitor.create(sourceModule.getAst());
ParsedItem root = new ParsedItem(visitor.getAll().toArray(new ASTEntryWithChildren[0]), null);
childrenToReturn = getChildrenFromParsedItem(wrappedResourceParent, root, file);
}
}
}
}
}
}
}
//------------------------------------------------------------- treat folders and others
else{
Object[] children = super.getChildren(wrappedResourceParent.getActualObject());
childrenToReturn = wrapChildren(wrappedResourceParent, wrappedResourceParent.getSourceFolder(), children);
}
return childrenToReturn;
}
/**
* This method changes the contents of the children so that the actual types are mapped to
* elements of our python model.
*
* @param parent the parent (from the python model)
* @param pythonSourceFolder this is the source folder that contains this resource
* @param children these are the children thot should be wrapped (note that this array
* is not actually changed -- a new array is created and returned).
*
* @return an array with the wrapped types
*/
protected Object[] wrapChildren(IWrappedResource parent, PythonSourceFolder pythonSourceFolder, Object[] children) {
Object[] childrenToReturn;
Object[] ret = new Object[children.length];
for (int i = 0; i < children.length; i++) {
Object object = children[i];
Object existing = getResourceInPythonModel((IResource) object, true);
if(existing == null){
if(object instanceof IFolder){
IFolder folder = (IFolder) object;
ret[i] = new PythonFolder(parent, folder, pythonSourceFolder);
}else if(object instanceof IFile){
IFile file = (IFile) object;
ret[i] = new PythonFile(parent, file, pythonSourceFolder);
}else if(object instanceof IResource){
ret[i] = new PythonResource(parent, (IResource) object, pythonSourceFolder);
}else{
ret[i] = existing;
}
}else{
ret[i] = existing;
}
}
childrenToReturn = ret;
return childrenToReturn;
}
/**
* @return the parent for some element.
*/
public Object getParent(Object element) {
if (element instanceof IWrappedResource) {
// just return the parent
IWrappedResource resource = (IWrappedResource) element;
return resource.getParentElement();
}
return super.getParent(element);
}
/**
* @param parentElement this is the elements returned
* @param root this is the parsed item that has children that we want to return
* @return the children elements (PythonNode) for the passed parsed item
*/
private Object[] getChildrenFromParsedItem(Object parentElement, ParsedItem root, PythonFile pythonFile) {
ParsedItem[] children = root.getChildren();
PythonNode p[] = new PythonNode[children.length];
int i = 0;
// in this case, we just want to return the roots
for (ParsedItem e : children) {
p[i] = new PythonNode(pythonFile, parentElement, e);
i++;
}
return p;
}
/*
* (non-Javadoc) Method declared on IContentProvider.
*/
public void dispose() {
try{
this.projectToSourceFolders = null;
if (viewer != null) {
IWorkspace[] workspace = null;
Object obj = viewer.getInput();
if (obj instanceof IWorkspace) {
workspace = new IWorkspace[]{(IWorkspace) obj};
} else if (obj instanceof IContainer) {
workspace = new IWorkspace[]{((IContainer) obj).getWorkspace()};
} else if (obj instanceof IWorkingSet) {
IWorkingSet newWorkingSet = (IWorkingSet) obj;
workspace = getWorkspaces(newWorkingSet);
}
if (workspace != null) {
for (IWorkspace w : workspace) {
w.removeResourceChangeListener(this);
}
}
}
PythonNatureListenersManager.removePythonNatureListener(this);
}catch (Exception e) {
PydevPlugin.log(e);
}
try{
super.dispose();
}catch (Exception e) {
PydevPlugin.log(e);
}
}
/*
* (non-Javadoc) Method declared on IContentProvider.
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
super.inputChanged(viewer, oldInput, newInput);
this.viewer = viewer;
IWorkspace[] oldWorkspace = null;
IWorkspace[] newWorkspace = null;
//get the old
if (oldInput instanceof IWorkspace) {
oldWorkspace = new IWorkspace[]{(IWorkspace) oldInput};
} else if (oldInput instanceof IContainer) {
oldWorkspace = new IWorkspace[]{((IContainer) oldInput).getWorkspace()};
} else if (oldInput instanceof IWorkingSet) {
IWorkingSet oldWorkingSet = (IWorkingSet) oldInput;
oldWorkspace = getWorkspaces(oldWorkingSet);
}
//and the new
if (newInput instanceof IWorkspace) {
newWorkspace = new IWorkspace[]{(IWorkspace) newInput};
} else if (newInput instanceof IContainer) {
newWorkspace = new IWorkspace[]{((IContainer) newInput).getWorkspace()};
} else if (newInput instanceof IWorkingSet) {
IWorkingSet newWorkingSet = (IWorkingSet) newInput;
newWorkspace = getWorkspaces(newWorkingSet);
}
//now, let's treat the workspace
if (oldWorkspace != null) {
for (IWorkspace workspace : oldWorkspace) {
workspace.removeResourceChangeListener(this);
}
}
if (newWorkspace != null) {
for (IWorkspace workspace : newWorkspace) {
workspace.addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -