📄 pydevplugin.java
字号:
try {
member.accept(new IResourceVisitor(){
public boolean visit(IResource resource) {
if(resource instanceof IFile){
ret.add((IFile) resource);
return false; //has no members
}
return true;
}
});
} catch (CoreException e) {
throw new RuntimeException(e);
}
return ret;
}
/**
* Returns the directories and python files in a list.
*
* @param file
* @return tuple with files in pos 0 and folders in pos 1
*/
public static List<File>[] getPyFilesBelow(File file, IProgressMonitor monitor, final boolean includeDirs, boolean checkHasInit) {
FileFilter filter = getPyFilesFileFilter(includeDirs);
return getPyFilesBelow(file, filter, monitor, true, checkHasInit);
}
/**
* @param includeDirs determines if we can include subdirectories
* @return a file filter only for python files (and other dirs if specified)
*/
public static FileFilter getPyFilesFileFilter(final boolean includeDirs) {
return new FileFilter() {
public boolean accept(File pathname) {
if (includeDirs){
if(pathname.isDirectory()){
return true;
}
if(PythonPathHelper.isValidSourceFile(pathname.toString())){
return true;
}
return false;
}else{
if(pathname.isDirectory()){
return false;
}
if(PythonPathHelper.isValidSourceFile(pathname.toString())){
return true;
}
return false;
}
}
};
}
public static List<File>[] getPyFilesBelow(File file, FileFilter filter, IProgressMonitor monitor, boolean checkHasInit) {
return getPyFilesBelow(file, filter, monitor, true, checkHasInit);
}
public static List<File>[] getPyFilesBelow(File file, FileFilter filter, IProgressMonitor monitor, boolean addSubFolders, boolean checkHasInit) {
return getPyFilesBelow(file, filter, monitor, addSubFolders, 0, checkHasInit);
}
/**
* Returns the directories and python files in a list.
*
* @param file
* @param addSubFolders: indicates if sub-folders should be added
* @return tuple with files in pos 0 and folders in pos 1
*/
@SuppressWarnings("unchecked")
private static List<File>[] getPyFilesBelow(File file, FileFilter filter, IProgressMonitor monitor, boolean addSubFolders, int level, boolean checkHasInit) {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
List<File> filesToReturn = new ArrayList<File>();
List<File> folders = new ArrayList<File>();
if (file.exists() == true) {
if (file.isDirectory()) {
File[] files = null;
if (filter != null) {
files = file.listFiles(filter);
} else {
files = file.listFiles();
}
boolean hasInit = false;
List<File> foldersLater = new LinkedList<File>();
for (int i = 0; i < files.length; i++) {
File file2 = files[i];
if(file2.isFile()){
filesToReturn.add(file2);
monitor.worked(1);
monitor.setTaskName("Found:" + file2.toString());
if (checkHasInit && hasInit == false){
if(PythonPathHelper.isValidInitFile(file2.getName())){
hasInit = true;
}
}
}else{
foldersLater.add(file2);
}
}
if(!checkHasInit || hasInit || level == 0){
folders.add(file);
for (Iterator iter = foldersLater.iterator(); iter.hasNext();) {
File file2 = (File) iter.next();
if(file2.isDirectory() && addSubFolders){
List<File>[] below = getPyFilesBelow(file2, filter, monitor, addSubFolders, level+1, checkHasInit);
filesToReturn.addAll(below[0]);
folders.addAll(below[1]);
monitor.worked(1);
}
}
}
} else if (file.isFile()) {
filesToReturn.add(file);
} else{
throw new RuntimeException("Not dir nor file... what is it?");
}
}
return new List[] { filesToReturn, folders };
}
//PyUnit integration
/** Listener list **/
private List listeners = new ArrayList();
@SuppressWarnings("unchecked")
public void addTestListener(ITestRunListener listener) {
listeners.add(listener);
}
public void removeTestListener(ITestRunListener listener) {
listeners.remove(listener);
}
public List getListeners() {
return listeners;
}
public void runTests(String moduleDir, String moduleName, IProject project) throws IOException, CoreException {
new PyUnitTestRunner().runTests(moduleDir, moduleName, project);
}
public void fireTestsStarted(int count) {
for (Iterator all=getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testsStarted(count);
}
}
public void fireTestsFinished() {
for (Iterator all=getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testsFinished();
}
}
public void fireTestStarted(String klass, String methodName) {
for (Iterator all=getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testStarted(klass, methodName);
}
}
public void fireTestFailed(String klass, String methodName, String trace) {
for (Iterator all=getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testFailed(klass, methodName, trace);
}
}
/**
* @param file the file we want to get info on.
* @return a tuple with the pythonnature to be used and the name of the module represented by the file in that scenario.
*/
public static Tuple<SystemPythonNature, String> getInfoForFile(File file){
String modName = null;
IInterpreterManager pythonInterpreterManager = getPythonInterpreterManager(false);
IInterpreterManager jythonInterpreterManager = getJythonInterpreterManager(false);
if(pythonInterpreterManager == null || jythonInterpreterManager == null){
return null;
}
SystemPythonNature systemPythonNature = new SystemPythonNature(pythonInterpreterManager);
SystemPythonNature pySystemPythonNature = systemPythonNature;
SystemPythonNature jySystemPythonNature = null;
try {
modName = systemPythonNature.resolveModule(file);
} catch (Exception e) {
// that's ok
}
if(modName == null){
systemPythonNature = new SystemPythonNature(jythonInterpreterManager);
jySystemPythonNature = systemPythonNature;
try {
modName = systemPythonNature.resolveModule(file);
} catch (Exception e) {
// that's ok
}
}
if(modName != null){
return new Tuple<SystemPythonNature, String>(systemPythonNature, modName);
}else{
//unable to discover it
try {
// the default one is python (actually, this should never happen, but who knows)
pythonInterpreterManager.getDefaultInterpreter();
modName = getModNameFromFile(file);
return new Tuple<SystemPythonNature, String>(pySystemPythonNature, modName);
} catch (Exception e) {
//the python interpreter manager is not valid or not configured
try {
// the default one is jython
jythonInterpreterManager.getDefaultInterpreter();
modName = getModNameFromFile(file);
return new Tuple<SystemPythonNature, String>(jySystemPythonNature, modName);
} catch (Exception e1) {
// ok, nothing to do about it, no interpreter is configured
return null;
}
}
}
}
/**
* This is the last resort (should not be used anywhere else).
*/
private static String getModNameFromFile(File file) {
if(file == null){
return null;
}
String name = file.getName();
int i = name.indexOf('.');
if (i != -1){
return name.substring(0, i);
}
return name;
}
/**
* @return a preference store that has the pydev preference store and the default editors text store
*/
public static IPreferenceStore getChainedPrefStore() {
IPreferenceStore general = EditorsUI.getPreferenceStore();
IPreferenceStore preferenceStore = getDefault().getPreferenceStore();
ChainedPreferenceStore store = new ChainedPreferenceStore(new IPreferenceStore[] { general, preferenceStore });
return store;
}
public static String getIResourceOSString(IResource f) {
String fullPath = f.getRawLocation().toOSString();
//now, we have to make sure it is canonical...
File file = new File(fullPath);
if(file.exists()){
return REF.getFileAbsolutePath(file);
}else{
//it does not exist, so, we have to check its project to validate the part that we can
IProject project = f.getProject();
IPath location = project.getLocation();
File projectFile = location.toFile();
if(projectFile.exists()){
String projectFilePath = REF.getFileAbsolutePath(projectFile);
if(fullPath.startsWith(projectFilePath)){
//the case is all ok
return fullPath;
}else{
//the case appears to be different, so, let's check if this is it...
if(fullPath.toLowerCase().startsWith(projectFilePath.toLowerCase())){
String relativePart = fullPath.substring(projectFilePath.length());
//at least the first part was correct
return projectFilePath+relativePart;
}
}
}
}
//it may not be correct, but it was the best we could do...
return fullPath;
}
/**
* Writes to the workspace a given object (in the given filename)
*/
public static void writeToWorkspaceMetadata(Object obj, String fileName) {
Bundle bundle = Platform.getBundle("org.python.pydev");
IPath path = Platform.getStateLocation( bundle );
path = path.addTrailingSeparator();
path = path.append(fileName);
try {
FileOutputStream out = new FileOutputStream(path.toFile());
REF.writeToStreamAndCloseIt(obj, out);
} catch (Exception e) {
PydevPlugin.log(e);
throw new RuntimeException(e);
}
}
/**
* Loads from the workspace metadata a given object (given the filename)
*/
public static Object readFromWorkspaceMetadata(String fileName) {
Bundle bundle = Platform.getBundle("org.python.pydev");
IPath path = Platform.getStateLocation( bundle );
path = path.addTrailingSeparator();
path = path.append(fileName);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(path.toFile());
} catch (Exception e) {
throw new RuntimeException(e);
}
return REF.readFromInputStreamAndCloseIt(new ICallback<Object, ObjectInputStream>(){
public Object call(ObjectInputStream arg) {
try{
return arg.readObject();
}catch(Exception e){
throw new RuntimeException(e);
}
}},
fileInputStream);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -