From f8b5cbc6574c4317bc005b41841d505098c7d0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Tue, 8 Nov 2022 10:33:52 +0100 Subject: [PATCH] feat: auto-complete `--mount` args When typing `tutor local run --mount /path/to/edx-pl`, the mount option should be auto-completed to the full edx-platform repo path. That is, if shell completion is enabled: https://docs.tutor.overhang.io/install.html#shell-autocompletion Here, we make sure that the implicit form of the `--mount` argument is properly auto-completed. We are unable to get completion to work in the explicit form, because args that include colons do not even reach the `shell_completion` method. --- CHANGELOG.md | 1 + tutor/commands/compose.py | 13 +++++++++++++ tutor/hooks/consts.py | 1 + 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d4909e..2737b22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Every user-facing change should have an entry in this changelog. Please respect ## Unreleased +- [Improvement] Auto-complete implicit `local/dev --mount /path/to/...` options. (by @regisb) - 💥[Feature] Strong typing of action and filter hooks: this allows us to detect incorrect calls to `actions.add` or `filters.add` early. Strong typing forces us to break the `do` and `apply` API by removing the `context` named argument. Developers should replace `do(context=...)` by `do_from_context(..., )` (and similar for `apply`). ## v14.1.2 (2022-11-02) diff --git a/tutor/commands/compose.py b/tutor/commands/compose.py index f17f894..96b01c7 100644 --- a/tutor/commands/compose.py +++ b/tutor/commands/compose.py @@ -4,6 +4,7 @@ import typing as t from copy import deepcopy import click +from click.shell_completion import CompletionItem from typing_extensions import TypeAlias from tutor import bindmounts @@ -171,6 +172,18 @@ class MountParam(click.ParamType): raise self.fail(f"no mount found for {value}") return mounts + def shell_complete( + self, ctx: click.Context, param: click.Parameter, incomplete: str + ) -> t.List[CompletionItem]: + """ + Mount argument completion works only for the single path (implicit) form. The + reason is that colons break words in bash completion: + http://tiswww.case.edu/php/chet/bash/FAQ (E13) + Thus, we do not even attempt to auto-complete mount arguments that include + colons: such arguments will not even reach this method. + """ + return [CompletionItem(incomplete, type="file")] + mount_option = click.option( "-m", diff --git a/tutor/hooks/consts.py b/tutor/hooks/consts.py index 2f89623..ae8e762 100644 --- a/tutor/hooks/consts.py +++ b/tutor/hooks/consts.py @@ -8,6 +8,7 @@ from __future__ import annotations __license__ = "Apache 2.0" from typing import Any, Callable + import click from tutor.types import Config