File indexing completed on 2024-11-10 06:41:12

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams\gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KATE_COMPLETIONTESTMODELS_H
0009 #define KATE_COMPLETIONTESTMODELS_H
0010 
0011 #include "codecompletiontestmodel.h"
0012 #include <ktexteditor/codecompletionmodelcontrollerinterface.h>
0013 
0014 #include <ktexteditor/document.h>
0015 #include <ktexteditor/view.h>
0016 
0017 #include <QRegularExpression>
0018 
0019 using namespace KTextEditor;
0020 
0021 class CustomRangeModel : public CodeCompletionTestModel, public CodeCompletionModelControllerInterface
0022 {
0023     Q_OBJECT
0024     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
0025 public:
0026     explicit CustomRangeModel(KTextEditor::View *parent = nullptr, const QString &startText = QString())
0027         : CodeCompletionTestModel(parent, startText)
0028     {
0029     }
0030     Range completionRange(View *view, const Cursor &position) override
0031     {
0032         Range range = CodeCompletionModelControllerInterface::completionRange(view, position);
0033         if (range.start().column() > 0) {
0034             KTextEditor::Range preRange(Cursor(range.start().line(), range.start().column() - 1), Cursor(range.start().line(), range.start().column()));
0035             qDebug() << preRange << view->document()->text(preRange);
0036             if (view->document()->text(preRange) == QLatin1String("$")) {
0037                 range.expandToRange(preRange);
0038                 qDebug() << "using custom completion range" << range;
0039             }
0040         }
0041         return range;
0042     }
0043 
0044     bool shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion) override
0045     {
0046         Q_UNUSED(view);
0047         Q_UNUSED(range);
0048         static const QRegularExpression allowedText(QStringLiteral("^\\$?(?:\\w*)$"), QRegularExpression::UseUnicodePropertiesOption);
0049         return !allowedText.match(currentCompletion).hasMatch();
0050     }
0051 };
0052 
0053 class CustomAbortModel : public CodeCompletionTestModel, public CodeCompletionModelControllerInterface
0054 {
0055     Q_OBJECT
0056     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
0057 public:
0058     explicit CustomAbortModel(KTextEditor::View *parent = nullptr, const QString &startText = QString())
0059         : CodeCompletionTestModel(parent, startText)
0060     {
0061     }
0062 
0063     bool shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion) override
0064     {
0065         Q_UNUSED(view);
0066         Q_UNUSED(range);
0067         static const QRegularExpression allowedText(QStringLiteral("^(?:[\\w-]*)"), QRegularExpression::UseUnicodePropertiesOption);
0068         return !allowedText.match(currentCompletion).hasMatch();
0069     }
0070 };
0071 
0072 class EmptyFilterStringModel : public CodeCompletionTestModel, public CodeCompletionModelControllerInterface
0073 {
0074     Q_OBJECT
0075     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
0076 public:
0077     explicit EmptyFilterStringModel(KTextEditor::View *parent = nullptr, const QString &startText = QString())
0078         : CodeCompletionTestModel(parent, startText)
0079     {
0080     }
0081 
0082     QString filterString(View *, const Range &, const Cursor &) override
0083     {
0084         return QString();
0085     }
0086 };
0087 
0088 class UpdateCompletionRangeModel : public CodeCompletionTestModel, public CodeCompletionModelControllerInterface
0089 {
0090     Q_OBJECT
0091     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
0092 public:
0093     explicit UpdateCompletionRangeModel(KTextEditor::View *parent = nullptr, const QString &startText = QString())
0094         : CodeCompletionTestModel(parent, startText)
0095     {
0096     }
0097 
0098     Range updateCompletionRange(View *view, const Range &range) override
0099     {
0100         Q_UNUSED(view);
0101         if (view->document()->text(range) == QStringLiteral("ab")) {
0102             return Range(Cursor(range.start().line(), 0), range.end());
0103         }
0104         return range;
0105     }
0106     bool shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion) override
0107     {
0108         Q_UNUSED(view);
0109         Q_UNUSED(range);
0110         Q_UNUSED(currentCompletion);
0111         return false;
0112     }
0113 };
0114 
0115 class StartCompletionModel : public CodeCompletionTestModel, public CodeCompletionModelControllerInterface
0116 {
0117     Q_OBJECT
0118     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
0119 public:
0120     explicit StartCompletionModel(KTextEditor::View *parent = nullptr, const QString &startText = QString())
0121         : CodeCompletionTestModel(parent, startText)
0122     {
0123     }
0124 
0125     bool shouldStartCompletion(View *view, const QString &insertedText, bool userInsertion, const Cursor &position) override
0126     {
0127         Q_UNUSED(view);
0128         Q_UNUSED(userInsertion);
0129         Q_UNUSED(position);
0130         if (insertedText.isEmpty()) {
0131             return false;
0132         }
0133 
0134         QChar lastChar = insertedText.at(insertedText.size() - 1);
0135         if (lastChar == u'%') {
0136             return true;
0137         }
0138         return false;
0139     }
0140 };
0141 
0142 class ImmideatelyAbortCompletionModel : public CodeCompletionTestModel, public CodeCompletionModelControllerInterface
0143 {
0144     Q_OBJECT
0145     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
0146 public:
0147     explicit ImmideatelyAbortCompletionModel(KTextEditor::View *parent = nullptr, const QString &startText = QString())
0148         : CodeCompletionTestModel(parent, startText)
0149     {
0150     }
0151 
0152     bool shouldAbortCompletion(KTextEditor::View *view, const KTextEditor::Range &range, const QString &currentCompletion) override
0153     {
0154         Q_UNUSED(view);
0155         Q_UNUSED(range);
0156         Q_UNUSED(currentCompletion);
0157         return true;
0158     }
0159 };
0160 
0161 #endif