Clean up abbreviation implementation a bit

This commit is contained in:
ridiculousfish 2018-09-01 13:18:34 -07:00
parent 051605b593
commit 9269a5e0a8

View File

@ -1146,18 +1146,19 @@ bool fish_xdm_login_hack_hack_hack_hack(std::vector<std::string> *cmds, int argc
return result; return result;
} }
std::unordered_map<const wcstring, const wcstring> abbreviations; static owning_lock<std::unordered_map<wcstring, wcstring>> s_abbreviations;
void update_abbr_cache(const wchar_t *op, const wcstring &varname) { void update_abbr_cache(const wchar_t *op, const wcstring &varname) {
wcstring abbr; wcstring abbr;
if (!unescape_string(varname.substr(wcslen(L"_fish_abbr_")), &abbr, 0, STRING_STYLE_VAR)) { if (!unescape_string(varname.substr(wcslen(L"_fish_abbr_")), &abbr, 0, STRING_STYLE_VAR)) {
debug(1, L"Abbreviation var '%ls' is not correctly encoded, ignoring it.", varname.c_str()); debug(1, L"Abbreviation var '%ls' is not correctly encoded, ignoring it.", varname.c_str());
return; return;
} }
abbreviations.erase(abbr); auto abbreviations = s_abbreviations.acquire();
abbreviations->erase(abbr);
if (wcscmp(op, L"ERASE") != 0) { if (wcscmp(op, L"ERASE") != 0) {
const auto expansion = env_get(varname); const auto expansion = env_get(varname);
if (!expansion.missing_or_empty()) { if (!expansion.missing_or_empty()) {
abbreviations.emplace(std::make_pair(abbr, expansion->as_string())); abbreviations->emplace(abbr, expansion->as_string());
} }
} }
} }
@ -1165,20 +1166,9 @@ void update_abbr_cache(const wchar_t *op, const wcstring &varname) {
bool expand_abbreviation(const wcstring &src, wcstring *output) { bool expand_abbreviation(const wcstring &src, wcstring *output) {
if (src.empty()) return false; if (src.empty()) return false;
auto abbr = abbreviations.find(src); auto abbreviations = s_abbreviations.acquire();
if (abbr == abbreviations.end()) return false; auto abbr = abbreviations->find(src);
if (abbr == abbreviations->end()) return false;
if (output != NULL) output->assign(abbr->second); if (output != NULL) output->assign(abbr->second);
return true; return true;
#if 0
for (auto abbr : abbreviations) {
if (src == abbr.first) {
// We found a matching abbreviation. Set output to the expansion.
if (output != NULL) output->assign(abbr.second);
return true;
}
}
return false;
#endif
} }