File indexing completed on 2024-04-14 14:47:45

0001 /*
0002     SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef PHPEDITORINTEGRATOR_H
0008 #define PHPEDITORINTEGRATOR_H
0009 
0010 #include <language/editor/rangeinrevision.h>
0011 
0012 #include "phpduchainexport.h"
0013 
0014 #include "kdev-pg-token-stream.h"
0015 
0016 namespace Php
0017 {
0018 
0019 class ParseSession;
0020 struct AstNode;
0021 
0022 
0023 /**
0024  * Provides facilities for easy integration of a text editor component with
0025  * the information parsed from a source file.
0026  *
0027  * Uses a disguised singleton + stateful design.
0028  *
0029  * \todo introduce stacks for the state?
0030  */
0031 class KDEVPHPDUCHAIN_EXPORT EditorIntegrator
0032 {
0033 public:
0034     EditorIntegrator(ParseSession* session);
0035 
0036     ParseSession* parseSession() const;
0037 
0038     enum Edge {
0039         FrontEdge,
0040         BackEdge
0041     };
0042 
0043     enum RangeEdge {
0044         InnerEdge,
0045         OuterEdge
0046     };
0047 
0048     /**
0049      * Finds the location and \a file where the given \a token was parsed from.
0050      *
0051      * \param token token to locate
0052      * \param edge set to FrontEdge to return the start position of the token, BackEdge to return the end position.
0053      *
0054      * \returns the requested cursor relating to the start or end of the given token.
0055      */
0056     KDevelop::CursorInRevision findPosition(const KDevPG::TokenStream::Token& token, Edge edge = BackEdge) const;
0057 
0058     /**
0059      * Finds the location and \a file where the given \a token was parsed from.
0060      *
0061      * \param token token to locate
0062      * \param edge set to FrontEdge to return the start position of the token, BackEdge to return the end position.
0063      *
0064      * \returns the requested cursor relating to the start or end of the given token.
0065      */
0066     KDevelop::CursorInRevision findPosition(qint64 token, Edge edge = BackEdge) const;
0067 
0068     /**
0069      * Create a range encompassing the given AstNode \a node.
0070      *
0071      * \overload
0072      */
0073     KDevelop::RangeInRevision findRange(AstNode* node, RangeEdge = OuterEdge) const;
0074 
0075     /**
0076      * Create a range encompassing the given AstNode \a nodes.
0077      *
0078      * \overload
0079      */
0080     KDevelop::RangeInRevision findRange(AstNode* from, AstNode* to) const;
0081 
0082     /**
0083      * Create a range encompassing the given AstNode \a token.
0084      *
0085      * \overload
0086      */
0087     KDevelop::RangeInRevision findRange(const KDevPG::TokenStream::Token& token) const;
0088 
0089     /**
0090      * Create a range encompassing the given AstNode \a token.
0091      *
0092      * \overload
0093      */
0094     KDevelop::RangeInRevision findRange(qint64 token) const;
0095 
0096     /**
0097      * Create a range encompassing the given AstNode \a tokens.
0098      *
0099      * \overload
0100      */
0101     KDevelop::RangeInRevision findRange(qint64 start_token, qint64 end_token) const;
0102 
0103     /**
0104      * Retrieve the string represented by a token.
0105      */
0106     QString tokenToString(qint64 token) const;
0107 
0108 private:
0109     ParseSession* m_session;
0110 };
0111 
0112 }
0113 
0114 #endif // PHPEDITORINTEGRATOR_H
0115