File indexing completed on 2024-04-28 04:36:31

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
0003     SPDX-FileCopyrightText: 2008 Cédric Pasteur <cedric.pasteur@free.fr>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KDEVPLATFORM_ISOURCEFORMATTERCONTROLLER_H
0009 #define KDEVPLATFORM_ISOURCEFORMATTERCONTROLLER_H
0010 
0011 #include "interfacesexport.h"
0012 
0013 #include <QObject>
0014 #include <QString>
0015 
0016 #include <memory>
0017 
0018 class QUrl;
0019 
0020 namespace KDevelop {
0021 class IFileFormatter
0022 {
0023     Q_DISABLE_COPY_MOVE(IFileFormatter)
0024 public:
0025     IFileFormatter() = default;
0026     virtual ~IFileFormatter() = default;
0027 
0028     /**
0029      * Format text using packaged source formatter and style.
0030      * @param text the text to format
0031      * @param leftContext the context at the left side of the text.
0032      *        If it is in another line, it must end with a newline.
0033      * @param rightContext the context at the right side of the text.
0034      *        If it is in the next line, it must start with a newline.
0035      *
0036      * @note If the source formatter cannot work correctly with the context,
0037      *       it will just return the input text.
0038      */
0039     virtual QString format(const QString& text, const QString& leftContext = QString(),
0040                            const QString& rightContext = QString()) const = 0;
0041 };
0042 
0043 /** \short An interface to the controller managing all source formatter plugins
0044  */
0045 class KDEVPLATFORMINTERFACES_EXPORT ISourceFormatterController : public QObject
0046 {
0047     Q_OBJECT
0048 
0049 public:
0050     explicit ISourceFormatterController(QObject* parent = nullptr);
0051     ~ISourceFormatterController() override;
0052 
0053     using FileFormatterPtr = std::unique_ptr<IFileFormatter>;
0054 
0055     /**
0056      * Read user configuration for the given URL and package it into a file formatter object.
0057      * @param url the URL of a document to be formatted
0058      * @return the requested file formatter object or nullptr if no formatter is
0059      *         configured for @p url
0060      */
0061     virtual FileFormatterPtr fileFormatter(const QUrl& url) const = 0;
0062 
0063     ///\return @c true if there are formatters at all, @c false otherwise
0064     virtual bool hasFormatters() const = 0;
0065 
0066     /**
0067      * Disable source formatting
0068      * Once disabled, source formatting cannot be reenabled. Call this from within tests.
0069      */
0070     virtual void disableSourceFormatting() = 0;
0071     ///\return Whether or not source formatting is enabled
0072     virtual bool sourceFormattingEnabled() = 0;
0073 
0074 Q_SIGNALS:
0075     void hasFormattersChanged(bool hasFormatters);
0076 };
0077 
0078 }
0079 
0080 #endif // KDEVPLATFORM_ISOURCEFORMATTERCONTROLLER_H