Skip to content

Commit

Permalink
added file completer to search component for --internal-plan-file option
Browse files Browse the repository at this point in the history
  • Loading branch information
speckdavid committed Jul 18, 2024
1 parent a4d6fd7 commit b048352
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 3 additions & 2 deletions driver/tab_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def complete_planner_args(prefix, parsed_args, **kwargs):
if not last_option_was_mode_switch:
completions.append("--translate-options")

downward = bin_dir / "downward"
downward = bin_dir / "downward"
if downward.exists():
completions += get_completions_from_downward(
downward, parsed_args.search_options, prefix)
Expand All @@ -87,10 +87,11 @@ def get_completions_from_downward(downward, options, prefix):
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
comp_point, comp_line, comp_cword] + simulated_commandline
output = subprocess.check_output(cmd, text=True)
return output.split()


def get_completions_from_translator(translator, options, prefix):
simulated_commandline = [str(translator)] + options + [prefix]
comp_line = " ".join(simulated_commandline)
Expand Down
30 changes: 28 additions & 2 deletions src/search/command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "utils/strings.h"

#include <algorithm>
#include <filesystem>
#include <sstream>
#include <vector>

Expand Down Expand Up @@ -197,7 +198,32 @@ static vector<string> complete_args(
suggestions.push_back(feature->get_key());
}
} else if (last_arg == "--internal-plan-file") {
// suggest filename starting with current_word
// Suggest filename starting with current_word

// Split into directory and file_prefix
string directory = ".";
string file_prefix = current_word;
auto last_slash_pos = current_word.find_last_of("/\\");
if (last_slash_pos != string::npos) {
directory = current_word.substr(0, last_slash_pos);
file_prefix = current_word.substr(last_slash_pos + 1);
}

// Add file and directory names to suggestions
for (const auto& entry : filesystem::directory_iterator(directory)) {
string path = entry.path().string();

// Append slash to directories
if (entry.is_directory()) {
path += "/";
}

// Remove "./" prefix when not present in prefix
if (last_slash_pos == string::npos && directory == "." && path.starts_with("./")) {
path = path.substr(2);
}
suggestions.push_back(path);
}
} else if (last_arg == "--internal-previous-portfolio-plans") {
// no suggestions, integer expected
} else if (last_arg == "--search") {
Expand Down Expand Up @@ -244,7 +270,7 @@ void handle_tab_completion(int argc, const char **argv) {
"$COMP_LINE is the current command line.\n"
"$COMP_CWORD is an index into ${COMP_WORDS} of the word under the cursor.\n"
"$COMP_WORDS is the current command line split into words.\n"
);
);
}
int cursor_pos = parse_int_arg("COMP_POINT", static_cast<string>(argv[2]));
const string command_line = static_cast<string>(argv[3]);
Expand Down

0 comments on commit b048352

Please sign in to comment.