Refactor use selection as search pattern implementation

This commit is contained in:
Maxime Coste 2016-02-17 23:40:14 +00:00
parent 85a8a0c26d
commit 47df1374fe
2 changed files with 14 additions and 14 deletions

View File

@ -37,6 +37,15 @@ inline bool is_eol(const Buffer& buffer, ByteCoord coord)
return buffer.is_end(coord) or buffer[coord.line].length() == coord.column+1; return buffer.is_end(coord) or buffer[coord.line].length() == coord.column+1;
} }
inline bool is_bow(const Buffer& buffer, ByteCoord coord)
{
auto it = utf8::iterator<BufferIterator>(buffer.iterator_at(coord), buffer);
if (coord == ByteCoord{0,0})
return is_word(*it);
return not is_word(*(it-1)) and is_word(*it);
}
inline bool is_eow(const Buffer& buffer, ByteCoord coord) inline bool is_eow(const Buffer& buffer, ByteCoord coord)
{ {
if (buffer.is_end(coord) or coord == ByteCoord{0,0}) if (buffer.is_end(coord) or coord == ByteCoord{0,0})

View File

@ -670,22 +670,13 @@ void use_selection_as_search_pattern(Context& context, NormalParams params)
Vector<String> patterns; Vector<String> patterns;
auto& sels = context.selections(); auto& sels = context.selections();
const auto& buffer = context.buffer(); const auto& buffer = context.buffer();
using Utf8It = utf8::iterator<BufferIterator, utf8::InvalidPolicy::Pass>;
for (auto& sel : sels) for (auto& sel : sels)
{ {
Utf8It begin{buffer.iterator_at(sel.min()), buffer}; const auto beg = sel.min(), end = buffer.char_next(sel.max());
Utf8It end{buffer.iterator_at(sel.max()), buffer}; patterns.push_back(format("{}\\Q{}\\E{}",
++end; smart and is_bow(buffer, beg) ? "\\b" : "",
buffer.string(beg, end),
auto content = "\\Q" + buffer.string(begin.base().coord(), end.base().coord()) + "\\E"; smart and is_eow(buffer, end) ? "\\b" : ""));
if (smart)
{
if (is_word(*begin) and (begin == buffer.begin() or not is_word(*(begin-1))))
content = "\\b" + content;
if (is_word(*(end-1)) and (end == buffer.end() or not is_word(*end)))
content = content + "\\b";
}
patterns.push_back(std::move(content));
} }
const char reg = to_lower(params.reg ? params.reg : '/'); const char reg = to_lower(params.reg ? params.reg : '/');