| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "katetextline.h" |
| 8 | |
| 9 | namespace Kate |
| 10 | { |
| 11 | |
| 12 | int TextLine::firstChar() const |
| 13 | { |
| 14 | return nextNonSpaceChar(pos: 0); |
| 15 | } |
| 16 | |
| 17 | int TextLine::lastChar() const |
| 18 | { |
| 19 | return previousNonSpaceChar(pos: m_text.length() - 1); |
| 20 | } |
| 21 | |
| 22 | int TextLine::nextNonSpaceChar(int pos) const |
| 23 | { |
| 24 | Q_ASSERT(pos >= 0); |
| 25 | |
| 26 | for (int i = pos; i < m_text.length(); i++) { |
| 27 | if (!m_text[i].isSpace()) { |
| 28 | return i; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | int TextLine::previousNonSpaceChar(int pos) const |
| 36 | { |
| 37 | if (pos >= m_text.length()) { |
| 38 | pos = m_text.length() - 1; |
| 39 | } |
| 40 | |
| 41 | for (int i = pos; i >= 0; i--) { |
| 42 | if (!m_text[i].isSpace()) { |
| 43 | return i; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | QString TextLine::leadingWhitespace() const |
| 51 | { |
| 52 | if (firstChar() < 0) { |
| 53 | return string(column: 0, length: length()); |
| 54 | } |
| 55 | |
| 56 | return string(column: 0, length: firstChar()); |
| 57 | } |
| 58 | |
| 59 | int TextLine::indentDepth(int tabWidth) const |
| 60 | { |
| 61 | int d = 0; |
| 62 | const int len = m_text.length(); |
| 63 | const QChar *unicode = m_text.unicode(); |
| 64 | |
| 65 | for (int i = 0; i < len; ++i) { |
| 66 | if (unicode[i].isSpace()) { |
| 67 | if (unicode[i] == QLatin1Char('\t')) { |
| 68 | d += tabWidth - (d % tabWidth); |
| 69 | } else { |
| 70 | d++; |
| 71 | } |
| 72 | } else { |
| 73 | return d; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return d; |
| 78 | } |
| 79 | |
| 80 | bool TextLine::matchesAt(int column, const QString &match) const |
| 81 | { |
| 82 | if (column < 0) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | const int len = m_text.length(); |
| 87 | const int matchlen = match.length(); |
| 88 | |
| 89 | if ((column + matchlen) > len) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | const QChar *unicode = m_text.unicode(); |
| 94 | const QChar *matchUnicode = match.unicode(); |
| 95 | |
| 96 | for (int i = 0; i < matchlen; ++i) { |
| 97 | if (unicode[i + column] != matchUnicode[i]) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | int TextLine::toVirtualColumn(int column, int tabWidth) const |
| 106 | { |
| 107 | if (column < 0) { |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int x = 0; |
| 112 | const int zmax = qMin(a: column, b: m_text.length()); |
| 113 | const QChar *unicode = m_text.unicode(); |
| 114 | |
| 115 | for (int z = 0; z < zmax; ++z) { |
| 116 | if (unicode[z] == QLatin1Char('\t')) { |
| 117 | x += tabWidth - (x % tabWidth); |
| 118 | } else { |
| 119 | x++; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return x + column - zmax; |
| 124 | } |
| 125 | |
| 126 | int TextLine::fromVirtualColumn(int column, int tabWidth) const |
| 127 | { |
| 128 | if (column < 0) { |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | const int zmax = qMin(a: m_text.length(), b: column); |
| 133 | const QChar *unicode = m_text.unicode(); |
| 134 | |
| 135 | int x = 0; |
| 136 | int z = 0; |
| 137 | for (; z < zmax; ++z) { |
| 138 | int diff = 1; |
| 139 | if (unicode[z] == QLatin1Char('\t')) { |
| 140 | diff = tabWidth - (x % tabWidth); |
| 141 | } |
| 142 | |
| 143 | if (x + diff > column) { |
| 144 | break; |
| 145 | } |
| 146 | x += diff; |
| 147 | } |
| 148 | |
| 149 | return z + qMax(a: column - x, b: 0); |
| 150 | } |
| 151 | |
| 152 | int TextLine::virtualLength(int tabWidth) const |
| 153 | { |
| 154 | int x = 0; |
| 155 | const int len = m_text.length(); |
| 156 | const QChar *unicode = m_text.unicode(); |
| 157 | |
| 158 | for (int z = 0; z < len; ++z) { |
| 159 | if (unicode[z] == QLatin1Char('\t')) { |
| 160 | x += tabWidth - (x % tabWidth); |
| 161 | } else { |
| 162 | x++; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return x; |
| 167 | } |
| 168 | |
| 169 | void TextLine::addAttribute(const Attribute &attribute) |
| 170 | { |
| 171 | // try to append to previous range, if same attribute value |
| 172 | if (!m_attributesList.empty() && (m_attributesList.back().attributeValue == attribute.attributeValue) |
| 173 | && ((m_attributesList.back().offset + m_attributesList.back().length) == attribute.offset)) { |
| 174 | m_attributesList.back().length += attribute.length; |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | m_attributesList.push_back(t: attribute); |
| 179 | } |
| 180 | |
| 181 | int TextLine::attribute(int pos) const |
| 182 | { |
| 183 | const auto found = std::upper_bound(first: m_attributesList.cbegin(), last: m_attributesList.cend(), val: pos, comp: [](const int &p, const Attribute &x) { |
| 184 | return p < x.offset + x.length; |
| 185 | }); |
| 186 | if (found != m_attributesList.cend() && found->offset <= pos && pos < (found->offset + found->length)) { |
| 187 | return found->attributeValue; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |