| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "kateview.h" |
| 8 | |
| 9 | #include "cursor.h" |
| 10 | |
| 11 | #include "configpage.h" |
| 12 | |
| 13 | #include "editor.h" |
| 14 | |
| 15 | #include "document.h" |
| 16 | |
| 17 | #include "view.h" |
| 18 | |
| 19 | #include "plugin.h" |
| 20 | |
| 21 | #include "command.h" |
| 22 | #include "inlinenote.h" |
| 23 | #include "inlinenotedata.h" |
| 24 | #include "inlinenoteprovider.h" |
| 25 | #include "katerenderer.h" |
| 26 | #include "katevariableexpansionmanager.h" |
| 27 | #include "sessionconfiginterface.h" |
| 28 | #include "texthintinterface.h" |
| 29 | #include "variable.h" |
| 30 | |
| 31 | #include "abstractannotationitemdelegate.h" |
| 32 | #include "annotationinterface.h" |
| 33 | |
| 34 | #include "katecmd.h" |
| 35 | #include "kateconfig.h" |
| 36 | #include "kateglobal.h" |
| 37 | #include "katesyntaxmanager.h" |
| 38 | |
| 39 | using namespace KTextEditor; |
| 40 | |
| 41 | Cursor Cursor::fromString(QStringView str) noexcept |
| 42 | { |
| 43 | // parse format "(line, column)" |
| 44 | const int startIndex = str.indexOf(c: QLatin1Char('(')); |
| 45 | const int endIndex = str.indexOf(c: QLatin1Char(')')); |
| 46 | const int commaIndex = str.indexOf(c: QLatin1Char(',')); |
| 47 | |
| 48 | if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) { |
| 49 | return invalid(); |
| 50 | } |
| 51 | |
| 52 | bool ok1 = false; |
| 53 | bool ok2 = false; |
| 54 | |
| 55 | const int line = str.mid(pos: startIndex + 1, n: commaIndex - startIndex - 1).toInt(ok: &ok1); |
| 56 | const int column = str.mid(pos: commaIndex + 1, n: endIndex - commaIndex - 1).toInt(ok: &ok2); |
| 57 | |
| 58 | if (!ok1 || !ok2) { |
| 59 | return invalid(); |
| 60 | } |
| 61 | |
| 62 | return {line, column}; |
| 63 | } |
| 64 | |
| 65 | QString Cursor::toString() const |
| 66 | { |
| 67 | return QStringLiteral("(%1, %2)" ).arg(a: m_line).arg(a: m_column); |
| 68 | } |
| 69 | |
| 70 | QDebug operator<<(QDebug s, KTextEditor::Cursor cursor) |
| 71 | { |
| 72 | s.nospace() << "(" << cursor.line() << ", " << cursor.column() << ")" ; |
| 73 | return s.space(); |
| 74 | } |
| 75 | |
| 76 | size_t KTextEditor::qHash(KTextEditor::Cursor cursor, size_t seed) noexcept |
| 77 | { |
| 78 | return qHashMulti(seed, args: cursor.line(), args: cursor.column()); |
| 79 | } |
| 80 | |
| 81 | Editor::Editor(EditorPrivate *impl) |
| 82 | : QObject() |
| 83 | , d(impl) |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | Editor::~Editor() = default; |
| 88 | |
| 89 | Editor *KTextEditor::Editor::instance() |
| 90 | { |
| 91 | // Just use internal KTextEditor::EditorPrivate::self() |
| 92 | return KTextEditor::EditorPrivate::self(); |
| 93 | } |
| 94 | |
| 95 | QString Editor::defaultEncoding() const |
| 96 | { |
| 97 | // return default encoding in global config object |
| 98 | return d->documentConfig()->encoding(); |
| 99 | } |
| 100 | |
| 101 | bool Editor::registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc) |
| 102 | { |
| 103 | const auto var = Variable(name, description, expansionFunc, false); |
| 104 | return d->variableExpansionManager()->addVariable(variable: var); |
| 105 | } |
| 106 | |
| 107 | bool Editor::registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc) |
| 108 | { |
| 109 | const auto var = Variable(prefix, description, expansionFunc, true); |
| 110 | return d->variableExpansionManager()->addVariable(variable: var); |
| 111 | } |
| 112 | |
| 113 | bool Editor::unregisterVariable(const QString &variable) |
| 114 | { |
| 115 | return d->variableExpansionManager()->removeVariable(name: variable); |
| 116 | } |
| 117 | |
| 118 | bool Editor::expandVariable(const QString &variable, KTextEditor::View *view, QString &output) const |
| 119 | { |
| 120 | return d->variableExpansionManager()->expandVariable(variable, view, output); |
| 121 | } |
| 122 | |
| 123 | QString Editor::expandText(const QString &text, KTextEditor::View *view) const |
| 124 | { |
| 125 | return d->variableExpansionManager()->expandText(text, view); |
| 126 | } |
| 127 | |
| 128 | void Editor::addVariableExpansion(const QList<QWidget *> &widgets, const QStringList &variables) const |
| 129 | { |
| 130 | d->variableExpansionManager()->showDialog(widgets, names: variables); |
| 131 | } |
| 132 | |
| 133 | QFont Editor::font() const |
| 134 | { |
| 135 | return d->rendererConfig()->baseFont(); |
| 136 | } |
| 137 | |
| 138 | KSyntaxHighlighting::Theme Editor::theme() const |
| 139 | { |
| 140 | return KateHlManager::self()->repository().theme(themeName: d->rendererConfig()->schema()); |
| 141 | } |
| 142 | |
| 143 | const KSyntaxHighlighting::Repository &Editor::repository() const |
| 144 | { |
| 145 | return KateHlManager::self()->repository(); |
| 146 | } |
| 147 | |
| 148 | bool View::insertText(const QString &text) |
| 149 | { |
| 150 | KTextEditor::Document *doc = document(); |
| 151 | if (!doc) { |
| 152 | return false; |
| 153 | } |
| 154 | return doc->insertText(position: cursorPosition(), text, block: blockSelection()); |
| 155 | } |
| 156 | |
| 157 | bool View::isStatusBarEnabled() const |
| 158 | { |
| 159 | // is the status bar around? |
| 160 | return !!d->statusBar(); |
| 161 | } |
| 162 | |
| 163 | void View::setStatusBarEnabled(bool enable) |
| 164 | { |
| 165 | // no state change, do nothing |
| 166 | if (enable == !!d->statusBar()) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | // else toggle it |
| 171 | d->toggleStatusBar(); |
| 172 | } |
| 173 | |
| 174 | bool View::insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script) |
| 175 | { |
| 176 | return d->insertTemplateInternal(insertPosition, templateString, script); |
| 177 | } |
| 178 | |
| 179 | bool View::evaluateScript(const QString &script, QVariant *result) |
| 180 | { |
| 181 | return d->evaluateScriptInternal(script, result); |
| 182 | } |
| 183 | |
| 184 | KSyntaxHighlighting::Theme View::theme() const |
| 185 | { |
| 186 | return KateHlManager::self()->repository().theme(themeName: d->rendererConfig()->schema()); |
| 187 | } |
| 188 | |
| 189 | void View::setCursorPositions(const QList<KTextEditor::Cursor> &positions) |
| 190 | { |
| 191 | d->setCursors(positions); |
| 192 | } |
| 193 | |
| 194 | QList<KTextEditor::Cursor> View::cursorPositions() const |
| 195 | { |
| 196 | return d->cursors(); |
| 197 | } |
| 198 | |
| 199 | void View::setSelections(const QList<KTextEditor::Range> &ranges) |
| 200 | { |
| 201 | d->setSelections(ranges); |
| 202 | } |
| 203 | |
| 204 | QList<KTextEditor::Range> View::selectionRanges() const |
| 205 | { |
| 206 | return d->selectionRanges(); |
| 207 | } |
| 208 | |
| 209 | ConfigPage::ConfigPage(QWidget *parent) |
| 210 | : QWidget(parent) |
| 211 | , d(nullptr) |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | ConfigPage::~ConfigPage() = default; |
| 216 | |
| 217 | QString ConfigPage::fullName() const |
| 218 | { |
| 219 | return name(); |
| 220 | } |
| 221 | |
| 222 | QIcon ConfigPage::icon() const |
| 223 | { |
| 224 | return QIcon::fromTheme(QStringLiteral("document-properties" )); |
| 225 | } |
| 226 | |
| 227 | View::View(ViewPrivate *impl, QWidget *parent) |
| 228 | : QWidget(parent) |
| 229 | , KXMLGUIClient() |
| 230 | , d(impl) |
| 231 | { |
| 232 | } |
| 233 | |
| 234 | View::~View() = default; |
| 235 | |
| 236 | Plugin::Plugin(QObject *parent) |
| 237 | : QObject(parent) |
| 238 | , d(nullptr) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | Plugin::~Plugin() = default; |
| 243 | |
| 244 | int Plugin::configPages() const |
| 245 | { |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | ConfigPage *Plugin::configPage(int, QWidget *) |
| 250 | { |
| 251 | return nullptr; |
| 252 | } |
| 253 | |
| 254 | SessionConfigInterface::SessionConfigInterface() = default; |
| 255 | |
| 256 | SessionConfigInterface::~SessionConfigInterface() = default; |
| 257 | |
| 258 | TextHintProvider::TextHintProvider() = default; |
| 259 | |
| 260 | TextHintProvider::~TextHintProvider() = default; |
| 261 | |
| 262 | InlineNoteProvider::InlineNoteProvider() = default; |
| 263 | |
| 264 | InlineNoteProvider::~InlineNoteProvider() = default; |
| 265 | |
| 266 | KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider, |
| 267 | const KTextEditor::View *view, |
| 268 | const KTextEditor::Cursor position, |
| 269 | int index, |
| 270 | bool underMouse, |
| 271 | const QFont &font, |
| 272 | int lineHeight) |
| 273 | : m_provider(provider) |
| 274 | , m_view(view) |
| 275 | , m_position(position) |
| 276 | , m_index(index) |
| 277 | , m_underMouse(underMouse) |
| 278 | , m_font(font) |
| 279 | , m_lineHeight(lineHeight) |
| 280 | { |
| 281 | } |
| 282 | |
| 283 | InlineNote::InlineNote(const KateInlineNoteData &data) |
| 284 | : d(data) |
| 285 | { |
| 286 | } |
| 287 | |
| 288 | qreal InlineNote::width() const |
| 289 | { |
| 290 | return d.m_provider->inlineNoteSize(note: *this).width(); |
| 291 | } |
| 292 | |
| 293 | bool KTextEditor::InlineNote::underMouse() const |
| 294 | { |
| 295 | return d.m_underMouse; |
| 296 | } |
| 297 | |
| 298 | void KTextEditor::InlineNoteProvider::inlineNoteActivated(const InlineNote ¬e, Qt::MouseButtons buttons, const QPoint &globalPos) |
| 299 | { |
| 300 | Q_UNUSED(note); |
| 301 | Q_UNUSED(buttons); |
| 302 | Q_UNUSED(globalPos); |
| 303 | } |
| 304 | |
| 305 | void KTextEditor::InlineNoteProvider::inlineNoteFocusInEvent(const KTextEditor::InlineNote ¬e, const QPoint &globalPos) |
| 306 | { |
| 307 | Q_UNUSED(note); |
| 308 | Q_UNUSED(globalPos); |
| 309 | } |
| 310 | |
| 311 | void KTextEditor::InlineNoteProvider::inlineNoteFocusOutEvent(const KTextEditor::InlineNote ¬e) |
| 312 | { |
| 313 | Q_UNUSED(note); |
| 314 | } |
| 315 | |
| 316 | void KTextEditor::InlineNoteProvider::inlineNoteMouseMoveEvent(const KTextEditor::InlineNote ¬e, const QPoint &globalPos) |
| 317 | { |
| 318 | Q_UNUSED(note); |
| 319 | Q_UNUSED(globalPos); |
| 320 | } |
| 321 | |
| 322 | KTextEditor::InlineNoteProvider *InlineNote::provider() const |
| 323 | { |
| 324 | return d.m_provider; |
| 325 | } |
| 326 | |
| 327 | const KTextEditor::View *InlineNote::view() const |
| 328 | { |
| 329 | return d.m_view; |
| 330 | } |
| 331 | |
| 332 | QFont InlineNote::font() const |
| 333 | { |
| 334 | return d.m_font; |
| 335 | } |
| 336 | |
| 337 | int InlineNote::index() const |
| 338 | { |
| 339 | return d.m_index; |
| 340 | } |
| 341 | |
| 342 | int InlineNote::lineHeight() const |
| 343 | { |
| 344 | return d.m_lineHeight; |
| 345 | } |
| 346 | |
| 347 | KTextEditor::Cursor InlineNote::position() const |
| 348 | { |
| 349 | return d.m_position; |
| 350 | } |
| 351 | |
| 352 | Command::Command(const QStringList &cmds, QObject *parent) |
| 353 | : QObject(parent) |
| 354 | , m_cmds(cmds) |
| 355 | , d(nullptr) |
| 356 | { |
| 357 | // register this command |
| 358 | static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(cmd: this); |
| 359 | } |
| 360 | |
| 361 | Command::~Command() |
| 362 | { |
| 363 | // unregister this command, if instance is still there! |
| 364 | if (KTextEditor::Editor::instance()) { |
| 365 | static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(cmd: this); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | bool Command::supportsRange(const QString &) |
| 370 | { |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | KCompletion *Command::completionObject(KTextEditor::View *, const QString &) |
| 375 | { |
| 376 | return nullptr; |
| 377 | } |
| 378 | |
| 379 | bool Command::wantsToProcessText(const QString &) |
| 380 | { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | void Command::processText(KTextEditor::View *, const QString &) |
| 385 | { |
| 386 | } |
| 387 | |
| 388 | void View::setScrollPosition(KTextEditor::Cursor cursor) |
| 389 | { |
| 390 | d->setScrollPositionInternal(cursor); |
| 391 | } |
| 392 | |
| 393 | void View::setHorizontalScrollPosition(int x) |
| 394 | { |
| 395 | d->setHorizontalScrollPositionInternal(x); |
| 396 | } |
| 397 | |
| 398 | KTextEditor::Cursor View::maxScrollPosition() const |
| 399 | { |
| 400 | return d->maxScrollPositionInternal(); |
| 401 | } |
| 402 | |
| 403 | int View::firstDisplayedLine(LineType lineType) const |
| 404 | { |
| 405 | return d->firstDisplayedLineInternal(lineType); |
| 406 | } |
| 407 | |
| 408 | int View::lastDisplayedLine(LineType lineType) const |
| 409 | { |
| 410 | return d->lastDisplayedLineInternal(lineType); |
| 411 | } |
| 412 | |
| 413 | QRect View::textAreaRect() const |
| 414 | { |
| 415 | return d->textAreaRectInternal(); |
| 416 | } |
| 417 | |
| 418 | StyleOptionAnnotationItem::StyleOptionAnnotationItem() |
| 419 | : contentFontMetrics(QFont()) |
| 420 | { |
| 421 | } |
| 422 | |
| 423 | StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other) |
| 424 | : QStyleOption(Version, Type) |
| 425 | , contentFontMetrics(QFont()) |
| 426 | { |
| 427 | *this = other; |
| 428 | } |
| 429 | |
| 430 | StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version) |
| 431 | : QStyleOption(version, Type) |
| 432 | , contentFontMetrics(QFont()) |
| 433 | { |
| 434 | } |
| 435 | |
| 436 | AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent) |
| 437 | : QObject(parent) |
| 438 | { |
| 439 | } |
| 440 | |
| 441 | AbstractAnnotationItemDelegate::~AbstractAnnotationItemDelegate() = default; |
| 442 | |
| 443 | AnnotationModel::~AnnotationModel() = default; |
| 444 | |
| 445 | #include "moc_abstractannotationitemdelegate.cpp" |
| 446 | #include "moc_annotationinterface.cpp" |
| 447 | #include "moc_application.cpp" |
| 448 | #include "moc_codecompletionmodel.cpp" |
| 449 | #include "moc_command.cpp" |
| 450 | #include "moc_configpage.cpp" |
| 451 | #include "moc_document.cpp" |
| 452 | #include "moc_editor.cpp" |
| 453 | #include "moc_inlinenoteprovider.cpp" |
| 454 | #include "moc_mainwindow.cpp" |
| 455 | #include "moc_message.cpp" |
| 456 | #include "moc_plugin.cpp" |
| 457 | #include "moc_view.cpp" |
| 458 | |