File: macronavigationcontext.cpp

package info (click to toggle)
kdevelop 4%3A4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,360 kB
  • ctags: 11,484
  • sloc: cpp: 105,371; python: 522; ansic: 488; lex: 422; sh: 139; ruby: 120; makefile: 51; xml: 42; php: 12
file content (156 lines) | stat: -rw-r--r-- 4,712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
   Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "macronavigationcontext.h"

#include <QtGui/QTextDocument>

#include <KLocale>

#include <KTextEditor/Editor>
#include <KTextEditor/Document>
#include <KTextEditor/View>
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/HighlightInterface>

#include <language/duchain/duchain.h>
#include <interfaces/icore.h>
#include <interfaces/ipartcontroller.h>

#include <QVBoxLayout>
#include <QLabel>

#include "../../parser/rpp/macrorepository.h"
#include "../../parser/rpp/chartools.h"


namespace Cpp {
using namespace KDevelop;
using namespace rpp;

KTextEditor::View* createDocAndView(const QString& data, KTextEditor::Document** docPtr)
{
  if (data.isEmpty()) {
    return 0;
  }
  KTextEditor::Document* doc = ICore::self()->partController()->editorPart()->createDocument(0);
  *docPtr = doc;
  doc->setText(data);
  doc->setMode("C++");
  doc->setReadWrite(false);
  KTextEditor::View* view = doc->createView(0);
  if (KTextEditor::ConfigInterface* config = qobject_cast<KTextEditor::ConfigInterface*>(view)) {
    config->setConfigValue("icon-bar", false);
    config->setConfigValue("folding-bar", false);
    config->setConfigValue("line-numbers", false);
    config->setConfigValue("dynamic-word-wrap", true);
  }
  return view;
}

MacroNavigationContext::MacroNavigationContext(const pp_macro& macro, QString preprocessedBody)
: AbstractNavigationContext(TopDUContextPointer(0))
, m_macro(new rpp::pp_macro(macro))
, m_body(preprocessedBody)
, m_preprocessed(0)
, m_definition(0)
, m_widget(0)
{
  KTextEditor::View* preprocessedView = createDocAndView(preprocessedBody.trimmed(), &m_preprocessed);
  QString definition = QString::fromUtf8(stringFromContents((uint*)m_macro->definition(), m_macro->definitionSize()).trimmed());
  KTextEditor::View* definitionView = createDocAndView(definition, &m_definition);

  m_widget = new QWidget;
  QVBoxLayout* layout = new QVBoxLayout(m_widget);
  if (m_preprocessed) {
    layout->addWidget(new QLabel(i18n("Preprocessed Body:")));
    layout->addWidget(preprocessedView);
  } else {
    layout->addWidget(new QLabel(i18n("Preprocessed Body: (empty)")));
  }
  if (m_definition) {
    layout->addWidget(new QLabel(i18n("Body:")));
    layout->addWidget(definitionView);
  } else {
    layout->addWidget(new QLabel(i18n("Body: (empty)")));
  }
  m_widget->setLayout(layout);
}

MacroNavigationContext::~MacroNavigationContext()
{
  delete m_preprocessed;
  delete m_definition;
  delete m_widget;
  delete m_macro;
}

QString MacroNavigationContext::name() const
{
  return m_macro->name.str();
}

QWidget* MacroNavigationContext::widget() const
{
  return m_widget;
}

QString MacroNavigationContext::html(bool shorten)
{
  clear();
  modifyHtml() += "<html><body><p>" + fontSizePrefix(shorten);
  addExternalHtml(m_prefix);

  QString args;

  if(m_macro->formalsSize()) {
    args = "(";

    bool first = true;
    FOREACH_CUSTOM(const IndexedString& b, m_macro->formals(), m_macro->formalsSize()) {
      if(!first)
        args += ", ";
      first = false;
      args += b.str();
    }

    args += ')';
  }

  const KUrl url = m_macro->file.toUrl();
  const QString path = url.pathOrUrl();
  KTextEditor::Cursor cursor(m_macro->sourceLine, 0);
  NavigationAction action(url, cursor);
  modifyHtml() += i18nc("%1: macro type, i.e.: 'Function macro' or just 'Macro'"
                        "%2: the macro name and arguments"
                        "%3 the link to the definition",
                        "%1: %2, defined in %3",
                        (m_macro->function_like ? i18n("Function macro") : i18n("Macro")),
                        importantHighlight(m_macro->name.str()) + args,
                        createLink(QString("%1 :%2").arg(path).arg(cursor.line()+1), path, action));

  modifyHtml() += fontSizeSuffix(shorten) + "</p></body></html>";
  return currentHtml();
}

QString MacroNavigationContext::body() const
{
  return m_body;
}

}