File indexing completed on 2024-05-05 05:52:19

0001 /*  This file is part of the Kate project.
0002  *  Based on the snippet plugin from KDevelop 4.
0003  *
0004  *  SPDX-FileCopyrightText: 2007 Robert Gruber <rgruber@users.sourceforge.net>
0005  *  SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
0006  *  SPDX-FileCopyrightText: 2012 Christoph Cullmann <cullmann@kde.org>
0007  *
0008  *  SPDX-License-Identifier: LGPL-2.0-or-later
0009  */
0010 
0011 #pragma once
0012 
0013 #include <QDir>
0014 #include <QStandardItem>
0015 #include <QStringList>
0016 
0017 namespace KTextEditor
0018 {
0019 }
0020 
0021 /**
0022  * Each object of this type represents a repository of snippets. Each repository
0023  * has a name and will be saved to an XML file that includes all items of this repository.
0024  *
0025  * To access the snippets in this repo, iterate over it's children and dynamic_cast as required.
0026  * To add a snippet, @p appendRow() it.
0027  * To access the name of the repository, use @p text() and @p setText().
0028  *
0029  * NOTE: Unchecked repositories are considered "disabled" in the sense that their snippets
0030  *       won't show up during code completion.
0031  *
0032  * @author Robert Gruber <rgruber@users.sourceforge.net>
0033  * @author Milian Wolff <mail@milianw.de>
0034  */
0035 class SnippetRepository : public QStandardItem
0036 {
0037 public:
0038     /**
0039      * Creates a new SnippetRepository. When @p file exists it will be parsed (XML).
0040      *
0041      * @param file Location of the snippet's repository file.
0042      */
0043     explicit SnippetRepository(const QString &file);
0044     ~SnippetRepository() override;
0045 
0046     /**
0047      * Creates a snippet repository for the given name and adds it to the SnippetStore.
0048      */
0049     static SnippetRepository *createRepoFromName(const QString &name);
0050 
0051     static constexpr inline int RepoItemType = QStandardItem::UserType + 1;
0052 
0053     int type() const override
0054     {
0055         return RepoItemType;
0056     }
0057 
0058     static SnippetRepository *fromItem(QStandardItem *item)
0059     {
0060         if (item && item->type() == RepoItemType) {
0061             return static_cast<SnippetRepository *>(item);
0062         }
0063         return nullptr;
0064     }
0065 
0066     /**
0067      * The license for the snippets contained in this repository.
0068      */
0069     QString license() const;
0070     /**
0071      * Sets the license for the snippets contained in this repository.
0072      */
0073     void setLicense(const QString &license);
0074 
0075     /**
0076      * The author(s) of the snippets contained in this repository.
0077      */
0078     QString authors() const;
0079     /**
0080      * Sets the author(s) of the snippets contained in this repository.
0081      */
0082     void setAuthors(const QString &authors);
0083 
0084     /**
0085      * The valid filetypes for the snippets contained in this repository.
0086      * Empty list means no restriction on the modes.
0087      * @see KTextEditor::Document::mode()
0088      */
0089     QStringList fileTypes() const;
0090     /**
0091      * Sets the valid filetypes for the snippets contained in this repository.
0092      * An empty list, or any list which contains an element "*" is treated as
0093      * a no-restriction filter.
0094      */
0095     void setFileTypes(const QStringList &filetypes);
0096 
0097     /**
0098      * The path to this repository's file.
0099      */
0100     const QString &file() const;
0101 
0102     /**
0103      * The namespace associated with this repository.
0104      * Used in CodeCompletion for filtering.
0105      */
0106     QString completionNamespace() const;
0107     /**
0108      * Sets the code completion namespace for this repository.
0109      */
0110     void setCompletionNamespace(const QString &completionNamespace);
0111 
0112     /**
0113      * The QtScript(s) associated with this repository.
0114      *
0115      * @since KDE 4.5
0116      */
0117     QString script() const;
0118 
0119     /**
0120      * Sets the QtScript(s) associated with this repository.
0121      *
0122      * @since KDE 4.5
0123      */
0124     void setScript(const QString &script);
0125 
0126     /**
0127      * Remove this repository from the disk. Also deletes the item and all its children.
0128      */
0129     void remove();
0130 
0131     /**
0132      * Save this repository to disk.
0133      */
0134     void save();
0135 
0136     /**
0137      * Get directory for data storage from QStandardPaths
0138      */
0139     static QDir dataPath();
0140 
0141     QVariant data(int role = Qt::UserRole + 1) const override;
0142     void setData(const QVariant &value, int role = Qt::UserRole + 1) override;
0143 
0144     /// parses the XML file and load the containing snippets.
0145     void parseFile();
0146 
0147 private:
0148     /// path to the repository file
0149     QString m_file;
0150     /// license of the snippets in this repo
0151     QString m_license;
0152     /// author(s) of the snippets in this repo
0153     QString m_authors;
0154     /// valid filetypes for the snippets in this repo
0155     QStringList m_filetypes;
0156     /// filtering namespace for code completion
0157     QString m_namespace;
0158     /// QtScript with functions to be used in the snippets; common to all snippets
0159     QString m_script;
0160 };