1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-23 19:46:33 +03:00

LineAndColumn: segregate into WindowCoord and BufferCoord

Having the same type to specify coordinates in window space or buffer
space is error prone, now the compiler will tell if we use one for
another.
This commit is contained in:
Maxime Coste 2011-09-05 19:06:31 +00:00
parent d5012c9379
commit 6668151c78
6 changed files with 77 additions and 50 deletions

View File

@ -135,7 +135,7 @@ void Buffer::insert(const BufferIterator& position, const BufferString& string)
compute_lines();
}
BufferIterator Buffer::iterator_at(const LineAndColumn& line_and_column) const
BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const
{
if (m_lines.empty())
return begin();
@ -145,9 +145,9 @@ BufferIterator Buffer::iterator_at(const LineAndColumn& line_and_column) const
return BufferIterator(*this, m_lines[line] + column);
}
LineAndColumn Buffer::line_and_column_at(const BufferIterator& iterator) const
BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const
{
LineAndColumn result;
BufferCoord result;
if (not m_lines.empty())
{
result.line = line_at(iterator);
@ -174,12 +174,12 @@ BufferSize Buffer::line_length(BufferPos line) const
return end - m_lines[line];
}
LineAndColumn Buffer::clamp(const LineAndColumn& line_and_column) const
BufferCoord Buffer::clamp(const BufferCoord& line_and_column) const
{
if (m_lines.empty())
return LineAndColumn();
return BufferCoord();
LineAndColumn result(line_and_column.line, line_and_column.column);
BufferCoord result(line_and_column.line, line_and_column.column);
result.line = Kakoune::clamp<int>(0, m_lines.size() - 1, result.line);
result.column = Kakoune::clamp<int>(0, line_length(result.line), result.column);
return result;

View File

@ -4,6 +4,8 @@
#include <string>
#include <vector>
#include "utils.hh"
namespace Kakoune
{
@ -13,13 +15,10 @@ typedef int BufferSize;
typedef char BufferChar;
typedef std::basic_string<BufferChar> BufferString;
struct LineAndColumn
struct BufferCoord : LineAndColumn
{
BufferPos line;
BufferPos column;
LineAndColumn(BufferPos line = 0, BufferPos column = 0)
: line(line), column(column) {}
BufferCoord(int line = 0, int column = 0)
: LineAndColumn(line, column) {}
};
class BufferIterator
@ -82,10 +81,10 @@ public:
BufferSize length() const;
BufferIterator iterator_at(const LineAndColumn& line_and_column) const;
LineAndColumn line_and_column_at(const BufferIterator& iterator) const;
BufferIterator iterator_at(const BufferCoord& line_and_column) const;
BufferCoord line_and_column_at(const BufferIterator& iterator) const;
LineAndColumn clamp(const LineAndColumn& line_and_column) const;
BufferCoord clamp(const BufferCoord& line_and_column) const;
const std::string& name() const { return m_name; }

View File

@ -15,11 +15,11 @@ void draw_window(Window& window)
getmaxyx(stdscr, max_y, max_x);
max_y -= 1;
window.set_dimensions(LineAndColumn(max_y, max_x));
window.set_dimensions(WindowCoord(max_y, max_x));
window.update_display_buffer();
LineAndColumn position;
WindowCoord position;
for (const DisplayAtom& atom : window.display_buffer())
{
const std::string& content = atom.content;
@ -64,7 +64,7 @@ void draw_window(Window& window)
addch('~');
}
const LineAndColumn& cursor_position = window.cursor_position();
const WindowCoord& cursor_position = window.cursor_position();
move(cursor_position.line, cursor_position.column);
}
@ -136,7 +136,7 @@ void do_insert(Window& window)
{
print_status("-- INSERT --");
std::string inserted;
LineAndColumn pos = window.cursor_position();
WindowCoord pos = window.cursor_position();
move(pos.line, pos.column);
refresh();
while(true)
@ -281,10 +281,10 @@ void do_search(Window& window)
std::unordered_map<char, std::function<void (Window& window, int count)>> keymap =
{
{ 'h', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(LineAndColumn(0, -count)); window.empty_selections(); } },
{ 'j', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(LineAndColumn(count, 0)); window.empty_selections(); } },
{ 'k', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(LineAndColumn(-count, 0)); window.empty_selections(); } },
{ 'l', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(LineAndColumn(0, count)); window.empty_selections(); } },
{ 'h', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(0, -count)); window.empty_selections(); } },
{ 'j', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(count, 0)); window.empty_selections(); } },
{ 'k', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(-count, 0)); window.empty_selections(); } },
{ 'l', [](Window& window, int count) { if (count == 0) count = 1; window.move_cursor(WindowCoord(0, count)); window.empty_selections(); } },
{ 'd', [](Window& window, int count) { window.erase(); window.empty_selections(); } },
{ 'c', [](Window& window, int count) { window.erase(); do_insert(window); } },
{ 'i', [](Window& window, int count) { do_insert(window); } },

18
src/utils.hh Normal file
View File

@ -0,0 +1,18 @@
#ifndef utils_hh_INCLUDED
#define utils_hh_INCLUDED
namespace Kakoune
{
struct LineAndColumn
{
int line;
int column;
LineAndColumn(int line = 0, int column = 0)
: line(line), column(column) {}
};
}
#endif // utils_hh_INCLUDED

View File

@ -28,9 +28,9 @@ void Window::erase()
}
}
static LineAndColumn measure_string(const Window::String& string)
static WindowCoord measure_string(const Window::String& string)
{
LineAndColumn result(0, 0);
WindowCoord result(0, 0);
for (size_t i = 0; i < string.length(); ++i)
{
if (string[i] == '\n')
@ -64,7 +64,7 @@ void Window::append(const String& string)
{
if (m_selections.empty())
{
move_cursor(LineAndColumn(0 , 1));
move_cursor(WindowCoord(0 , 1));
insert(string);
}
@ -74,24 +74,24 @@ void Window::append(const String& string)
}
}
LineAndColumn Window::window_to_buffer(const LineAndColumn& window_pos) const
BufferCoord Window::window_to_buffer(const WindowCoord& window_pos) const
{
return LineAndColumn(m_position.line + window_pos.line,
m_position.column + window_pos.column);
return BufferCoord(m_position.line + window_pos.line,
m_position.column + window_pos.column);
}
LineAndColumn Window::buffer_to_window(const LineAndColumn& buffer_pos) const
WindowCoord Window::buffer_to_window(const BufferCoord& buffer_pos) const
{
return LineAndColumn(buffer_pos.line - m_position.line,
buffer_pos.column - m_position.column);
return WindowCoord(buffer_pos.line - m_position.line,
buffer_pos.column - m_position.column);
}
BufferIterator Window::iterator_at(const LineAndColumn& window_pos) const
BufferIterator Window::iterator_at(const WindowCoord& window_pos) const
{
return m_buffer->iterator_at(window_to_buffer(window_pos));
}
LineAndColumn Window::line_and_column_at(const BufferIterator& iterator) const
WindowCoord Window::line_and_column_at(const BufferIterator& iterator) const
{
return buffer_to_window(m_buffer->line_and_column_at(iterator));
}
@ -119,11 +119,11 @@ void Window::select(bool append, const Selector& selector)
scroll_to_keep_cursor_visible_ifn();
}
void Window::move_cursor(const LineAndColumn& offset)
void Window::move_cursor(const WindowCoord& offset)
{
LineAndColumn target_position =
window_to_buffer(LineAndColumn(m_cursor.line + offset.line,
m_cursor.column + offset.column));
BufferCoord target_position =
window_to_buffer(WindowCoord(m_cursor.line + offset.line,
m_cursor.column + offset.column));
m_cursor = buffer_to_window(m_buffer->clamp(target_position));
@ -165,7 +165,7 @@ void Window::update_display_buffer()
}
}
void Window::set_dimensions(const LineAndColumn& dimensions)
void Window::set_dimensions(const WindowCoord& dimensions)
{
m_dimensions = dimensions;
}

View File

@ -3,12 +3,21 @@
#include <memory>
#include <functional>
#include "utils.hh"
#include "buffer.hh"
#include "display_buffer.hh"
namespace Kakoune
{
struct WindowCoord : LineAndColumn
{
WindowCoord(int line = 0, int column = 0)
: LineAndColumn(line, column) {}
};
struct Selection
{
Selection(const BufferIterator& begin, const BufferIterator& end)
@ -33,24 +42,25 @@ public:
void insert(const String& string);
void append(const String& string);
const LineAndColumn& position() const { return m_position; }
const LineAndColumn& cursor_position() const { return m_cursor; }
const BufferCoord& position() const { return m_position; }
const WindowCoord& cursor_position() const { return m_cursor; }
const std::shared_ptr<Buffer>& buffer() const { return m_buffer; }
LineAndColumn window_to_buffer(const LineAndColumn& window_pos) const;
LineAndColumn buffer_to_window(const LineAndColumn& buffer_pos) const;
BufferIterator iterator_at(const LineAndColumn& window_pos) const;
LineAndColumn line_and_column_at(const BufferIterator& iterator) const;
BufferCoord window_to_buffer(const WindowCoord& window_pos) const;
WindowCoord buffer_to_window(const BufferCoord& buffer_pos) const;
void move_cursor(const LineAndColumn& offset);
BufferIterator iterator_at(const WindowCoord& window_pos) const;
WindowCoord line_and_column_at(const BufferIterator& iterator) const;
void move_cursor(const WindowCoord& offset);
const SelectionList& selections() const { return m_selections; }
void empty_selections();
void select(bool append, const Selector& selector);
void set_dimensions(const LineAndColumn& dimensions);
void set_dimensions(const WindowCoord& dimensions);
const DisplayBuffer& display_buffer() const { return m_display_buffer; }
@ -60,9 +70,9 @@ private:
void scroll_to_keep_cursor_visible_ifn();
std::shared_ptr<Buffer> m_buffer;
LineAndColumn m_position;
LineAndColumn m_cursor;
LineAndColumn m_dimensions;
BufferCoord m_position;
WindowCoord m_cursor;
WindowCoord m_dimensions;
SelectionList m_selections;
DisplayBuffer m_display_buffer;
};