use of org.python.pydev.core.IModule 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;
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class CtxParticipant method getCompletionsForIInfo.
/**
* Gets completions given a module and related info.
*/
private TokensList getCompletionsForIInfo(ICompletionState state, IPythonNature nature, IInfo iInfo) throws CompletionRecursionException {
ICompletionState copy = state.getCopy();
String path = iInfo.getPath();
String act = iInfo.getName();
if (path != null) {
act = path + "." + act;
}
copy.setActivationToken(act);
ICodeCompletionASTManager manager = nature.getAstManager();
IModule mod = manager.getModule(iInfo.getDeclaringModuleName(), nature, true, state);
if (mod != null) {
state.checkFindDefinitionMemory(mod, iInfo.getDeclaringModuleName() + "." + act);
TokensList tks = manager.getCompletionsForModule(mod, copy);
if (tks != null) {
return tks;
}
}
return null;
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class PyReferenceSearcher method prepareSearch.
/**
* Prepares for an upcoming use of {@link #search(RefactoringRequest)}. This must be called
* before a search is performed.
*
* @param request the search request.
* @throws SearchException if the AST can not be found or the definition for the
* identifier isn't valid or can't otherwise be searched.
* @throws BadLocationException
* @throws TooManyMatchesException
*/
public void prepareSearch(RefactoringRequest request) throws SearchException, TooManyMatchesException, BadLocationException {
List<IRefactorRenameProcess> processes = requestToProcesses.get(request);
// Clear the existing processes for the request
processes.clear();
ItemPointer[] pointers;
if (request.isModuleRenameRefactoringRequest()) {
IModule module = request.getModule();
pointers = new ItemPointer[] { new ItemPointer(request.file, new Location(0, 0), new Location(0, 0), new Definition(1, 1, "", null, null, module, false), null) };
} else {
SimpleNode ast = request.getAST();
if (ast == null) {
throw new SearchException("AST not generated (syntax error).");
}
IPyRefactoring pyRefactoring = AbstractPyRefactoring.getPyRefactoring();
request.communicateWork("Finding definition");
pointers = pyRefactoring.findDefinition(request);
}
if (pointers.length == 0) {
// no definition found
IRefactorRenameProcess p = RefactorProcessFactory.getRenameAnyProcess();
processes.add(p);
} else {
for (ItemPointer pointer : pointers) {
if (pointer.definition == null) {
throw new SearchException(INVALID_DEFINITION + pointer);
}
IRefactorRenameProcess p = RefactorProcessFactory.getProcess(pointer.definition, request);
if (p == null) {
throw new SearchException(INVALID_DEFINITION + pointer.definition);
}
processes.add(p);
}
}
if (processes.isEmpty()) {
throw new SearchException("The pre-conditions were not satisfied.");
}
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class AbstractASTManager method getCompletionFromFuncDefReturn.
@Override
public TokensList getCompletionFromFuncDefReturn(ICompletionState state, IModule module, IDefinition definition, boolean considerYieldTheReturnType) throws CompletionRecursionException {
TokensList ret = new TokensList();
if (!(module instanceof SourceModule)) {
return ret;
}
if (!(definition instanceof Definition)) {
return ret;
}
Definition def = (Definition) definition;
FunctionDef functionDef = (FunctionDef) def.ast;
ITypeInfo type = NodeUtils.getReturnTypeFromFuncDefAST(functionDef);
if (type != null) {
ICompletionState copy = state.getCopy();
copy.setActivationToken(type.getActTok());
try (NoExceptionCloseable x = copy.pushLookingFor(LookingFor.LOOKING_FOR_INSTANCED_VARIABLE)) {
stmtType[] body = functionDef.body;
if (body.length > 0) {
copy.setLine(body[0].beginLine - 1);
copy.setCol(body[0].beginColumn - 1);
}
IModule definitionModule = def.module;
state.checkDefinitionMemory(definitionModule, def);
TokensList tks = this.getCompletionsForModule(definitionModule, copy);
if (tks.notEmpty()) {
// TODO: This is not ideal... ideally, we'd return this info along instead of setting
// it in the token, but this may be hard as we have to touch LOTS of places for
// this information to get to the needed place.
tks.setGeneratorType(type);
ret.addAll(tks);
// Ok, resolved rtype!
return ret;
} else {
// Try to deal with some token that's not imported
List<IPyDevCompletionParticipant> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_COMPLETION);
for (IPyDevCompletionParticipant participant : participants) {
TokensList collection = participant.getCompletionsForType(copy);
if (collection != null && collection.notEmpty()) {
ret.addAll(collection);
// Ok, resolved rtype!
return ret;
}
}
}
}
}
List<Return> returns = ReturnVisitor.findReturns(functionDef);
Stream<exprType> map = returns.stream().map(r -> r.value);
if (considerYieldTheReturnType) {
List<Yield> yields = YieldVisitor.findYields(functionDef);
map = Stream.concat(map, yields.stream().map(yield -> yield.value));
}
for (Iterator<exprType> it = map.iterator(); it.hasNext(); ) {
exprType value = it.next();
if (value == null) {
continue;
}
String act = NodeUtils.getFullRepresentationString(value);
if (act == null) {
// may happen if the return we're seeing is a return without anything (keep on going to check other returns)
continue;
}
ITokenCompletionRequest request = new TokenCompletionRequest(act, def.module, state.getNature(), "", def.line - 1, def.col - 1);
TokensList tokensList = new TokensList();
ICompletionState copy = state.getCopy();
copy.setActivationToken(act);
copy.setLine(value.beginLine - 1);
copy.setCol(value.beginColumn - 1);
LookingFor lookingFor = null;
if (value instanceof Call) {
lookingFor = ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE;
}
try (NoExceptionCloseable x = state.pushLookingFor(lookingFor)) {
IModule definitionModule = def.module;
state.checkDefinitionMemory(definitionModule, def);
try {
PyCodeCompletion.doTokenCompletion(request, this, tokensList, act, copy);
} catch (MisconfigurationException | IOException | CoreException | PythonNatureWithoutProjectException e) {
throw new RuntimeException(e);
}
if (lookingFor != null) {
tokensList.setLookingFor(lookingFor);
}
ret.addAll(tokensList);
}
}
return ret;
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class AbstractASTManager method internalGenerateGetCompletionsForModule.
/**
* This method should only be accessed from the public getCompletionsForModule (which caches the result).
*/
private TokensList internalGenerateGetCompletionsForModule(IModule module, ICompletionState state, boolean searchSameLevelMods, boolean lookForArgumentCompletion) throws CompletionRecursionException {
if (DebugSettings.DEBUG_CODE_COMPLETION) {
log("internalGenerateGetCompletionsForModule", module, state);
}
state.checkMaxTimeForCompletion();
TokensList importedModules = new TokensList();
ILocalScope localScope = null;
int line = state.getLine();
int col = state.getCol();
if (state.getLocalImportsGotten() == false) {
// in the first analyzed module, we have to get the local imports too.
state.setLocalImportsGotten(true);
if (module != null && line >= 0) {
localScope = module.getLocalScope(line, col);
if (localScope != null) {
importedModules.addAll(localScope.getLocalImportedModules(line + 1, col + 1, module.getName()));
}
}
}
TokensList builtinsCompletions = getBuiltinsCompletions(state);
if (builtinsCompletions != null) {
return builtinsCompletions;
}
String act = state.getActivationToken();
int parI = act.indexOf('(');
if (parI != -1) {
state.setFullActivationToken(act);
act = ParsingUtils.removeCalls(act);
state.setActivationToken(act);
state.setLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
}
if (module != null) {
// get the tokens (global, imported and wild imported)
TokensList globalTokens = module.getGlobalTokens();
TokensList tokenImportedModules = module.getTokenImportedModules();
importedModules.addAll(tokenImportedModules);
state.setTokenImportedModules(importedModules);
TokensList wildImportedModules = module.getWildImportedModules();
// now, lets check if this is actually a module that is an __init__ (if so, we have to get all
// the other .py files as modules that are in the same level as the __init__)
Set<IToken> initial = new HashSet<IToken>();
if (searchSameLevelMods) {
// or only folders -- not classes -- in java).
if (module.isPackage()) {
HashSet<IToken> gotten = new HashSet<IToken>();
// the module also decides how to get its submodules
getAbsoluteImportTokens(module.getPackageFolderName(), gotten, IToken.TYPE_IMPORT, true, null, false);
for (IToken token : gotten) {
if (token.getRepresentation().equals("__init__") == false) {
initial.add(token);
}
}
}
}
if (state.getActivationToken().length() == 0) {
TokensList completions = getGlobalCompletions(globalTokens, importedModules, wildImportedModules, state, module);
// now find the locals for the module
if (line >= 0) {
TokensList localTokens = module.getLocalTokens(line, col, localScope);
completions.addAll(localTokens);
}
// just add all that are in the same level if it was an __init__
completions.addAll(new TokensList(initial.toArray(EMPTY_ITOKEN_ARRAY)));
return completions;
} else {
// ok, we have a token, find it and get its completions.
// first check if the token is a module... if it is, get the completions for that module.
TokensList tokens = findTokensOnImportedMods(importedModules, state, module);
if (tokens != null && tokens.size() > 0) {
return decorateWithLocal(tokens, localScope, state);
}
// if it is an __init__, modules on the same level are treated as local tokens
if (searchSameLevelMods) {
tokens = searchOnSameLevelMods(initial, state);
if (tokens != null && tokens.size() > 0) {
return decorateWithLocal(tokens, localScope, state);
}
}
// wild imports: recursively go and get those completions and see if any matches it.
for (IterTokenEntry entry : wildImportedModules) {
IToken name = entry.getToken();
IModule mod = getModule(name.getAsRelativeImport(module.getName()), state.getNature(), false, // relative (for wild imports this is ok... only a module can be used in wild imports)
state);
if (mod == null) {
// absolute
mod = getModule(name.getOriginalRep(), state.getNature(), false, state);
}
if (mod != null) {
state.checkFindModuleCompletionsMemory(mod, state.getActivationToken());
TokensList completionsForModule = getCompletionsForModule(mod, state);
if (completionsForModule.size() > 0) {
return decorateWithLocal(completionsForModule, localScope, state);
}
} else {
// "Module not found:" + name.getRepresentation()
}
}
// it was not a module (would have returned already), so, try to get the completions for a global token defined.
tokens = module.getGlobalTokens(state, this);
if (tokens.size() > 0) {
return decorateWithLocal(tokens, localScope, state);
}
// If it was still not found, go to builtins.
IModule builtinsMod = getBuiltinMod(state.getNature(), state);
if (builtinsMod != null && builtinsMod != module) {
tokens = getCompletionsForModule(builtinsMod, state);
if (tokens.notEmpty()) {
if (tokens.getFirst().getRepresentation().equals("ERROR:") == false) {
return decorateWithLocal(tokens, localScope, state);
}
}
}
// Let's check if we have to unpack it...
try (NoExceptionCloseable x = state.pushLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE)) {
if (state.getActivationToken().endsWith(".__getitem__")) {
String activationToken = state.getActivationToken();
String compoundActivationToken = activationToken.substring(0, activationToken.length() - 12);
TokensList ret = getCompletionsUnpackingObject(module, state.getCopyWithActTok(compoundActivationToken), localScope, new UnpackInfo());
if (ret != null && ret.size() > 0) {
return ret;
}
} else {
if (localScope != null) {
ISimpleNode foundAtASTNode = localScope.getFoundAtASTNode();
if (foundAtASTNode instanceof For) {
For for1 = (For) foundAtASTNode;
// case where we may have to unpack some iteration
// e.g.: for a, b in x.items():
TokensList ret = getCompletionsUnpackingForLoop(module, state, localScope, for1);
if (ret != null && ret.size() > 0) {
return ret;
}
// Note: we don't bail out here because it's possible that the user has
// added the type on the context (because on a for unpacking either we find it
// when checking the for loop unpack or the user has to explicitly give
// us a hint).
} else if (foundAtASTNode instanceof With) {
With with = (With) foundAtASTNode;
TokensList ret = getCompletionsUnpackingWith(module, state, localScope, with);
if (ret != null && ret.size() > 0) {
return ret;
}
}
}
}
}
if (lookForArgumentCompletion && localScope != null) {
TokensList ret = getCompletionsFromTokenInLocalScope(module, state, searchSameLevelMods, lookForArgumentCompletion, localScope);
if (ret != null && ret.size() > 0) {
return ret;
}
}
// nothing worked so far, so, let's look for an assignment...
return getAssignCompletions(module, state, lookForArgumentCompletion, localScope);
}
} else {
Log.log("Module passed in is null!!");
}
return new TokensList();
}
Aggregations