Skip to content

Commit

Permalink
always set completer
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianPommerening committed Jul 17, 2024
1 parent da81a52 commit 1c7b10f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 81 deletions.
4 changes: 2 additions & 2 deletions driver/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def parse_args():
"this path does not exist, it tries the directory "
"'<repo>/builds/BUILD/bin', where the build script creates "
"them by default.")
tab_completion.add_build_arg_completer(build_arg)
build_arg.completer = tab_completion.complete_build_arg
driver_other.add_argument(
"--debug", action="store_true",
help="alias for --build=debug --validate")
Expand Down Expand Up @@ -428,7 +428,7 @@ def parse_args():
planner_args = parser.add_argument(
"planner_args", nargs=argparse.REMAINDER,
help="file names and options passed on to planner components")
tab_completion.add_planner_args_completer(planner_args)
planner_args.completer = tab_completion.complete_planner_args

# Using argparse.REMAINDER relies on the fact that the first
# argument that doesn't belong to the driver doesn't look like an
Expand Down
149 changes: 70 additions & 79 deletions driver/tab_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _split_off_filenames(planner_args):
return planner_args[:num_filenames], planner_args[num_filenames:], double_dash_in_options


def _build_arg_completion(prefix, parsed_args, **kwargs):
def complete_build_arg(prefix, parsed_args, **kwargs):
if parsed_args.debug:
argcomplete.warn("The option --debug is an alias for --build=debug. Do no specify both --debug and --build.")
exit(1)
Expand All @@ -64,86 +64,77 @@ def _build_arg_completion(prefix, parsed_args, **kwargs):
return [p.name for p in builds_folder.iterdir() if p.is_dir()]


def add_build_arg_completer(build_arg):
build_arg.completer = _build_arg_completion

def complete_planner_args(prefix, parsed_args, **kwargs):
if parsed_args.build and parsed_args.debug:
argcomplete.warn("The option --debug is an alias for --build=debug. Do no specify both --debug and --build.")
exit(1)

def _planner_args_completion(prefix, parsed_args, **kwargs):
if HAS_ARGCOMPLETE:
if parsed_args.build and parsed_args.debug:
argcomplete.warn("The option --debug is an alias for --build=debug. Do no specify both --debug and --build.")
exit(1)

build = parsed_args.build
if not build:
if parsed_args.debug:
build = "debug"
else:
build = "release"

filenames, options, double_dash_in_options = _split_off_filenames(parsed_args.planner_args)

completions = []

if not options and not (len(filenames) == 2 and double_dash_in_options):
completions.append("--")

translate_options = []
search_options = []
last_option_was_mode_switch = False

curr_options = search_options
for option in options:
if option == "--translate-options":
curr_options = translate_options
last_option_was_mode_switch = True
elif option == "--search-options":
curr_options = search_options
last_option_was_mode_switch = True
else:
curr_options.append(option)
last_option_was_mode_switch = False

if filenames:
if curr_options is search_options:
build = parsed_args.build
if not build:
if parsed_args.debug:
build = "debug"
else:
build = "release"

filenames, options, double_dash_in_options = _split_off_filenames(parsed_args.planner_args)

completions = []

if not options and not (len(filenames) == 2 and double_dash_in_options):
completions.append("--")

translate_options = []
search_options = []
last_option_was_mode_switch = False

curr_options = search_options
for option in options:
if option == "--translate-options":
curr_options = translate_options
last_option_was_mode_switch = True
elif option == "--search-options":
curr_options = search_options
last_option_was_mode_switch = True
else:
curr_options.append(option)
last_option_was_mode_switch = False

if filenames:
if curr_options is search_options:
if not last_option_was_mode_switch:
completions.append("--translate-options")

downward = Path(util.REPO_ROOT_DIR) / "builds" / build / "bin" / "downward"
if downward.exists():
simulated_commandline = [str(downward)] + search_options + [prefix]
comp_line = " ".join(simulated_commandline)
comp_point = str(len(comp_line))
comp_cword = str(len(simulated_commandline) - 1)
cmd = [str(downward), "--bash-complete",
comp_point, comp_line, comp_cword] + simulated_commandline
output = subprocess.check_output(cmd, text=True)
completions += output.split()
else:
tranlator_arguments_path = Path(util.REPO_ROOT_DIR) / "builds" / build / "bin" / "translate" / "arguments.py"
if tranlator_arguments_path.exists():
spec = importlib.util.spec_from_file_location("arguments", tranlator_arguments_path)
tranlator_arguments = importlib.util.module_from_spec(spec)
sys.modules["arguments"] = tranlator_arguments
spec.loader.exec_module(tranlator_arguments)

# We create a new parser that will handle the autocompletion
# for the translator
translator_parser = argparse.ArgumentParser()
tranlator_arguments.add_options(translator_parser)
if not last_option_was_mode_switch:
completions.append("--translate-options")

downward = Path(util.REPO_ROOT_DIR) / "builds" / build / "bin" / "downward"
if downward.exists():
simulated_commandline = [str(downward)] + search_options + [prefix]
comp_line = " ".join(simulated_commandline)
comp_point = str(len(comp_line))
comp_cword = str(len(simulated_commandline) - 1)
cmd = [str(downward), "--bash-complete",
comp_point, comp_line, comp_cword] + simulated_commandline
output = subprocess.check_output(cmd, text=True)
completions += output.split()
else:
tranlator_arguments_path = Path(util.REPO_ROOT_DIR) / "builds" / build / "bin" / "translate" / "arguments.py"
if tranlator_arguments_path.exists():
spec = importlib.util.spec_from_file_location("arguments", tranlator_arguments_path)
tranlator_arguments = importlib.util.module_from_spec(spec)
sys.modules["arguments"] = tranlator_arguments
spec.loader.exec_module(tranlator_arguments)

# We create a new parser that will handle the autocompletion
# for the translator
translator_parser = argparse.ArgumentParser()
tranlator_arguments.add_options(translator_parser)
if not last_option_was_mode_switch:
translator_parser.add_argument("--search-options", action="store_true")
argcomplete.autocomplete(translator_parser)

if len(filenames) < 2 and not double_dash_in_options and not translate_options and not search_options and not last_option_was_mode_switch:
file_completer = argcomplete.FilesCompleter()
completions += file_completer(prefix, **kwargs)

return completions


def add_planner_args_completer(planner_args):
planner_args.completer = _planner_args_completion
translator_parser.add_argument("--search-options", action="store_true")
argcomplete.autocomplete(translator_parser)

if len(filenames) < 2 and not double_dash_in_options and not translate_options and not search_options and not last_option_was_mode_switch:
file_completer = argcomplete.FilesCompleter()
completions += file_completer(prefix, **kwargs)

return completions


def enable(parser):
Expand Down

0 comments on commit 1c7b10f

Please sign in to comment.