kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

display_buffer.cc (12534B)


      1 #include "display_buffer.hh"
      2 
      3 #include "assert.hh"
      4 #include "buffer.hh"
      5 #include "face_registry.hh"
      6 #include "format.hh"
      7 #include "utf8.hh"
      8 
      9 namespace Kakoune
     10 {
     11 
     12 BufferIterator get_iterator(const Buffer& buffer, BufferCoord coord)
     13 {
     14     // Correct one past the end of line as next line
     15     if (not buffer.is_end(coord) and coord.column == buffer[coord.line].length())
     16         coord = coord.line+1;
     17     return buffer.iterator_at(coord);
     18 }
     19 
     20 StringView DisplayAtom::content() const
     21 {
     22     switch (m_type)
     23     {
     24         case Range:
     25         {
     26             auto line = (*m_buffer)[m_range.begin.line];
     27             if (m_range.begin.line == m_range.end.line)
     28                 return line.substr(m_range.begin.column, m_range.end.column - m_range.begin.column);
     29             else if (m_range.begin.line+1 == m_range.end.line and m_range.end.column == 0)
     30                 return line.substr(m_range.begin.column);
     31             break;
     32         }
     33         case Text:
     34         case ReplacedRange:
     35             return m_text;
     36     }
     37     kak_assert(false);
     38     return {};
     39 }
     40 
     41 ColumnCount DisplayAtom::length() const
     42 {
     43     switch (m_type)
     44     {
     45         case Range:
     46             return utf8::column_distance(get_iterator(*m_buffer, m_range.begin),
     47                                          get_iterator(*m_buffer, m_range.end));
     48         case Text:
     49         case ReplacedRange:
     50             return m_text.column_length();
     51     }
     52     kak_assert(false);
     53     return 0;
     54 }
     55 
     56 bool DisplayAtom::empty() const
     57 {
     58     if (m_type == Range)
     59         return m_range.begin == m_range.end;
     60     else
     61         return m_text.empty();
     62 }
     63 
     64 ColumnCount DisplayAtom::trim_begin(ColumnCount count)
     65 {
     66     ColumnCount res = 0;
     67     if (m_type == Range)
     68     {
     69         auto it = get_iterator(*m_buffer, m_range.begin);
     70         auto end = get_iterator(*m_buffer, m_range.end);
     71         while (it != end and res < count)
     72             res += codepoint_width(utf8::read_codepoint(it, end));
     73         m_range.begin = std::min(it.coord(), m_range.end);
     74     }
     75     else
     76     {
     77         auto it = m_text.begin();
     78         while (it != m_text.end() and res < count)
     79             res += codepoint_width(utf8::read_codepoint(it, m_text.end()));
     80         m_text = String{it, m_text.end()};
     81     }
     82 
     83     return res;
     84 }
     85 
     86 ColumnCount DisplayAtom::trim_end_to_length(ColumnCount count)
     87 {
     88     ColumnCount res = 0;
     89     if (m_type == Range)
     90     {
     91         auto it = get_iterator(*m_buffer, m_range.begin);
     92         auto end = get_iterator(*m_buffer, m_range.end);
     93         while (it != end and res < count)
     94             res += codepoint_width(utf8::read_codepoint(it, end));
     95         m_range.end = std::min(it.coord(), m_range.end);
     96     }
     97     else
     98     {
     99         auto it = m_text.begin();
    100         while (it != m_text.end() and res < count)
    101             res += codepoint_width(utf8::read_codepoint(it, m_text.end()));
    102         m_text = String{m_text.begin(), it};
    103     }
    104 
    105     return res;
    106 }
    107 
    108 DisplayLine::DisplayLine(AtomList atoms)
    109     : m_atoms(std::move(atoms))
    110 {
    111     compute_range();
    112 }
    113 
    114 DisplayLine::iterator DisplayLine::split(iterator it, BufferCoord pos)
    115 {
    116     kak_assert(it->type() == DisplayAtom::Range);
    117     kak_assert(it->begin() < pos);
    118     kak_assert(it->end() > pos);
    119 
    120     DisplayAtom atom = *it;
    121     atom.m_range.end = pos;
    122     it->m_range.begin = pos;
    123     return m_atoms.insert(it, std::move(atom));
    124 }
    125 
    126 DisplayLine::iterator DisplayLine::split(iterator it, ColumnCount count)
    127 {
    128     kak_assert(count > 0);
    129     kak_assert(count < it->length());
    130 
    131     if (it->type() == DisplayAtom::Text or it->type() == DisplayAtom::ReplacedRange)
    132     {
    133         DisplayAtom atom = *it;
    134         atom.m_text = atom.m_text.substr(0, count).str();
    135         it->m_text = it->m_text.substr(count).str();
    136         return m_atoms.insert(it, std::move(atom));
    137     }
    138     auto pos = utf8::advance(get_iterator(it->buffer(), it->begin()),
    139                              get_iterator(it->buffer(), it->end()),
    140                              count).coord();
    141     if (pos == it->begin()) // Can happen if we try to split in the middle of a multi-column codepoint
    142         return m_atoms.insert(it, {it->buffer(), {pos, pos}, it->face});
    143     if (pos == it->end())
    144         return std::prev(m_atoms.insert(std::next(it), {it->buffer(), {pos, pos}, it->face}));
    145     return split(it, pos);
    146 }
    147 
    148 DisplayLine::iterator DisplayLine::split(BufferCoord pos)
    149 {
    150     auto it = find_if(begin(), end(), [pos](const DisplayAtom& a) {
    151         return (a.has_buffer_range() and a.begin() >= pos) or
    152                (a.type() == DisplayAtom::Range and a.end() > pos);
    153     });
    154     if (it == end() or it->begin() >= pos)
    155         return it;
    156     return ++split(it, pos);
    157 }
    158 
    159 DisplayLine::iterator DisplayLine::insert(iterator it, DisplayAtom atom)
    160 {
    161     if (atom.has_buffer_range())
    162     {
    163         m_range.begin = std::min(m_range.begin, atom.begin());
    164         m_range.end = std::max(m_range.end, atom.end());
    165     }
    166     return m_atoms.insert(it, std::move(atom));
    167 }
    168 
    169 DisplayAtom& DisplayLine::push_back(DisplayAtom atom)
    170 {
    171     if (atom.has_buffer_range())
    172     {
    173         m_range.begin = std::min(m_range.begin, atom.begin());
    174         m_range.end = std::max(m_range.end, atom.end());
    175     }
    176     m_atoms.push_back(std::move(atom));
    177     return m_atoms.back();
    178 }
    179 
    180 DisplayLine DisplayLine::extract(iterator beg, iterator end)
    181 {
    182     if (beg == this->begin() and end == this->end())
    183         return DisplayLine{std::move(*this)};
    184 
    185     DisplayLine extracted{AtomList(std::move_iterator(beg), std::move_iterator(end))};
    186     m_atoms.erase(beg, end);
    187     if (extracted.m_range.begin == m_range.begin or
    188         extracted.m_range.end == m_range.end)
    189         compute_range(true);
    190     return extracted;
    191 }
    192 
    193 DisplayLine::iterator DisplayLine::erase(iterator beg, iterator end)
    194 {
    195     auto res = m_atoms.erase(beg, end);
    196     compute_range(true);
    197     return res;
    198 }
    199 
    200 void DisplayLine::optimize()
    201 {
    202     if (m_atoms.empty())
    203         return;
    204 
    205     auto atom_it = m_atoms.begin();
    206     for (auto next_it = atom_it + 1; next_it != m_atoms.end(); ++next_it)
    207     {
    208         auto& atom = *atom_it;
    209         auto& next = *next_it;
    210 
    211         const auto type = atom.type();
    212         if (type == next.type() and atom.face == next.face)
    213         {
    214             if (type == DisplayAtom::Text)
    215                 atom.m_text += next.m_text;
    216             else if ((type == DisplayAtom::Range or
    217                       type == DisplayAtom::ReplacedRange) and
    218                      next.begin() == atom.end())
    219             {
    220                 atom.m_range.end = next.end();
    221                 if (type == DisplayAtom::ReplacedRange)
    222                     atom.m_text += next.m_text;
    223             }
    224             else
    225                 *++atom_it = std::move(*next_it);
    226         }
    227         else
    228             *++atom_it = std::move(*next_it);
    229     }
    230     m_atoms.erase(atom_it+1, m_atoms.end());
    231 }
    232 
    233 ColumnCount DisplayLine::length() const
    234 {
    235     ColumnCount len = 0;
    236     for (auto& atom : m_atoms)
    237         len += atom.length();
    238     return len;
    239 }
    240 
    241 bool DisplayLine::trim(ColumnCount front, ColumnCount col_count)
    242 {
    243     return trim_from(0_col, front, col_count);
    244 }
    245 
    246 bool DisplayLine::trim_from(ColumnCount first_col, ColumnCount front, ColumnCount col_count)
    247 {
    248     auto it = begin();
    249     while (first_col > 0 and it != end())
    250     {
    251         auto len = it->length();
    252         if (len <= first_col)
    253         {
    254             ++it;
    255             first_col -= len;
    256         }
    257         else
    258         {
    259             if (front > 0)
    260                 it = ++split(it, front);
    261             first_col = 0;
    262         }
    263     }
    264 
    265     auto front_it = it;
    266     Optional<DisplayAtom> padding;
    267     while (front > 0 and it != end())
    268     {
    269         front -= it->trim_begin(front);
    270         kak_assert(it->empty() or front <= 0);
    271         if (front < 0)
    272             padding.emplace(it->has_buffer_range()
    273                 ? DisplayAtom{it->buffer(), {it->begin(), it->begin()}, String{' ', -front}, it->face}
    274                 : DisplayAtom{String{' ', -front}, it->face});
    275 
    276         if (it->empty())
    277             ++it;
    278     }
    279     it = m_atoms.erase(front_it, it);
    280     if (padding)
    281         it = m_atoms.insert(it, std::move(*padding));
    282 
    283     it = begin();
    284     for (; it != end() and col_count > 0; ++it)
    285         col_count -= it->trim_end_to_length(col_count);
    286     bool did_trim = it != end() and col_count == 0;
    287     m_atoms.erase(it, end());
    288 
    289     compute_range(true);
    290     return did_trim;
    291 }
    292 
    293 const BufferRange init_range{ {INT_MAX, INT_MAX}, {INT_MIN, INT_MIN} };
    294 
    295 void DisplayLine::compute_range(bool preserve_if_no_atoms)
    296 {
    297     auto first = find_if(m_atoms, [](const auto& atom) { return atom.has_buffer_range(); });
    298     if (first == m_atoms.end())
    299         m_range = preserve_if_no_atoms and m_range != init_range ? m_range : BufferRange{{0, 0}, {0, 0}};
    300     else
    301     {
    302         auto last = std::find_if(m_atoms.rbegin(), std::reverse_iterator(first+1), [](const auto& atom) { return atom.has_buffer_range(); });
    303         m_range.begin = first->begin();
    304         m_range.end = last->end();
    305     }
    306 
    307     kak_assert(m_range.begin <= m_range.end);
    308 }
    309 
    310 void DisplayBuffer::compute_range()
    311 {
    312     m_range = init_range;
    313     for (auto& line : m_lines)
    314     {
    315         m_range.begin = std::min(line.range().begin, m_range.begin);
    316         m_range.end = std::max(line.range().end, m_range.end);
    317     }
    318     if (m_range == init_range)
    319         m_range = { { 0, 0 }, { 0, 0 } };
    320     kak_assert(m_range.begin <= m_range.end);
    321 }
    322 
    323 void DisplayBuffer::optimize()
    324 {
    325     for (auto& line : m_lines)
    326         line.optimize();
    327 }
    328 
    329 DisplayLine parse_display_line(StringView line, Face& face, const FaceRegistry& faces, const HashMap<String, DisplayLine>& builtins)
    330 {
    331     DisplayLine res;
    332     bool was_antislash = false;
    333     auto pos = line.begin();
    334     String content;
    335     for (auto it = line.begin(), end = line.end(); it != end; ++it)
    336     {
    337         const char c = *it;
    338         if (c == '{')
    339         {
    340             if (was_antislash)
    341             {
    342                 content += StringView{pos, it};
    343                 content.back() = '{';
    344                 pos = it + 1;
    345             }
    346             else
    347             {
    348                 content += StringView{pos, it};
    349                 if (not content.empty())
    350                     res.push_back({std::move(content), face});
    351                 content.clear();
    352                 auto closing = std::find(it+1, end, '}');
    353                 if (closing == end)
    354                     throw runtime_error("unclosed face definition");
    355                 if (*(it+1) == '{' and closing+1 != end and *(closing+1) == '}')
    356                 {
    357                     auto builtin_it = builtins.find(StringView{it+2, closing});
    358                     if (builtin_it == builtins.end())
    359                         throw runtime_error(format("undefined atom {}", StringView{it+2, closing}));
    360                     for (auto& atom : builtin_it->value)
    361                         res.push_back(atom);
    362                     // closing is now at the first char of "}}", advance it to the second
    363                     ++closing;
    364                 }
    365                 else if (closing == it+2 and *(it+1) == '\\')
    366                 {
    367                     pos = closing + 1;
    368                     break;
    369                 }
    370                 else
    371                     face = faces[{it+1, closing}];
    372                 it = closing;
    373                 pos = closing + 1;
    374             }
    375         }
    376         if (c == '\n' or c == '\t') // line breaks and tabs are forbidden, replace with space
    377         {
    378             content += StringView{pos, it+1};
    379             content.back() = ' ';
    380             pos = it + 1;
    381         }
    382 
    383         if (c == '\\')
    384         {
    385             if (was_antislash)
    386             {
    387                 content += StringView{pos, it};
    388                 pos = it + 1;
    389                 was_antislash = false;
    390             }
    391             else
    392                 was_antislash = true;
    393         }
    394         else
    395             was_antislash = false;
    396     }
    397     content += StringView{pos, line.end()};
    398     if (not content.empty())
    399         res.push_back({std::move(content), face});
    400     return res;
    401 }
    402 
    403 DisplayLine parse_display_line(StringView line, const FaceRegistry& faces, const HashMap<String, DisplayLine>& builtins)
    404 {
    405     Face face{};
    406     return parse_display_line(line, face, faces, builtins);
    407 }
    408 
    409 DisplayLineList parse_display_line_list(StringView content, const FaceRegistry& faces, const HashMap<String, DisplayLine>& builtins)
    410 {
    411     return content | split<StringView>('\n')
    412                    | transform([&, face=Face{}](StringView s) mutable {
    413                          return parse_display_line(s, face, faces, builtins);
    414                      })
    415                    | gather<DisplayLineList>();
    416 }
    417 
    418 }