merge AtomContent into DisplayAtom

This commit is contained in:
Maxime Coste 2013-07-24 13:55:57 +01:00
parent f6308409a1
commit d6425f1d50
6 changed files with 82 additions and 91 deletions

View File

@ -5,7 +5,7 @@
namespace Kakoune
{
void AtomContent::trim_begin(CharCount count)
void DisplayAtom::trim_begin(CharCount count)
{
if (m_type == BufferRange)
m_begin = utf8::advance(m_buffer->iterator_at(m_begin),
@ -14,7 +14,7 @@ void AtomContent::trim_begin(CharCount count)
m_text = m_text.substr(count);
}
void AtomContent::trim_end(CharCount count)
void DisplayAtom::trim_end(CharCount count)
{
if (m_type == BufferRange)
m_end = utf8::advance(m_buffer->iterator_at(m_end),
@ -31,32 +31,32 @@ DisplayLine::DisplayLine(AtomList atoms)
DisplayLine::iterator DisplayLine::split(iterator it, BufferCoord pos)
{
kak_assert(it->content.type() == AtomContent::BufferRange);
kak_assert(it->content.begin() < pos);
kak_assert(it->content.end() > pos);
kak_assert(it->type() == DisplayAtom::BufferRange);
kak_assert(it->begin() < pos);
kak_assert(it->end() > pos);
DisplayAtom atom = *it;
atom.content.m_end = pos;
it->content.m_begin = pos;
atom.m_end = pos;
it->m_begin = pos;
return m_atoms.insert(it, std::move(atom));
}
DisplayLine::iterator DisplayLine::insert(iterator it, DisplayAtom atom)
{
if (atom.content.has_buffer_range())
if (atom.has_buffer_range())
{
m_range.first = std::min(m_range.first, atom.content.begin());
m_range.second = std::max(m_range.second, atom.content.end());
m_range.first = std::min(m_range.first, atom.begin());
m_range.second = std::max(m_range.second, atom.end());
}
return m_atoms.insert(it, std::move(atom));
}
void DisplayLine::push_back(DisplayAtom atom)
{
if (atom.content.has_buffer_range())
if (atom.has_buffer_range())
{
m_range.first = std::min(m_range.first, atom.content.begin());
m_range.second = std::max(m_range.second, atom.content.end());
m_range.first = std::min(m_range.first, atom.begin());
m_range.second = std::max(m_range.second, atom.end());
}
m_atoms.push_back(std::move(atom));
}
@ -76,21 +76,21 @@ void DisplayLine::optimize()
if (atom.colors == next_atom.colors and
atom.attribute == next_atom.attribute and
atom.content.type() == next_atom.content.type())
atom.type() == next_atom.type())
{
auto type = atom.content.type();
if ((type == AtomContent::BufferRange or
type == AtomContent::ReplacedBufferRange) and
next_atom.content.begin() == atom.content.end())
auto type = atom.type();
if ((type == DisplayAtom::BufferRange or
type == DisplayAtom::ReplacedBufferRange) and
next_atom.begin() == atom.end())
{
atom.content.m_end = next_atom.content.end();
if (type == AtomContent::ReplacedBufferRange)
atom.content.m_text += next_atom.content.m_text;
atom.m_end = next_atom.end();
if (type == DisplayAtom::ReplacedBufferRange)
atom.m_text += next_atom.m_text;
merged = true;
}
if (type == AtomContent::Text)
if (type == DisplayAtom::Text)
{
atom.content.m_text += next_atom.content.m_text;
atom.m_text += next_atom.m_text;
merged = true;
}
}
@ -105,7 +105,7 @@ CharCount DisplayLine::length() const
{
CharCount len = 0;
for (auto& atom : m_atoms)
len += atom.content.length();
len += atom.length();
return len;
}
@ -113,13 +113,13 @@ void DisplayLine::trim(CharCount first_char, CharCount char_count)
{
for (auto it = begin(); first_char > 0 and it != end(); )
{
if (not it->content.has_buffer_range())
if (not it->has_buffer_range())
{
++it;
continue;
}
auto len = it->content.length();
auto len = it->length();
if (len <= first_char)
{
m_atoms.erase(it);
@ -127,16 +127,16 @@ void DisplayLine::trim(CharCount first_char, CharCount char_count)
}
else
{
it->content.trim_begin(first_char);
it->trim_begin(first_char);
first_char = 0;
}
}
auto it = begin();
for (; it != end() and char_count > 0; ++it)
char_count -= it->content.length();
char_count -= it->length();
if (char_count < 0)
(it-1)->content.trim_end(-char_count);
(it-1)->trim_end(-char_count);
m_atoms.erase(it, end());
compute_range();
@ -147,10 +147,10 @@ void DisplayLine::compute_range()
m_range = { {INT_MAX, INT_MAX}, {INT_MIN, INT_MIN} };
for (auto& atom : m_atoms)
{
if (not atom.content.has_buffer_range())
if (not atom.has_buffer_range())
continue;
m_range.first = std::min(m_range.first, atom.content.begin());
m_range.second = std::max(m_range.second, atom.content.end());
m_range.first = std::min(m_range.first, atom.begin());
m_range.second = std::max(m_range.second, atom.end());
}
}

View File

@ -29,16 +29,17 @@ enum Attributes
Bold = 8
};
struct AtomContent
struct DisplayAtom
{
public:
enum Type { BufferRange, ReplacedBufferRange, Text };
AtomContent(const Buffer& buffer, BufferCoord begin, BufferCoord end)
DisplayAtom(const Buffer& buffer, BufferCoord begin, BufferCoord end)
: m_type(BufferRange), m_buffer(&buffer), m_begin(begin), m_end(end) {}
AtomContent(String str)
: m_type(Text), m_text(std::move(str)) {}
DisplayAtom(String str, ColorPair colors = { Colors::Default, Colors::Default },
Attribute attribute = Normal)
: m_type(Text), m_text(std::move(str)), colors(colors), attribute(attribute) {}
String content() const
{
@ -97,6 +98,11 @@ public:
void trim_begin(CharCount count);
void trim_end(CharCount count);
public:
ColorPair colors = {Colors::Default, Colors::Default};
Attribute attribute = Normal;
private:
friend class DisplayLine;
@ -108,19 +114,6 @@ private:
String m_text;
};
struct DisplayAtom
{
ColorPair colors;
Attribute attribute;
AtomContent content;
DisplayAtom(AtomContent content,
ColorPair colors = {Colors::Default, Colors::Default},
Attribute attribute = Normal)
: content{std::move(content)}, colors{colors}, attribute{attribute}
{}
};
using BufferRange = std::pair<BufferCoord, BufferCoord>;
using AtomList = std::vector<DisplayAtom>;

View File

@ -37,19 +37,19 @@ void highlight_range(DisplayBuffer& display_buffer,
for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it)
{
bool is_replaced = atom_it->content.type() == AtomContent::ReplacedBufferRange;
bool is_replaced = atom_it->type() == DisplayAtom::ReplacedBufferRange;
if (not atom_it->content.has_buffer_range() or
if (not atom_it->has_buffer_range() or
(skip_replaced and is_replaced))
continue;
if (end <= atom_it->content.begin() or begin >= atom_it->content.end())
if (end <= atom_it->begin() or begin >= atom_it->end())
continue;
if (not is_replaced and begin > atom_it->content.begin())
if (not is_replaced and begin > atom_it->begin())
atom_it = ++line.split(atom_it, begin);
if (not is_replaced and end < atom_it->content.end())
if (not is_replaced and end < atom_it->end())
{
atom_it = line.split(atom_it, end);
func(*atom_it);
@ -221,11 +221,11 @@ void expand_tabulations(const Window& window, DisplayBuffer& display_buffer)
{
for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it)
{
if (atom_it->content.type() != AtomContent::BufferRange)
if (atom_it->type() != DisplayAtom::BufferRange)
continue;
auto begin = buffer.iterator_at(atom_it->content.begin());
auto end = buffer.iterator_at(atom_it->content.end());
auto begin = buffer.iterator_at(atom_it->begin());
auto end = buffer.iterator_at(atom_it->end());
for (BufferIterator it = begin; it != end; ++it)
{
if (*it == '\t')
@ -250,7 +250,7 @@ void expand_tabulations(const Window& window, DisplayBuffer& display_buffer)
String padding;
for (int i = 0; i < count; ++i)
padding += ' ';
atom_it->content.replace(padding);
atom_it->replace(padding);
break;
}
}
@ -272,7 +272,7 @@ void show_line_numbers(const Window& window, DisplayBuffer& display_buffer)
{
char buffer[10];
snprintf(buffer, 10, format, (int)line.range().first.line + 1);
DisplayAtom atom = DisplayAtom(AtomContent(buffer));
DisplayAtom atom{buffer};
atom.colors = colors;
line.insert(line.begin(), std::move(atom));
}
@ -309,11 +309,11 @@ void expand_unprintable(const Window& window, DisplayBuffer& display_buffer)
{
for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it)
{
if (atom_it->content.type() == AtomContent::BufferRange)
if (atom_it->type() == DisplayAtom::BufferRange)
{
using Utf8It = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pass>;
for (Utf8It it = buffer.iterator_at(atom_it->content.begin()),
end = buffer.iterator_at(atom_it->content.end()); it != end; ++it)
for (Utf8It it = buffer.iterator_at(atom_it->begin()),
end = buffer.iterator_at(atom_it->end()); it != end; ++it)
{
Codepoint cp = *it;
if (cp != '\n' and iscntrl((int)cp))
@ -321,11 +321,11 @@ void expand_unprintable(const Window& window, DisplayBuffer& display_buffer)
std::ostringstream oss;
oss << "U+" << std::hex << cp;
String str = oss.str();
if (it.base().coord() != atom_it->content.begin())
if (it.base().coord() != atom_it->begin())
atom_it = ++line.split(atom_it, it.base().coord());
if ((it+1).base().coord() != atom_it->content.end())
if ((it+1).base().coord() != atom_it->end())
atom_it = line.split(atom_it, (it+1).base().coord());
atom_it->content.replace(str);
atom_it->replace(str);
atom_it->colors = { Colors::Red, Colors::Black };
break;
}
@ -363,7 +363,7 @@ public:
auto it = find_if(lines, [&](const LineAndFlag& l) { return std::get<0>(l) == line_num; });
String content = it != lines.end() ? std::get<2>(*it) : empty;
content += String(' ', width - content.char_length());
DisplayAtom atom{AtomContent(std::move(content))};
DisplayAtom atom{std::move(content)};
atom.colors = { it != lines.end() ? std::get<1>(*it) : Colors::Default , m_bg };
line.insert(line.begin(), std::move(atom));
}

View File

@ -221,7 +221,7 @@ void NCursesUI::draw_line(const DisplayLine& line, CharCount col_index) const
set_color(stdscr, atom.colors);
String content = atom.content.content();
String content = atom.content();
if (content[content.length()-1] == '\n' and
content.char_length() - 1 < m_dimensions.column - col_index)
{
@ -281,7 +281,7 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer,
{
String title;
for (auto& atom : mode_line)
title += atom.content.content();
title += atom.content();
title += " - Kakoune";
printf("%s%s%s", tsl, title.c_str(), fsl);
}

View File

@ -94,7 +94,7 @@ public:
void write(const DisplayAtom& atom)
{
write(atom.content.content());
write(atom.content());
write(atom.colors);
write(atom.attribute);
}
@ -186,7 +186,7 @@ ColorPair read<ColorPair>(int socket)
template<>
DisplayAtom read<DisplayAtom>(int socket)
{
DisplayAtom atom(AtomContent(read<String>(socket)));
DisplayAtom atom(read<String>(socket));
atom.colors = read<ColorPair>(socket);
atom.attribute = read<Attribute>(socket);
return atom;

View File

@ -74,7 +74,7 @@ void Window::update_display_buffer()
LineCount buffer_line = m_position.line + line;
if (buffer_line >= buffer().line_count())
break;
lines.emplace_back(AtomList{ {AtomContent(buffer(), buffer_line, buffer_line+1)} });
lines.emplace_back(AtomList{ {buffer(), buffer_line, buffer_line+1} });
}
m_display_buffer.compute_range();
@ -118,21 +118,21 @@ static CharCount adapt_view_pos(const DisplayLine& line, BufferCoord pos, CharCo
CharCount non_buffer_column = 0;
for (auto& atom : line)
{
if (atom.content.has_buffer_range())
if (atom.has_buffer_range())
{
if (atom.content.begin() <= pos and atom.content.end() > pos)
if (atom.begin() <= pos and atom.end() > pos)
{
if (buffer_column < view_pos)
return buffer_column;
auto last_column = buffer_column + atom.content.length();
auto last_column = buffer_column + atom.length();
if (last_column >= view_pos + view_size - non_buffer_column)
return last_column - view_size + non_buffer_column;
}
buffer_column += atom.content.length();
buffer_column += atom.length();
}
else
non_buffer_column += atom.content.length();
non_buffer_column += atom.length();
}
return view_pos;
}
@ -154,7 +154,7 @@ void Window::scroll_to_keep_cursor_visible_ifn()
// highlight only the line containing the cursor
DisplayBuffer display_buffer;
DisplayBuffer::LineList& lines = display_buffer.lines();
lines.emplace_back(AtomList{ AtomContent(buffer(), last.line, last.line+1) });
lines.emplace_back(AtomList{ {buffer(), last.line, last.line+1} });
display_buffer.compute_range();
m_highlighters(*this, display_buffer);
@ -179,16 +179,15 @@ CharCount find_display_column(const DisplayLine& line, const Buffer& buffer,
CharCount column = 0;
for (auto& atom : line)
{
auto& content = atom.content;
if (content.has_buffer_range() and
coord >= content.begin() and coord < content.end())
if (atom.has_buffer_range() and
coord >= atom.begin() and coord < atom.end())
{
if (content.type() == AtomContent::BufferRange)
column += utf8::distance(buffer.iterator_at(content.begin()),
if (atom.type() == DisplayAtom::BufferRange)
column += utf8::distance(buffer.iterator_at(atom.begin()),
buffer.iterator_at(coord));
return column;
}
column += content.length();
column += atom.length();
}
return column;
}
@ -199,14 +198,13 @@ BufferCoord find_buffer_coord(const DisplayLine& line, const Buffer& buffer,
auto& range = line.range();
for (auto& atom : line)
{
auto& content = atom.content;
CharCount len = content.length();
if (content.has_buffer_range() and column < len)
CharCount len = atom.length();
if (atom.has_buffer_range() and column < len)
{
if (content.type() == AtomContent::BufferRange)
return utf8::advance(buffer.iterator_at(content.begin()), buffer.iterator_at(range.second),
if (atom.type() == DisplayAtom::BufferRange)
return utf8::advance(buffer.iterator_at(atom.begin()), buffer.iterator_at(range.second),
std::max(0_char, column)).coord();
return content.begin();
return atom.begin();
}
column -= len;
}
@ -232,8 +230,8 @@ BufferCoord Window::offset_coord(const BufferCoord& coord, LineCount offset)
auto line = clamp(coord.line + offset, 0_line, buffer().line_count()-1);
DisplayBuffer display_buffer;
DisplayBuffer::LineList& lines = display_buffer.lines();
lines.emplace_back(AtomList{ {AtomContent(buffer(), coord.line, coord.line+1)} });
lines.emplace_back(AtomList{ {AtomContent(buffer(), line, line+1)} });
lines.emplace_back(AtomList{ {buffer(), coord.line, coord.line+1} });
lines.emplace_back(AtomList{ {buffer(), line, line+1} });
display_buffer.compute_range();
m_highlighters(*this, display_buffer);
m_builtin_highlighters(*this, display_buffer);