Skip to content

Commit cb545ee

Browse files
committed
Add IPython completion hook
1 parent 9d1833f commit cb545ee

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

julia/magic.py

+37
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,37 @@ def julia(self, line, cell=None):
6666
return ans
6767

6868

69+
def julia_completer(self, event):
70+
pos = event.line.find("%julia")
71+
if pos < 0:
72+
return []
73+
pos += len("%julia") # pos: beginning of Julia code
74+
julia_code = event.line[pos:]
75+
julia_pos = len(event.text_until_cursor) - pos
76+
77+
julia = Julia()
78+
completions = julia.eval("""
79+
import REPL
80+
(string, pos) -> begin
81+
ret, _, should_complete =
82+
REPL.completions(string, pos)
83+
if should_complete
84+
return map(REPL.completion_text, ret)
85+
else
86+
return String[]
87+
end
88+
end
89+
""")(julia_code, julia_pos)
90+
91+
if "." in event.symbol:
92+
# When completing (say) "Base.s" we need to add the prefix "Base."
93+
prefix = event.symbol.rsplit(".", 1)[0]
94+
completions = [".".join((prefix, c)) for c in completions]
95+
return completions
96+
# See:
97+
# IPython.core.completer.dispatch_custom_completer
98+
99+
69100
# Add to the global docstring the class information.
70101
__doc__ = __doc__.format(
71102
JULIAMAGICS_DOC=' ' * 8 + JuliaMagics.__doc__,
@@ -80,3 +111,9 @@ def julia(self, line, cell=None):
80111
def load_ipython_extension(ip):
81112
"""Load the extension in IPython."""
82113
ip.register_magics(JuliaMagics)
114+
ip.set_hook("complete_command", julia_completer,
115+
re_key="%?%julia")
116+
# See:
117+
# https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.hooks.html
118+
# IPython.core.interactiveshell.init_completer
119+
# IPython.core.completerlib (quick_completer etc.)

0 commit comments

Comments
 (0)