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

0001 /*
0002     SPDX-FileCopyrightText: 2011 Yannick Motta <yannick.motta@gmail.com>
0003     SPDX-FileCopyrightText: 2011 Martin Heide <martin.heide@gmx.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KDEVPLATFORM_IBUDDYDOCUMENTFINDER_H
0009 #define KDEVPLATFORM_IBUDDYDOCUMENTFINDER_H
0010 
0011 #include <QVector>
0012 #include <QUrl>
0013 
0014 #include "interfacesexport.h"
0015 
0016 namespace KDevelop {
0017 
0018 /**
0019  * @short Implement this to add buddy document functionality to your language plugin.
0020  *
0021  * It enables the DocumentController (shell) to find related documents
0022  * (normally declaration/definition files, like foo.h and foo.cpp).
0023  * The DocumentController will position tabs of buddy documents next to
0024  * each other if the option UISettings.TabBarArrangeBuddies is 1.
0025  * The implementation also determines the order of the tabs in the tabbar.
0026  *
0027  * For finding a buddy document, the DocumentController addresses the
0028  * IBuddyDocumentFinder object that is registered for this mimetype:
0029  *
0030  * This class provides static "registry" functions to handle a specific buddy finding
0031  * method (i.e. an object of class IBuddyDocumentFinder) for different
0032  * mimetypes. Like this, you can have for example an IBuddyDocumentFinder
0033  * for C++ files which considers foo.cpp and foo.h as buddies, and an
0034  * IBuddyDocumentFinder for Ada files which considers bar.adb and bar.ads as
0035  * buddies.
0036  * Like this, the concept of buddy documents is extensible for every language
0037  * for which support is added to KDevelop.
0038  *
0039  * If you want to add IBuddyDocumentFinder functionality to your language
0040  * plugin, your main class will inherit from IBuddyDocumentFinder (or have an
0041  * attribute of this type). Then in your constructor, call
0042  * @code addFinder(mimetype, this) @endcode
0043  * for each mimetype that your plugin supports. It is no problem to register the
0044  * same IBuddyDocumentFinder for several mimetypes.
0045  *
0046  * In the same way, in your destructor, you'll call
0047  * @code removeFinder(mimetype, this) @endcode
0048  * for each supported mimetype, to avoid that your IBuddyDocumentFinder object
0049  * is used beyond its lifetime.
0050  *
0051  * After you have registered it, your IBuddyDocumentFinder implementation will be found by
0052  * @code finderForMimeType(mimetype) @endcode
0053  * For example, the shell's DocumentController calls this function with idoc->mimeType()
0054  * in order to find a buddy document of a particular IDocument *idoc.
0055  */
0056 class KDEVPLATFORMINTERFACES_EXPORT IBuddyDocumentFinder
0057 {
0058 public:
0059     virtual ~IBuddyDocumentFinder(){}
0060 
0061     /**
0062      * Called to determine if two document URLs should be considered as related.
0063      *
0064      * @return true, if the two documents are buddies.
0065      * For example, a C++ implementation would return true for
0066      * areBuddies(QUrl::fromLocalFile("...../foo.h"), QUrl::fromLocalFile("...../foo.cpp")).
0067      */
0068     virtual bool areBuddies(const QUrl& url1, const QUrl& url2) = 0;
0069 
0070     /**
0071      * @brief Called to determine the order of two documents in the tabbar.
0072      *
0073      * Example: a C++ implementation that wants to place the tab of the .h
0074      * file left of the .cpp tab must return true for <br>
0075      *   <i>buddyOrder(QUrl::fromLocalFile("foo.h"),   QUrl::fromLocalFile("foo.cpp"))</i><br>
0076      * and false for <br>
0077      *   <i>buddyOrder(QUrl::fromLocalFile("foo.cpp"), QUrl::fromLocalFile("foo.h"))</i>
0078      *
0079      * accepts two documents which are buddies,
0080      *         this means areBuddies(url1,url2) returned true.
0081      * @return true if url1's tab should be placed left of url2's tab and
0082      *         false for the inverse.
0083      */
0084     virtual bool buddyOrder(const QUrl& url1, const QUrl& url2) = 0;
0085 
0086 
0087     /**
0088      * Returns a list of QUrls of potential buddies of the document
0089      * provided by @p url.
0090      *
0091      * The urls are potential buddies and it is not ensured that the files
0092      * really exist.
0093      *
0094      * @returns list of potential buddy documents or an empty list
0095      *  if non are available.
0096      */
0097     virtual QVector<QUrl> potentialBuddies(const QUrl& url) const = 0;
0098 
0099     /**
0100      * @brief Registers a <i>IBuddyDocumentFinder</i> object for a mimetype.
0101      *
0102      * @details To be called in the constructor of language plugins.
0103      *   Afterwards, finderForMimeType( @p mimeType ) will return @p finder ,
0104      *   as long as the entry is not overwritten by another call to addFinder.
0105      */
0106     static void addFinder(const QString& mimeType, IBuddyDocumentFinder* finder);
0107 
0108     /**
0109      * @brief Un-registers a <i>IBuddyDocumentFinder</i> object for a mimetype.
0110      *
0111      * @details To be called in the destructor of language plugins.
0112      *   Afterwards, finderForMimeType( @p mimeType ) will return 0, until a new
0113      *   entry for this mimetype is created by addFinder().
0114      */
0115     static void removeFinder(const QString& mimeType);
0116 
0117     /**
0118      * Returns the registered IBuddyDocumentFinder for this mimetype, or 0.
0119      *
0120      * Used in the DocumentController (shell).
0121      */
0122     static IBuddyDocumentFinder* finderForMimeType(const QString& mimeType);
0123 };
0124 
0125 }
0126 
0127 #endif