use of org.python.pydev.shared_core.code_completion.ICompletionProposalHandle in project Pydev by fabioz.
the class PyCodeCompletionUtils method onlyValid.
/**
* Filters the python completions so that only the completions we care about are shown (given the qualifier)
* @param pythonAndTemplateProposals the completions to sort / filter
* @param qualifier the qualifier we care about
* @param onlyForCalltips if we should filter having in mind that we're going to show it for a calltip
* @return the completions to show to the user
*/
public static ICompletionProposalHandle[] onlyValid(TokensOrProposalsList pythonAndTemplateProposals, String qualifier, boolean onlyForCalltips, boolean useSubstringMatchInCodeCompletion, IProject project) {
// FOURTH: Now, we have all the proposals, only thing is deciding which ones are valid (depending on
// qualifier) and sorting them correctly.
final Map<String, List<ICompletionProposalHandle>> returnProposals = new HashMap<String, List<ICompletionProposalHandle>>();
int len = pythonAndTemplateProposals.size();
IFilter nameFilter = getNameFilter(useSubstringMatchInCodeCompletion, qualifier);
for (IterEntry entry : pythonAndTemplateProposals) {
Object o = entry.object;
if (o instanceof ICompletionProposalHandle) {
ICompletionProposalHandle proposal = (ICompletionProposalHandle) o;
String displayString;
if (proposal instanceof IPyCompletionProposal2) {
IPyCompletionProposal2 pyCompletionProposal = (IPyCompletionProposal2) proposal;
displayString = pyCompletionProposal.getInternalDisplayStringRepresentation();
} else {
displayString = proposal.getDisplayString();
}
if (onlyForCalltips) {
if (displayString.equals(qualifier)) {
addProposal(returnProposals, proposal, displayString);
} else if (displayString.length() > qualifier.length() && displayString.startsWith(qualifier)) {
if (displayString.charAt(qualifier.length()) == '(') {
addProposal(returnProposals, proposal, displayString);
}
}
} else if (nameFilter.acceptName(displayString)) {
List<ICompletionProposalHandle> existing = returnProposals.get(displayString);
if (existing != null) {
// a proposal with the same string is already there...
boolean addIt = true;
if (proposal instanceof IPyCompletionProposal) {
IPyCompletionProposal propP = (IPyCompletionProposal) proposal;
OUT: for (Iterator<ICompletionProposalHandle> it = existing.iterator(); it.hasNext(); ) {
ICompletionProposalHandle curr = it.next();
int overrideBehavior = propP.getOverrideBehavior(curr);
switch(overrideBehavior) {
case IPyCompletionProposal.BEHAVIOR_COEXISTS:
// just go on (it will be added later)
break;
case IPyCompletionProposal.BEHAVIOR_OVERRIDES:
it.remove();
break;
case IPyCompletionProposal.BEHAVIOR_IS_OVERRIDEN:
addIt = false;
break OUT;
}
}
}
if (addIt) {
existing.add(proposal);
}
} else {
// it's null, so, 1st insertion...
List<ICompletionProposalHandle> lst = new ArrayList<ICompletionProposalHandle>();
lst.add(proposal);
returnProposals.put(displayString, lst);
}
}
} else {
throw new RuntimeException("Error: expected instanceof ICompletionProposalHandle and received: " + o.getClass().getName());
}
}
// and fill with list elements
Collection<List<ICompletionProposalHandle>> values = returnProposals.values();
ArrayList<ICompletionProposalHandle> tproposals = new ArrayList<ICompletionProposalHandle>();
for (List<ICompletionProposalHandle> value : values) {
tproposals.addAll(value);
}
ICompletionProposalHandle[] proposals = tproposals.toArray(new ICompletionProposalHandle[returnProposals.size()]);
return proposals;
}
use of org.python.pydev.shared_core.code_completion.ICompletionProposalHandle in project Pydev by fabioz.
the class PyCodeCompletionsForTypedDict method getStringCompletions.
public TokensOrProposalsList getStringCompletions() throws CoreException, BadLocationException, IOException, MisconfigurationException, PythonNatureWithoutProjectException, CompletionRecursionException {
if (artificialRequest.isPresent()) {
CompletionRequest request = artificialRequest.get();
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(false);
TokensList tokenCompletions = new TokensList();
IPythonNature nature = request.nature;
if (nature == null) {
throw new RuntimeException("Unable to get python nature.");
}
ICodeCompletionASTManager astManager = nature.getAstManager();
if (astManager == null) {
// we're probably still loading it.
return new TokensOrProposalsList();
}
String trimmed = request.getActivationToken().replace('.', ' ').trim();
// We know it has to be a token completion, so, call it directly.
PyCodeCompletion.doTokenCompletion(request, astManager, tokenCompletions, trimmed, state);
TokensOrProposalsList tokensList = new TokensOrProposalsList();
tokensList.addAll(tokenCompletions);
List<ICompletionProposalHandle> completionProposals = new ArrayList<>();
PyCodeCompletion.changeItokenToCompletionPropostal(request, completionProposals, tokensList, false, state);
TokensOrProposalsList ret = new TokensOrProposalsList(completionProposals);
return ret;
}
return null;
}
use of org.python.pydev.shared_core.code_completion.ICompletionProposalHandle in project Pydev by fabioz.
the class PyStringCodeCompletion method getCodeCompletionProposals.
/**
* Needed interface for adding the completions on a request
* @throws MisconfigurationException
* @throws PythonNatureWithoutProjectException
* @throws IOException
*/
@Override
public TokensOrProposalsList getCodeCompletionProposals(CompletionRequest request) throws CoreException, BadLocationException, MisconfigurationException, IOException, PythonNatureWithoutProjectException {
List<ICompletionProposalHandle> completionProposals = new ArrayList<>();
// don't show templates in strings
request.showTemplates = false;
fillWithEpydocFields(request, completionProposals);
TokensOrProposalsList ret = new TokensOrProposalsList();
if (completionProposals.size() == 0) {
// if the size is not 0, it means that this is a place for the '@' stuff, and not for the 'default' context for a string.
IDocument doc = request.doc;
FastPartitioner fastPartitioner = ((FastPartitioner) PyPartitionScanner.checkPartitionScanner(doc));
ITypedRegion partition = fastPartitioner.getPartition(request.documentOffset);
String partitionType = partition.getType();
if (IPythonPartitions.F_STRING_PARTITIONS.contains(partitionType)) {
// Now we are going to check whether where we are in the given completion offset
int requestOffset = request.documentOffset;
int partitionOffset = partition.getOffset();
int partitionLine = doc.getLineOfOffset(partitionOffset);
int partitionCol = partitionOffset - doc.getLineOffset(partitionLine);
String str = doc.get(partitionOffset, partition.getLength());
FStringsAST ast = null;
try {
ast = FStringsGrammarFactory.createGrammar(str).f_string();
} catch (Throwable e) {
// Just ignore any errors for this.
}
if (ast != null && ast.hasChildren()) {
for (SimpleNode node : ast.getBalancedExpressionsToBeEvaluatedInRegularGrammar()) {
int nodeOffset;
int nodeEndOffset;
if (node.beginLine > 1) {
nodeOffset = TextSelectionUtils.getAbsoluteCursorOffset(doc, partitionLine + node.beginLine - 1, node.beginColumn - 1);
} else {
nodeOffset = TextSelectionUtils.getAbsoluteCursorOffset(doc, partitionLine + node.beginLine - 1, partitionCol + node.beginColumn - 1);
}
if (node.endLine > 1) {
nodeEndOffset = TextSelectionUtils.getAbsoluteCursorOffset(doc, partitionLine + node.endLine - 1, node.endColumn);
} else {
nodeEndOffset = TextSelectionUtils.getAbsoluteCursorOffset(doc, partitionLine + node.endLine - 1, partitionCol + node.endColumn);
}
if (requestOffset >= nodeOffset && requestOffset <= nodeEndOffset) {
// request is inside a format, so we have to get a normal code completion to it
return new PyCodeCompletion().getCodeCompletionProposals(request);
}
}
}
}
PyCodeCompletionsForTypedDict pyCodeCompletionsForTypedDict = new PyCodeCompletionsForTypedDict(request);
if (pyCodeCompletionsForTypedDict.isTypedDictCompletionRequest()) {
// If it's a typed dict completion request, don't go into other requests.
TokensOrProposalsList completionsForTypedDict;
try {
completionsForTypedDict = pyCodeCompletionsForTypedDict.getStringCompletions();
if (completionsForTypedDict != null) {
return completionsForTypedDict;
}
} catch (CompletionRecursionException e) {
Log.log(e);
}
return new TokensOrProposalsList();
}
TokensOrProposalsList stringGlobalsFromParticipants = getStringGlobalsFromParticipants(request, CompletionStateFactory.getEmptyCompletionState(request.activationToken, request.nature, new CompletionCache()));
ret.addAll(stringGlobalsFromParticipants);
// the code-below does not work well because the module may not have an actual import for the activation token,
// so, it is useless too many times
// if(request.activationToken.length() != 0){
// PyCodeCompletion completion = new PyCodeCompletion();
// ret.addAll(completion.getCodeCompletionProposals(viewer, request));
// }
}
fillWithParams(request, completionProposals);
ret.addAll(new TokensOrProposalsList(completionProposals));
return ret;
}
use of org.python.pydev.shared_core.code_completion.ICompletionProposalHandle 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.shared_core.code_completion.ICompletionProposalHandle in project Pydev by fabioz.
the class QuickAssistDontTrace method getProps.
@Override
public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, IPyEdit edit, int offset) throws BadLocationException, MisconfigurationException {
List<ICompletionProposalHandle> l = new ArrayList<>();
String cursorLineContents = ps.getCursorLineContents();
String messageToIgnore = "@DontTrace";
if (!cursorLineContents.contains(messageToIgnore)) {
ICompletionProposalHandle proposal = CompletionProposalFactory.get().createIgnoreCompletionProposalInSameLine(messageToIgnore, ps.getEndLineOffset(), 0, offset, imageCache.get(UIConstants.ASSIST_ANNOTATION), messageToIgnore.substring(1), null, null, IPyCompletionProposal.PRIORITY_DEFAULT, edit, cursorLineContents, ps, null);
l.add(proposal);
}
return l;
}
Aggregations