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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
This file is part of kdev-python, the python language plugin for KDevelop
Copyright (C) 2012 Sven Brauch <svenbrauch@googlemail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "variablecontroller.h"
#include "variable.h"
#include "debugsession.h"
#include "pdbframestackmodel.h"
#include <codehelpers.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/declaration.h>
#include <language/duchain/duchain.h>
#include <interfaces/ilanguagecontroller.h>
#include <debugger/variable/variablecollection.h>
#include <debugger/framestack/framestackmodel.h>
#include <interfaces/icore.h>
#include <QStack>
#include <KMessageBox>
#include <KLocalizedString>
#include <QDebug>
#include "debuggerdebug.h"
using namespace KDevelop;
namespace Python {
VariableController::VariableController(IDebugSession* parent) : IVariableController(parent)
{
m_updateTimer.setSingleShot(true);
m_updateTimer.setInterval(100);
QObject::connect(&m_updateTimer, &QTimer::timeout,
this, &VariableController::_update);
}
void VariableController::addWatch(KDevelop::Variable* variable)
{
variableCollection()->watches()->add(variable->expression());
}
void VariableController::addWatchpoint(KDevelop::Variable* /*variable*/)
{
qCWarning(KDEV_PYTHON_DEBUGGER) << "addWatchpoint requested (not implemented)";
}
void VariableController::handleEvent(IDebugSession::event_t event)
{
if ( event == IDebugSession::thread_or_frame_changed ) {
DebugSession* s = static_cast<DebugSession*>(session());
PdbFrameStackModel* model = static_cast<PdbFrameStackModel*>(s->frameStackModel());
int delta = model->currentFrame() - model->debuggerAtFrame();
model->setDebuggerAtFrame(model->currentFrame());
bool positive = delta > 0;
qCDebug(KDEV_PYTHON_DEBUGGER) << "changing frame by" << delta;
for ( int i = delta; i != 0; i += ( positive ? -1 : 1 ) ) {
qCDebug(KDEV_PYTHON_DEBUGGER) << ( positive ? "up" : "down" ) << model->currentFrame() << model->debuggerAtFrame();
s->addSimpleInternalCommand(positive ? "up" : "down");
}
}
KDevelop::IVariableController::handleEvent(event);
}
KDevelop::Variable* VariableController::createVariable(KDevelop::TreeModel* model, KDevelop::TreeItem* parent, const QString& expression, const QString& display)
{
return new Variable(model, parent, expression, display);
}
KTextEditor::Range VariableController::expressionRangeUnderCursor(KTextEditor::Document* doc, const KTextEditor::Cursor& cursor)
{
QString prefix;
DUChainReadLocker lock;
if ( ! doc->isModified() ) {
if ( TopDUContext* context = DUChain::self()->chainForDocument(doc->url()) ) {
DUContext* contextAtCursor = context->findContextAt(CursorInRevision(cursor.line(), cursor.column()));
if ( contextAtCursor && contextAtCursor->type() == DUContext::Class ) {
if ( contextAtCursor->owner() && ! contextAtCursor->owner()->identifier().isEmpty() ) {
prefix = contextAtCursor->owner()->identifier().toString() + ".";
}
}
}
}
else {
qCDebug(KDEV_PYTHON_DEBUGGER) << "duchain unavailable for document" << doc->url() << "or document out of date";
}
TextDocumentLazyLineFetcher linefetcher(doc);
KTextEditor::Cursor startCursor;
auto text = prefix + CodeHelpers::expressionUnderCursor(linefetcher, cursor, startCursor);
return {startCursor, startCursor + KTextEditor::Cursor{0, text.length()}};
}
void VariableController::localsUpdateReady(QByteArray rawData)
{
QRegExp formatExtract("([a-zA-Z0-9_]+) \\=\\> (.*)");
QList<QByteArray> data = rawData.split('\n');
data.removeAll({});
qCDebug(KDEV_PYTHON_DEBUGGER) << "locals update:" << data;
int i = 0;
QStringList vars;
QMap<QString, QString> values;
while ( i < data.length() ) {
QByteArray d = data.at(i);
if ( formatExtract.exactMatch(d) ) {
QString key = formatExtract.capturedTexts().at(1);
vars << key;
values[key] = formatExtract.capturedTexts().at(2);
}
else qCWarning(KDEV_PYTHON_DEBUGGER) << "mismatch:" << d;
i++;
}
QList<KDevelop::Variable*> variableObjects = KDevelop::ICore::self()->debugController()->variableCollection()
->locals()->updateLocals(vars);
for ( int i = 0; i < variableObjects.length(); i++ ) {
KDevelop::Variable* v = variableObjects[i];
auto model = v->model();
auto parent = model->indexForItem(v, 0);
auto childCount = v->model()->rowCount(parent);
qCDebug(KDEV_PYTHON_DEBUGGER) << "updating:" << v->expression() << "active children:" << childCount;
for ( int j = 0; j < childCount; j++ ) {
auto index = model->index(j, 0, parent);
auto child = static_cast<KDevelop::TreeItem*>(index.internalPointer());
if ( auto childVariable = qobject_cast<Variable*>(child) ) {
qCDebug(KDEV_PYTHON_DEBUGGER) << " got child var:" << childVariable->expression();
v->fetchMoreChildren();
break;
}
}
v->setValue(values[v->expression()]);
v->setHasMoreInitial(true);
}
}
void VariableController::update() {
m_updateTimer.start();
}
void VariableController::_update()
{
qCDebug(KDEV_PYTHON_DEBUGGER) << " ************************* update requested";
DebugSession* d = static_cast<DebugSession*>(parent());
if (autoUpdate() & UpdateWatches) {
variableCollection()->watches()->reinstall();
}
if (autoUpdate() & UpdateLocals) {
// TODO find a more elegant solution for this import!
InternalPdbCommand* import = new InternalPdbCommand(nullptr, nullptr, "import __kdevpython_debugger_utils\n");
InternalPdbCommand* cmd = new InternalPdbCommand(this, "localsUpdateReady",
"__kdevpython_debugger_utils.format_locals(__kdevpython_debugger_utils.__kdevpython_builtin_locals())\n");
d->addCommand(import);
d->addCommand(cmd);
}
}
}
|