Add leading/trailing ellipsis for truncated search results

This commit is contained in:
Allan Odgaard
2016-08-24 18:05:12 +02:00
parent 5d4b9e3168
commit 20f90b507b
3 changed files with 11 additions and 0 deletions

View File

@@ -272,6 +272,11 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si
std::string middle = m.excerpt.substr(from, to - from);
std::string suffix = m.excerpt.substr(to);
if(m.truncate_head)
prefix.insert(0, "…");
if(m.truncate_tail)
suffix.insert(suffix.size(), "…");
if(replacementString)
middle = m.captures.empty() ? to_s(replacementString) : format_string::expand(to_s(replacementString), m.captures);

View File

@@ -210,9 +210,11 @@ namespace find
text::pos_t to(crlfCount, it.to - bol);
size_t toOffset = bol == it.to ? bol : (eol != std::string::npos ? (it.to <= eol ? eol : eol + crlf.size()) : text.size());
size_t orgFromOffset = fromOffset;
if(it.from - fromOffset > 200)
fromOffset = utf8::find_safe_end(text.begin(), text.begin() + it.from - ((it.from - fromOffset) % 150)) - text.begin();
size_t orgToOffset = toOffset;
if(toOffset - fromOffset > 500)
toOffset = utf8::find_safe_end(text.begin(), text.begin() + std::max<size_t>(fromOffset + 500, it.to)) - text.begin();
@@ -222,6 +224,8 @@ namespace find
match_t res(document, crc32.checksum(), it.from, it.to, text::range_t(from, to), it.captures);
res.excerpt = text.substr(fromOffset, toOffset - fromOffset);
res.excerpt_offset = fromOffset;
res.truncate_head = orgFromOffset < fromOffset;
res.truncate_tail = toOffset < orgToOffset;
res.line_number = from.line;
res.newlines = crlf;
results.push_back(res);

View File

@@ -26,6 +26,8 @@ namespace find
size_t excerpt_offset = 0;
size_t line_number = 0;
std::string newlines = "\n";
bool truncate_head = false;
bool truncate_tail = false;
};
struct scan_path_t