Rewrite select_lines and trim_partial_lines

Fixes #338 for real (I hope)
This commit is contained in:
Maxime Coste 2015-07-23 21:03:38 +01:00
parent 2946504a17
commit 58101645ab

View File

@ -517,42 +517,39 @@ Selection select_argument(const Buffer& buffer, const Selection& selection,
Selection select_lines(const Buffer& buffer, const Selection& selection) Selection select_lines(const Buffer& buffer, const Selection& selection)
{ {
// no need to be utf8 aware for is_eol as we only use \n as line seperator ByteCoord anchor = selection.anchor();
BufferIterator first = buffer.iterator_at(selection.anchor()); ByteCoord cursor = selection.cursor();
BufferIterator last = buffer.iterator_at(selection.cursor()); ByteCoord& to_line_start = anchor <= cursor ? anchor : cursor;
BufferIterator& to_line_start = first <= last ? first : last; ByteCoord& to_line_end = anchor <= cursor ? cursor : anchor;
BufferIterator& to_line_end = first <= last ? last : first;
--to_line_start; to_line_start.column = 0;
skip_while_reverse(to_line_start, buffer.begin(), [](char cur) { return not is_eol(cur); }); to_line_end.column = buffer[to_line_end.line].length()-1;
if (is_eol(*to_line_start))
{
if (++to_line_start == buffer.end())
--to_line_start;
}
skip_while(to_line_end, buffer.end(), [](char cur) { return not is_eol(cur); }); return target_eol({anchor, cursor});
if (to_line_end == buffer.end())
--to_line_end;
return target_eol({first.coord(), last.coord()});
} }
Selection trim_partial_lines(const Buffer& buffer, const Selection& selection) Selection trim_partial_lines(const Buffer& buffer, const Selection& selection)
{ {
// same as select_lines ByteCoord anchor = selection.anchor();
BufferIterator first = buffer.iterator_at(selection.anchor()); ByteCoord cursor = selection.cursor();
BufferIterator last = buffer.iterator_at(selection.cursor()); ByteCoord& to_line_start = anchor <= cursor ? anchor : cursor;
BufferIterator& to_line_start = first <= last ? first : last; ByteCoord& to_line_end = anchor <= cursor ? cursor : anchor;
BufferIterator& to_line_end = first <= last ? last : first;
while (to_line_start != buffer.begin() and *(to_line_start-1) != '\n') if (to_line_start.column != 0)
++to_line_start; to_line_start = to_line_start.line+1;
while (to_line_end.coord() != buffer.back_coord() and if (to_line_end.column != buffer[to_line_end.line].length()-1)
*(to_line_end+1) != '\n' and to_line_end != to_line_start) {
--to_line_end; if (to_line_end.line == 0)
return selection;
return target_eol({first.coord(), last.coord()}); auto prev_line = to_line_end.line-1;
to_line_end = ByteCoord{ prev_line, buffer[prev_line].length()-1 };
}
if (to_line_start > to_line_end)
return selection;
return target_eol({anchor, cursor});
} }
void select_buffer(SelectionList& selections) void select_buffer(SelectionList& selections)