1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtextdocument_p.h"
5#include "qtextblock_p.h"
6
7QT_BEGIN_NAMESPACE
8
9namespace Utils {
10
11TextDocument::TextDocument(const QString &text)
12{
13 setPlainText(text);
14}
15
16TextBlock TextDocument::findBlockByNumber(int blockNumber) const
17{
18 return (blockNumber >= 0 && blockNumber < m_blocks.size())
19 ? m_blocks.at(i: blockNumber).textBlock
20 : TextBlock();
21}
22
23TextBlock TextDocument::findBlockByLineNumber(int lineNumber) const
24{
25 return findBlockByNumber(blockNumber: lineNumber);
26}
27
28QChar TextDocument::characterAt(int pos) const
29{
30 return m_content.at(i: pos);
31}
32
33int TextDocument::characterCount() const
34{
35 return m_content.size();
36}
37
38TextBlock TextDocument::begin() const
39{
40 return m_blocks.isEmpty() ? TextBlock() : m_blocks.at(i: 0).textBlock;
41}
42
43TextBlock TextDocument::firstBlock() const
44{
45 return begin();
46}
47
48TextBlock TextDocument::lastBlock() const
49{
50 return m_blocks.isEmpty() ? TextBlock() : m_blocks.last().textBlock;
51}
52
53std::optional<int> TextDocument::version() const
54{
55 return m_version;
56}
57
58void TextDocument::setVersion(std::optional<int> v)
59{
60 m_version = v;
61}
62
63QString TextDocument::toPlainText() const
64{
65 return m_content;
66}
67
68void TextDocument::setPlainText(const QString &text)
69{
70 m_content = text;
71 m_blocks.clear();
72
73 const auto appendToBlocks = [this](int blockNumber, int start, int length) {
74 Block block;
75 block.textBlock.setBlockNumber(blockNumber);
76 block.textBlock.setPosition(start);
77 block.textBlock.setDocument(this);
78 block.textBlock.setLength(length);
79 m_blocks.append(t: block);
80 };
81
82 int blockStart = 0;
83 int blockNumber = -1;
84 while (blockStart < text.size()) {
85 int blockEnd = text.indexOf(ch: u'\n', from: blockStart) + 1;
86 if (blockEnd == 0)
87 blockEnd = text.size();
88 appendToBlocks(++blockNumber, blockStart, blockEnd - blockStart);
89 blockStart = blockEnd;
90 }
91 // Add an empty block if the text ends with \n. This is required for retrieving
92 // the actual line of the text editor if requested, for example, in findBlockByNumber.
93 // Consider a case with text aa\nbb\n\n. You are on 4th line of the text editor and even
94 // if it is an empty line, we introduce a text block for it to maybe use later.
95 if (text.endsWith(c: u'\n'))
96 appendToBlocks(++blockNumber, blockStart, 0);
97}
98
99bool TextDocument::isModified() const
100{
101 return m_modified;
102}
103
104void TextDocument::setModified(bool modified)
105{
106 m_modified = modified;
107}
108
109void TextDocument::setUserState(int blockNumber, int state)
110{
111 if (blockNumber >= 0 && blockNumber < m_blocks.size())
112 m_blocks[blockNumber].userState = state;
113}
114
115int TextDocument::userState(int blockNumber) const
116{
117 return (blockNumber >= 0 && blockNumber < m_blocks.size()) ? m_blocks[blockNumber].userState
118 : -1;
119}
120
121QMutex *TextDocument::mutex() const
122{
123 return &m_mutex;
124}
125
126} // namespace Utils
127
128QT_END_NAMESPACE
129

source code of qtdeclarative/src/qmlls/qtextdocument.cpp