use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.
the class RefactoringRenameTestBase method setUp.
/**
* In the setUp, it initializes the files in the refactoring project
* @see com.python.pydev.analysis.refactoring.refactorer.refactorings.renamelocal.RefactoringLocalTestBase#setUp()
*/
@Override
public void setUp() throws Exception {
super.setUp();
if (filesInRefactoringProject == null) {
filesInRefactoringProject = PyFileListing.getPyFilesBelow(new File(TestDependent.TEST_COM_REFACTORING_PYSRC_LOC), new NullProgressMonitor(), true).getFoundPyFileInfos();
ArrayList<Tuple<List<ModulesKey>, IPythonNature>> iFiles = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
List<ModulesKey> modules = new ArrayList<ModulesKey>();
iFiles.add(new Tuple<List<ModulesKey>, IPythonNature>(modules, natureRefactoring));
FastStringBuffer tempBuf = new FastStringBuffer();
for (PyFileInfo info : filesInRefactoringProject) {
File f = info.getFile();
String modName = info.getModuleName(tempBuf);
ModulesKey modulesKey = new ModulesKey(modName, f);
modules.add(modulesKey);
SourceModule mod = (SourceModule) AbstractModule.createModule(modName, f, natureRefactoring, true);
// also create the additional info so that it can be used for finds
AbstractAdditionalTokensInfo additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(natureRefactoring);
additionalInfo.addAstInfo(mod.getAst(), modulesKey, false);
}
// RefactorerFindReferences.FORCED_RETURN = iFiles;
}
setUpConfigWorkspaceFiles();
}
use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.
the class GlobalsTwoPanelElementSelector2 method fillContentProvider.
/**
* This is the place where we put all the info in the content provider. Note that here we must add
* ALL the info -- later, we'll filter it based on the active working set.
*/
@Override
protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
if (itemsFilter instanceof InfoFilter) {
if (progressMonitor != null) {
progressMonitor.beginTask("Searching...", this.additionalInfo.size());
}
for (AbstractAdditionalTokensInfo additionalInfo : this.additionalInfo) {
if (progressMonitor != null) {
if (progressMonitor.isCanceled()) {
return;
} else {
progressMonitor.worked(1);
}
}
// no duplicates
Collection<IInfo> allTokens = new HashSet<IInfo>(additionalInfo.getAllTokens());
for (IInfo iInfo : allTokens) {
contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, iInfo), itemsFilter);
}
// Also show to the user the modules available as globals (2.2.3)
IModulesManager modulesManager = null;
try {
if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
IProject project = projectInterpreterInfo.getProject();
PythonNature nature = PythonNature.getPythonNature(project);
if (nature != null) {
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager != null) {
modulesManager = astManager.getModulesManager();
}
}
} else if (additionalInfo instanceof AdditionalSystemInterpreterInfo) {
AdditionalSystemInterpreterInfo systemInterpreterInfo = (AdditionalSystemInterpreterInfo) additionalInfo;
IInterpreterInfo defaultInterpreterInfo = systemInterpreterInfo.getManager().getDefaultInterpreterInfo(false);
modulesManager = defaultInterpreterInfo.getModulesManager();
}
} catch (Throwable e) {
Log.log(e);
}
if (modulesManager != null) {
SortedMap<ModulesKey, ModulesKey> allDirectModulesStartingWith = modulesManager.getAllDirectModulesStartingWith("");
Collection<ModulesKey> values = allDirectModulesStartingWith.values();
for (ModulesKey modulesKey : values) {
contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, new ModInfo(modulesKey.name, modulesManager.getNature(), modulesKey.file != null ? modulesKey.file.toString() : null, 1, 1)), itemsFilter);
}
}
}
}
if (progressMonitor != null) {
progressMonitor.done();
}
}
use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.
the class PyOrganizeImports method organizeImports.
@SuppressWarnings("unchecked")
private void organizeImports(PyEdit edit, final IDocument doc, IFile f, PySelection ps) throws MisconfigurationException, PythonNatureWithoutProjectException {
DocumentRewriteSession session = null;
String endLineDelim = ps.getEndLineDelim();
List<IOrganizeImports> participants = null;
if (f == null && !automatic) {
// organizing single file ...
// let's see if someone wants to make a better implementation in another plugin...
participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_ORGANIZE_IMPORTS);
for (IOrganizeImports organizeImports : participants) {
if (!organizeImports.beforePerformArrangeImports(ps, edit, f)) {
return;
}
}
}
String fileContents = doc.get();
if (fileContents.contains("isort:skip_file") || fileContents.length() == 0) {
return;
}
IAdaptable projectAdaptable = edit != null ? edit : f;
String indentStr = edit != null ? edit.getIndentPrefs().getIndentationString() : DefaultIndentPrefs.get(f).getIndentationString();
session = TextSelectionUtils.startWrite(doc);
try {
// Important: the remove and later update have to be done in the same session (since the remove
// will just remove some names and he actual perform will remove the remaining if needed).
// i.e.: from a import b <-- b will be removed by the OrganizeImportsFixesUnused and the
// from a will be removed in the performArrangeImports later on.
boolean removeUnusedImports = false;
if (!automatic) {
// Only go through the removal of unused imports if it's manually activated (not on automatic mode).
removeUnusedImports = ImportsPreferencesPage.getDeleteUnusedImports(projectAdaptable);
if (removeUnusedImports) {
new OrganizeImportsFixesUnused().beforePerformArrangeImports(ps, edit, f);
}
}
Set<String> knownThirdParty = new HashSet<String>();
// isort itself already has a reasonable stdLib, so, don't do our own.
String importEngine = ImportsPreferencesPage.getImportEngine(projectAdaptable);
if (edit != null) {
IPythonNature pythonNature = edit.getPythonNature();
if (pythonNature != null) {
IInterpreterInfo projectInterpreter = pythonNature.getProjectInterpreter();
ISystemModulesManager modulesManager = projectInterpreter.getModulesManager();
ModulesKey[] onlyDirectModules = modulesManager.getOnlyDirectModules();
Set<String> stdLib = new HashSet<>();
for (ModulesKey modulesKey : onlyDirectModules) {
if (modulesKey.file == null) {
int i = modulesKey.name.indexOf('.');
String name;
if (i < 0) {
name = modulesKey.name;
} else {
name = modulesKey.name.substring(0, i);
}
// Add all names to std lib
stdLib.add(name);
}
}
for (ModulesKey modulesKey : onlyDirectModules) {
int i = modulesKey.name.indexOf('.');
String name;
if (i < 0) {
name = modulesKey.name;
} else {
name = modulesKey.name.substring(0, i);
}
// Consider all in site-packages to be third party.
if (modulesKey.file != null && modulesKey.file.toString().contains("site-packages")) {
stdLib.remove(name);
knownThirdParty.add(name);
}
}
}
}
switch(importEngine) {
case ImportsPreferencesPage.IMPORT_ENGINE_ISORT:
if (fileContents.length() > 0) {
File targetFile = edit != null ? edit.getEditorFile() : null;
if (targetFile == null) {
if (f != null) {
IPath location = f.getLocation();
if (location != null) {
targetFile = location.toFile();
}
}
}
String isortResult = JythonModules.makeISort(fileContents, targetFile, knownThirdParty);
if (isortResult != null) {
try {
DocUtils.updateDocRangeWithContents(doc, fileContents, isortResult.toString());
} catch (Exception e) {
Log.log(StringUtils.format("Error trying to apply isort result. Curr doc:\n>>>%s\n<<<.\nNew doc:\\n>>>%s\\n<<<.", fileContents, isortResult.toString()), e);
}
}
}
break;
case ImportsPreferencesPage.IMPORT_ENGINE_REGULAR_SORT:
performArrangeImports(doc, removeUnusedImports, endLineDelim, indentStr, automatic, edit);
break;
default:
// case ImportsPreferencesPage.IMPORT_ENGINE_PEP_8:
if (f == null) {
f = edit.getIFile();
}
IProject p = f != null ? f.getProject() : null;
pep8PerformArrangeImports(doc, removeUnusedImports, endLineDelim, p, indentStr, automatic, edit);
break;
}
if (participants != null) {
for (IOrganizeImports organizeImports : participants) {
organizeImports.afterPerformArrangeImports(ps, edit);
}
}
} finally {
TextSelectionUtils.endWrite(doc, session);
}
}
use of org.python.pydev.core.ModulesKey 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.ModulesKey in project Pydev by fabioz.
the class AbstractASTManager method getAbsoluteImportTokens.
/**
* @param moduleToGetTokensFrom the string that represents the token from where we are getting the imports
* @param set the set where the tokens should be added
* @param importInfo if null, only the 1st element of the module will be added, otherwise, it'll check the info
* to see if it should add only the 1st element of the module or the complete module (e.g.: add only xml or
* xml.dom and other submodules too)
*/
public void getAbsoluteImportTokens(String moduleToGetTokensFrom, Set<IToken> inputOutput, int type, boolean onlyFilesOnSameLevel, ImportInfo importInfo, boolean onlyGetDirectModules) {
// boolean getSubModules = false;
// if(importInfo != null){
// //we only want to get submodules if we're in:
// //from xxx
// //import xxx
// //
// //We do NOT want to get it on:
// //from xxx import yyy
// if(importInfo.hasFromSubstring != importInfo.hasImportSubstring){
// getSubModules = true;
// }
// }
HashMap<String, IToken> temp = new HashMap<String, IToken>();
SortedMap<ModulesKey, ModulesKey> modulesStartingWith;
if (onlyGetDirectModules) {
modulesStartingWith = modulesManager.getAllDirectModulesStartingWith(moduleToGetTokensFrom);
} else {
modulesStartingWith = modulesManager.getAllModulesStartingWith(moduleToGetTokensFrom);
}
Iterator<ModulesKey> itModules = modulesStartingWith.keySet().iterator();
while (itModules.hasNext()) {
ModulesKey key = itModules.next();
String element = key.name;
// if (element.startsWith(moduleToGetTokensFrom)) { we don't check that anymore because we get all the modules starting with it already
if (onlyFilesOnSameLevel && key.file != null && key.file.isDirectory()) {
// we only want those that are in the same directory, and not in other directories...
continue;
}
element = element.substring(moduleToGetTokensFrom.length());
// we only check this if we only want file modules (in
if (onlyFilesOnSameLevel && StringUtils.countChars('.', element) > 1) {
continue;
}
boolean goForIt = false;
// if we have xml token (not using the qualifier here)
if (moduleToGetTokensFrom.length() != 0) {
if (element.length() > 0 && element.charAt(0) == ('.')) {
element = element.substring(1);
goForIt = true;
}
} else {
goForIt = true;
}
if (element.length() > 0 && goForIt) {
List<String> splitted = StringUtils.dotSplit(element);
if (splitted.size() > 0) {
String strToAdd;
strToAdd = splitted.get(0);
// if(!getSubModules){
// }else{
// if(element.endsWith(".__init__")){
// strToAdd = element.substring(0, element.length()-9);
// }else{
// strToAdd = element;
// }
// }
// this is the completion
temp.put(strToAdd, new ConcreteToken(strToAdd, "", "", moduleToGetTokensFrom, type, modulesManager.getNature()));
}
}
// }
}
inputOutput.addAll(temp.values());
}
Aggregations