Formatting

This commit is contained in:
ridiculousfish 2013-06-02 01:14:26 -07:00
parent fd15d30987
commit 464187491f
8 changed files with 64 additions and 62 deletions

View file

@ -1772,13 +1772,13 @@ static bool subsequence_in_string(const wcstring &seq, const wcstring &str)
{ {
return false; return false;
} }
/* Empty strings are considered to be subsequences of everything */ /* Empty strings are considered to be subsequences of everything */
if (seq.empty()) if (seq.empty())
{ {
return true; return true;
} }
size_t str_idx, seq_idx; size_t str_idx, seq_idx;
for (seq_idx = str_idx = 0; seq_idx < seq.size() && str_idx < str.size(); seq_idx++) for (seq_idx = str_idx = 0; seq_idx < seq.size() && str_idx < str.size(); seq_idx++)
{ {
@ -1795,16 +1795,16 @@ static bool subsequence_in_string(const wcstring &seq, const wcstring &str)
str_idx = char_loc + 1; str_idx = char_loc + 1;
} }
} }
/* We succeeded if we exhausted our sequence */ /* We succeeded if we exhausted our sequence */
assert(seq_idx <= seq.size()); assert(seq_idx <= seq.size());
return seq_idx == seq.size(); return seq_idx == seq.size();
} }
string_fuzzy_match_t::string_fuzzy_match_t(enum fuzzy_match_type_t t, size_t distance_first, size_t distance_second) : string_fuzzy_match_t::string_fuzzy_match_t(enum fuzzy_match_type_t t, size_t distance_first, size_t distance_second) :
type(t), type(t),
match_distance_first(distance_first), match_distance_first(distance_first),
match_distance_second(distance_second) match_distance_second(distance_second)
{ {
} }

View file

@ -250,22 +250,22 @@ enum fuzzy_match_type_t
{ {
/* We match the string exactly: FOOBAR matches FOOBAR */ /* We match the string exactly: FOOBAR matches FOOBAR */
fuzzy_match_exact = 0, fuzzy_match_exact = 0,
/* We match a prefix of the string: FO matches FOOBAR */ /* We match a prefix of the string: FO matches FOOBAR */
fuzzy_match_prefix, fuzzy_match_prefix,
/* We match the string exactly, but in a case insensitive way: foobar matches FOOBAR */ /* We match the string exactly, but in a case insensitive way: foobar matches FOOBAR */
fuzzy_match_case_insensitive, fuzzy_match_case_insensitive,
/* We match a prefix of the string, in a case insensitive way: foo matches FOOBAR */ /* We match a prefix of the string, in a case insensitive way: foo matches FOOBAR */
fuzzy_match_prefix_case_insensitive, fuzzy_match_prefix_case_insensitive,
/* We match a substring of the string: OOBA matches FOOBAR */ /* We match a substring of the string: OOBA matches FOOBAR */
fuzzy_match_substring, fuzzy_match_substring,
/* A subsequence match with insertions only: FBR matches FOOBAR */ /* A subsequence match with insertions only: FBR matches FOOBAR */
fuzzy_match_subsequence_insertions_only, fuzzy_match_subsequence_insertions_only,
/* We don't match the string */ /* We don't match the string */
fuzzy_match_none fuzzy_match_none
}; };
@ -302,14 +302,14 @@ static inline bool match_type_shares_prefix(fuzzy_match_type_t t)
struct string_fuzzy_match_t struct string_fuzzy_match_t
{ {
enum fuzzy_match_type_t type; enum fuzzy_match_type_t type;
/* Strength of the match. The value depends on the type. Lower is stronger. */ /* Strength of the match. The value depends on the type. Lower is stronger. */
size_t match_distance_first; size_t match_distance_first;
size_t match_distance_second; size_t match_distance_second;
/* Constructor */ /* Constructor */
string_fuzzy_match_t(enum fuzzy_match_type_t t, size_t distance_first = 0, size_t distance_second = 0); string_fuzzy_match_t(enum fuzzy_match_type_t t, size_t distance_first = 0, size_t distance_second = 0);
/* Return -1, 0, 1 if this match is (respectively) better than, equal to, or worse than rhs */ /* Return -1, 0, 1 if this match is (respectively) better than, equal to, or worse than rhs */
int compare(const string_fuzzy_match_t &rhs) const; int compare(const string_fuzzy_match_t &rhs) const;
}; };

View file

@ -375,7 +375,7 @@ class completer_t
{ {
return !!(flags & COMPLETION_REQUEST_FUZZY_MATCH); return !!(flags & COMPLETION_REQUEST_FUZZY_MATCH);
} }
fuzzy_match_type_t max_fuzzy_match_type() const fuzzy_match_type_t max_fuzzy_match_type() const
{ {
/* If we are doing fuzzy matching, request all types; if not request only prefix matching */ /* If we are doing fuzzy matching, request all types; if not request only prefix matching */
@ -439,11 +439,11 @@ public:
expand_flags_t result = 0; expand_flags_t result = 0;
if (this->type() == COMPLETE_AUTOSUGGEST) if (this->type() == COMPLETE_AUTOSUGGEST)
result |= EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_JOBS; result |= EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_JOBS;
/* Allow fuzzy matching */ /* Allow fuzzy matching */
if (this->fuzzy()) if (this->fuzzy())
result |= EXPAND_FUZZY_MATCH; result |= EXPAND_FUZZY_MATCH;
return result; return result;
} }
@ -1207,7 +1207,7 @@ void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool
this->complete_cmd_desc(str_cmd); this->complete_cmd_desc(str_cmd);
} }
} }
if (use_function) if (use_function)
{ {
//function_get_names( &possible_comp, cmd[0] == L'_' ); //function_get_names( &possible_comp, cmd[0] == L'_' );
@ -1658,22 +1658,22 @@ bool completer_t::complete_variable(const wcstring &str, size_t start_offset)
const wchar_t *var = &whole_var[start_offset]; const wchar_t *var = &whole_var[start_offset];
size_t varlen = wcslen(var); size_t varlen = wcslen(var);
bool res = false; bool res = false;
const wcstring_list_t names = complete_get_variable_names(); const wcstring_list_t names = complete_get_variable_names();
for (size_t i=0; i<names.size(); i++) for (size_t i=0; i<names.size(); i++)
{ {
const wcstring & env_name = names.at(i); const wcstring & env_name = names.at(i);
string_fuzzy_match_t match = string_fuzzy_match_string(var, env_name, this->max_fuzzy_match_type()); string_fuzzy_match_t match = string_fuzzy_match_string(var, env_name, this->max_fuzzy_match_type());
if (match.type == fuzzy_match_none) if (match.type == fuzzy_match_none)
{ {
// No match // No match
continue; continue;
} }
wcstring comp; wcstring comp;
int flags = 0; int flags = 0;
if (! match_type_requires_full_replacement(match.type)) if (! match_type_requires_full_replacement(match.type))
{ {
// Take only the suffix // Take only the suffix
@ -1685,21 +1685,21 @@ bool completer_t::complete_variable(const wcstring &str, size_t start_offset)
comp.append(env_name); comp.append(env_name);
flags = COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE; flags = COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE;
} }
wcstring desc; wcstring desc;
if (this->wants_descriptions()) if (this->wants_descriptions())
{ {
env_var_t value_unescaped = env_get_string(env_name); env_var_t value_unescaped = env_get_string(env_name);
if (value_unescaped.missing()) if (value_unescaped.missing())
continue; continue;
wcstring value = expand_escape_variable(value_unescaped); wcstring value = expand_escape_variable(value_unescaped);
if (this->type() != COMPLETE_AUTOSUGGEST) if (this->type() != COMPLETE_AUTOSUGGEST)
desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str()); desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str());
} }
append_completion(this->completions, comp.c_str(), desc.c_str(), flags, match); append_completion(this->completions, comp.c_str(), desc.c_str(), flags, match);
res = true; res = true;
} }

View file

@ -122,7 +122,7 @@ public:
is case insensitive. is case insensitive.
*/ */
int flags; int flags;
/* Construction. Note: defining these so that they are not inlined reduces the executable size. */ /* Construction. Note: defining these so that they are not inlined reduces the executable size. */
completion_t(const wcstring &comp, const wcstring &desc = L"", string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact), int flags_val = 0); completion_t(const wcstring &comp, const wcstring &desc = L"", string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact), int flags_val = 0);
completion_t(const completion_t &); completion_t(const completion_t &);

View file

@ -57,7 +57,7 @@ enum
/** Don't expand home directories */ /** Don't expand home directories */
EXPAND_SKIP_HOME_DIRECTORIES = 1 << 9, EXPAND_SKIP_HOME_DIRECTORIES = 1 << 9,
/** Allow fuzzy matching */ /** Allow fuzzy matching */
EXPAND_FUZZY_MATCH = 1 << 10 EXPAND_FUZZY_MATCH = 1 << 10
}; };

View file

@ -988,18 +988,18 @@ static void test_complete(void)
assert(completions.at(0).completion == L"oo1"); assert(completions.at(0).completion == L"oo1");
assert(completions.at(1).completion == L"oo2"); assert(completions.at(1).completion == L"oo2");
assert(completions.at(2).completion == L"oo3"); assert(completions.at(2).completion == L"oo3");
completions.clear(); completions.clear();
complete(L"$1", completions, COMPLETION_REQUEST_DEFAULT); complete(L"$1", completions, COMPLETION_REQUEST_DEFAULT);
assert(completions.empty()); assert(completions.empty());
completions.clear(); completions.clear();
complete(L"$1", completions, COMPLETION_REQUEST_DEFAULT | COMPLETION_REQUEST_FUZZY_MATCH); complete(L"$1", completions, COMPLETION_REQUEST_DEFAULT | COMPLETION_REQUEST_FUZZY_MATCH);
assert(completions.size() == 2); assert(completions.size() == 2);
assert(completions.at(0).completion == L"$Foo1"); assert(completions.at(0).completion == L"$Foo1");
assert(completions.at(1).completion == L"$Bar1"); assert(completions.at(1).completion == L"$Bar1");
complete_set_variable_names(NULL); complete_set_variable_names(NULL);
} }

View file

@ -1139,12 +1139,12 @@ static void run_pager(const wcstring &prefix, int is_quoted, const std::vector<c
prefix_esc.c_str()); prefix_esc.c_str());
escaped_separator = escape(COMPLETE_SEP_STR, 1); escaped_separator = escape(COMPLETE_SEP_STR, 1);
for (size_t i=0; i< comp.size(); i++) for (size_t i=0; i< comp.size(); i++)
{ {
long base_len=-1; long base_len=-1;
const completion_t &el = comp.at(i); const completion_t &el = comp.at(i);
wcstring completion_text; wcstring completion_text;
wcstring description_text; wcstring description_text;
@ -1498,7 +1498,7 @@ static void prioritize_completions(std::vector<completion_t> &comp)
if (el.match.type < best_type) if (el.match.type < best_type)
best_type = el.match.type; best_type = el.match.type;
} }
/* Throw out completions whose match types are not the best. */ /* Throw out completions whose match types are not the best. */
i = comp.size(); i = comp.size();
while (i--) while (i--)
@ -1508,7 +1508,7 @@ static void prioritize_completions(std::vector<completion_t> &comp)
comp.erase(comp.begin() + i); comp.erase(comp.begin() + i);
} }
} }
/* Sort the remainder */ /* Sort the remainder */
sort(comp.begin(), comp.end(), compare_completions_by_match_type); sort(comp.begin(), comp.end(), compare_completions_by_match_type);
} }
@ -1536,7 +1536,7 @@ static const completion_t *cycle_competions(const std::vector<completion_t> &com
const completion_t &c = comp.at(idx); const completion_t &c = comp.at(idx);
/* Try this completion */ /* Try this completion */
if (! (c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(command_line, c.flags)) if (!(c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(command_line, c.flags))
{ {
/* Success */ /* Success */
result = &c; result = &c;
@ -1605,7 +1605,7 @@ static bool handle_completions(const std::vector<completion_t> &comp)
the token doesn't contain evil operators the token doesn't contain evil operators
like {} like {}
*/ */
if (! (c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(tok, c.flags)) if (!(c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(tok, c.flags))
{ {
completion_insert(c.completion.c_str(), c.flags); completion_insert(c.completion.c_str(), c.flags);
} }
@ -1618,7 +1618,7 @@ static bool handle_completions(const std::vector<completion_t> &comp)
if (!done) if (!done)
{ {
/* Determine the type of the best match(es) */ /* Determine the type of the best match(es) */
fuzzy_match_type_t best_match_type = fuzzy_match_none; fuzzy_match_type_t best_match_type = fuzzy_match_none;
for (size_t i=0; i < comp.size(); i++) for (size_t i=0; i < comp.size(); i++)
@ -1629,19 +1629,19 @@ static bool handle_completions(const std::vector<completion_t> &comp)
best_match_type = el.match.type; best_match_type = el.match.type;
} }
} }
/* Determine whether we are going to replace the token or not. If any commands of the best type do not require replacement, then ignore all those that want to use replacement */ /* Determine whether we are going to replace the token or not. If any commands of the best type do not require replacement, then ignore all those that want to use replacement */
bool will_replace_token = true; bool will_replace_token = true;
for (size_t i=0; i< comp.size(); i++) for (size_t i=0; i< comp.size(); i++)
{ {
const completion_t &el = comp.at(i); const completion_t &el = comp.at(i);
if (el.match.type == best_match_type && ! (el.flags & COMPLETE_REPLACES_TOKEN)) if (el.match.type == best_match_type && !(el.flags & COMPLETE_REPLACES_TOKEN))
{ {
will_replace_token = false; will_replace_token = false;
break; break;
} }
} }
/* Decide which completions survived. There may be a lot of them; it would be nice if we could figure out how to avoid copying them here */ /* Decide which completions survived. There may be a lot of them; it would be nice if we could figure out how to avoid copying them here */
std::vector<completion_t> surviving_completions; std::vector<completion_t> surviving_completions;
for (size_t i=0; i < comp.size(); i++) for (size_t i=0; i < comp.size(); i++)
@ -1650,21 +1650,21 @@ static bool handle_completions(const std::vector<completion_t> &comp)
/* Only use completions with the best match type */ /* Only use completions with the best match type */
if (el.match.type != best_match_type) if (el.match.type != best_match_type)
continue; continue;
/* Only use completions that match replace_token */ /* Only use completions that match replace_token */
bool completion_replace_token = !! (el.flags & COMPLETE_REPLACES_TOKEN); bool completion_replace_token = !!(el.flags & COMPLETE_REPLACES_TOKEN);
if (completion_replace_token != will_replace_token) if (completion_replace_token != will_replace_token)
continue; continue;
/* Don't use completions that want to replace, if we cannot replace them */ /* Don't use completions that want to replace, if we cannot replace them */
if (completion_replace_token && ! reader_can_replace(tok, el.flags)) if (completion_replace_token && ! reader_can_replace(tok, el.flags))
continue; continue;
/* This completion survived */ /* This completion survived */
surviving_completions.push_back(el); surviving_completions.push_back(el);
} }
/* Try to find a common prefix to insert among the surviving completions */ /* Try to find a common prefix to insert among the surviving completions */
wcstring common_prefix; wcstring common_prefix;
complete_flags_t flags = 0; complete_flags_t flags = 0;
@ -1682,7 +1682,8 @@ static bool handle_completions(const std::vector<completion_t> &comp)
{ {
/* Determine the shared prefix length. */ /* Determine the shared prefix length. */
size_t idx, max = mini(common_prefix.size(), el.completion.size()); size_t idx, max = mini(common_prefix.size(), el.completion.size());
for (idx=0; idx < max; idx++) { for (idx=0; idx < max; idx++)
{
wchar_t ac = common_prefix.at(idx), bc = el.completion.at(idx); wchar_t ac = common_prefix.at(idx), bc = el.completion.at(idx);
bool matches = (ac == bc); bool matches = (ac == bc);
/* If we are replacing the token, allow case to vary */ /* If we are replacing the token, allow case to vary */
@ -1694,17 +1695,17 @@ static bool handle_completions(const std::vector<completion_t> &comp)
if (! matches) if (! matches)
break; break;
} }
/* idx is now the length of the new common prefix */ /* idx is now the length of the new common prefix */
common_prefix.resize(idx); common_prefix.resize(idx);
prefix_is_partial_completion = true; prefix_is_partial_completion = true;
/* Early out if we decide there's no common prefix */ /* Early out if we decide there's no common prefix */
if (idx == 0) if (idx == 0)
break; break;
} }
} }
if (! common_prefix.empty()) if (! common_prefix.empty())
{ {
/* We got something. If more than one completion contributed, then it means we have a prefix; don't insert a space after it */ /* We got something. If more than one completion contributed, then it means we have a prefix; don't insert a space after it */
@ -1722,7 +1723,7 @@ static bool handle_completions(const std::vector<completion_t> &comp)
assert(data->buff_pos >= prefix_start); assert(data->buff_pos >= prefix_start);
len = data->buff_pos - prefix_start; len = data->buff_pos - prefix_start;
if (match_type_requires_full_replacement(best_match_type)) if (match_type_requires_full_replacement(best_match_type))
{ {
// No prefix // No prefix
@ -1751,7 +1752,7 @@ static bool handle_completions(const std::vector<completion_t> &comp)
reader_repaint_without_autosuggestion(); reader_repaint_without_autosuggestion();
write_loop(1, "\n", 1); write_loop(1, "\n", 1);
run_pager(prefix, is_quoted, surviving_completions); run_pager(prefix, is_quoted, surviving_completions);
} }
s_reset(&data->screen, screen_reset_abandon_line); s_reset(&data->screen, screen_reset_abandon_line);

View file

@ -217,12 +217,12 @@ static bool wildcard_complete_internal(const wcstring &orig,
debug(2, L"Got null string on line %d of file %s", __LINE__, __FILE__); debug(2, L"Got null string on line %d of file %s", __LINE__, __FILE__);
return 0; return 0;
} }
bool has_match = false; bool has_match = false;
string_fuzzy_match_t fuzzy_match(fuzzy_match_exact); string_fuzzy_match_t fuzzy_match(fuzzy_match_exact);
const bool at_end_of_wildcard = (*wc == L'\0'); const bool at_end_of_wildcard = (*wc == L'\0');
const wchar_t *completion_string = NULL; const wchar_t *completion_string = NULL;
// Hack hack hack // Hack hack hack
// Implement EXPAND_FUZZY_MATCH by short-circuiting everything if there are no remaining wildcards // Implement EXPAND_FUZZY_MATCH by short-circuiting everything if there are no remaining wildcards
if ((expand_flags & EXPAND_FUZZY_MATCH) && ! at_end_of_wildcard && ! wildcard_has(wc, true)) if ((expand_flags & EXPAND_FUZZY_MATCH) && ! at_end_of_wildcard && ! wildcard_has(wc, true))
@ -231,7 +231,7 @@ static bool wildcard_complete_internal(const wcstring &orig,
if (fuzzy_match.type != fuzzy_match_none) if (fuzzy_match.type != fuzzy_match_none)
{ {
has_match = true; has_match = true;
/* If we're not a prefix or exact match, then we need to replace the token. Note that in this case we're not going to call ourselves recursively, so these modified flags won't "leak" except into the completion. */ /* If we're not a prefix or exact match, then we need to replace the token. Note that in this case we're not going to call ourselves recursively, so these modified flags won't "leak" except into the completion. */
if (match_type_requires_full_replacement(fuzzy_match.type)) if (match_type_requires_full_replacement(fuzzy_match.type))
{ {
@ -247,9 +247,10 @@ static bool wildcard_complete_internal(const wcstring &orig,
} }
} }
} }
/* Maybe we satisfied the wildcard normally */ /* Maybe we satisfied the wildcard normally */
if (! has_match) { if (! has_match)
{
bool file_has_leading_dot = (is_first && str[0] == L'.'); bool file_has_leading_dot = (is_first && str[0] == L'.');
if (at_end_of_wildcard && ! file_has_leading_dot) if (at_end_of_wildcard && ! file_has_leading_dot)
{ {
@ -264,7 +265,7 @@ static bool wildcard_complete_internal(const wcstring &orig,
} }
} }
} }
if (has_match) if (has_match)
{ {
/* Wildcard complete */ /* Wildcard complete */
@ -281,7 +282,7 @@ static bool wildcard_complete_internal(const wcstring &orig,
} }
else else
{ {
if (desc_func && ! (expand_flags & EXPAND_NO_DESCRIPTIONS)) if (desc_func && !(expand_flags & EXPAND_NO_DESCRIPTIONS))
{ {
/* /*
A description generating function is specified, call A description generating function is specified, call
@ -299,7 +300,7 @@ static bool wildcard_complete_internal(const wcstring &orig,
append_completion(out, out_completion, out_desc, flags, fuzzy_match); append_completion(out, out_completion, out_desc, flags, fuzzy_match);
return true; return true;
} }
if (*wc == ANY_STRING) if (*wc == ANY_STRING)
{ {
bool res=false; bool res=false;