blob: 54e200274ed5b89669a676faab139ad3f05a3a08 (
plain)
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
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qtextdocument_p.h"
#include "qtextblock_p.h"
namespace Utils {
TextDocument::TextDocument(const QString &text)
{
setPlainText(text);
}
TextBlock TextDocument::findBlockByNumber(int blockNumber) const
{
return (blockNumber >= 0 && blockNumber < m_blocks.size())
? m_blocks.at(blockNumber).textBlock
: TextBlock();
}
TextBlock TextDocument::findBlockByLineNumber(int lineNumber) const
{
return findBlockByNumber(lineNumber);
}
QChar TextDocument::characterAt(int pos) const
{
return m_content.at(pos);
}
int TextDocument::characterCount() const
{
return m_content.size();
}
TextBlock TextDocument::begin() const
{
return m_blocks.isEmpty() ? TextBlock() : m_blocks.at(0).textBlock;
}
TextBlock TextDocument::firstBlock() const
{
return begin();
}
TextBlock TextDocument::lastBlock() const
{
return m_blocks.isEmpty() ? TextBlock() : m_blocks.last().textBlock;
}
std::optional<int> TextDocument::version() const
{
return m_version;
}
void TextDocument::setVersion(std::optional<int> v)
{
m_version = v;
}
QString TextDocument::toPlainText() const
{
return m_content;
}
void TextDocument::setPlainText(const QString &text)
{
m_content = text;
m_blocks.clear();
const auto appendToBlocks = [this](int blockNumber, int start, int length) {
Block block;
block.textBlock.setBlockNumber(blockNumber);
block.textBlock.setPosition(start);
block.textBlock.setDocument(this);
block.textBlock.setLength(length);
m_blocks.append(block);
};
int blockStart = 0;
int blockNumber = -1;
while (blockStart < text.size()) {
int blockEnd = text.indexOf(u'\n', blockStart) + 1;
if (blockEnd == 0)
blockEnd = text.size();
appendToBlocks(++blockNumber, blockStart, blockEnd - blockStart);
blockStart = blockEnd;
}
// Add an empty block if the text ends with \n. This is required for retrieving
// the actual line of the text editor if requested, for example, in findBlockByNumber.
// Consider a case with text aa\nbb\n\n. You are on 4th line of the text editor and even
// if it is an empty line, we introduce a text block for it to maybe use later.
if (text.endsWith(u'\n'))
appendToBlocks(++blockNumber, blockStart, 0);
}
bool TextDocument::isModified() const
{
return m_modified;
}
void TextDocument::setModified(bool modified)
{
m_modified = modified;
}
void TextDocument::setUserState(int blockNumber, int state)
{
if (blockNumber >= 0 && blockNumber < m_blocks.size())
m_blocks[blockNumber].userState = state;
}
int TextDocument::userState(int blockNumber) const
{
return (blockNumber >= 0 && blockNumber < m_blocks.size()) ? m_blocks[blockNumber].userState
: -1;
}
QMutex *TextDocument::mutex() const
{
return &m_mutex;
}
} // namespace Utils
|