File indexing completed on 2024-04-21 15:24:17

0001 /*
0002     SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "editorintegrator.h"
0008 
0009 #include <ktexteditor/document.h>
0010 
0011 #include "phpast.h"
0012 #include "parsesession.h"
0013 
0014 using namespace Php;
0015 
0016 EditorIntegrator::EditorIntegrator(ParseSession* session)
0017         : m_session(session)
0018 {
0019 }
0020 
0021 KDevelop::CursorInRevision EditorIntegrator::findPosition(qint64 token, Edge edge) const
0022 {
0023     const KDevPG::TokenStream::Token& t = m_session->tokenStream()->at(token);
0024     return findPosition(t, edge);
0025 }
0026 
0027 KDevelop::CursorInRevision EditorIntegrator::findPosition(const KDevPG::TokenStream::Token & token, Edge edge) const
0028 {
0029     if (edge == BackEdge) {
0030         // Apparently KTE expects a range to go until _after_ the last character that should be included
0031         // however the parser calculates endCol as the index _before_ the last included character, so adjust here
0032         return m_session->positionAt(token.end + 1);
0033     } else {
0034         return m_session->positionAt(token.begin);
0035     }
0036 }
0037 
0038 KDevelop::RangeInRevision EditorIntegrator::findRange(AstNode * node, RangeEdge edge) const
0039 {
0040     Q_UNUSED(edge);
0041     return KDevelop::RangeInRevision(findPosition(node->startToken, FrontEdge), findPosition(node->endToken, BackEdge));
0042 }
0043 
0044 KDevelop::RangeInRevision EditorIntegrator::findRange(qint64 startToken, qint64 endToken) const
0045 {
0046     return KDevelop::RangeInRevision(findPosition(startToken, FrontEdge), findPosition(endToken, BackEdge));
0047 }
0048 
0049 KDevelop::RangeInRevision EditorIntegrator::findRange(qint64 token) const
0050 {
0051     return KDevelop::RangeInRevision(findPosition(token, FrontEdge), findPosition(token, BackEdge));
0052 }
0053 
0054 KDevelop::RangeInRevision EditorIntegrator::findRange(AstNode* from, AstNode* to) const
0055 {
0056     return KDevelop::RangeInRevision(findPosition(from->startToken, FrontEdge), findPosition(to->endToken, BackEdge));
0057 }
0058 
0059 KDevelop::RangeInRevision EditorIntegrator::findRange(const KDevPG::TokenStream::Token & token) const
0060 {
0061     return KDevelop::RangeInRevision(findPosition(token, FrontEdge), findPosition(token, BackEdge));
0062 }
0063 
0064 QString EditorIntegrator::tokenToString(qint64 token) const
0065 {
0066     return m_session->symbol(token);
0067 }
0068 
0069 ParseSession * EditorIntegrator::parseSession() const
0070 {
0071     return m_session;
0072 }
0073