~aleteoryx/muditaos

ref: 0823d82e5141f44812c54debf07245d0ca746124 muditaos/module-gui/gui/widgets/TextDocument.cpp -rw-r--r-- 3.6 KiB
0823d82e — Radoslaw Wicik [EGD-3743] Update copyrights in fies - add empty line after license 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "TextDocument.hpp"
#include <cassert>
#include <utility>
#include <TextFormat.hpp>

namespace gui
{

    const std::string TextDocument::newline = "\n";

    TextDocument::TextDocument(const std::list<TextBlock> blocks) : blocks(std::move(blocks))
    {}

    TextDocument::~TextDocument()
    {
        destroy();
    }

    void TextDocument::destroy()
    {
        blocks.clear();
    }

    void TextDocument::append(std::list<TextBlock> &&block_list)
    {
        for (auto &&el : block_list) {
            this->blocks.emplace_back(std::move(el));
        }
    }

    void TextDocument::append(TextBlock &&text)
    {
        blocks.emplace_back(std::move(text));
    }

    void TextDocument::addNewline(BlockCursor &cursor, TextBlock::End eol)
    {
        assert(cursor.getBlockNr() < blocks.size());
        auto [l_block, r_block] = split(cursor);
        l_block.setEnd(eol);
    }

    auto TextDocument::getText() const -> UTF8
    {
        UTF8 output;
        if (blocks.size() != 0) {
            for (auto &el : blocks) {
                output += el.getText();
            }
        }

        return output;
    }

    auto TextDocument::getBlockCursor(unsigned int position) -> BlockCursor
    {
        unsigned int block_no      = 0;
        unsigned int loop_position = 0;

        for (auto &el : blocks) {

            if (el.length() == 0) {
                return BlockCursor(this, 0, block_no, el.getFormat()->getFont());
            }

            if (loop_position + el.length() > position) { // data found
                return BlockCursor(this, position - loop_position, block_no, el.getFormat()->getFont());
            }

            // data not found in block_number, early exit,
            loop_position += el.length();
            ++block_no;
        }
        // TODO ok... here we might want to return BlockCursor(this) <- but returning text::npos / empty/none block if
        // we wanted to return anything
        return BlockCursor();
    }

    auto TextDocument::getText(BlockCursor cursor) -> std::string
    {
        if (cursor) {
            return cursor.getText();
        }
        return "";
    }

    [[nodiscard]] auto TextDocument::getBlocks() const -> const std::list<TextBlock> &
    {
        return blocks;
    }

    [[nodiscard]] auto TextDocument::getBlock(BlockCursor *cursor) const -> const TextBlock *
    {
        if (cursor != nullptr && *cursor) {
            return &operator()(*cursor);
        }
        return nullptr;
    }

    auto TextDocument::operator()(const BlockCursor &cursor) const -> const TextBlock &
    {
        assert(cursor.getBlockNr() < blocks.size());
        return *std::next(blocks.begin(), cursor.getBlockNr());
    }

    void TextDocument::removeBlock(unsigned int block_nr)
    {
        if (block_nr >= blocks.size() || block_nr == text::npos) {
            return;
        }
        blocks.erase(std::next(blocks.begin(), block_nr));
    }

    void TextDocument::removeBlock(std::list<TextBlock>::iterator it)
    {
        blocks.erase(it);
    }

    auto TextDocument::split(BlockCursor &cursor) -> std::pair<TextBlock &, TextBlock &>
    {
        auto to_split = std::next(blocks.begin(), cursor.getBlockNr());

        auto text = to_split->getText(cursor.getPosition());
        auto font = to_split->getFormat()->getFont();
        auto end  = to_split->getEnd();

        auto newblock = TextBlock(text, font, end);

        to_split->setText(to_split->getText().substr(0, cursor.getPosition()));
        blocks.insert(std::next(to_split), std::move(newblock));

        return {*to_split, *(std::next(to_split))};
    }

} // namespace gui