use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class PydevConsoleInterpreter method getNatureAndRelatedNatures.
private Set<IPythonNature> getNatureAndRelatedNatures() {
Set<IPythonNature> ret = new HashSet<IPythonNature>();
for (IPythonNature iPythonNature : this.initialNatures) {
try {
ICodeCompletionASTManager astManager = iPythonNature.getAstManager();
if (astManager == null) {
continue;
}
IModulesManager modulesManager = astManager.getModulesManager();
if (modulesManager == null) {
continue;
}
IModulesManager[] managersInvolved = modulesManager.getManagersInvolved(true);
for (IModulesManager iModulesManager : managersInvolved) {
ret.add(iModulesManager.getNature());
}
} catch (Exception e) {
Log.log(e);
}
}
return ret;
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class PydevConsoleInterpreter method getCompletions.
@Override
@SuppressWarnings("unchecked")
public ICompletionProposalHandle[] getCompletions(IScriptConsoleViewer viewer, String commandLine, int position, int offset, int whatToShow) throws Exception {
final String text = commandLine.substring(0, position);
ActivationTokenAndQualifier tokenAndQual = PySelection.getActivationTokenAndQualifier(new Document(text), text.length(), true, false);
String textForCompletionInConsole = PySelection.getTextForCompletionInConsole(new Document(text), text.length());
if (PySelection.isCompletionForLiteralNumber(tokenAndQual.activationToken)) {
// suppress completions that would be invalid
return new ICompletionProposalHandle[0];
}
// Code-completion for imports
ImportInfo importsTipper = ImportsSelection.getImportsTipperStr(text, false);
Set<IPythonNature> natureAndRelatedNatures = getNatureAndRelatedNatures();
if (importsTipper.importsTipperStr.length() != 0) {
importsTipper.importsTipperStr = importsTipper.importsTipperStr.trim();
Set<IToken> tokens = new TreeSet<IToken>();
final boolean onlyGetDirectModules = true;
// Check all the natures.
for (final IPythonNature nature : natureAndRelatedNatures) {
ICodeCompletionASTManager astManager = nature.getAstManager();
TokensList importTokens = astManager.getCompletionsForImport(importsTipper, new ICompletionRequest() {
@Override
public IPythonNature getNature() {
return nature;
}
@Override
public File getEditorFile() {
return null;
}
@Override
public IModule getModule() throws MisconfigurationException {
return null;
}
}, onlyGetDirectModules);
// only get all modules for the 1st one we analyze (no need to get on the others)
for (IterTokenEntry entry : importTokens) {
IToken iToken = entry.getToken();
tokens.add(iToken);
}
}
int qlen = tokenAndQual.qualifier.length();
List<ICompletionProposalHandle> ret = new ArrayList<ICompletionProposalHandle>(tokens.size());
Iterator<IToken> it = tokens.iterator();
for (int i = 0; i < tokens.size(); i++) {
IToken t = it.next();
int replacementOffset = offset - qlen;
String representation = t.getRepresentation();
if (representation.startsWith(tokenAndQual.qualifier)) {
ret.add(CompletionProposalFactory.get().createPyLinkedModeCompletionProposal(representation, replacementOffset, qlen, representation.length(), t, null, null, IPyCompletionProposal.PRIORITY_DEFAULT, IPyCompletionProposal.ON_APPLY_DEFAULT, "", null));
}
}
return ret.toArray(new ICompletionProposalHandle[ret.size()]);
}
// END Code-completion for imports
String actTok = tokenAndQual.activationToken;
if (tokenAndQual.qualifier != null && tokenAndQual.qualifier.length() > 0) {
if (actTok.length() > 0 && actTok.charAt(actTok.length() - 1) != '.') {
actTok += '.';
}
actTok += tokenAndQual.qualifier;
}
boolean showOnlyTemplates = whatToShow == AbstractCompletionProcessorWithCycling.SHOW_ONLY_TEMPLATES;
boolean showForTabCompletion = whatToShow == AbstractCompletionProcessorWithCycling.SHOW_FOR_TAB_COMPLETIONS;
// simple completions (clients)
ArrayList<ICompletionProposalHandle> results = new ArrayList<ICompletionProposalHandle>();
if (!showForTabCompletion) {
for (ISimpleAssistParticipant2 participant : simpleParticipants) {
results.addAll(participant.computeConsoleProposals(tokenAndQual.activationToken, tokenAndQual.qualifier, offset));
}
}
ProposalsComparator proposalsComparator = new ProposalsComparator(tokenAndQual.qualifier, null);
ArrayList<ICompletionProposalHandle> results2 = new ArrayList<ICompletionProposalHandle>();
if (!showOnlyTemplates) {
// shell completions
if (consoleCommunication != null) {
ICompletionProposalHandle[] consoleCompletions = consoleCommunication.getCompletions(text, textForCompletionInConsole, offset, showForTabCompletion);
// If we're only showing ipython completions, then short-circuit the rest
if (showForTabCompletion) {
return consoleCompletions;
}
results2.addAll(Arrays.asList(consoleCompletions));
}
}
if (tokenAndQual.activationToken.length() == 0) {
// templates (only if we have no activation token)
IPyTemplateCompletionProcessor pyTemplateCompletionProcessor = new PyTemplateCompletionProcessor();
pyTemplateCompletionProcessor.addTemplateProposals((ITextViewer) viewer, offset, results2);
}
Collections.sort(results2, proposalsComparator);
ArrayList<ICompletionProposalHandle> results3 = new ArrayList<ICompletionProposalHandle>();
if (!showOnlyTemplates) {
// other participants
List<Object> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_COMPLETION);
for (Object participant : participants) {
if (participant instanceof IPyDevCompletionParticipant2) {
IPyDevCompletionParticipant2 participant2 = (IPyDevCompletionParticipant2) participant;
results3.addAll(participant2.computeConsoleCompletions(tokenAndQual, natureAndRelatedNatures, viewer, offset));
}
}
Collections.sort(results3, proposalsComparator);
}
results.addAll(results2);
results.addAll(results3);
return results.toArray(new ICompletionProposalHandle[results.size()]);
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class NatureGroup method calculateChildren.
@Override
protected void calculateChildren() throws MisconfigurationException {
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager == null) {
addLeaf("AST manager == null (should happen only in the plugin initialization) -- skipping other checks.");
return;
}
IModulesManager projectModulesManager = astManager.getModulesManager();
if (projectModulesManager == null) {
addLeaf("Modules manager == null (should happen only in the plugin initialization) -- skipping other checks.");
return;
}
PythonPathHelper pythonPathHelper = (PythonPathHelper) projectModulesManager.getPythonPathHelper();
if (pythonPathHelper == null) {
addLeaf("PythonPathHelper == null (should happen only in the plugin initialization) -- skipping other checks.");
return;
}
List<String> pythonpath = pythonPathHelper.getPythonpath();
for (String s : pythonpath) {
addLeaf("PYTHONPATH: " + s);
}
HashSet<ModulesKey> expectedModuleNames = new HashSet<ModulesKey>();
for (String string : pythonpath) {
File file = new File(string);
if (file.isDirectory()) {
// TODO: Handle zip file modules!
Collection<PyFileInfo> modulesBelow = PythonPathHelper.getModulesBelow(file, new NullProgressMonitor(), pythonpath).getFoundPyFileInfos();
for (PyFileInfo fileInfo : modulesBelow) {
File moduleFile = fileInfo.getFile();
String modName = pythonPathHelper.resolveModule(FileUtils.getFileAbsolutePath(moduleFile), true, nature.getProject());
if (modName != null) {
expectedModuleNames.add(new ModulesKey(modName, moduleFile));
} else {
if (PythonPathHelper.isValidModuleLastPart(StringUtils.stripExtension((moduleFile.getName())))) {
addLeaf(StringUtils.format("Unable to resolve module: %s (gotten null module name)", moduleFile));
}
}
}
} else {
if (!file.exists()) {
addLeaf(StringUtils.format("File %s is referenced in the pythonpath but does not exist.", file));
} else {
addLeaf(org.python.pydev.shared_core.string.StringUtils.format("File %s not handled (TODO: Fix zip files support in the viewer).", file));
}
}
}
IntegrityInfo info = new IntegrityInfo();
info.nature = nature;
ModulesKey[] onlyDirectModules = projectModulesManager.getOnlyDirectModules();
TreeSet<ModulesKey> inModulesManager = new TreeSet<ModulesKey>(Arrays.asList(onlyDirectModules));
Set<String> allAdditionalInfoModuleNames = new TreeSet<String>();
List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> additionalInfoAndNature = AdditionalProjectInterpreterInfo.getAdditionalInfoAndNature(nature, false, false, false);
AbstractAdditionalTokensInfo additionalProjectInfo;
if (additionalInfoAndNature.size() == 0) {
addChild(new LeafElement(this, "No additional infos found (1 expected) -- skipping other checks."));
return;
} else {
if (additionalInfoAndNature.size() > 1) {
addChild(new LeafElement(this, StringUtils.format("%s additional infos found (only 1 expected) -- continuing checks but analysis may be wrong.", additionalInfoAndNature.size())));
}
additionalProjectInfo = additionalInfoAndNature.get(0).o1;
allAdditionalInfoModuleNames.addAll(additionalProjectInfo.getAllModulesWithTokens());
}
for (ModulesKey key : inModulesManager) {
if (!expectedModuleNames.contains(key)) {
info.modulesNotInDisk.add(key);
addChild(new LeafElement(this, StringUtils.format("%s exists in memory but not in the disk.", key)));
}
}
ModulesKey tempKey = new ModulesKey(null, null);
for (String s : allAdditionalInfoModuleNames) {
tempKey.name = s;
if (!expectedModuleNames.contains(tempKey)) {
info.additionalModulesNotInDisk.add(s);
addChild(new LeafElement(this, StringUtils.format("%s exists in the additional info but not in the disk.", s)));
}
}
for (ModulesKey key : expectedModuleNames) {
boolean isInModulesManager = inModulesManager.contains(key);
if (!isInModulesManager) {
info.modulesNotInMemory.add(key);
addChild(new LeafElement(this, StringUtils.format("%s exists in the disk but not in memory.", key)));
}
if (!allAdditionalInfoModuleNames.contains(key.name)) {
try {
AbstractModule mod = AbstractModule.createModule(key.name, key.file, info.nature, true);
if (!(mod instanceof SourceModule)) {
continue;
}
SourceModule module = (SourceModule) mod;
if (module == null || module.getAst() == null) {
addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
} else {
try {
Iterator<ASTEntry> innerEntriesForAST = AbstractAdditionalDependencyInfo.getInnerEntriesForAST(module.getAst()).o2;
if (innerEntriesForAST.hasNext()) {
info.moduleNotInAdditionalInfo.add(module);
addChild(new LeafElement(this, StringUtils.format("The additional info index of the module: %s is not updated.", key.name)));
}
} catch (Exception e) {
addChild(new LeafElement(this, StringUtils.format("Unexpected error happened on: %s - %s: %s", key.name, key.file, e.getMessage())));
}
}
} catch (IOException e) {
// OK, it cannot be parsed, so, we cannot generate its info
addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
}
}
}
// modules manager
if (info.modulesNotInDisk.size() > 0) {
for (ModulesKey m : info.modulesNotInDisk) {
addChild(new LeafElement(this, StringUtils.format("FIX: Removing from modules manager: %s", m)));
}
projectModulesManager.removeModules(info.modulesNotInDisk);
}
for (ModulesKey key : info.modulesNotInMemory) {
addChild(new LeafElement(this, "FIX: Adding to modules manager: " + key));
projectModulesManager.addModule(key);
}
// additional info
for (String s : info.additionalModulesNotInDisk) {
addChild(new LeafElement(this, StringUtils.format("FIX: Removing from additional info: %s", s)));
additionalProjectInfo.removeInfoFromModule(s, true);
}
for (SourceModule mod : info.moduleNotInAdditionalInfo) {
addChild(new LeafElement(this, StringUtils.format("FIX: Adding to additional info: %s", mod.getName())));
additionalProjectInfo.addAstInfo(mod.getAst(), mod.getModulesKey(), true);
}
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class PythonCompletionWithBuiltinsTest method testRecursion.
public void testRecursion() throws FileNotFoundException, Exception, CompletionRecursionException {
String file = TestDependent.TEST_PYSRC_TESTING_LOC + "testrec3/rec.py";
String strDoc = "RuntimeError.";
File f = new File(file);
try {
ICodeCompletionASTManager astManager = nature.getAstManager();
ICompletionState state = CompletionStateFactory.getEmptyCompletionState("RuntimeError", nature, new CompletionCache());
IModule module = AbstractASTManager.createModule(f, new Document(FileUtils.getFileContents(f)), nature);
astManager.getCompletionsForModule(module, state, true, true);
} catch (CompletionRecursionException e) {
// that's ok... we're asking for it here...
}
requestCompl(f, strDoc, strDoc.length(), -1, new String[] { "__doc__", "__getitem__()", "__init__()", "__str__()" });
}
use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.
the class CtxParticipant method findItemPointerFromPyTestFixture.
private ItemPointer findItemPointerFromPyTestFixture(IPythonNature nature, ICompletionState completionCache, String fixtureName) {
try {
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager != null) {
List<IInfo> tokensEqualTo = AdditionalProjectInterpreterInfo.getTokensEqualTo(fixtureName, nature, AdditionalProjectInterpreterInfo.TOP_LEVEL);
for (IInfo iInfo : tokensEqualTo) {
List<ItemPointer> pointers = new LinkedListWarningOnSlowOperations<>();
AnalysisPlugin.getDefinitionFromIInfo(pointers, astManager, nature, iInfo, completionCache, true, true);
for (ItemPointer itemPointer : pointers) {
if (itemPointer.definition.ast instanceof FunctionDef) {
FunctionDef functionDef = (FunctionDef) itemPointer.definition.ast;
if (functionDef.decs != null) {
for (decoratorsType dec : functionDef.decs) {
String decoratorFuncName = NodeUtils.getRepresentationString(dec.func);
if (decoratorFuncName != null) {
if (FIXTURE_PATTERN.matcher(decoratorFuncName).find() || YIELD_FIXTURE_PATTERN.matcher(decoratorFuncName).find()) {
return itemPointer;
}
}
}
}
}
}
}
}
} catch (MisconfigurationException e1) {
Log.log(e1);
}
return null;
}
Aggregations