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();
}
}
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();
}
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());
}
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);
}
}
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;
}
Aggregations