Eliminate the silly autosuggest_parsed_command_t class; make it just a function

This commit is contained in:
ridiculousfish 2012-05-07 17:43:05 -07:00
parent 0c79bb6e7c
commit 4df6b599b8

View file

@ -601,21 +601,11 @@ static int has_expand_reserved( const wchar_t *str )
return 0;
}
/* A class representing the result of parsing a command line, containing both the last command and its arguments. This is used by autosuggestions */
class autosuggest_parsed_command_t {
public:
/* The command, like "cd" */
wcstring command;
/* Arguments to the command */
wcstring_list_t arguments;
/* Position in the string of the start of the last argument */
int last_arg_pos;
autosuggest_parsed_command_t(const wcstring &str) {
/* Parse a command line. Return by reference the last command, its arguments, and the offset in the string of the beginning of the last argument. This is used by autosuggestions */
static bool autosuggest_parse_command(const wcstring &str, wcstring *out_command, wcstring_list_t *out_arguments, int *out_last_arg_pos)
{
if (str.empty())
return;
return false;
wcstring cmd;
wcstring_list_t args;
@ -720,12 +710,13 @@ class autosuggest_parsed_command_t {
/* Remember our command if we have one */
if (had_cmd) {
this->command.swap(cmd);
this->arguments.swap(args);
this->last_arg_pos = arg_pos;
if (out_command) out_command->swap(cmd);
if (out_arguments) out_arguments->swap(args);
if (out_last_arg_pos) *out_last_arg_pos = arg_pos;
}
return had_cmd;
}
};
bool autosuggest_suggest_special(const wcstring &str, const wcstring &working_directory, wcstring &outSuggestion) {
if (str.empty())
@ -735,12 +726,16 @@ bool autosuggest_suggest_special(const wcstring &str, const wcstring &working_di
/* Parse the string */
const autosuggest_parsed_command_t parsed(str);
wcstring parsed_command;
wcstring_list_t parsed_arguments;
int parsed_last_arg_pos = -1;
if (! autosuggest_parse_command(str, &parsed_command, &parsed_arguments, &parsed_last_arg_pos))
return false;
bool result = false;
if (parsed.command == L"cd" && ! parsed.arguments.empty()) {
if (parsed_command == L"cd" && ! parsed_arguments.empty()) {
/* We can possibly handle this specially */
wcstring dir = parsed.arguments.back();
wcstring dir = parsed_arguments.back();
wcstring suggested_path;
/* We always return true because we recognized the command. This prevents us from falling back to dumber algorithms; for example we won't suggest a non-directory for the cd command. */
@ -780,7 +775,7 @@ bool autosuggest_suggest_special(const wcstring &str, const wcstring &working_di
/* Success */
outSuggestion = str;
outSuggestion.erase(parsed.last_arg_pos);
outSuggestion.erase(parsed_last_arg_pos);
outSuggestion.append(suggested_path);
}
} else {
@ -796,11 +791,15 @@ bool autosuggest_special_validate_from_history(const wcstring &str, const wcstri
bool handled = false, suggestionOK = false;
/* Parse the string */
autosuggest_parsed_command_t parsed(str);
wcstring parsed_command;
wcstring_list_t parsed_arguments;
int parsed_last_arg_pos = -1;
if (! autosuggest_parse_command(str, &parsed_command, &parsed_arguments, &parsed_last_arg_pos))
return false;
if (parsed.command == L"cd" && ! parsed.arguments.empty()) {
if (parsed_command == L"cd" && ! parsed_arguments.empty()) {
/* We can possibly handle this specially */
wcstring dir = parsed.arguments.back();
wcstring dir = parsed_arguments.back();
if (expand_one(dir, EXPAND_SKIP_CMDSUBST))
{
handled = true;