Improve import completions (#4265)

* completers: import: Move `import` completer before `bash` completer

If this completer returns a result, it's definitely an import statement

* completers: import: Complete `import` keyword in `from ... import` statements

* news: Add improve-import-completions
This commit is contained in:
Daniel Shimon 2021-05-11 01:48:53 +03:00 committed by GitHub
parent 59819cf28f
commit 35f1ba1819
Failed to generate hash of commit
4 changed files with 28 additions and 2 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* Complete ``import`` keyword in ``from ... import`` statements.
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* ``import`` completions always work.
**Security:**
* <news item>

View file

@ -73,7 +73,7 @@ def test_complete_python_ctx():
class A:
def wow():
pass
a = A()
res = complete_python(CompletionContext(python=PythonContext("a.w", 2, ctx=locals())))
@ -86,6 +86,7 @@ def test_complete_python_ctx():
(CommandContext(args=(CommandArg("import"),), arg_index=1, prefix="pathli"), {"pathlib"}),
(CommandContext(args=(CommandArg("from"),), arg_index=1, prefix="pathli"), {"pathlib "}),
(CommandContext(args=(CommandArg("import"),), arg_index=1, prefix="os.pa"), {"os.path"}),
(CommandContext(args=(CommandArg("from"), CommandArg("x"),), arg_index=2), {"import"}),
(CommandContext(args=(
CommandArg("import"), CommandArg("os,"),
), arg_index=2, prefix="pathli"), {"pathlib"}),

View file

@ -33,9 +33,9 @@ def default_completers():
("rmdir", complete_rmdir),
("xonfig", complete_xonfig),
("xontrib", complete_xontrib),
("import", complete_import),
("bash", complete_from_bash),
("man", complete_from_man),
("import", complete_import),
("python", complete_python),
("path", complete_path),
(

View file

@ -308,6 +308,8 @@ def complete_import(context: CompletionContext):
if arg_index >= 1 and args[0].value == "import":
# completing module to import
return complete_module(prefix)
if arg_index == 2 and args[0].value == "from":
return {RichCompletion("import", append_space=True)}
if arg_index > 2 and args[0].value == "from" and args[2].value == "import":
# complete thing inside a module
try: