use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class AnalysisBuilderVisitor method visitChangedResource.
public void visitChangedResource(final IResource resource, final ICallback0<IDocument> document, final IProgressMonitor monitor, boolean forceAnalysis) {
// we may need to 'force' the analysis when a module is renamed, because the first message we receive is
// a 'delete' and after that an 'add' -- which is later mapped to this method, so, if we don't have info
// on the module we should analyze it because it is 'probably' a rename.
final PythonNature nature = getPythonNature(resource);
if (nature == null) {
return;
}
// Put things from the memo to final variables as we might need them later on and we cannot get them from
// the memo later.
final String moduleName;
final SourceModule[] module = new SourceModule[] { null };
final IDocument doc;
doc = document.call();
if (doc == null) {
return;
}
try {
moduleName = getModuleName(resource, nature);
} catch (MisconfigurationException e) {
Log.log(e);
return;
}
// depending on the level of analysis we have to do, we'll decide whether we want
// to make the full parse (slower) or the definitions parse (faster but only with info
// related to the definitions)
ICallback<IModule, Integer> moduleCallback = new ICallback<IModule, Integer>() {
@Override
public IModule call(Integer arg) {
if (arg == IAnalysisBuilderRunnable.FULL_MODULE) {
if (module[0] != null) {
return module[0];
} else {
try {
module[0] = getSourceModule(resource, doc, nature);
} catch (MisconfigurationException e1) {
throw new RuntimeException(e1);
}
if (module[0] != null) {
return module[0];
}
try {
module[0] = createSoureModule(resource, doc, moduleName);
} catch (MisconfigurationException e) {
throw new RuntimeException(e);
}
return module[0];
}
} else if (arg == IAnalysisBuilderRunnable.DEFINITIONS_MODULE) {
if (DebugSettings.DEBUG_ANALYSIS_REQUESTS) {
org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "PyDevBuilderPrefPage.getAnalyzeOnlyActiveEditor()");
}
IFile f = (IFile) resource;
IPath location = f.getLocation();
if (location != null) {
String file = location.toOSString();
File f2 = new File(file);
return new SourceModule(moduleName, f2, FastDefinitionsParser.parse(doc.get(), moduleName, f2), null, nature);
}
return null;
} else {
throw new RuntimeException("Unexpected parameter: " + arg);
}
}
};
long documentTime = this.getDocumentTime();
if (documentTime == -1) {
Log.log("Warning: The document time in the visitor is -1. Changing for current time.");
documentTime = System.currentTimeMillis();
}
doVisitChangedResource(nature, resource, doc, moduleCallback, null, monitor, forceAnalysis, AnalysisBuilderRunnable.ANALYSIS_CAUSE_BUILDER, documentTime, false);
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class AnalysisPlugin method getDefinitionFromIInfo.
/**
* @param pointers the list where the pointers will be added (if null, a new one will be created).
* @param manager the manager to be used to get the definition.
* @param nature the nature to be used.
* @param info the info that we are looking for.
* @param force whether we should force getting the ItemPointer if it's not readily available.
* @return whether we actually tried to look for a completion or just bailed out due to force being == false.
*/
public static boolean getDefinitionFromIInfo(List<ItemPointer> pointers, ICodeCompletionASTManager manager, IPythonNature nature, IInfo info, ICompletionState completionCache, boolean requireIDefinition, boolean force) {
if (pointers == null) {
pointers = new ArrayList<>();
}
if (!requireIDefinition) {
String file = info.getFile();
if (file != null) {
File f = new File(file);
int line = info.getLine();
int col = info.getCol();
if (line > 0 && col > 0) {
// 0 is invalid.
ItemPointer itemPointer = new ItemPointer(f, new Location(line - 1, col - 1), new Location(line - 1, col - 1), null, null, f.toURI());
pointers.add(itemPointer);
return true;
}
}
}
if (!force) {
return false;
}
IModule mod;
String tok;
mod = manager.getModule(info.getDeclaringModuleName(), nature, true, completionCache);
if (mod != null) {
if (info.getType() == IInfo.MOD_IMPORT_TYPE) {
Definition definition = new Definition(1, 1, "", null, null, mod);
PyRefactoringFindDefinition.getAsPointers(pointers, new Definition[] { definition });
return true;
}
// ok, now that we found the module, we have to get the actual definition
tok = "";
String path = info.getPath();
if (path != null && path.length() > 0) {
tok = path + ".";
}
tok += info.getName();
try {
IDefinition[] definitions = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(tok, nature, completionCache), -1, -1, nature);
if ((definitions == null || definitions.length == 0) && path != null && path.length() > 0) {
// this can happen if we have something as an attribute in the path:
// class Bar(object):
// def __init__(self):
// self.xxx = 10
//
// so, we'de get a find definition for Bar.__init__.xxx which is something we won't find
// for now, let's simply return a match in the correct context (although the correct way of doing
// it would be analyzing that context to find the match)
IDefinition[] contextDefinitions = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(path, nature, completionCache), -1, -1, nature);
if (contextDefinitions != null && contextDefinitions.length > 0) {
for (IDefinition iDefinition : contextDefinitions) {
if (iDefinition instanceof Definition) {
Definition definition = (Definition) iDefinition;
if (definition.ast instanceof FunctionDef) {
FunctionDef functionDef = (FunctionDef) definition.ast;
if (functionDef.args != null) {
exprType[] args = functionDef.args.args;
if (args != null && args.length > 0) {
// I.e.: only analyze functions with at least one argument (for self or cls).
Map<String, SourceToken> repToTokenWithArgs = new HashMap<String, SourceToken>();
HeuristicFindAttrs heuristicFindAttrs = new HeuristicFindAttrs(HeuristicFindAttrs.WHITIN_ANY, HeuristicFindAttrs.IN_ASSIGN, "", definition.module.getName(), null, repToTokenWithArgs, nature);
heuristicFindAttrs.visitFunctionDef(functionDef);
List<IToken> tokens = heuristicFindAttrs.getTokens();
List<IDefinition> newDefs = new ArrayList<>();
for (IToken iToken : tokens) {
if (info.getName().equals(iToken.getRepresentation())) {
newDefs.add(new Definition(iToken, definition.scope, definition.module));
}
}
definitions = newDefs.toArray(new IDefinition[newDefs.size()]);
}
}
}
}
}
}
}
PyRefactoringFindDefinition.getAsPointers(pointers, definitions);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return true;
}
use of org.python.pydev.core.IModule 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.IModule in project Pydev by fabioz.
the class TddQuickFixParticipant method addProps.
@Override
public void addProps(MarkerAnnotationAndPosition markerAnnotation, IAnalysisPreferences analysisPreferences, String line, PySelection ps, int offset, IPythonNature nature, PyEdit edit, List<ICompletionProposalHandle> props) throws BadLocationException, CoreException {
if (nature == null) {
return;
}
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager == null) {
return;
}
if (markerAnnotation.position == null) {
return;
}
IMarker marker = markerAnnotation.markerAnnotation.getMarker();
Integer id = (Integer) marker.getAttribute(AnalysisRunner.PYDEV_ANALYSIS_TYPE);
int start = markerAnnotation.position.offset;
int end = start + markerAnnotation.position.length;
ps.setSelection(start, end);
String markerContents;
try {
markerContents = ps.getSelectedText();
} catch (Exception e1) {
// Selection may be wrong.
return;
}
IDocument doc = ps.getDoc();
List<String> parametersAfterCall = ps.getParametersAfterCall(end);
switch(id) {
case IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE:
addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall);
addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall);
break;
case IAnalysisPreferences.TYPE_UNDEFINED_IMPORT_VARIABLE:
// Say we had something as:
// import sys
// sys.Bar
// in which case 'Bar' is undefined
// in this situation, the activationTokenAndQual would be "sys." and "Bar"
// and we want to get the definition for "sys"
String[] activationTokenAndQual = ps.getActivationTokenAndQualifier(true);
if (activationTokenAndQual[0].endsWith(".")) {
ArrayList<IDefinition> selected = findDefinitions(nature, edit, start - 2, doc);
for (IDefinition iDefinition : selected) {
IModule module = iDefinition.getModule();
if (module.getFile() != null) {
Definition definition = (Definition) iDefinition;
File file = module.getFile();
if (definition.ast == null) {
// if we have no ast in the definition, it means the module itself was found (global scope)
// Add option to create class at the given module!
addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
} else if (definition.ast instanceof ClassDef) {
ClassDef classDef = (ClassDef) definition.ast;
// Ok, we should create a field or method in this case (accessing a classmethod or staticmethod)
PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();
String className = NodeUtils.getNameFromNameTok(classDef.name);
pyCreateMethod.setCreateInClass(className);
pyCreateMethod.setCreateAs(PyCreateMethodOrField.CLASSMETHOD);
addCreateClassmethodOption(ps, edit, props, markerContents, parametersAfterCall, pyCreateMethod, file, className);
}
}
}
}
break;
case IAnalysisPreferences.TYPE_UNRESOLVED_IMPORT:
// This case is the following: from other_module4 import Foo
// with 'Foo' being undefined.
// So, we have to suggest creating a Foo class/method in other_module4
PyImportsHandling importsHandling = new PyImportsHandling(ps.getDoc(), false);
int offsetLine = ps.getLineOfOffset(start);
String selectedText = ps.getSelectedText();
Tuple<IModule, String> found = null;
String foundFromImportStr = null;
boolean isImportFrom = false;
OUT: for (ImportHandle handle : importsHandling) {
if (handle.startFoundLine == offsetLine || handle.endFoundLine == offsetLine || (handle.startFoundLine < offsetLine && handle.endFoundLine > offsetLine)) {
List<ImportHandleInfo> importInfo = handle.getImportInfo();
for (ImportHandleInfo importHandleInfo : importInfo) {
String fromImportStr = importHandleInfo.getFromImportStr();
List<String> importedStr = importHandleInfo.getImportedStr();
for (String imported : importedStr) {
if (selectedText.equals(imported)) {
if (fromImportStr != null) {
foundFromImportStr = fromImportStr + "." + imported;
isImportFrom = true;
} else {
// if fromImportStr == null, it's not a from xxx import yyy (i.e.: simple import)
foundFromImportStr = imported;
}
try {
String currentModule = nature.resolveModule(edit.getEditorFile());
ICompletionState state = CompletionStateFactory.getEmptyCompletionState(nature, new CompletionCache());
found = nature.getAstManager().findModule(foundFromImportStr, currentModule, state, new SourceModule(currentModule, edit.getEditorFile(), edit.getAST(), null, nature));
} catch (Exception e) {
Log.log(e);
}
break OUT;
}
}
}
break OUT;
}
}
boolean addOptionToCreateClassOrMethod = isImportFrom;
if (found != null && found.o1 != null) {
// or just create a class or method at the end.
if (found.o1 instanceof SourceModule) {
// if all was found, there's nothing left to create.
if (found.o2 != null && found.o2.length() > 0) {
SourceModule sourceModule = (SourceModule) found.o1;
File file = sourceModule.getFile();
if (found.o2.indexOf('.') != -1) {
// We have to create some intermediary structure.
if (!addOptionToCreateClassOrMethod) {
// Cannot create class or method from the info (only the module structure).
if (sourceModule.getName().endsWith(".__init__")) {
File f = getFileStructure(file.getParentFile(), found.o2);
addCreateModuleOption(ps, edit, props, markerContents, f);
}
} else {
// Ok, the leaf may be a class or method.
if (sourceModule.getName().endsWith(".__init__")) {
String moduleName = FullRepIterable.getWithoutLastPart(sourceModule.getName());
String withoutLastPart = FullRepIterable.getWithoutLastPart(found.o2);
moduleName += "." + withoutLastPart;
String classOrMethodName = FullRepIterable.getLastPart(found.o2);
File f = getFileStructure(file.getParentFile(), withoutLastPart);
addCreateClassInNewModuleOption(ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
addCreateMethodInNewModuleOption(ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
}
}
} else {
// Ok, it's all there, we just have to create the leaf.
if (!addOptionToCreateClassOrMethod || sourceModule.getName().endsWith(".__init__")) {
// Cannot create class or method from the info (only the module structure).
if (sourceModule.getName().endsWith(".__init__")) {
File f = new File(file.getParent(), found.o2 + FileTypesPreferences.getDefaultDottedPythonExtension());
addCreateModuleOption(ps, edit, props, markerContents, f);
}
} else {
// Ok, the leaf may be a class or method.
addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
}
}
}
}
} else if (foundFromImportStr != null) {
// We couldn't find anything there, so, we have to create the modules structure as needed and
// maybe create a class or module at the end (but only if it's an import from).
// Ok, it's all there, we just have to create the leaf.
// Discover the source folder where we should create the structure.
File editorFile = edit.getEditorFile();
String onlyProjectPythonPathStr = nature.getPythonPathNature().getOnlyProjectPythonPathStr(false);
List<String> split = StringUtils.splitAndRemoveEmptyTrimmed(onlyProjectPythonPathStr, '|');
for (int i = 0; i < split.size(); i++) {
String fullPath = FileUtils.getFileAbsolutePath(split.get(i));
fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
split.set(i, fullPath);
}
HashSet<String> projectSourcePath = new HashSet<String>(split);
if (projectSourcePath.size() == 0) {
// No source folder for editor... this shouldn't happen (code analysis wouldn't even run on it).
return;
}
String fullPath = FileUtils.getFileAbsolutePath(editorFile);
fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
String foundSourceFolderFullPath = null;
if (projectSourcePath.size() == 1) {
foundSourceFolderFullPath = projectSourcePath.iterator().next();
} else {
for (String string : projectSourcePath) {
if (fullPath.startsWith(string)) {
// Use this as the source folder
foundSourceFolderFullPath = string;
break;
}
}
}
if (foundSourceFolderFullPath != null) {
if (!addOptionToCreateClassOrMethod) {
// Cannot create class or method from the info (only the module structure).
File f = getFileStructure(new File(foundSourceFolderFullPath), foundFromImportStr);
addCreateModuleOption(ps, edit, props, foundFromImportStr, f);
} else {
// Ok, the leaf may be a class or method.
String moduleName = FullRepIterable.getWithoutLastPart(foundFromImportStr);
File file = getFileStructure(new File(foundSourceFolderFullPath), moduleName);
String lastPart = FullRepIterable.getLastPart(foundFromImportStr);
addCreateClassInNewModuleOption(ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
addCreateMethodInNewModuleOption(ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
}
}
}
break;
}
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class ArgumentsChecker method checkNameFound.
/*default*/
void checkNameFound(Call callNode, SourceToken sourceToken) throws Exception {
FunctionDef functionDefinitionReferenced;
boolean callingBoundMethod = false;
SimpleNode ast = sourceToken.getAst();
if (ast instanceof FunctionDef) {
functionDefinitionReferenced = (FunctionDef) ast;
analyzeCallAndFunctionMatch(callNode, functionDefinitionReferenced, sourceToken, callingBoundMethod);
} else if (ast instanceof ClassDef) {
ClassDef classDef = (ClassDef) ast;
SimpleNode initNode = defToConsideredInit.get(classDef);
callingBoundMethod = true;
if (initNode == null) {
String className = ((NameTok) classDef.name).id;
Definition foundDef = sourceToken.getDefinition();
IModule mod = this.current;
if (foundDef != null) {
mod = foundDef.module;
}
SimpleNode n = NodeUtils.getNodeFromPath(classDef, "__init__");
if (n instanceof FunctionDef) {
initNode = n;
} else {
IDefinition[] definition = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(className + ".__init__", this.nature, this.completionCache), -1, -1, this.nature);
for (IDefinition iDefinition : definition) {
Definition d = (Definition) iDefinition;
if (d.ast instanceof FunctionDef) {
initNode = d.ast;
defToConsideredInit.put(classDef, initNode);
break;
}
}
}
}
if (initNode instanceof FunctionDef) {
functionDefinitionReferenced = (FunctionDef) initNode;
analyzeCallAndFunctionMatch(callNode, functionDefinitionReferenced, sourceToken, callingBoundMethod);
}
}
}
Aggregations