Search in sources :

Example 1 with IAbstractJavaClassModule

use of org.python.pydev.ast.codecompletion.revisited.modules.IAbstractJavaClassModule in project Pydev by fabioz.

the class AbstractAdditionalDependencyInfo method updateKeysIfNeededAndSave.

/**
 * If info == null we're dealing with project info (otherwise we're dealing with interpreter info).
 *
 * The main difference is that we don't index builtin modules for projects (but maybe we should?). Still,
 * to index builtin modules we have to be a bit more careful, especially on changes (i.e.: when a builtin
 * becomes a source module and vice-versa).
 */
public void updateKeysIfNeededAndSave(PyPublicTreeMap<ModulesKey, ModulesKey> keysFound, InterpreterInfo info, IProgressMonitor monitor) {
    Map<CompleteIndexKey, CompleteIndexKey> keys = this.completeIndex.keys();
    ArrayList<ModulesKey> newKeys = new ArrayList<ModulesKey>();
    ArrayList<ModulesKey> removedKeys = new ArrayList<ModulesKey>();
    // temporary
    CompleteIndexKey tempKey = new CompleteIndexKey((ModulesKey) null);
    boolean isJython = info != null ? info.getInterpreterType() == IInterpreterManager.INTERPRETER_TYPE_JYTHON : true;
    Iterator<ModulesKey> it = keysFound.values().iterator();
    while (it.hasNext()) {
        ModulesKey next = it.next();
        if (next.file != null) {
            // Can be a .pyd or a .py
            long lastModified = FileUtils.lastModified(next.file);
            if (lastModified != 0) {
                tempKey.key = next;
                CompleteIndexKey completeIndexKey = keys.get(tempKey);
                if (completeIndexKey == null) {
                    newKeys.add(next);
                } else {
                    if (completeIndexKey.lastModified != lastModified) {
                        // Just re-add it if the time changed!
                        newKeys.add(next);
                    }
                }
            }
        } else {
            // at this point, it's always a compiled module (forced builtin), so, we can't check if it was modified (just re-add it).
            tempKey.key = next;
            CompleteIndexKey completeIndexKey = keys.get(tempKey);
            if (completeIndexKey == null) {
                // Only add if it's not there already.
                newKeys.add(next);
            }
        }
    }
    Iterator<CompleteIndexKey> it2 = keys.values().iterator();
    while (it2.hasNext()) {
        CompleteIndexKey next = it2.next();
        if (!keysFound.containsKey(next.key)) {
            removedKeys.add(next.key);
        }
    }
    boolean hasNew = newKeys.size() != 0;
    boolean hasRemoved = removedKeys.size() != 0;
    modulesAddedAndRemoved.call(new Tuple(newKeys, removedKeys));
    Set<File> ignoreFiles = new HashSet<File>();
    // Remove first!
    if (hasRemoved) {
        for (ModulesKey removedKey : removedKeys) {
            // Don't generate deltas (we'll save it in the end).
            this.removeInfoFromModule(removedKey.name, false);
        }
    }
    // Add last (a module could be removed/added).
    if (hasNew) {
        FastStringBuffer buffer = new FastStringBuffer();
        int currI = 0;
        int total = newKeys.size();
        IModuleRequestState moduleRequest = new BaseModuleRequest(true);
        for (ModulesKey newKey : newKeys) {
            currI += 1;
            if (monitor.isCanceled()) {
                return;
            }
            if (PythonPathHelper.canAddAstInfoForSourceModule(newKey)) {
                buffer.clear().append("Indexing ").append(currI).append(" of ").append(total).append(" (source module): ").append(newKey.name).append("  (").append(currI).append(" of ").append(total).append(")");
                try {
                    // Don't generate deltas (we'll save it in the end).
                    this.addAstInfo(newKey, false);
                } catch (Exception e) {
                    Log.log(e);
                }
            } else {
                if (info != null) {
                    if (isJython && ignoreFiles.contains(newKey.file)) {
                        continue;
                    }
                    buffer.clear().append("Indexing ").append(currI).append(" of ").append(total).append(" (builtin module): ").append(newKey.name);
                    monitor.setTaskName(buffer.toString());
                    IModule builtinModule = info.getModulesManager().getModule(newKey.name, info.getModulesManager().getNature(), true, moduleRequest);
                    if (builtinModule != null) {
                        if (builtinModule instanceof IAbstractJavaClassModule) {
                            if (newKey.file != null) {
                                ignoreFiles.add(newKey.file);
                            } else {
                                Log.log("Not expecting null file for java class module: " + newKey);
                            }
                            continue;
                        }
                        boolean removeFirst = keys.containsKey(new CompleteIndexKey(newKey));
                        addAstForCompiledModule(builtinModule, info, newKey, removeFirst);
                    }
                }
            }
        }
    }
    if (hasNew || hasRemoved) {
        if (DebugSettings.DEBUG_INTERPRETER_AUTO_UPDATE) {
            org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, StringUtils.format("Additional info modules. Added: %s Removed: %s", newKeys, removedKeys));
        }
        save();
    }
}
Also used : IModule(org.python.pydev.core.IModule) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) IModuleRequestState(org.python.pydev.core.IModuleRequestState) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) CompleteIndexKey(org.python.pydev.core.cache.CompleteIndexKey) ModulesKey(org.python.pydev.core.ModulesKey) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) IAbstractJavaClassModule(org.python.pydev.ast.codecompletion.revisited.modules.IAbstractJavaClassModule) HashSet(java.util.HashSet)

Example 2 with IAbstractJavaClassModule

use of org.python.pydev.ast.codecompletion.revisited.modules.IAbstractJavaClassModule in project Pydev by fabioz.

the class ItemPointer method getFileAsURI.

public URI getFileAsURI() {
    if (this.uri != null) {
        return this.uri;
    }
    // Also see org.python.pydev.editor.actions.PyOpenAction
    Object file = this.file;
    if (file instanceof File) {
        File f = (File) file;
        String filename = f.getName();
        if (// treating files without any extension!
        PythonPathHelper.isValidSourceFile(filename) || filename.indexOf('.') == -1 || (zipFilePath != null && PythonPathHelper.isValidSourceFile(zipFilePath))) {
        // Keep on going as we were going...
        } else if (definition instanceof IJavaDefinition) {
            Log.log(new RuntimeException("Not currently able to convert JavaDefinition to URI."));
            return null;
        } else {
            boolean giveError = true;
            if (definition != null && definition.module instanceof IAbstractJavaClassModule) {
                Log.log(new RuntimeException("Not currently able to convert AbstractJavaClassModule to URI."));
                return null;
            } else {
                if (FileTypesPreferences.isValidDll(filename)) {
                    if (f.exists()) {
                        // It's a pyd or dll, let's check if it was a cython module to open it...
                        File parentFile = f.getParentFile();
                        File newFile = new File(parentFile, StringUtils.stripExtension(f.getName()) + "." + "pyx");
                        if (!newFile.exists()) {
                            newFile = new File(parentFile, StringUtils.stripExtension(f.getName()) + "." + "pxd");
                        }
                        if (!newFile.exists()) {
                            newFile = new File(parentFile, StringUtils.stripExtension(f.getName()) + "." + "pxi");
                        }
                        if (newFile.exists()) {
                            giveError = false;
                            file = newFile;
                        }
                    }
                }
            }
            if (giveError) {
                return null;
            }
        }
    }
    if (zipFilePath != null) {
        Log.log(StringUtils.format("Not currently able to convert file with zip path to URI.\nFile: %s\nStart: %s\nEnd: %s\nZip path: %s\n", file, start, end, zipFilePath));
        return null;
    } else if (file instanceof IFile) {
        IFile f = (IFile) file;
        return f.getRawLocationURI();
    } else if (file instanceof IPath) {
        IPath path = (IPath) file;
        return path.toFile().toURI();
    } else if (file instanceof File) {
        return ((File) file).toURI();
    } else if (file instanceof URI) {
        return (URI) file;
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IJavaDefinition(org.python.pydev.ast.codecompletion.revisited.modules.IJavaDefinition) File(java.io.File) IFile(org.eclipse.core.resources.IFile) URI(java.net.URI) IAbstractJavaClassModule(org.python.pydev.ast.codecompletion.revisited.modules.IAbstractJavaClassModule)

Aggregations

File (java.io.File)2 IAbstractJavaClassModule (org.python.pydev.ast.codecompletion.revisited.modules.IAbstractJavaClassModule)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 IJavaDefinition (org.python.pydev.ast.codecompletion.revisited.modules.IJavaDefinition)1 BaseModuleRequest (org.python.pydev.core.BaseModuleRequest)1 IModule (org.python.pydev.core.IModule)1 IModuleRequestState (org.python.pydev.core.IModuleRequestState)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 ModulesKey (org.python.pydev.core.ModulesKey)1 CompleteIndexKey (org.python.pydev.core.cache.CompleteIndexKey)1 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1