Skip to content

Commit 5394055

Browse files
committed
Add tab completion for %model
1 parent d18ff5c commit 5394055

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

mypython/completion.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import traceback
3636

3737
from .dircompletion import DirCompleter
38-
from .magic import MAGICS
38+
from .magic import MAGICS, MAGIC_COMPLETIONS
3939

4040
def get_jedi_script_from_document(document, _locals, _globals, session):
4141
import jedi # We keep this import in-line, to improve start-up time.
@@ -99,6 +99,11 @@ def get_completions(self, document, complete_event):
9999
for magic in MAGICS:
100100
if text_before_cursor.startswith(magic + ' '):
101101
text_before_cursor = text_before_cursor[len(magic) + 1:]
102+
if magic in MAGIC_COMPLETIONS:
103+
for completion in MAGIC_COMPLETIONS[magic]():
104+
yield Completion(completion,
105+
-len(document.text_before_cursor), display_meta=magic)
106+
return
102107
break
103108

104109
if complete_event.completion_requested or self._complete_python_while_typing(document):

mypython/magic.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ def inner(rest):
5858
NON_PYTHON_MAGICS.append('%' + f.__name__[:-len('_magic')])
5959
return inner
6060

61+
MAGIC_COMPLETIONS = {}
62+
63+
def completions(get_completions):
64+
"""
65+
Use like
66+
67+
@completions(lambda: ['a', 'b', 'c'])
68+
def x_magic(rest):
69+
...
70+
"""
71+
def wrapper(f):
72+
MAGIC_COMPLETIONS['%' + f.__name__[:-len('_magic')]] = get_completions
73+
return f
74+
return wrapper
75+
6176
def error(message):
6277
return """\
6378
import sys as _sys
@@ -402,14 +417,20 @@ def timings_magic(rest):
402417
print(f'{{_i:2d}} {{_format_time(TIMINGS[_i])}}')
403418
"""
404419

420+
def get_ai_models():
421+
yield from list(ai.MODELS)
422+
for model in ai.MODELS:
423+
yield from ai.MODELS[model]['model_aliases']
424+
425+
@completions(get_ai_models)
405426
@nonpython
406427
def model_magic(rest):
407428
"""
408429
Set the current model for the completion engine.
409430
"""
410431
rest = rest.strip()
411432
if not rest:
412-
return error("no model specified")
433+
return error("no model specified: available models are %s" % ', '.join(ai.MODELS))
413434

414435
for model in ai.MODELS:
415436
if rest == model:

0 commit comments

Comments
 (0)