Distinguish between non-eol max column target and plain max column

This commit is contained in:
Maxime Coste 2022-06-08 19:43:03 +10:00
parent b5e565bd6a
commit df79d0c245
6 changed files with 9 additions and 7 deletions

View File

@ -155,14 +155,15 @@ BufferCoord Buffer::clamp(BufferCoord coord) const
return coord;
}
BufferCoord Buffer::offset_coord(BufferCoord coord, CharCount offset, ColumnCount, bool) const
BufferCoord Buffer::offset_coord(BufferCoord coord, CharCount offset, ColumnCount) const
{
return utf8::advance(iterator_at(coord), offset < 0 ? begin() : end()-1, offset).coord();
}
BufferCoordAndTarget Buffer::offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop, bool avoid_eol) const
BufferCoordAndTarget Buffer::offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop) const
{
const auto column = coord.target == -1 ? get_column(*this, tabstop, coord) : coord.target;
const bool avoid_eol = coord.target < max_column;
const auto line = Kakoune::clamp(coord.line + offset, 0_line, line_count()-1);
const auto max_column = get_column(*this, tabstop, {line, m_lines[line].length()-1});
const auto final_column = std::max(0_col, std::min(column, max_column - (avoid_eol ? 1 : 0)));

View File

@ -188,8 +188,8 @@ public:
// returns nearest valid coordinates from given ones
BufferCoord clamp(BufferCoord coord) const;
BufferCoord offset_coord(BufferCoord coord, CharCount offset, ColumnCount, bool) const;
BufferCoordAndTarget offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop, bool avoid_eol) const;
BufferCoord offset_coord(BufferCoord coord, CharCount offset, ColumnCount) const;
BufferCoordAndTarget offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop) const;
const String& name() const { return m_name; }
const String& display_name() const { return m_display_name; }

View File

@ -1445,7 +1445,7 @@ private:
const ColumnCount tabstop = context().options()["tabstop"].get<int>();
for (auto& sel : selections)
{
auto cursor = context().buffer().offset_coord(sel.cursor(), offset, tabstop, false);
auto cursor = context().buffer().offset_coord(sel.cursor(), offset, tabstop);
sel.anchor() = sel.cursor() = cursor;
}
selections.sort_and_merge_overlapping();

View File

@ -2076,7 +2076,7 @@ void move_cursor(Context& context, NormalParams params)
auto& selections = context.selections();
for (auto& sel : selections)
{
auto cursor = context.buffer().offset_coord(sel.cursor(), offset, tabstop, true);
auto cursor = context.buffer().offset_coord(sel.cursor(), offset, tabstop);
sel.anchor() = mode == SelectMode::Extend ? sel.anchor() : cursor;
sel.cursor() = cursor;
}

View File

@ -11,6 +11,7 @@ namespace Kakoune
using CaptureList = Vector<String, MemoryDomain::Selections>;
constexpr ColumnCount max_column{std::numeric_limits<int>::max()};
constexpr ColumnCount max_non_eol_column{max_column-1};
// A selection is a Selection, associated with a CaptureList
struct Selection

View File

@ -184,7 +184,7 @@ select_to_line_end(const Context& context, const Selection& selection)
buffer.iterator_at(line)).coord();
if (end < begin) // Do not go backward when cursor is on eol
end = begin;
return Selection{only_move ? end : begin, {end, max_column}};
return Selection{only_move ? end : begin, {end, max_non_eol_column}};
}
template Optional<Selection> select_to_line_end<false>(const Context&, const Selection&);
template Optional<Selection> select_to_line_end<true>(const Context&, const Selection&);