Search in sources :

Example 1 with BaseModuleRequest

use of org.python.pydev.core.BaseModuleRequest 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 BaseModuleRequest

use of org.python.pydev.core.BaseModuleRequest in project Pydev by fabioz.

the class DjangoNewProjectPage method getNextPage.

@Override
public IWizardPage getNextPage() {
    String projectType = this.getProjectType();
    IInterpreterManager interpreterManager;
    if (IPythonNature.Versions.ALL_JYTHON_VERSIONS.contains(projectType)) {
        interpreterManager = InterpreterManagersAPI.getJythonInterpreterManager();
    } else if (IPythonNature.Versions.ALL_IRONPYTHON_VERSIONS.contains(projectType)) {
        interpreterManager = InterpreterManagersAPI.getIronpythonInterpreterManager();
    } else {
        // if others fail, consider it python
        interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
    }
    try {
        String projectInterpreter = this.getProjectInterpreter();
        IInterpreterInfo interpreterInfo;
        if (projectInterpreter.toLowerCase().equals("default")) {
            interpreterInfo = interpreterManager.getDefaultInterpreterInfo(false);
        } else {
            interpreterInfo = interpreterManager.getInterpreterInfo(projectInterpreter, new NullProgressMonitor());
        }
        IModule module = interpreterInfo.getModulesManager().getModuleWithoutBuiltins("django.core.__init__", null, false, new BaseModuleRequest(false));
        if (module == null) {
            DjangoNotAvailableWizardPage page = new DjangoNotAvailableWizardPage("Django not available", interpreterInfo);
            page.setWizard(this.getWizard());
            return page;
        }
    } catch (MisconfigurationException e) {
        ErrorWizardPage page = new ErrorWizardPage("Unexpected error.", "An unexpected error happened:\n" + e.getMessage());
        page.setWizard(this.getWizard());
        return page;
    }
    return super.getNextPage();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 3 with BaseModuleRequest

use of org.python.pydev.core.BaseModuleRequest in project Pydev by fabioz.

the class ForceCodeAnalysisOnTree method forceCodeAnalysisOnFiles.

public static void forceCodeAnalysisOnFiles(PythonNature nature, IProgressMonitor monitor, List<IFile> filesToVisit, Set<IFile> filesVisited, List<IExternalCodeAnalysisVisitor> externalVisitors) {
    if (nature == null) {
        return;
    }
    AnalysisBuilderVisitor visitor = new AnalysisBuilderVisitor();
    visitor.visitingWillStart(new NullProgressMonitor(), false, null);
    FastStringBuffer buf = new FastStringBuffer();
    for (IFile f : filesToVisit) {
        if (monitor.isCanceled()) {
            break;
        }
        if (filesVisited.contains(f)) {
            continue;
        }
        filesVisited.add(f);
        monitor.setTaskName(buf.clear().append("Scheduling: ").append(f.getName()).toString());
        IDocument doc = FileUtilsFileBuffer.getDocFromResource(f);
        visitor.memo = new VisitorMemo();
        visitor.memo.put(PyDevBuilderVisitor.IS_FULL_BUILD, false);
        long documentTime = System.currentTimeMillis();
        visitor.memo.put(PyDevBuilderVisitor.DOCUMENT_TIME, documentTime);
        String moduleName;
        try {
            moduleName = nature.resolveModule(f);
        } catch (MisconfigurationException e) {
            Log.log(e);
            continue;
        }
        if (moduleName == null) {
            continue;
        }
        AnalysisBuilderVisitor.setModuleNameInCache(visitor.memo, f, moduleName);
        if (!PythonPathHelper.isValidSourceFile(f)) {
            AnalysisRunner.deleteMarkers(f, true);
            continue;
        }
        IModule module = nature.getAstManager().getModule(moduleName, nature, true, new BaseModuleRequest(false));
        if (module == null) {
            Log.log(IStatus.WARNING, "Unable to get module: " + moduleName + " for resource: " + f, null);
            continue;
        }
        visitor.doVisitChangedResource(nature, f, doc, null, module, new NullProgressMonitor(), true, AnalysisBuilderRunnable.ANALYSIS_CAUSE_PARSER, documentTime, false, externalVisitors);
    }
    visitor.visitingEnded(new NullProgressMonitor());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) IFile(org.eclipse.core.resources.IFile) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) MisconfigurationException(org.python.pydev.core.MisconfigurationException) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) VisitorMemo(org.python.pydev.ast.builder.VisitorMemo) AnalysisBuilderVisitor(com.python.pydev.analysis.additionalinfo.builders.AnalysisBuilderVisitor) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with BaseModuleRequest

use of org.python.pydev.core.BaseModuleRequest in project Pydev by fabioz.

the class ForcedLibGroup method calculateChildren.

@Override
protected void calculateChildren() throws MisconfigurationException {
    SystemModulesManager m = (SystemModulesManager) this.interpreterInfo.getModulesManager();
    AbstractModule builtinModule = m.getBuiltinModule(forcedLib, true, new BaseModuleRequest(true));
    TokensList globalTokens = builtinModule.getGlobalTokens();
    ArrayList<LeafElement> lst = new ArrayList<LeafElement>();
    for (IterTokenEntry entry : globalTokens) {
        IToken iToken = entry.getToken();
        lst.add(new LeafElement(this, iToken.getRepresentation()));
    }
    Collections.sort(lst, new Comparator<LeafElement>() {

        @Override
        public int compare(LeafElement o1, LeafElement o2) {
            return o1.toString().compareTo(o2.toString());
        }
    });
    for (LeafElement leafElement : lst) {
        addChild(leafElement);
    }
}
Also used : IToken(org.python.pydev.core.IToken) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) ArrayList(java.util.ArrayList) IterTokenEntry(org.python.pydev.core.IterTokenEntry) SystemModulesManager(org.python.pydev.ast.codecompletion.revisited.SystemModulesManager) TokensList(org.python.pydev.core.TokensList) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule)

Example 5 with BaseModuleRequest

use of org.python.pydev.core.BaseModuleRequest in project Pydev by fabioz.

the class PyRenameEntryPoint method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException, OperationCanceledException {
    // Clear (will be filled now).
    allChanges.clear();
    fRequest.pushMonitor(pm);
    RefactoringStatus status = new RefactoringStatus();
    try {
        if (this.fRequest.isModuleRenameRefactoringRequest() && this.fRequest.getSimpleResourceRename() && this.fRequest.getIFileResource() != null) {
            // Ok, simple resource change
            return status;
        }
        final Map<IPath, Tuple<TextChange, MultiTextEdit>> fileToChangeInfo = new HashMap<IPath, Tuple<TextChange, MultiTextEdit>>();
        final Set<IResource> affectedResources = new HashSet<>();
        for (RefactoringRequest request : this.fRequest.getRequests()) {
            if (request.isModuleRenameRefactoringRequest()) {
                boolean searchInit = true;
                IModule module = request.getTargetNature().getAstManager().getModule(request.inputName, request.getTargetNature(), !searchInit, // i.e.: the parameter is dontSearchInit (so, pass in negative form to search)
                new BaseModuleRequest(request.acceptTypeshed));
                if (module != null) {
                    String partName = module.getName().endsWith(".__init__") ? "package" : "module";
                    status.addFatalError("Unable to perform module rename because a " + partName + " named: " + request.inputName + " already exists.");
                    return status;
                }
            }
            List<IRefactorRenameProcess> processes = pyReferenceSearcher.getProcesses(request);
            if (processes == null || processes.size() == 0) {
                status.addFatalError("Refactoring Process not defined: the refactoring cycle did not complete correctly.");
                return status;
            }
            request.getMonitor().beginTask("Finding references", processes.size());
            try {
                pyReferenceSearcher.search(request);
            } catch (PyReferenceSearcher.SearchException e) {
                status.addFatalError(e.getMessage());
                return status;
            }
            TextEditCreation textEditCreation = new TextEditCreation(request.qualifier, request.inputName, request.getModule().getName(), request.getDoc(), processes, status, request.getIFile()) {

                @Override
                protected Tuple<TextChange, MultiTextEdit> getTextFileChange(IFile workspaceFile, IDocument doc) {
                    if (workspaceFile == null) {
                        // used for tests
                        TextChange docChange = PyDocumentChange.create("Current module: " + moduleName, doc);
                        MultiTextEdit rootEdit = new MultiTextEdit();
                        docChange.setEdit(rootEdit);
                        docChange.setKeepPreviewEdits(true);
                        allChanges.add(docChange);
                        return new Tuple<TextChange, MultiTextEdit>(docChange, rootEdit);
                    }
                    IPath fullPath = workspaceFile.getFullPath();
                    Tuple<TextChange, MultiTextEdit> tuple = fileToChangeInfo.get(fullPath);
                    if (tuple == null) {
                        TextFileChange docChange = new SynchronizedTextFileChange("RenameChange: " + inputName, workspaceFile);
                        docChange.setTextType("py");
                        MultiTextEdit rootEdit = new MultiTextEdit();
                        docChange.setEdit(rootEdit);
                        docChange.setKeepPreviewEdits(true);
                        allChanges.add(docChange);
                        affectedResources.add(workspaceFile);
                        tuple = new Tuple<TextChange, MultiTextEdit>(docChange, rootEdit);
                        fileToChangeInfo.put(fullPath, tuple);
                    }
                    return tuple;
                }

                @Override
                protected PyRenameResourceChange createResourceChange(IResource resourceToRename, String newName, RefactoringRequest request) {
                    IContainer target = null;
                    if (request instanceof ModuleRenameRefactoringRequest) {
                        target = ((ModuleRenameRefactoringRequest) request).getTarget();
                    }
                    PyRenameResourceChange change = new PyRenameResourceChange(resourceToRename, initialName, newName, StringUtils.format("Changing %s to %s", initialName, inputName), target);
                    allChanges.add(change);
                    affectedResources.add(resourceToRename);
                    return change;
                }
            };
            textEditCreation.fillRefactoringChangeObject(request, context);
            if (status.hasFatalError() || request.getMonitor().isCanceled()) {
                return status;
            }
        }
        checkResourcesToBeChanged(affectedResources, context, status);
    } catch (OperationCanceledException e) {
    // OK
    } finally {
        fRequest.popMonitor().done();
    }
    return status;
}
Also used : IModule(org.python.pydev.core.IModule) IFile(org.eclipse.core.resources.IFile) PyRenameResourceChange(com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange) HashMap(java.util.HashMap) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) IPyRefactoringRequest(org.python.pydev.ast.refactoring.IPyRefactoringRequest) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) IRefactorRenameProcess(com.python.pydev.analysis.refactoring.wizards.IRefactorRenameProcess) IContainer(org.eclipse.core.resources.IContainer) HashSet(java.util.HashSet) IPath(org.eclipse.core.runtime.IPath) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) Tuple(org.python.pydev.shared_core.structure.Tuple) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

BaseModuleRequest (org.python.pydev.core.BaseModuleRequest)21 IModule (org.python.pydev.core.IModule)19 IFile (org.eclipse.core.resources.IFile)7 File (java.io.File)6 HashSet (java.util.HashSet)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Tuple (org.python.pydev.shared_core.structure.Tuple)5 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)4 IProjectModulesManager (org.python.pydev.core.IProjectModulesManager)4 IPythonNature (org.python.pydev.core.IPythonNature)4 ModulesKey (org.python.pydev.core.ModulesKey)4 PythonNature (org.python.pydev.plugin.nature.PythonNature)4 ArrayList (java.util.ArrayList)3 IProject (org.eclipse.core.resources.IProject)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 IDocument (org.eclipse.jface.text.IDocument)3 CompiledModule (org.python.pydev.ast.codecompletion.revisited.modules.CompiledModule)3 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)3 MisconfigurationException (org.python.pydev.core.MisconfigurationException)3 PyRenameEntryPoint (com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint)2