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 #include "editorintegrator.h"
0020 
0021 #include <ktexteditor/document.h>
0022 
0023 #include "cssast.h"
0024 #include "parsesession.h"
0025 
0026 using namespace KTextEditor;
0027 using namespace Css;
0028 
0029 EditorIntegrator::EditorIntegrator()
0030         : m_session(nullptr)
0031 {
0032 }
0033 
0034 void EditorIntegrator::setParseSession(ParseSession* session)
0035 {
0036     m_session = session;
0037 }
0038 
0039 KDevelop::CursorInRevision EditorIntegrator::findPosition(qint64 token, Edge edge) const
0040 {
0041     const KDevPG::TokenStream::Token& t = m_session->tokenStream()->at(token);
0042     return findPosition(t, edge);
0043 }
0044 
0045 KDevelop::CursorInRevision EditorIntegrator::findPosition(const KDevPG::TokenStream::Token & token, Edge edge) const
0046 {
0047     if (edge == BackEdge) {
0048         // Apparently KTE expects a range to go until _after_ the last character that should be included
0049         // however the parser calculates endCol as the index _before_ the last included character, so adjust here
0050         return m_session->positionAt(token.end + 1);
0051     } else {
0052         return m_session->positionAt(token.begin);
0053     }
0054 }
0055 
0056 KDevelop::RangeInRevision EditorIntegrator::findRange(AstNode * node, RangeEdge edge)
0057 {
0058     Q_UNUSED(edge);
0059     return KDevelop::RangeInRevision(findPosition(node->startToken, FrontEdge), findPosition(node->endToken, BackEdge));
0060 }
0061 
0062 KDevelop::RangeInRevision EditorIntegrator::findRange(qint64 token)
0063 {
0064     return KDevelop::RangeInRevision(findPosition(token, FrontEdge), findPosition(token, BackEdge));
0065 }
0066 
0067 KDevelop::RangeInRevision EditorIntegrator::findRange(qint64 startToken, qint64 endToken)
0068 {
0069     return KDevelop::RangeInRevision(findPosition(startToken, FrontEdge), findPosition(endToken, BackEdge));
0070 }
0071 
0072 KDevelop::RangeInRevision EditorIntegrator::findRange(AstNode* from, AstNode* to)
0073 {
0074     return KDevelop::RangeInRevision(findPosition(from->startToken, FrontEdge), findPosition(to->endToken, BackEdge));
0075 }
0076 
0077 KDevelop::RangeInRevision EditorIntegrator::findRange(const KDevPG::TokenStream::Token & token)
0078 {
0079     return KDevelop::RangeInRevision(findPosition(token, FrontEdge), findPosition(token, BackEdge));
0080 }
0081 
0082 QString EditorIntegrator::tokenToString(qint64 token) const
0083 {
0084     return m_session->symbol(token);
0085 }
0086 
0087 QString EditorIntegrator::nodeToString(AstNode* node) const
0088 {
0089     return m_session->symbol(node);
0090 }
0091 
0092 
0093 ParseSession * EditorIntegrator::parseSession() const
0094 {
0095     Q_ASSERT(m_session);
0096     return m_session;
0097 }
0098