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

0001 /* This file is part of KDevelop
0002     Copyright 2006 Hamish Rodda <rodda@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License version 2 as published by the Free Software Foundation.
0007 
0008    This library is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    Library General Public License for more details.
0012 
0013    You should have received a copy of the GNU Library General Public License
0014    along with this library; see the file COPYING.LIB.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #ifndef CSSEDITORINTEGRATOR_H
0020 #define CSSEDITORINTEGRATOR_H
0021 
0022 #include <language/editor/rangeinrevision.h>
0023 
0024 #include "parserexport.h"
0025 #include "kdev-pg-token-stream.h"
0026 
0027 namespace Css
0028 {
0029 
0030 class ParseSession;
0031 struct AstNode;
0032 
0033 
0034 /**
0035  * Provides facilities for easy integration of a text editor component with
0036  * the information parsed from a source file.
0037  *
0038  * Uses a disguised singleton + stateful design.
0039  *
0040  * \todo introduce stacks for the state?
0041  */
0042 class KDEVCSSPARSER_EXPORT EditorIntegrator
0043 {
0044 public:
0045     enum Edge {
0046         FrontEdge,
0047         BackEdge
0048     };
0049 
0050     enum RangeEdge {
0051         InnerEdge,
0052         OuterEdge
0053     };
0054 
0055     EditorIntegrator();
0056     void setParseSession(ParseSession* session);
0057     ParseSession* parseSession() const;
0058 
0059     /**
0060      * Finds the location and \a file where the given \a token was parsed from.  This function
0061      * does not change any of the EditorIntegrator's state.
0062      *
0063      * \param token token to locate
0064      * \param edge set to FrontEdge to return the start position of the token, BackEdge to return the end position.
0065      *
0066      * \returns the requested cursor relating to the start or end of the given token.
0067      */
0068     KDevelop::CursorInRevision findPosition(const KDevPG::TokenStream::Token& token, Edge edge = BackEdge) const;
0069 
0070     /**
0071      * Finds the location and \a file where the given \a token was parsed from.
0072      * This function does not change any of the EditorIntegrator's state.
0073      *
0074      * \param token token to locate
0075      * \param edge set to FrontEdge to return the start position of the token, BackEdge to return the end position.
0076      *
0077      * \returns the requested cursor relating to the start or end of the given token.
0078      */
0079     KDevelop::CursorInRevision findPosition(qint64 token, Edge edge = BackEdge) const;
0080 
0081     /**
0082      * Create a range encompassing the given AstNode \a node.
0083      * This function does not change any of the EditorIntegrator's state.
0084      *
0085      * \overload
0086      */
0087     KDevelop::RangeInRevision findRange(AstNode* node, RangeEdge = OuterEdge);
0088 
0089     /**
0090      * Create a range encompassing the given AstNode \a nodes.
0091      * This function does not change any of the EditorIntegrator's state.
0092      *
0093      * \overload
0094      */
0095     KDevelop::RangeInRevision findRange(AstNode* from, AstNode* to);
0096 
0097     /**
0098      * Create a range encompassing the given AstNode \a token.
0099      * This function does not change any of the EditorIntegrator's state.
0100      *
0101      * \overload
0102      */
0103     KDevelop::RangeInRevision findRange(const KDevPG::TokenStream::Token& token);
0104 
0105     /**
0106      * Create a range encompassing the given AstNode \a token.
0107      * This function does not change any of the EditorIntegrator's state.
0108      *
0109      * \overload
0110      */
0111     KDevelop::RangeInRevision findRange(qint64 token);
0112 
0113     /**
0114      * Create a range encompassing the given AstNode \a tokens.
0115      * This function does not change any of the EditorIntegrator's state.
0116      *
0117      * \overload
0118      */
0119     KDevelop::RangeInRevision findRange(qint64 start_token, qint64 end_token);
0120 
0121     /**
0122      * Retrieve the string represented by a token.
0123      */
0124     QString tokenToString(qint64 token) const;
0125     QString nodeToString(AstNode *node) const;
0126 
0127 private:
0128     ParseSession* m_session;
0129 };
0130 
0131 }
0132 
0133 #endif // PHPEDITORINTEGRATOR_H
0134