use of org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter in project Pydev by fabioz.
the class CtxParticipant method getCompletionsForMethodParameter.
/**
* IPyDevCompletionParticipant
* @throws CompletionRecursionException
*/
@Override
public TokensList getCompletionsForMethodParameter(ICompletionState state, ILocalScope localScope, TokensList interfaceForLocal) throws CompletionRecursionException {
ArrayList<IToken> ret = new ArrayList<IToken>();
String qual = state.getQualifier();
String activationToken = state.getActivationToken();
FastStack<ISimpleNode> scopeStack = localScope.getScopeStack();
if (!scopeStack.empty()) {
Object peek = scopeStack.peek();
if (peek instanceof FunctionDef) {
FunctionDef testFuncDef = (FunctionDef) peek;
String representationString = NodeUtils.getRepresentationString(testFuncDef);
if (representationString != null && representationString.startsWith("test")) {
ICodeCompletionASTManager astManager = state.getNature().getAstManager();
if (astManager != null) {
ItemPointer itemPointer = findItemPointerFromPyTestFixture(state.getNature(), state, activationToken);
if (itemPointer != null) {
TokensList completionsFromItemPointer = getCompletionsFromItemPointer(state, astManager, itemPointer);
if (completionsFromItemPointer != null && completionsFromItemPointer.notEmpty()) {
return completionsFromItemPointer;
}
}
}
}
}
}
if (qual.length() >= PyCodeCompletionPreferences.getCharsForContextInsensitiveGlobalTokensCompletion()) {
// at least n characters
// if we have a parameter, do code-completion with all available tokens, since we don't know what's the type which
// may actually be received
boolean useSubstringMatchInCodeCompletion = PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion();
List<IInfo> tokensStartingWith;
if (useSubstringMatchInCodeCompletion) {
IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(useSubstringMatchInCodeCompletion, qual);
try {
tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith("", state.getNature(), AbstractAdditionalTokensInfo.INNER);
} catch (MisconfigurationException e) {
Log.log(e);
return new TokensList(ret);
}
for (IInfo info : tokensStartingWith) {
if (nameFilter.acceptName(info.getName())) {
ret.add(new SourceToken(null, info.getName(), null, null, info.getDeclaringModuleName(), info.getType(), info.getNature()));
}
}
} else {
try {
tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith(qual, state.getNature(), AbstractAdditionalTokensInfo.INNER);
} catch (MisconfigurationException e) {
Log.log(e);
return new TokensList(ret);
}
for (IInfo info : tokensStartingWith) {
ret.add(new SourceToken(null, info.getName(), null, null, info.getDeclaringModuleName(), info.getType(), info.getNature()));
}
}
}
return new TokensList(ret);
}
use of org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter in project Pydev by fabioz.
the class PyCodeCompletion method getCodeCompletionProposals.
@Override
public TokensOrProposalsList getCodeCompletionProposals(CompletionRequest request) throws CoreException, BadLocationException, IOException, MisconfigurationException, PythonNatureWithoutProjectException {
if (request.getPySelection().getCursorLineContents().trim().startsWith("#")) {
// this may happen if the context is still not correctly computed in python
return new PyStringCodeCompletion().getCodeCompletionProposals(request);
}
if (PySelection.isCompletionForLiteralNumber(request.getActivationToken())) {
// suppress completions that would be invalid
return new TokensOrProposalsList();
}
if (DebugSettings.DEBUG_CODE_COMPLETION) {
org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Starting getCodeCompletionProposals");
org.python.pydev.shared_core.log.ToLogFile.addLogLevel();
org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Request:" + request);
}
ArrayList<ICompletionProposalHandle> ret = new ArrayList<ICompletionProposalHandle>();
// let's see if we should do a code-completion in the current scope...
// this engine does not work 'correctly' in the default scope on:
// - class definitions - after 'class' and before '('
// - method definitions - after 'def' and before '('
PySelection ps = request.getPySelection();
int lineCtx = ps.isRightAfterDeclarationInLine();
if (lineCtx != PySelection.DECLARATION_NONE) {
if (lineCtx == PySelection.DECLARATION_METHOD) {
createOverrideCodeCompletions(request, ret, ps);
}
request.showTemplates = false;
return new TokensOrProposalsList(ret);
}
try {
IPythonNature nature = request.nature;
checkPythonNature(nature);
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager == null) {
// we're probably still loading it.
return new TokensOrProposalsList(ret);
}
// list of Object[], IToken or ICompletionProposalHandle
TokensOrProposalsList tokensList = new TokensOrProposalsList();
String trimmed = request.activationToken.replace('.', ' ').trim();
ImportInfo importsTipper = getImportsTipperStr(request);
int line = request.doc.getLineOfOffset(request.documentOffset);
IRegion region = request.doc.getLineInformation(line);
ICompletionState state = new CompletionState(line, request.documentOffset - region.getOffset(), null, request.nature, request.qualifier);
state.setCancelMonitor(request.getCancelMonitor());
state.setIsInCalltip(request.isInCalltip);
Map<String, IterTokenEntry> alreadyChecked = new HashMap<>();
boolean importsTip = false;
if (request.activationToken.startsWith("super()")) {
createSuperCodeCompletions(request, tokensList, ps);
} else if (importsTipper.importsTipperStr.length() != 0) {
// code completion in imports
// if found after (, but in an import, it is not a calltip!
request.isInCalltip = false;
// if found after (, but in an import, it is not a calltip!
request.isInMethodKeywordParam = false;
importsTip = doImportCompletion(request, astManager, tokensList, importsTipper);
} else if (trimmed.length() > 0 && request.activationToken.indexOf('.') != -1) {
// code completion for a token
if (false) {
// disabled for now.
fillTokensWithJediCompletions(request, request.getPySelection(), request.nature, astManager, tokensList);
} else {
TokensList tokenCompletions = new TokensList();
doTokenCompletion(request, astManager, tokenCompletions, trimmed, state);
tokensList.addAll(tokenCompletions);
}
handleKeywordParam(request, line, alreadyChecked);
} else {
// go to globals
if (request.isInCalltip) {
// # SomeCall(|<- here)
state.setLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
}
doGlobalsCompletion(request, astManager, tokensList, state);
// At this point, after doing the globals completion, we may also need to check if we need to show
// keyword parameters to the user.
handleKeywordParam(request, line, alreadyChecked);
}
List<Object> analyzedTokens = null;
if (request.qualifier.length() >= PyCodeCompletionPreferences.getArgumentsDeepAnalysisNChars()) {
// this can take some time on the analysis, so, let's let the user choose on how many chars does he
// want to do the analysis...
state.pushFindResolveImportMemoryCtx();
try {
IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(request.useSubstringMatchInCodeCompletion, request.qualifier);
int i = 0;
analyzedTokens = new ArrayList<Object>();
// Note: later on we'll clear the tokensList and re-add the tokens on alreadyChecked.
for (Iterator<IterEntry> it = tokensList.iterator(); it.hasNext(); ) {
i++;
if (i > 10000) {
// tokensList.size(), request));
break;
}
IterEntry entry = it.next();
Object o = entry.object;
if (!(o instanceof IToken)) {
// Don't check things that are not tokens
analyzedTokens.add(o);
} else {
IToken initialToken = (IToken) o;
IToken token = initialToken;
String strRep = token.getRepresentation();
IterTokenEntry iterTokenEntry = alreadyChecked.get(strRep);
IToken prev;
if (iterTokenEntry == null) {
prev = null;
} else {
prev = iterTokenEntry.getToken();
}
if (prev != null) {
if (prev.getArgs().length() != 0) {
// we already have a version with args... just keep going
continue;
}
}
if (!nameFilter.acceptName(strRep)) {
// just re-add it if we're going to actually use it (depending on the qualifier)
continue;
}
IModule current = request.getModule();
while (token.isImportFrom()) {
if (token.getArgs().length() > 0) {
// if we already have the args, there's also no reason to do it (that's what we'll do here)
break;
}
ICompletionState s = state.getCopyForResolveImportWithActTok(token.getRepresentation());
s.checkFindResolveImportMemory(token);
ImmutableTuple<IModule, IToken> modTok = astManager.resolveImport(s, token, current);
IToken token2 = modTok.o2;
current = modTok.o1;
if (token2 != null && initialToken != token2) {
String args = token2.getArgs();
if (args.length() > 0) {
// put it into the map (may override previous if it didn't have args)
initialToken.setArgs(args);
initialToken.setDocStr(token2.getDocStr());
if (initialToken instanceof SourceToken && token2 instanceof SourceToken) {
SourceToken initialSourceToken = (SourceToken) initialToken;
SourceToken token2SourceToken = (SourceToken) token2;
initialSourceToken.setAst(token2SourceToken.getAst());
}
break;
}
if (token2 == null || (token2.equals(token) && token2.getArgs().equals(token.getArgs()) && token2.getParentPackage().equals(token.getParentPackage()))) {
break;
}
token = token2;
} else {
break;
}
}
// Completions of B should have B with the 'a' argument.
if (token instanceof SourceToken) {
SourceToken sourceToken = (SourceToken) token;
if (sourceToken.getAst() instanceof ClassDef && token.getArgs().length() == 0) {
ClassDef classDef = (ClassDef) sourceToken.getAst();
if (classDef.bases != null && classDef.bases.length > 0) {
String parentPackage = sourceToken.getParentPackage();
if (parentPackage != null) {
IModule module;
if (parentPackage.equals(current.getName())) {
module = current;
} else {
module = astManager.getModule(parentPackage, nature, true, state);
}
if (module != null) {
if (module instanceof SourceModule) {
SourceModule sourceModule = (SourceModule) module;
OUT: for (int j = 0; j < classDef.bases.length; j++) {
try {
LookingFor lookingFor = state.getLookingFor();
TokensList completions;
try {
ClassDefTokensExtractor classTokensExtractor = new ClassDefTokensExtractor(classDef, sourceModule, state);
completions = classTokensExtractor.getCompletionsForBase(astManager, classDef.bases[j]);
} finally {
// Completions at this point shouldn't change the state of what we were looking for.
state.setLookingFor(lookingFor, true);
}
if (completions != null && completions.size() > 0) {
for (IterTokenEntry entry1 : completions) {
IToken comp = entry1.getToken();
if ("__init__".equals(comp.getRepresentation())) {
if (comp.getArgs().length() > 0) {
initialToken.setArgs(comp.getArgs());
}
break OUT;
}
}
}
} catch (CompletionRecursionException e) {
// Ignore (just don't get the args).
}
}
}
}
}
}
}
}
alreadyChecked.put(strRep, new IterTokenEntry(initialToken, entry.lookingFor));
}
}
} finally {
state.popFindResolveImportMemoryCtx();
}
}
if (analyzedTokens == null) {
tokensList.addAll(new TokensListMixedLookingFor(alreadyChecked.values()));
} else {
tokensList = new TokensOrProposalsList(analyzedTokens.toArray(new Object[0]));
tokensList.addAll(new TokensListMixedLookingFor(alreadyChecked.values()));
}
changeItokenToCompletionPropostal(request, ret, tokensList, importsTip, state);
} catch (CompletionRecursionException e) {
if (onCompletionRecursionException != null) {
onCompletionRecursionException.call(e);
}
if (DebugSettings.DEBUG_CODE_COMPLETION) {
org.python.pydev.shared_core.log.ToLogFile.toLogFile(e);
}
// PydevPlugin.log(e);
// ret.add(new CompletionProposal("",request.documentOffset,0,0,null,e.getMessage(), null,null));
}
if (DebugSettings.DEBUG_CODE_COMPLETION) {
org.python.pydev.shared_core.log.ToLogFile.remLogLevel();
org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Finished completion. Returned:" + ret.size() + " completions.\r\n");
}
return new TokensOrProposalsList(ret);
}
use of org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter in project Pydev by fabioz.
the class CtxParticipant method getThem.
// Editor completions ----------------------------------------------------------------------------------------------
private Collection<ICompletionProposalHandle> getThem(CompletionRequest request, ICompletionState state, boolean addAutoImport) throws MisconfigurationException {
List<ICompletionProposalHandle> completions = new ArrayList<>();
if (request.isInCalltip) {
return completions;
}
HashSet<String> importedNames = getImportedNames(state);
String qual = request.qualifier;
if (qual.length() >= PyCodeCompletionPreferences.getCharsForContextInsensitiveGlobalTokensCompletion()) {
// at least n characters required...
IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(request.useSubstringMatchInCodeCompletion, qual);
String initialModule = request.resolveModule();
List<IInfo> tokensStartingWith;
if (request.useSubstringMatchInCodeCompletion) {
tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith("", request.nature, AbstractAdditionalTokensInfo.TOP_LEVEL);
} else {
tokensStartingWith = AdditionalProjectInterpreterInfo.getTokensStartingWith(qual, request.nature, AbstractAdditionalTokensInfo.TOP_LEVEL);
}
FastStringBuffer realImportRep = new FastStringBuffer();
FastStringBuffer displayString = new FastStringBuffer();
FastStringBuffer tempBuf = new FastStringBuffer();
IAdaptable projectAdaptable = request.getNature() != null ? request.getNature().getProject() : null;
boolean doIgnoreImportsStartingWithUnder = AnalysisPreferences.doIgnoreImportsStartingWithUnder(projectAdaptable);
for (IInfo info : tokensStartingWith) {
// there always must be a declaringModuleName
String declaringModuleName = info.getDeclaringModuleName();
if (initialModule != null && declaringModuleName != null) {
if (initialModule.equals(declaringModuleName) || "builtins".equals(declaringModuleName) || "__builtin__".equals(declaringModuleName)) {
continue;
}
}
boolean hasInit = false;
if (declaringModuleName.endsWith(".__init__")) {
// remove the .__init__
declaringModuleName = declaringModuleName.substring(0, declaringModuleName.length() - 9);
hasInit = true;
}
String rep = info.getName();
if (!nameFilter.acceptName(rep) || importedNames.contains(rep)) {
continue;
}
if (addAutoImport) {
realImportRep.clear();
realImportRep.append("from ");
realImportRep.append(AnalysisPreferences.removeImportsStartingWithUnderIfNeeded(declaringModuleName, tempBuf, doIgnoreImportsStartingWithUnder));
realImportRep.append(" import ");
realImportRep.append(rep);
}
displayString.clear();
displayString.append(rep);
displayString.append(" - ");
displayString.append(declaringModuleName);
if (hasInit) {
displayString.append(".__init__");
}
String displayAsStr = displayString.toString();
ICompletionProposalHandle proposal = CompletionProposalFactory.get().createCtxInsensitiveImportComplProposal(rep, request.documentOffset - request.qlen, request.qlen, realImportRep.length(), info.getType(), displayAsStr, null, "", displayAsStr.equals(qual) ? IPyCompletionProposal.PRIORITY_GLOBALS_EXACT : IPyCompletionProposal.PRIORITY_GLOBALS, realImportRep.toString(), new CompareContext(info.getNature()));
completions.add(proposal);
}
}
return completions;
}
use of org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter in project Pydev by fabioz.
the class CtxParticipant method computeConsoleCompletions.
// Console completions ---------------------------------------------------------------------------------------------
/**
* IPyDevCompletionParticipant2
*/
@Override
public Collection<ICompletionProposalHandle> computeConsoleCompletions(ActivationTokenAndQualifier tokenAndQual, Set<IPythonNature> naturesUsed, IScriptConsoleViewer viewer, int requestOffset) {
List<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.getCharsForContextInsensitiveGlobalTokensCompletion() && naturesUsed != null && naturesUsed.size() > 0) {
// at least n characters required...
boolean addAutoImport = AnalysisPreferences.doAutoImport(null);
int qlen = qual.length();
boolean useSubstringMatchInCodeCompletion = PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion();
IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(useSubstringMatchInCodeCompletion, qual);
for (IPythonNature nature : naturesUsed) {
AbstractAdditionalTokensInfo additionalInfo;
try {
if (nature instanceof SystemPythonNature) {
SystemPythonNature systemPythonNature = (SystemPythonNature) nature;
additionalInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(systemPythonNature.getRelatedInterpreterManager(), systemPythonNature.getProjectInterpreter().getExecutableOrJar());
fillNatureCompletionsForConsole(viewer, requestOffset, completions, qual, addAutoImport, qlen, nameFilter, nature, additionalInfo, useSubstringMatchInCodeCompletion);
} else {
additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
fillNatureCompletionsForConsole(viewer, requestOffset, completions, qual, addAutoImport, qlen, nameFilter, nature, additionalInfo, useSubstringMatchInCodeCompletion);
}
} catch (MisconfigurationException e) {
Log.log(e);
}
}
}
return completions;
}
use of org.python.pydev.ast.codecompletion.PyCodeCompletionUtils.IFilter in project Pydev by fabioz.
the class ImportsCompletionParticipant method fillCompletions.
private void fillCompletions(int requestOffset, ArrayList<ICompletionProposalHandle> completions, String qual, IPythonNature nature, int qlen, boolean addAutoImport, IScriptConsoleViewer viewer) {
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager == null) {
return;
}
IModulesManager modulesManager = astManager.getModulesManager();
try {
if (modulesManager == null) {
// Just getting it here is likely to raise an error if it's not well configured.
nature.getProjectInterpreter();
}
} catch (PythonNatureWithoutProjectException e) {
throw new RuntimeException(e);
} catch (MisconfigurationException e) {
throw new RuntimeException(e);
}
String lowerQual = qual.toLowerCase();
final boolean useSubstringMatchInCodeCompletion = PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion();
Set<String> allModuleNames = PyCodeCompletionUtils.getModulesNamesToFilterOn(useSubstringMatchInCodeCompletion, modulesManager, qual);
IFilter nameFilter = PyCodeCompletionUtils.getNameFilter(useSubstringMatchInCodeCompletion, qual);
FastStringBuffer realImportRep = new FastStringBuffer();
FastStringBuffer displayString = new FastStringBuffer();
HashSet<String> alreadyFound = new HashSet<String>();
ICompareContext compareContext = new CompareContext(nature);
for (String name : allModuleNames) {
FullRepIterable iterable = new FullRepIterable(name);
for (String string : iterable) {
// clear the buffer...
realImportRep.clear();
String[] strings = FullRepIterable.headAndTail(string);
String importRep = strings[1];
if (!nameFilter.acceptName(importRep)) {
continue;
}
displayString.clear();
displayString.append(importRep);
String packageName = strings[0];
if (packageName.length() > 0) {
if (addAutoImport) {
realImportRep.append("from ");
realImportRep.append(packageName);
realImportRep.append(" ");
}
displayString.append(" - ");
displayString.append(packageName);
}
if (addAutoImport) {
realImportRep.append("import ");
realImportRep.append(strings[1]);
}
String found = displayString.toString();
if (alreadyFound.contains(found)) {
continue;
}
alreadyFound.add(found);
String displayAsStr = realImportRep.toString();
ICompletionProposalHandle proposal = CompletionProposalFactory.get().createPyConsoleCompletion(importRep, requestOffset - qlen, qlen, realImportRep.length(), IInfo.USE_PACKAGE_ICON, found, null, "", displayAsStr.toLowerCase().equals(lowerQual) ? IPyCompletionProposal.PRIORITY_PACKAGES_EXACT : IPyCompletionProposal.PRIORITY_PACKAGES, displayAsStr, viewer, compareContext);
completions.add(proposal);
}
}
}
Aggregations