File indexing completed on 2024-05-12 15:50:05

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef KSYNTAXHIGHLIGHTING_REPOSITORY_H
0008 #define KSYNTAXHIGHLIGHTING_REPOSITORY_H
0009 
0010 #include "ksyntaxhighlighting_export.h"
0011 
0012 #include <QVector>
0013 #include <QtGlobal>
0014 
0015 #include <memory>
0016 
0017 QT_BEGIN_NAMESPACE
0018 class QString;
0019 class QPalette;
0020 QT_END_NAMESPACE
0021 
0022 /**
0023  * @namespace KSyntaxHighlighting
0024  *
0025  * Syntax highlighting engine for Kate syntax definitions.
0026  * In order to access the syntax highlighting Definition files, use the
0027  * class Repository.
0028  *
0029  * @see Repository
0030  */
0031 namespace KSyntaxHighlighting
0032 {
0033 class Definition;
0034 class RepositoryPrivate;
0035 class Theme;
0036 
0037 /**
0038  * @brief Syntax highlighting repository.
0039  *
0040  * @section repo_intro Introduction
0041  *
0042  * The Repository gives access to all syntax Definitions available on the
0043  * system, typically described in *.xml files. The Definition files are read
0044  * from the resource that is compiled into the executable, and from the file
0045  * system. If a Definition exists in the resource and on the file system,
0046  * then the one from the file system is chosen.
0047  *
0048  * @section repo_access Definitions and Themes
0049  *
0050  * Typically, only one instance of the Repository is needed. This single
0051  * instance can be thought of as a singleton you keep alive throughout the
0052  * lifetime of your application. Then, either call definitionForName() with the
0053  * given language name (e.g. "QML" or "Java"), or definitionForFileName() to
0054  * obtain a Definition based on the filename/mimetype of the file. The
0055  * function definitions() returns a list of all available syntax Definition%s.
0056  *
0057  * In addition to Definitions, the Repository also provides a list of Themes.
0058  * A Theme is defined by a set of default text style colors as well as editor
0059  * colors. These colors together provide all required colros for drawing all
0060  * primitives of a text editor. All available Theme%s can be queried through
0061  * themes(), and a Theme with a specific name is obtained through theme().
0062  * Additionally, defaultTheme() provides a way to obtain a default theme for
0063  * either a light or a black color theme.
0064  *
0065  * @section repo_search_paths Search Paths
0066  *
0067  * All highlighting Definition and Theme files are compiled into the shared
0068  * KSyntaxHighlighting library by using the Qt resource system. Loading
0069  * additional files from disk is supported as well.
0070  *
0071  * Loading syntax Definition files works as follows:
0072  *
0073  * -# First, all syntax highlighting files are loaded that are located in
0074  *    QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("org.kde.syntax-highlighting/syntax"), QStandardPaths::LocateDirectory);
0075  *    Under Unix, this uses $XDG_DATA_HOME and $XDG_DATA_DIRS, which could
0076  *    map to $HOME/.local5/share/org.kde.syntax-highlighting/syntax and
0077  *    /usr/share/org.kde.syntax-highlighting/syntax.
0078  *
0079  * -# Next, for backwards compatibility with Kate, all syntax highlighting
0080  *    files are loaded that are located in
0081  *    QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("katepart5/syntax"), QStandardPaths::LocateDirectory);
0082  *    Again, under Unix, this uses $XDG_DATA_HOME and $XDG_DATA_DIRS, which
0083  *    could map to $HOME/.local5/share/katepart5/syntax and
0084  *    /usr/share/katepart5/syntax.
0085  *
0086  * -# Then, all files compiled into the library through resources are loaded.
0087  *    The internal resource path is ":/org.kde.syntax-highlighting/syntax".
0088  *    This path should never be touched by other applications.
0089  *
0090  * -# Then, all custom files compiled into resources are loaded.
0091  *    The resource path is ":/org.kde.syntax-highlighting/syntax-addons".
0092  *    This path can be used by other libraries/applications to bundle specialized definitions.
0093  *    Per default this path isn't used by the framework itself.
0094  *
0095  * -# Finally, the search path can be extended by calling addCustomSearchPath().
0096  *    A custom search path can either be a path on disk or again a path to
0097  *    a Qt resource.
0098  *
0099  * Similarly, loading Theme files works as follows:
0100  *
0101  * -# First, all Theme files are loaded that are located in
0102  *    QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("org.kde.syntax-highlighting/themes"), QStandardPaths::LocateDirectory);
0103  *    Under Unix, this uses $XDG_DATA_HOME and $XDG_DATA_DIRS, which could
0104  *    map to $HOME/.local5/share/org.kde.syntax-highlighting/themes and
0105  *    /usr/share/org.kde.syntax-highlighting/themes.
0106  *
0107  * -# Then, all files compiled into the library through resources are loaded.
0108  *    The internal resource path is ":/org.kde.syntax-highlighting/themes".
0109  *    This path should never be touched by other applications.
0110  *
0111  * -# Then, all custom files compiled into resources are loaded.
0112  *    The resource path is ":/org.kde.syntax-highlighting/themes-addons".
0113  *    This path can be used by other libraries/applications to bundle specialized themes.
0114  *    Per default this path isn't used by the framework itself.
0115  *
0116  * -# Finally, all Theme%s located in the paths added addCustomSearchPath()
0117  *    are loaded.
0118  *
0119  * @note Whenever a Definition or a Theme exists twice, the variant with
0120  *       higher version is used.
0121  *
0122  * @note The QStandardPaths lookup can be disabled by compiling the framework with the -DNO_STANDARD_PATHS define.
0123  *
0124  * @see Definition, Theme, AbstractHighlighter
0125  * @since 5.28
0126  */
0127 class KSYNTAXHIGHLIGHTING_EXPORT Repository
0128 {
0129 public:
0130     /**
0131      * Create a new syntax definition repository.
0132      * This will read the meta data information of all available syntax
0133      * definition, which is a moderately expensive operation, it's therefore
0134      * recommended to keep a single instance of Repository around as long
0135      * as you need highlighting in your application.
0136      */
0137     Repository();
0138     ~Repository();
0139 
0140     /**
0141      * Returns the Definition named @p defName.
0142      *
0143      * If no Definition is found, Definition::isValid() of the returned instance
0144      * returns false.
0145      *
0146      * @note This uses case sensitive, untranslated names. For instance,
0147      *       the javascript.xml definition file sets its name to @e JavaScript.
0148      *       Therefore, only the string "JavaScript" will return a valid
0149      *       Definition file.
0150      */
0151     Definition definitionForName(const QString &defName) const;
0152 
0153     /**
0154      * Returns the best matching Definition for the file named @p fileName.
0155      * The match is performed based on the \e extensions and @e mimetype of
0156      * the definition files. If multiple matches are found, the one with the
0157      * highest priority is returned.
0158      *
0159      * If no match is found, Definition::isValid() of the returned instance
0160      * returns false.
0161      */
0162     Definition definitionForFileName(const QString &fileName) const;
0163 
0164     /**
0165      * Returns all Definition%s for the file named @p fileName sorted by priority.
0166      * The match is performed based on the \e extensions and @e mimetype of
0167      * the definition files.
0168      *
0169      * @since 5.56
0170      */
0171     QVector<Definition> definitionsForFileName(const QString &fileName) const;
0172 
0173     /**
0174      * Returns the best matching Definition to the type named @p mimeType
0175      *
0176      * If no match is found, Definition::isValid() of the returned instance
0177      * returns false.
0178      *
0179      * @since 5.50
0180      */
0181     Definition definitionForMimeType(const QString &mimeType) const;
0182 
0183     /**
0184      * Returns all Definition%s to the type named @p mimeType sorted by priority
0185      *
0186      * @since 5.56
0187      */
0188     QVector<Definition> definitionsForMimeType(const QString &mimeType) const;
0189 
0190     /**
0191      * Returns all available Definition%s.
0192      * Definition%ss are ordered by translated section and translated names,
0193      * for consistent displaying.
0194      */
0195     QVector<Definition> definitions() const;
0196 
0197     /**
0198      * Returns all available color themes.
0199      * The returned list should never be empty.
0200      */
0201     QVector<Theme> themes() const;
0202 
0203     /**
0204      * Returns the theme called @p themeName.
0205      * If the requested theme cannot be found, the retunred Theme is invalid,
0206      * see Theme::isValid().
0207      */
0208     Theme theme(const QString &themeName) const;
0209 
0210     /**
0211      * Built-in default theme types.
0212      * @see defaultTheme()
0213      */
0214     enum DefaultTheme {
0215         //! Theme with a light background color.
0216         LightTheme,
0217         //! Theme with a dark background color.
0218         DarkTheme
0219     };
0220 
0221     /**
0222      * Returns a default theme instance of the given type.
0223      * The returned Theme is guaranteed to be a valid theme.
0224      * @since 5.79
0225      */
0226     Theme defaultTheme(DefaultTheme t = LightTheme) const;
0227 
0228     /**
0229      * Returns a default theme instance of the given type.
0230      * The returned Theme is guaranteed to be a valid theme.
0231      *
0232      * KF6: remove in favor of const variant
0233      */
0234     Theme defaultTheme(DefaultTheme t = LightTheme);
0235 
0236     /**
0237      * Returns the best matching theme for the given palette
0238      * @since 5.79
0239      **/
0240     Theme themeForPalette(const QPalette &palette) const;
0241 
0242     /**
0243      * Returns the best matching theme for the given palette
0244      * @since 5.77
0245      *
0246      * KF6: remove in favor of const variant
0247      **/
0248     Theme themeForPalette(const QPalette &palette);
0249 
0250     /**
0251      * Reloads the repository.
0252      * This is a moderately expensive operations and should thus only be
0253      * triggered when the installed syntax definition files changed.
0254      */
0255     void reload();
0256 
0257     /**
0258      * Add a custom search path to the repository.
0259      * This path will be searched in addition to the usual locations for syntax
0260      * and theme definition files. Both locations on disk as well as Qt
0261      * resource paths are supported.
0262      *
0263      * @note Internally, the two sub-folders @p path/syntax as well as
0264      *       @p path/themes are searched for additional Definition%s and
0265      *       Theme%s. Do not append @e syntax or @e themes to @p path
0266      *       yourself.
0267      *
0268      * @note Calling this triggers a reload() of the repository.
0269      *
0270      * @since 5.39
0271      */
0272     void addCustomSearchPath(const QString &path);
0273 
0274     /**
0275      * Returns the list of custom search paths added to the repository.
0276      * By default, this list is empty.
0277      *
0278      * @see addCustomSearchPath()
0279      * @since 5.39
0280      */
0281     QVector<QString> customSearchPaths() const;
0282 
0283 private:
0284     Q_DISABLE_COPY(Repository)
0285     friend class RepositoryPrivate;
0286     std::unique_ptr<RepositoryPrivate> d;
0287 };
0288 
0289 }
0290 
0291 #endif // KSYNTAXHIGHLIGHTING_REPOSITORY_H