Subject: bug#47575: python-shell-completion-at-point omits %magic commands

From: JD Smith <[email-masked]>
Date: Sat, 3 Apr 2021 00:21:54 -0400

When using iPython as the inferior shell of python.el, `python-shell-completion-at-point' skips a `simple-operator’ regexp which include ?%.  In iPython, magic commands begin with ‘%’, and the fallback completion method happily provides them for completion:

In [67]: __PYTHON_EL_get_completions("%ru")
Out[91]: ['%%ruby', '%run’]

python-shell-completion-at-point trims this to “ru”, which also provides the same completions above, but which `try-completions’ rejects as non-matching.   So that:

In [1]: %ru[Tab]

Leads to “No matches”.

The solution would be to not skip the ?% character if it is at the start of the line.  The modulo operator cannot occur there in any case. 

One other point: the fallback mechanism sends and compiles the relatively long __PYTHON_EL_get_completions function each and every time it is called.  Because of how CAPF functions, this actually occurs 3 times in quick succession with a single Tab press like the above!  A more performant approach (esp. over remote buffer connections) would be to just call __PYTHON_EL_get_completions, and check for failure, setting up that function again if necessary.  This would parallel the readline completer native method, which is only setup once.