use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class PythonSourceFolderWizard method doCreateNew.
@Override
protected IFile doCreateNew(IProgressMonitor monitor) throws CoreException {
IProject project = filePage.getValidatedProject();
String name = filePage.getValidatedName();
if (project == null || !project.exists()) {
throw new RuntimeException("The project selected does not exist in the workspace.");
}
IPythonPathNature pathNature = PythonNature.getPythonPathNature(project);
if (pathNature == null) {
IPythonNature nature = PythonNature.addNature(project, monitor, null, null, null, null, null);
pathNature = nature.getPythonPathNature();
if (pathNature == null) {
throw new RuntimeException("Unable to add the nature to the seleted project.");
}
}
IFolder folder = project.getFolder(name);
if (folder.exists()) {
Log.log("Source folder already exists. Nothing new was created");
return null;
}
folder.create(true, true, monitor);
String newPath = folder.getFullPath().toString();
String curr = pathNature.getProjectSourcePath(false);
if (curr == null) {
curr = "";
}
if (curr.endsWith("|")) {
curr = curr.substring(0, curr.length() - 1);
}
String newPathRel = PyStructureConfigHelpers.convertToProjectRelativePath(project.getFullPath().toString(), newPath);
if (curr.length() > 0) {
// there is already some path
Set<String> projectSourcePathSet = pathNature.getProjectSourcePathSet(true);
if (!projectSourcePathSet.contains(newPath)) {
// only add to the path if it doesn't already contain the new path
curr += "|" + newPathRel;
}
} else {
// there is still no other path
curr = newPathRel;
}
pathNature.setProjectSourcePath(curr);
PythonNature.getPythonNature(project).rebuildPath();
return null;
}
use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class RefactorerFindDefinition method findDefinition.
/**
* This function is used to find the definition for some token.
* It may return a list of ItemPointer because the actual definition may not be
* easy to find (so, multiple places that could be the definitions for
* the given token may be returned... and it may be up to the user to actually
* choose the best match).
* @throws BadLocationException
*
* @see org.python.pydev.ast.refactoring.IPyRefactoring#findDefinition(org.python.pydev.ast.refactoring.RefactoringRequest)
*/
public ItemPointer[] findDefinition(RefactoringRequest request) throws BadLocationException {
try {
request.getMonitor().beginTask("Find definition", 100);
List<ItemPointer> pointers = new ArrayList<ItemPointer>();
CompletionState completionState = new CompletionState();
completionState.setAcceptTypeshed(request.acceptTypeshed);
ArrayList<IDefinition> selected = new ArrayList<IDefinition>();
String[] tokenAndQual;
try {
tokenAndQual = PyRefactoringFindDefinition.findActualDefinition(request, completionState, selected);
} catch (CompletionRecursionException e1) {
Log.log(e1);
return new ItemPointer[0];
}
if (tokenAndQual == null) {
return new ItemPointer[0];
}
PyRefactoringFindDefinition.getAsPointers(pointers, selected.toArray(new Definition[0]));
if (pointers.size() == 0 && ((Boolean) request.getAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, true))) {
String lookForInterface = tokenAndQual[1];
List<IInfo> tokensEqualTo;
try {
tokensEqualTo = AdditionalProjectInterpreterInfo.getTokensEqualTo(lookForInterface, request.nature, AbstractAdditionalTokensInfo.TOP_LEVEL | AbstractAdditionalTokensInfo.INNER);
ICodeCompletionASTManager manager = request.nature.getAstManager();
if (manager == null) {
return new ItemPointer[0];
}
if (tokensEqualTo.size() > 50) {
// too many matches for that...
throw new TooManyMatchesException("Too Many matches (" + tokensEqualTo.size() + ") were found for the requested token:" + lookForInterface, tokensEqualTo.size());
}
request.communicateWork(StringUtils.format("Found: %s possible matches.", tokensEqualTo.size()));
IPythonNature nature = request.nature;
for (IInfo info : tokensEqualTo) {
AnalysisPlugin.getDefinitionFromIInfo(pointers, manager, nature, info, completionState, true, true);
request.checkCancelled();
}
} catch (MisconfigurationException e) {
Log.log(e);
return new ItemPointer[0];
}
}
request.communicateWork(StringUtils.format("Found: %s matches.", pointers.size()));
return pointers.toArray(new ItemPointer[0]);
} catch (BadLocationException e) {
throw e;
} catch (OperationCanceledException e) {
// that's ok... it was cancelled
throw e;
} finally {
request.getMonitor().done();
}
}
use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class RefactorerFindReferences method findPossibleReferences.
/**
* Find the references that may have the text we're looking for.
*
* @param request the request with the info for the find
* @return an array of IFile with the files that may have the references we're
* interested about (note that those may not actually contain the matches we're
* interested in -- it is just a helper to refine our search).
*/
public List<Tuple<List<ModulesKey>, IPythonNature>> findPossibleReferences(RefactoringRequest request) throws OperationCanceledException {
String initialName = request.qualifier;
List<Tuple<List<ModulesKey>, IPythonNature>> ret = request.getPossibleReferences(initialName);
if (ret != null) {
return ret;
}
if (FORCED_RETURN != null) {
ret = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
for (Tuple<List<ModulesKey>, IPythonNature> f : FORCED_RETURN) {
// only for testing purposes
for (ModulesKey k : f.o1) {
String object = FileUtils.getFileContents(k.file);
if (object.indexOf(request.qualifier) != -1) {
ret.add(new Tuple<List<ModulesKey>, IPythonNature>(Arrays.asList(k), f.o2));
}
}
}
return ret;
}
ret = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
try {
try {
IProject project = request.nature.getProject();
List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> infoAndNature = null;
if (project == null) {
if (request.nature instanceof SystemPythonNature) {
SystemPythonNature systemPythonNature = (SystemPythonNature) request.nature;
int interpreterType = systemPythonNature.getInterpreterType();
List<IPythonNature> naturesRelatedTo = PythonNature.getPythonNaturesRelatedTo(interpreterType);
infoAndNature = new ArrayList<Tuple<AbstractAdditionalTokensInfo, IPythonNature>>();
for (IPythonNature iPythonNature : naturesRelatedTo) {
if (iPythonNature.getProject() != null && iPythonNature.getProject().isAccessible()) {
AbstractAdditionalTokensInfo o1 = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(iPythonNature);
if (o1 != null) {
infoAndNature.add(new Tuple<AbstractAdditionalTokensInfo, IPythonNature>(o1, iPythonNature));
}
}
}
}
} else {
infoAndNature = AdditionalProjectInterpreterInfo.getAdditionalInfoAndNature(request.nature, false, true, true);
}
if (infoAndNature == null || infoAndNature.size() == 0) {
return ret;
}
// long initial = System.currentTimeMillis();
request.getMonitor().beginTask("Find possible references", infoAndNature.size());
request.getMonitor().setTaskName("Find possible references");
try {
for (Tuple<AbstractAdditionalTokensInfo, IPythonNature> tuple : infoAndNature) {
try {
SubProgressMonitor sub = new SubProgressMonitor(request.getMonitor(), 1);
request.pushMonitor(sub);
if (tuple.o1 instanceof AdditionalProjectInterpreterInfo && tuple.o2 != null) {
AdditionalProjectInterpreterInfo info = (AdditionalProjectInterpreterInfo) tuple.o1;
List<ModulesKey> modulesWithToken = info.getModulesWithToken(initialName, sub);
if (sub.isCanceled()) {
break;
}
ret.add(new Tuple<List<ModulesKey>, IPythonNature>(modulesWithToken, tuple.o2));
}
} finally {
request.popMonitor().done();
}
}
} finally {
request.getMonitor().done();
}
// System.out.println("Total: " + ((System.currentTimeMillis() - initial) / 1000.));
} catch (MisconfigurationException e) {
throw new RuntimeException(e);
}
} catch (OperationCanceledException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
request.setPossibleReferences(initialName, ret);
return ret;
}
use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class ImportsCompletionParticipant method computeConsoleCompletions.
// Console completions ---------------------------------------------------------------------------------------------
@Override
public Collection<ICompletionProposalHandle> computeConsoleCompletions(ActivationTokenAndQualifier tokenAndQual, Set<IPythonNature> naturesUsed, IScriptConsoleViewer viewer, int requestOffset) {
ArrayList<ICompletionProposalHandle> completions = new ArrayList<ICompletionProposalHandle>();
if (tokenAndQual.activationToken != null && tokenAndQual.activationToken.length() > 0) {
// we only want
return completions;
}
String qual = tokenAndQual.qualifier;
if (qual.length() >= PyCodeCompletionPreferences.getCharsForContextInsensitiveModulesCompletion() && naturesUsed != null && naturesUsed.size() > 0) {
// at least n characters required...
int qlen = qual.length();
boolean addAutoImport = AnalysisPreferences.doAutoImport(null);
for (IPythonNature nature : naturesUsed) {
fillCompletions(requestOffset, completions, qual, nature, qlen, addAutoImport, viewer);
}
}
return completions;
}
use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class AbstractManageEnvEditorAction method run.
@Override
public void run() {
try {
final IPythonNature pythonNature = edit.getPythonNature();
if (pythonNature == null) {
PyDialogHelpers.openCritical("Unable to execute pip", "The related editor does not have an associated python nature.");
return;
}
IInterpreterInfo projectInterpreter = pythonNature.getProjectInterpreter();
if (projectInterpreter == null) {
PyDialogHelpers.openCritical("Unable to execute pip", "The related editor does not have an associated interpreter.");
return;
}
doRun(pythonNature, projectInterpreter);
} catch (MisconfigurationException | PythonNatureWithoutProjectException e) {
Log.log(e);
}
}
Aggregations