Support arbitrary newlines for search result excerpts

Previously we expected only LF to mark a newline but in theory we can receive CRLF or CR separated text (from files on disk) which would cause incorrect excerpt for matches crossing line boundaries.
This commit is contained in:
Allan Odgaard
2016-05-24 16:49:54 +02:00
parent 7c074b92e5
commit b202ad3e02
2 changed files with 6 additions and 4 deletions

View File

@@ -53,7 +53,7 @@ static void append (ns::attr_string_t& dst, std::string const& src, size_t from,
dst.add(src.substr(begin, to-begin));
}
static NSAttributedString* AttributedStringForMatch (std::string const& text, size_t from, size_t to, size_t n)
static NSAttributedString* AttributedStringForMatch (std::string const& text, size_t from, size_t to, size_t n, std::string const& newlines)
{
ns::attr_string_t str;
str.add(ns::style::line_break(NSLineBreakByTruncatingTail));
@@ -66,7 +66,8 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si
size_t last = text.size();
for(size_t it = 0; it != last; )
{
size_t eol = std::find(text.begin() + it, text.end(), '\n') - text.begin();
size_t eol = text.find(newlines, it);
eol = eol != std::string::npos ? eol : last;
if(oak::cap(it, from, eol) == from)
{
@@ -103,7 +104,7 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si
str.add(ns::style::unbold);
}
if(++eol == to)
if((eol += newlines.size()) == to)
inMatch = false;
if(eol != last)
@@ -270,7 +271,7 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si
<< text::format("%zu-%zu: Range is not valid UTF-8, please contact: http://macromates.com/support", m.first, m.last);
}
_excerpt = AttributedStringForMatch(prefix + middle + suffix, prefix.size(), prefix.size() + middle.size(), m.line_number);
_excerpt = AttributedStringForMatch(prefix + middle + suffix, prefix.size(), prefix.size() + middle.size(), m.line_number, m.newlines);
_excerptReplaceString = replacementString;
return _excerpt;
}

View File

@@ -25,6 +25,7 @@ namespace find
std::string excerpt;
size_t excerpt_offset = 0;
size_t line_number = 0;
std::string newlines = "\n";
};
struct scan_path_t