File indexing completed on 2024-05-05 05:51:21

0001 /*
0002     SPDX-FileCopyrightText: 2018 Sven Brauch <mail@svenbrauch.de>
0003     SPDX-FileCopyrightText: 2018 Michal Srb <michalsrb@gmail.com>
0004     SPDX-FileCopyrightText: 2020 Jan Paul Batrina <jpmbatrina01@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include <unordered_map>
0012 
0013 #include <KTextEditor/ConfigPage>
0014 #include <KTextEditor/InlineNoteProvider>
0015 #include <KTextEditor/MainWindow>
0016 #include <KTextEditor/Plugin>
0017 
0018 #include <QHash>
0019 #include <QList>
0020 #include <QRegularExpression>
0021 #include <QVariant>
0022 
0023 class ColorPickerInlineNoteProvider : public KTextEditor::InlineNoteProvider
0024 {
0025     Q_OBJECT
0026 public:
0027     explicit ColorPickerInlineNoteProvider(KTextEditor::Document *doc);
0028     ~ColorPickerInlineNoteProvider() override;
0029 
0030     void updateColorMatchingCriteria();
0031     // if startLine == -1, update all notes. endLine is optional
0032     void updateNotes(int startLine = -1, int endLine = -1);
0033 
0034     QList<int> inlineNotes(int line) const override;
0035     QSize inlineNoteSize(const KTextEditor::InlineNote &note) const override;
0036     void paintInlineNote(const KTextEditor::InlineNote &note, QPainter &painter, Qt::LayoutDirection) const override;
0037     void inlineNoteActivated(const KTextEditor::InlineNote &note, Qt::MouseButtons buttons, const QPoint &globalPos) override;
0038 
0039 private:
0040     KTextEditor::Document *m_doc;
0041     int m_startChangedLines = -1;
0042     int m_endChangedLines = -1;
0043     int m_previousNumLines = -1;
0044 
0045     struct ColorIndices {
0046         // When m_putPreviewAfterColor is true, otherColorIndices holds the starting color indices while colorNoteIndices holds the end color indices (and vice
0047         // versa) colorNoteIndices[i] corresponds to otherColorIndices[i]
0048         QList<int> colorNoteIndices;
0049         QList<int> otherColorIndices;
0050     };
0051 
0052     // mutable is used here since InlineNoteProvider::inlineNotes() is const only, and we update the notes lazily (only when inlineNotes() is called)
0053     mutable QHash<int, ColorIndices> m_colorNoteIndices;
0054 
0055     QRegularExpression m_colorRegex;
0056     QList<int> m_matchHexLengths;
0057     bool m_putPreviewAfterColor;
0058     bool m_matchNamedColors;
0059 };
0060 
0061 class KateColorPickerPlugin : public KTextEditor::Plugin
0062 {
0063     Q_OBJECT
0064 public:
0065     explicit KateColorPickerPlugin(QObject *parent = nullptr, const QVariantList & = QVariantList());
0066     ~KateColorPickerPlugin() override;
0067 
0068     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
0069     void readConfig();
0070 
0071 private:
0072     void addDocument(KTextEditor::Document *doc);
0073 
0074     int configPages() const override
0075     {
0076         return 1;
0077     }
0078     KTextEditor::ConfigPage *configPage(int number = 0, QWidget *parent = nullptr) override;
0079 
0080     KTextEditor::MainWindow *m_mainWindow;
0081     std::unordered_map<KTextEditor::Document *, std::unique_ptr<ColorPickerInlineNoteProvider>> m_inlineColorNoteProviders;
0082 };