Add support for target column preservation

Fixes #64
This commit is contained in:
Maxime Coste 2014-09-09 19:35:54 +01:00
parent 7fc1564c0c
commit 5092494427
8 changed files with 30 additions and 13 deletions

View File

@ -137,14 +137,14 @@ ByteCoord Buffer::offset_coord(ByteCoord coord, CharCount offset)
return {coord.line, line.byte_count_to(character)}; return {coord.line, line.byte_count_to(character)};
} }
ByteCoord Buffer::offset_coord(ByteCoord coord, LineCount offset) ByteCoordAndTarget Buffer::offset_coord(ByteCoordAndTarget coord, LineCount offset)
{ {
auto character = m_lines[coord.line].char_count_to(coord.column); auto character = coord.target == -1 ? m_lines[coord.line].char_count_to(coord.column) : coord.target;
auto line = Kakoune::clamp(coord.line + offset, 0_line, line_count()-1); auto line = Kakoune::clamp(coord.line + offset, 0_line, line_count()-1);
auto& content = m_lines[line]; auto& content = m_lines[line];
character = std::max(0_char, std::min(character, content.char_length() - 2)); character = std::max(0_char, std::min(character, content.char_length() - 2));
return {line, content.byte_count_to(character)}; return {line, content.byte_count_to(character), character};
} }
String Buffer::string(ByteCoord begin, ByteCoord end) const String Buffer::string(ByteCoord begin, ByteCoord end) const

View File

@ -136,7 +136,7 @@ public:
ByteCoord clamp(ByteCoord coord) const; ByteCoord clamp(ByteCoord coord) const;
ByteCoord offset_coord(ByteCoord coord, CharCount offset); ByteCoord offset_coord(ByteCoord coord, CharCount offset);
ByteCoord offset_coord(ByteCoord coord, LineCount offset); ByteCoordAndTarget offset_coord(ByteCoordAndTarget coord, LineCount offset);
const String& name() const { return m_name; } const String& name() const { return m_name; }
String display_name() const; String display_name() const;

View File

@ -99,6 +99,19 @@ struct CharCoord : LineAndColumn<CharCoord, LineCount, CharCount>
: LineAndColumn(line, column) {} : LineAndColumn(line, column) {}
}; };
struct ByteCoordAndTarget : ByteCoord
{
[[gnu::always_inline]]
constexpr ByteCoordAndTarget(LineCount line = 0, ByteCount column = 0, CharCount target = -1)
: ByteCoord(line, column), target(target) {}
[[gnu::always_inline]]
constexpr ByteCoordAndTarget(ByteCoord coord, CharCount target = -1)
: ByteCoord(coord), target(target) {}
CharCount target;
};
} }
#endif // coord_hh_INCLUDED #endif // coord_hh_INCLUDED

View File

@ -580,7 +580,7 @@ void highlight_selections(const Context& context, HighlightFlags flags, DisplayB
auto& sel = context.selections()[i]; auto& sel = context.selections()[i];
const bool forward = sel.anchor() <= sel.cursor(); const bool forward = sel.anchor() <= sel.cursor();
ByteCoord begin = forward ? sel.anchor() : buffer.char_next(sel.cursor()); ByteCoord begin = forward ? sel.anchor() : buffer.char_next(sel.cursor());
ByteCoord end = forward ? sel.cursor() : buffer.char_next(sel.anchor()); ByteCoord end = forward ? (ByteCoord)sel.cursor() : buffer.char_next(sel.anchor());
const bool primary = (i == context.selections().main_index()); const bool primary = (i == context.selections().main_index());
Face sel_face = get_face(primary ? "PrimarySelection" : "SecondarySelection"); Face sel_face = get_face(primary ? "PrimarySelection" : "SecondarySelection");

View File

@ -21,10 +21,10 @@ struct Selection
void merge_with(const Selection& range); void merge_with(const Selection& range);
ByteCoord& anchor() { return m_anchor; } ByteCoord& anchor() { return m_anchor; }
ByteCoord& cursor() { return m_cursor; } ByteCoordAndTarget& cursor() { return m_cursor; }
const ByteCoord& anchor() const { return m_anchor; } const ByteCoord& anchor() const { return m_anchor; }
const ByteCoord& cursor() const { return m_cursor; } const ByteCoordAndTarget& cursor() const { return m_cursor; }
CaptureList& captures() { return m_captures; } CaptureList& captures() { return m_captures; }
const CaptureList& captures() const { return m_captures; } const CaptureList& captures() const { return m_captures; }
@ -42,7 +42,7 @@ struct Selection
private: private:
ByteCoord m_anchor; ByteCoord m_anchor;
ByteCoord m_cursor; ByteCoordAndTarget m_cursor;
CaptureList m_captures; CaptureList m_captures;
}; };

View File

@ -18,7 +18,11 @@ inline void clear_selections(SelectionList& selections)
inline void flip_selections(SelectionList& selections) inline void flip_selections(SelectionList& selections)
{ {
for (auto& sel : selections) for (auto& sel : selections)
std::swap(sel.anchor(), sel.cursor()); {
ByteCoord tmp = sel.anchor();
sel.anchor() = sel.cursor();
sel.cursor() = tmp;
}
selections.check_invariant(); selections.check_invariant();
} }

View File

@ -253,7 +253,7 @@ ByteCoord Window::offset_coord(ByteCoord coord, CharCount offset)
return buffer().offset_coord(coord, offset); return buffer().offset_coord(coord, offset);
} }
ByteCoord Window::offset_coord(ByteCoord coord, LineCount offset) ByteCoordAndTarget Window::offset_coord(ByteCoordAndTarget coord, LineCount offset)
{ {
auto line = clamp(coord.line + offset, 0_line, buffer().line_count()-1); auto line = clamp(coord.line + offset, 0_line, buffer().line_count()-1);
DisplayBuffer display_buffer; DisplayBuffer display_buffer;
@ -267,8 +267,8 @@ ByteCoord Window::offset_coord(ByteCoord coord, LineCount offset)
m_highlighters(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer); m_highlighters(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer);
m_builtin_highlighters(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer); m_builtin_highlighters(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer);
CharCount column = find_display_column(lines[0], buffer(), coord); CharCount column = coord.target == -1 ? find_display_column(lines[0], buffer(), coord) : coord.target;
return find_buffer_coord(lines[1], buffer(), column); return { find_buffer_coord(lines[1], buffer(), column), column };
} }
void Window::on_option_changed(const Option& option) void Window::on_option_changed(const Option& option)

View File

@ -51,7 +51,7 @@ public:
void forget_timestamp() { m_timestamp = -1; } void forget_timestamp() { m_timestamp = -1; }
ByteCoord offset_coord(ByteCoord coord, CharCount offset); ByteCoord offset_coord(ByteCoord coord, CharCount offset);
ByteCoord offset_coord(ByteCoord coord, LineCount offset); ByteCoordAndTarget offset_coord(ByteCoordAndTarget coord, LineCount offset);
private: private:
Window(const Window&) = delete; Window(const Window&) = delete;