File indexing completed on 2024-04-28 04:34:21

0001 /***************************************************************************
0002  *   Copyright 2009 Sandro Andrade <sandroandrade@kde.org>                 *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU Library General Public License as       *
0006  *   published by the Free Software Foundation; either version 2 of the    *
0007  *   License, or (at your option) any later version.                       *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU Library General Public     *
0015  *   License along with this program; if not, write to the                 *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0018  ***************************************************************************/
0019 
0020 #include "controlflowgraphnavigationcontext.h"
0021 
0022 #include <QTextDocument>
0023 
0024 #include <KLocale>
0025 
0026 #include <interfaces/icore.h>
0027 #include <interfaces/idocumentcontroller.h>
0028 
0029 #include <language/duchain/duchain.h>
0030 #include <language/duchain/duchainlock.h>
0031 #include <language/codegen/coderepresentation.h>
0032 
0033 using namespace KDevelop;
0034 
0035 ControlFlowGraphNavigationContext::ControlFlowGraphNavigationContext(const QString &label, const ArcUses &arcUses, TopDUContextPointer topContext, AbstractNavigationContext *previousContext)
0036  : AbstractNavigationContext(topContext, previousContext), m_label(label), m_arcUses (arcUses)
0037 {
0038 }
0039 
0040 ControlFlowGraphNavigationContext::~ControlFlowGraphNavigationContext()
0041 {
0042 }
0043 
0044 QString ControlFlowGraphNavigationContext::name() const
0045 {
0046     return i18n("Control flow graph navigation widget");
0047 }
0048 
0049 QString ControlFlowGraphNavigationContext::html(bool shorten)
0050 {
0051     clear();
0052     m_shorten = shorten;
0053     modifyHtml() += "<html><body><p><small><small>";
0054 
0055     QStringList nodes = m_label.split("->");
0056     if (nodes.size() < 2)
0057         return "";
0058 
0059     modifyHtml() += importantHighlight(i18n("Uses of %1 from %2", nodes[1], nodes[0])) + "<hr>";
0060     unsigned int i = m_arcUses.size()-1;
0061     QPair<RangeInRevision, IndexedString> pair;
0062     QListIterator< QPair<RangeInRevision, IndexedString> > iterator(m_arcUses);
0063     iterator.toBack();
0064 
0065     DUChainReadLocker lock(DUChain::lock());
0066     while (iterator.hasPrevious())
0067     {
0068         pair = iterator.previous();
0069         CodeRepresentation::Ptr code = createCodeRepresentation(pair.second);
0070         modifyHtml() += "<a href='" + QString::number(i--) + "'>" + pair.second.toUrl().fileName() + " (" + QString::number(pair.first.start.line+1) + ")</a>: " + Qt::escape(code->line(pair.first.start.line).trimmed()) + "<br>";
0071     }
0072 
0073     modifyHtml() += "</small></small></p></body></html>";
0074 
0075     return currentHtml();
0076 }
0077 
0078 void ControlFlowGraphNavigationContext::slotAnchorClicked(const QUrl &link)
0079 {
0080     int position = link.toString().toInt();
0081     QPair<RangeInRevision, IndexedString> pair = m_arcUses[position];
0082     DUChainReadLocker lock(DUChain::lock());
0083     KUrl url(pair.second.toUrl());
0084     CursorInRevision cursor = pair.first.start;
0085     int line = cursor.line;
0086     int column = cursor.column;
0087     lock.unlock();
0088     ICore::self()->documentController()->openDocument(url, KTextEditor::Cursor(line, column));
0089 }