@@ -66,6 +66,74 @@ def julia(self, line, cell=None):
6666 return ans
6767
6868
69+ class JuliaCompleter (object ):
70+
71+ """
72+ Simple completion for ``%julia`` line magic.
73+ """
74+
75+ @property
76+ def jlcomplete_texts (self ):
77+ try :
78+ return self ._jlcomplete_texts
79+ except AttributeError :
80+ pass
81+
82+ julia = Julia ()
83+ if julia .eval ('VERSION < v"0.7-"' ):
84+ self ._jlcomplete_texts = lambda * _ : []
85+ return self ._jlcomplete_texts
86+
87+ self ._jlcomplete_texts = julia .eval ("""
88+ import REPL
89+ (str, pos) -> begin
90+ ret, _, should_complete =
91+ REPL.completions(str, pos)
92+ if should_complete
93+ return map(REPL.completion_text, ret)
94+ else
95+ return []
96+ end
97+ end
98+ """ )
99+ return self ._jlcomplete_texts
100+
101+ def complete_command (self , _ip , event ):
102+ pos = event .text_until_cursor .find ("%julia" )
103+ if pos < 0 :
104+ return []
105+ pos += len ("%julia" ) # pos: beginning of Julia code
106+ julia_code = event .line [pos :]
107+ julia_pos = len (event .text_until_cursor ) - pos
108+
109+ completions = self .jlcomplete_texts (julia_code , julia_pos )
110+ if "." in event .symbol :
111+ # When completing (say) "Base.s" we need to add the prefix "Base."
112+ prefix = event .symbol .rsplit ("." , 1 )[0 ]
113+ completions = ["." .join ((prefix , c )) for c in completions ]
114+ return completions
115+ # See:
116+ # IPython.core.completer.dispatch_custom_completer
117+
118+ @classmethod
119+ def register (cls , ip ):
120+ """
121+ Register `.complete_command` to IPython hook.
122+
123+ Parameters
124+ ----------
125+ ip : IPython.InteractiveShell
126+ IPython `.InteractiveShell` instance passed to
127+ `load_ipython_extension`.
128+ """
129+ ip .set_hook ("complete_command" , cls ().complete_command ,
130+ str_key = "%julia" )
131+ # See:
132+ # https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.hooks.html
133+ # IPython.core.interactiveshell.init_completer
134+ # IPython.core.completerlib (quick_completer etc.)
135+
136+
69137# Add to the global docstring the class information.
70138__doc__ = __doc__ .format (
71139 JULIAMAGICS_DOC = ' ' * 8 + JuliaMagics .__doc__ ,
@@ -80,3 +148,4 @@ def julia(self, line, cell=None):
80148def load_ipython_extension (ip ):
81149 """Load the extension in IPython."""
82150 ip .register_magics (JuliaMagics )
151+ JuliaCompleter .register (ip )
0 commit comments