Move some buffer related utility functions to buffer_utils.{cc,hh}

This commit is contained in:
Maxime Coste 2014-04-28 19:48:23 +01:00
parent 49bc7f8767
commit 7190791927
6 changed files with 72 additions and 49 deletions

23
src/buffer_utils.cc Normal file
View File

@ -0,0 +1,23 @@
#include "buffer_utils.hh"
namespace Kakoune
{
CharCount get_column(const Buffer& buffer,
CharCount tabstop, BufferCoord coord)
{
auto& line = buffer[coord.line];
auto col = 0_char;
for (auto it = line.begin();
it != line.end() and coord.column > (int)(it - line.begin());
it = utf8::next(it))
{
if (*it == '\t')
col = (col / tabstop + 1) * tabstop;
else
++col;
}
return col;
}
}

47
src/buffer_utils.hh Normal file
View File

@ -0,0 +1,47 @@
#ifndef buffer_utils_hh_INCLUDED
#define buffer_utils_hh_INCLUDED
#include "buffer.hh"
#include "selection.hh"
namespace Kakoune
{
inline String content(const Buffer& buffer, const Selection& range)
{
return buffer.string(range.min(), buffer.char_next(range.max()));
}
inline BufferIterator erase(Buffer& buffer, const Selection& range)
{
return buffer.erase(buffer.iterator_at(range.min()),
utf8::next(buffer.iterator_at(range.max())));
}
inline CharCount char_length(const Buffer& buffer, const Selection& range)
{
return utf8::distance(buffer.iterator_at(range.min()),
utf8::next(buffer.iterator_at(range.max())));
}
inline void avoid_eol(const Buffer& buffer, BufferCoord& coord)
{
const auto column = coord.column;
const auto& line = buffer[coord.line];
if (column != 0 and column == line.length() - 1)
coord.column = line.byte_count_to(line.char_length() - 2);
}
inline void avoid_eol(const Buffer& buffer, Selection& sel)
{
avoid_eol(buffer, sel.anchor());
avoid_eol(buffer, sel.cursor());
}
CharCount get_column(const Buffer& buffer,
CharCount tabstop, BufferCoord coord);
}
#endif // buffer_utils_hh_INCLUDED

View File

@ -1,6 +1,7 @@
#include "assert.hh"
#include "buffer.hh"
#include "buffer_manager.hh"
#include "buffer_utils.hh"
#include "client_manager.hh"
#include "color_registry.hh"
#include "command_manager.hh"

View File

@ -1040,23 +1040,6 @@ void save_selections(Context& context, int)
" selections", get_color("Information") });
}
static CharCount get_column(const Buffer& buffer,
CharCount tabstop, BufferCoord coord)
{
auto& line = buffer[coord.line];
auto col = 0_char;
for (auto it = line.begin();
it != line.end() and coord.column > (int)(it - line.begin());
it = utf8::next(it))
{
if (*it == '\t')
col = (col / tabstop + 1) * tabstop;
else
++col;
}
return col;
}
void align(Context& context, int)
{
auto& selections = context.selections();

View File

@ -50,38 +50,6 @@ inline bool overlaps(const Selection& lhs, const Selection& rhs)
: lhs.min() <= rhs.max();
}
inline String content(const Buffer& buffer, const Selection& range)
{
return buffer.string(range.min(), buffer.char_next(range.max()));
}
inline BufferIterator erase(Buffer& buffer, const Selection& range)
{
return buffer.erase(buffer.iterator_at(range.min()),
utf8::next(buffer.iterator_at(range.max())));
}
inline CharCount char_length(const Buffer& buffer, const Selection& range)
{
return utf8::distance(buffer.iterator_at(range.min()),
utf8::next(buffer.iterator_at(range.max())));
}
inline void avoid_eol(const Buffer& buffer, BufferCoord& coord)
{
const auto column = coord.column;
const auto& line = buffer[coord.line];
if (column != 0 and column == line.length() - 1)
coord.column = line.byte_count_to(line.char_length() - 2);
}
inline void avoid_eol(const Buffer& buffer, Selection& sel)
{
avoid_eol(buffer, sel.anchor());
avoid_eol(buffer, sel.cursor());
}
static bool compare_selections(const Selection& lhs, const Selection& rhs)
{
return lhs.min() < rhs.min();

View File

@ -2,6 +2,7 @@
#define selectors_hh_INCLUDED
#include "selection.hh"
#include "buffer_utils.hh"
#include "unicode.hh"
#include "utf8_iterator.hh"