[docs]defadd_one_completer(name,func,loc="end"):new=collections.OrderedDict()ifloc=="start":# Add new completer before the first exclusive one.# We don't want new completers to be before the non-exclusive ones,# because then they won't be used when this completer is successful.# On the other hand, if the new completer is non-exclusive,# we want it to be before all other exclusive completers so that is will always work.items=list(XSH.completers.items())first_exclusive=next((ifori,(_,v)inenumerate(items)ifis_exclusive_completer(v)),len(items),)fork,vinitems[:first_exclusive]:new[k]=vnew[name]=funcfork,vinitems[first_exclusive:]:new[k]=velifloc=="end":fork,vinXSH.completers.items():new[k]=vnew[name]=funcelse:direction,rel=loc[0],loc[1:]found=Falsefork,vinXSH.completers.items():ifrel==kanddirection=="<":new[name]=funcfound=Truenew[k]=vifrel==kanddirection==">":new[name]=funcfound=Trueifnotfound:new[name]=funcXSH.completers.clear()XSH.completers.update(new)
[docs]deflist_completers():"""List the active completers"""o="Registered Completer Functions: (NX = Non Exclusive)\n\n"non_exclusive=" [NX]"_comp=XSH.completersml=max((len(i)foriin_comp),default=0)exclusive_len=ml+len(non_exclusive)+1_strs=[]forcin_comp:if_comp[c].__doc__isNone:doc="No description provided"else:doc=" ".join(_comp[c].__doc__.split())doc=justify(doc,80,exclusive_len+3)ifis_exclusive_completer(_comp[c]):_strs.append("{: <{}} : {}".format(c,exclusive_len,doc))else:_strs.append("{: <{}} {} : {}".format(c,ml,non_exclusive,doc))returno+"\n".join(_strs)+"\n"
[docs]defcomplete_completer_names(xsh,**_):"""Complete all loaded completer names"""forname,compinxsh.completers.items():doc=NumpyDoc(comp)yieldRichCompletion(name,description=doc.description)
[docs]defremove_completer(name:Annotated[str,Arg(completer=complete_completer_names)],):"""Removes a completer from xonsh Parameters ---------- name: NAME is a unique name of a completer (run "completer list" to see the current completers in order) """err=NoneifnamenotinXSH.completers:err=f"The name {name} is not a registered completer function."iferrisNone:delXSH.completers[name]returnelse:returnNone,err+"\n",1