Code cleanup

This commit is contained in:
Maxime Coste 2016-02-28 18:30:35 +00:00
parent aa39380f4b
commit 548e10597c
5 changed files with 17 additions and 15 deletions

View File

@ -162,6 +162,12 @@ bool contains(Container&& container, const T& value)
return find(container, value) != end(container); return find(container, value) != end(container);
} }
template<typename Container, typename T>
bool contains_that(Container&& container, T op)
{
return find_if(container, op) != end(container);
}
template<typename Container, typename U> template<typename Container, typename U>
void unordered_erase(Container&& vec, U&& value) void unordered_erase(Container&& vec, U&& value)
{ {

View File

@ -66,8 +66,8 @@ EnableIfWithoutBitOps<Enum> option_from_string(StringView str, Enum& e)
{ {
constexpr auto desc = enum_desc(Enum{}); constexpr auto desc = enum_desc(Enum{});
auto it = find_if(desc, [str](const EnumDesc<Enum>& d) { return d.name == str; }); auto it = find_if(desc, [str](const EnumDesc<Enum>& d) { return d.name == str; });
if (it == desc.end()) if (it == desc.end())
throw runtime_error(format("invalid enum value '{}'", str)); throw runtime_error(format("invalid enum value '{}'", str));
e = it->value; e = it->value;
} }

View File

@ -241,9 +241,8 @@ InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos,
} }
size_t timestamp = (size_t)str_to_int({match[4].first, match[4].second}); size_t timestamp = (size_t)str_to_int({match[4].first, match[4].second});
auto changes = buffer.changes_since(timestamp); auto changes = buffer.changes_since(timestamp);
if (find_if(changes, [&](const Buffer::Change& change){ if (contains_that(changes, [&](const Buffer::Change& change)
return change.begin < coord; { return change.begin < coord; }))
}) != changes.end())
return {}; return {};
if (cursor_pos.line == coord.line and cursor_pos.column >= coord.column) if (cursor_pos.line == coord.line and cursor_pos.column >= coord.column)

View File

@ -172,18 +172,15 @@ void goto_commands(Context& context, NormalParams params)
} }
case 'f': case 'f':
{ {
const Selection& sel = context.selections().main(); auto filename = content(buffer, context.selections().main());
String filename = content(buffer, sel);
static constexpr char forbidden[] = { '\'', '\\', '\0' }; static constexpr char forbidden[] = { '\'', '\\', '\0' };
for (auto c : filename) if (contains_that(filename, [](char c){ return contains(forbidden, c); }))
if (contains(forbidden, c)) return;
return;
auto paths = context.options()["path"].get<Vector<String, MemoryDomain::Options>>(); auto paths = context.options()["path"].get<Vector<String, MemoryDomain::Options>>();
const String& buffer_name = buffer.name(); StringView buffer_dir = split_path(buffer.name()).first;
auto it = find(reversed(buffer_name), '/'); if (not buffer_dir.empty())
if (it != buffer_name.rend()) paths.insert(paths.begin(), buffer_dir.str());
paths.insert(paths.begin(), String{buffer_name.begin(), it.base()});
String path = find_file(filename, paths); String path = find_file(filename, paths);
if (path.empty()) if (path.empty())

View File

@ -90,7 +90,7 @@ Vector<String> generate_env(StringView cmdline, const Context& context, const Sh
return s.length() > name.length() and return s.length() > name.length() and
prefix_match(s, name) and s[name.length()] == '='; prefix_match(s, name) and s[name.length()] == '=';
}; };
if (find_if(kak_env, match_name) != kak_env.end()) if (contains_that(kak_env, match_name))
continue; continue;
auto var_it = shell_context.env_vars.find(name); auto var_it = shell_context.env_vars.find(name);