DisplayBuffer: add a split method to split an atom

This commit is contained in:
Maxime Coste 2011-09-29 08:55:08 +00:00
parent 822fc0f822
commit e659ea2dab
3 changed files with 40 additions and 38 deletions

View File

@ -1,5 +1,7 @@
#include "display_buffer.hh" #include "display_buffer.hh"
#include "assert.h"
namespace Kakoune namespace Kakoune
{ {
@ -7,4 +9,17 @@ DisplayBuffer::DisplayBuffer()
{ {
} }
DisplayBuffer::iterator DisplayBuffer::split(iterator atom, size_t pos_in_atom)
{
assert(atom < end());
assert(pos_in_atom < atom->content.length());
DisplayAtom new_atom(atom->begin, atom->begin + pos_in_atom,
atom->content.substr(0, pos_in_atom),
atom->fg_color, atom->bg_color, atom->attribute);
atom->begin = atom->begin + pos_in_atom;
atom->content = atom->content.substr(pos_in_atom);
return insert(atom, std::move(new_atom));
}
} }

View File

@ -43,13 +43,16 @@ struct DisplayAtom
Attribute attribute; Attribute attribute;
DisplayAtom(BufferIterator begin, BufferIterator end, DisplayAtom(BufferIterator begin, BufferIterator end,
const std::string& content) const std::string& content,
Color fg_color = Color::Default,
Color bg_color = Color::Default,
Attribute attribute = Attributes::Normal)
: content(content), : content(content),
begin(begin), begin(begin),
end(end), end(end),
fg_color(Color::Default), fg_color(fg_color),
bg_color(Color::Default), bg_color(bg_color),
attribute(Attributes::Normal) attribute(attribute)
{} {}
}; };
@ -65,6 +68,7 @@ public:
void clear() { m_atoms.clear(); } void clear() { m_atoms.clear(); }
void append(const DisplayAtom& atom) { m_atoms.push_back(atom); } void append(const DisplayAtom& atom) { m_atoms.push_back(atom); }
iterator insert(iterator where, const DisplayAtom& atom) { return m_atoms.insert(where, atom); } iterator insert(iterator where, const DisplayAtom& atom) { return m_atoms.insert(where, atom); }
iterator split(iterator atom, size_t pos_in_atom);
iterator begin() { return m_atoms.begin(); } iterator begin() { return m_atoms.begin(); }
iterator end() { return m_atoms.end(); } iterator end() { return m_atoms.end(); }

View File

@ -61,45 +61,34 @@ public:
if (atom.begin >= sel.begin() and atom.begin < sel.end() and atom.end > sel.end()) if (atom.begin >= sel.begin() and atom.begin < sel.end() and atom.end > sel.end())
{ {
size_t length = sel.end() - atom.begin; size_t length = sel.end() - atom.begin;
DisplayAtom selected(atom.begin, sel.end(), atom_it = display_buffer.split(atom_it, length);
atom.content.substr(0, length)); atom_it->attribute |= Attributes::Underline;
selected.attribute = Attributes::Underline; ++atom_it;
atom.content = atom.content.substr(length);
atom.begin = sel.end();
atom_it = display_buffer.insert(atom_it, selected) + 1;
++sel_it; ++sel_it;
} }
// [---###---] // [---###---]
else if (atom.begin < sel.begin() and atom.end > sel.end()) else if (atom.begin < sel.begin() and atom.end > sel.end())
{ {
size_t prefix_length = sel.begin() - atom.begin; size_t prefix_length = sel.begin() - atom.begin;
DisplayAtom prefix(atom.begin, sel.begin(), atom_it = display_buffer.split(atom_it, prefix_length);
atom.content.substr(0, prefix_length));
size_t sel_length = sel.end() - sel.begin(); size_t sel_length = sel.end() - sel.begin();
DisplayAtom selected(sel.begin(), sel.end(), atom_it = display_buffer.split(atom_it + 1, sel_length);
atom.content.substr(prefix_length, sel_length)); atom_it->attribute |= Attributes::Underline;
selected.attribute = Attributes::Underline; ++atom_it;
atom.content = atom.content.substr(prefix_length + sel_length);
atom_it = display_buffer.insert(atom_it, selected);
atom_it = display_buffer.insert(atom_it, prefix);
atom_it += 2;
++sel_it; ++sel_it;
} }
// [------###] // [------###]
else if (atom.begin < sel.begin() and atom.end > sel.begin()) else if (atom.begin < sel.begin() and atom.end > sel.begin())
{ {
size_t length = sel.end() - atom.begin; size_t length = sel.begin() - atom.begin;
DisplayAtom prefix(atom.begin, sel.end(), atom_it = display_buffer.split(atom_it, length) + 1;
atom.content.substr(0, length)); atom_it->attribute |= Attributes::Underline;
atom.content = atom.content.substr(length); ++atom_it;
atom.begin = sel.end();
atom.attribute = Attributes::Underline;
atom_it = display_buffer.insert(atom_it, prefix) + 2;
} }
// [#########] // [#########]
else if (atom.begin >= sel.begin() and atom.end <= sel.end()) else if (atom.begin >= sel.begin() and atom.end <= sel.end())
{ {
atom.attribute = Attributes::Underline; atom_it->attribute |= Attributes::Underline;
++atom_it; ++atom_it;
} }
// [---------] // [---------]
@ -122,19 +111,13 @@ static void blink_void(DisplayBuffer& display_buffer)
for (auto atom_it = display_buffer.begin(); for (auto atom_it = display_buffer.begin();
atom_it != display_buffer.end();) atom_it != display_buffer.end();)
{ {
DisplayAtom& atom = *atom_it; size_t pos = atom_it->content.find("void");
size_t pos = atom.content.find("void");
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
DisplayAtom prefix(atom.begin, atom.begin + pos, atom_it = display_buffer.split(atom_it, pos) + 1;
atom.content.substr(0, pos)); atom_it = display_buffer.split(atom_it, 4);
prefix.attribute = atom.attribute; atom_it->attribute |= Attributes::Blink;
DisplayAtom match(prefix.end, prefix.end + 4, "void"); ++atom_it;
match.attribute = atom.attribute | Attributes::Blink;
atom.begin = prefix.end + 4;
atom.content = atom.content.substr(pos + 4);
atom_it = display_buffer.insert(atom_it, match);
atom_it = display_buffer.insert(atom_it, prefix) + 2;
} }
else else
++atom_it; ++atom_it;