From b66a6741dab7b6217ca3d25c314029b835c91a55 Mon Sep 17 00:00:00 2001
From: hjk <qtc-committer@nokia.com>
Date: Wed, 24 Nov 2010 18:36:17 +0100
Subject: [PATCH] debugger: enable breakpoint setting from a disassembler view

---
 src/plugins/debugger/breakhandler.cpp   | 30 ----------
 src/plugins/debugger/breakhandler.h     |  1 -
 src/plugins/debugger/debuggeragents.cpp |  9 ++-
 src/plugins/debugger/debuggerplugin.cpp | 80 ++++++++++++++++++++-----
 4 files changed, 71 insertions(+), 49 deletions(-)

diff --git a/src/plugins/debugger/breakhandler.cpp b/src/plugins/debugger/breakhandler.cpp
index 103598715bf..5145395d15a 100644
--- a/src/plugins/debugger/breakhandler.cpp
+++ b/src/plugins/debugger/breakhandler.cpp
@@ -756,36 +756,6 @@ void BreakHandler::appendBreakpoint(const BreakpointParameters &data)
     scheduleSynchronization();
 }
 
-void BreakHandler::toggleBreakpoint(const QString &fileName, int lineNumber,
-                                    quint64 address /* = 0 */)
-{
-    BreakpointId id(-1);
-
-    if (address) {
-        id = findBreakpointByAddress(address);
-    } else {
-        id = findBreakpointByFileAndLine(fileName, lineNumber, true);
-        if (id == BreakpointId(-1))
-            id = findBreakpointByFileAndLine(fileName, lineNumber, false);
-    }
-
-    if (id != BreakpointId(-1)) {
-        removeBreakpoint(id);
-    } else {
-        BreakpointParameters data;
-        if (address) {
-            data.type = BreakpointByAddress;
-            data.address = address;
-        } else {
-            data.type = BreakpointByFileAndLine;
-            data.fileName = fileName;
-            data.lineNumber = lineNumber;
-        }
-        appendBreakpoint(data);
-    }
-    debuggerCore()->synchronizeBreakpoints();
-}
-
 void BreakHandler::saveSessionData()
 {
     saveBreakpoints();
diff --git a/src/plugins/debugger/breakhandler.h b/src/plugins/debugger/breakhandler.h
index fbf471829ae..437b8fbc93b 100644
--- a/src/plugins/debugger/breakhandler.h
+++ b/src/plugins/debugger/breakhandler.h
@@ -88,7 +88,6 @@ public:
     static QIcon pendingBreakPointIcon();
     static QIcon emptyIcon();
 
-    void toggleBreakpoint(const QString &fileName, int lineNumber, quint64 address = 0);
     BreakpointId findBreakpointByFileAndLine(const QString &fileName,
         int lineNumber, bool useMarkerPosition = true);
     BreakpointId findBreakpointByAddress(quint64 address) const;
diff --git a/src/plugins/debugger/debuggeragents.cpp b/src/plugins/debugger/debuggeragents.cpp
index 3d28e5bd44f..b355937ec8a 100644
--- a/src/plugins/debugger/debuggeragents.cpp
+++ b/src/plugins/debugger/debuggeragents.cpp
@@ -40,11 +40,11 @@
 #include <coreplugin/mimedatabase.h>
 #include <coreplugin/icore.h>
 
+#include <texteditor/basetextdocument.h>
 #include <texteditor/basetexteditor.h>
-#include <texteditor/plaintexteditor.h>
 #include <texteditor/basetextmark.h>
+#include <texteditor/plaintexteditor.h>
 #include <texteditor/texteditorconstants.h>
-#include <texteditor/basetextdocument.h>
 
 #include <utils/qtcassert.h>
 
@@ -357,6 +357,11 @@ void DisassemblerViewAgent::setContents(const DisassemblerLines &contents)
         d->editor->setProperty(Debugger::Constants::OPENED_BY_DEBUGGER, true);
         d->editor->setProperty(Debugger::Constants::OPENED_WITH_DISASSEMBLY, true);
         d->configureMimeType();
+
+        BaseTextEditor *baseTextEdit =
+                qobject_cast<BaseTextEditor *>(d->editor->widget());
+        if (baseTextEdit)
+            baseTextEdit->setRequestMarkEnabled(true);
     }
 
     editorManager->activateEditor(d->editor);
diff --git a/src/plugins/debugger/debuggerplugin.cpp b/src/plugins/debugger/debuggerplugin.cpp
index b9a9116372c..743c7e4cfa4 100644
--- a/src/plugins/debugger/debuggerplugin.cpp
+++ b/src/plugins/debugger/debuggerplugin.cpp
@@ -431,8 +431,8 @@ static QToolButton *toolButton(QAction *action)
 
 // Retrieve file name and line and optionally address
 // from the data set on the text editor context menu action.
-static bool positionFromContextActionData(const QObject *sender,
-    QString *fileName, int *lineNumber, quint64 *address = 0)
+static bool positionFromActionData(const QObject *sender,
+    QString *fileName, int *lineNumber, quint64 *address)
 {
     const QAction *action = qobject_cast<const QAction *>(sender);
     QTC_ASSERT(action, return false);
@@ -440,8 +440,7 @@ static bool positionFromContextActionData(const QObject *sender,
     QTC_ASSERT(data.size() == 3, return false);
     *fileName = data.front().toString();
     *lineNumber = data.at(1).toInt();
-    if (address)
-        *address = data.at(2).toULongLong();
+    *address = data.at(2).toULongLong();
     return true;
 }
 
@@ -910,8 +909,12 @@ public slots:
         QString fileName;
         int lineNumber;
         quint64 address;
-        if (positionFromContextActionData(sender(), &fileName, &lineNumber, &address))
-            m_breakHandler->toggleBreakpoint(fileName, lineNumber, address);
+        if (positionFromActionData(sender(), &fileName, &lineNumber, &address)) {
+            if (address)
+                toggleBreakpointByAddress(address);
+            else
+                toggleBreakpointByFileAndLine(fileName, lineNumber);
+        }
     }
 
     void breakpointRemoveMarginActionTriggered()
@@ -992,7 +995,8 @@ public slots:
     void activatePreviousMode();
     void activateDebugMode();
     void toggleBreakpoint();
-    void toggleBreakpoint(const QString &fileName, int lineNumber);
+    void toggleBreakpointByFileAndLine(const QString &fileName, int lineNumber);
+    void toggleBreakpointByAddress(quint64 address);
     void onModeChanged(Core::IMode *mode);
     void showSettingsDialog();
 
@@ -1193,7 +1197,8 @@ public slots:
         // Run to line, file name and line number set as list.
         QString fileName;
         int lineNumber;
-        if (positionFromContextActionData(sender(), &fileName, &lineNumber))
+        quint64 address;
+        if (positionFromActionData(sender(), &fileName, &lineNumber, &address))
             handleExecRunToLine();
     }
 
@@ -1201,7 +1206,8 @@ public slots:
     {
         QString fileName;
         int lineNumber;
-        if (positionFromContextActionData(sender(), &fileName, &lineNumber))
+        quint64 address;
+        if (positionFromActionData(sender(), &fileName, &lineNumber, &address))
             currentEngine()->executeJumpToLine(fileName, lineNumber);
     }
 
@@ -2466,20 +2472,62 @@ void DebuggerPluginPrivate::toggleBreakpoint()
 {
     ITextEditor *textEditor = currentTextEditor();
     QTC_ASSERT(textEditor, return);
-    int lineNumber = textEditor->currentLine();
-    if (lineNumber >= 0)
-        toggleBreakpoint(textEditor->file()->fileName(), lineNumber);
+    const int lineNumber = textEditor->currentLine();
+    if (textEditor->property("DisassemblerView").toBool()) {
+        QString line = textEditor->contents()
+            .section('\n', lineNumber - 1, lineNumber - 1);
+        quint64 address = DisassemblerViewAgent::addressFromDisassemblyLine(line);
+        toggleBreakpointByAddress(address);
+    } else if (lineNumber >= 0) {
+        toggleBreakpointByFileAndLine(textEditor->file()->fileName(), lineNumber);
+    }
+}
+
+void DebuggerPluginPrivate::toggleBreakpointByFileAndLine(const QString &fileName,
+    int lineNumber)
+{
+    BreakHandler *handler = m_breakHandler;
+    BreakpointId id =
+        handler->findBreakpointByFileAndLine(fileName, lineNumber, true);
+    if (id == BreakpointId(-1))
+        id = handler->findBreakpointByFileAndLine(fileName, lineNumber, false);
+
+    if (id != BreakpointId(-1)) {
+        handler->removeBreakpoint(id);
+    } else {
+        BreakpointParameters data(BreakpointByFileAndLine);
+        data.fileName = fileName;
+        data.lineNumber = lineNumber;
+        handler->appendBreakpoint(data);
+    }
+    synchronizeBreakpoints();
 }
 
-void DebuggerPluginPrivate::toggleBreakpoint(const QString &fileName, int lineNumber)
+void DebuggerPluginPrivate::toggleBreakpointByAddress(quint64 address)
 {
-    m_breakHandler->toggleBreakpoint(fileName, lineNumber);
+    BreakHandler *handler = m_breakHandler;
+    BreakpointId id = handler->findBreakpointByAddress(address);
+
+    if (id != BreakpointId(-1)) {
+        handler->removeBreakpoint(id);
+    } else {
+        BreakpointParameters data(BreakpointByAddress);
+        data.address = address;
+        handler->appendBreakpoint(data);
+    }
+    synchronizeBreakpoints();
 }
 
 void DebuggerPluginPrivate::requestMark(ITextEditor *editor, int lineNumber)
 {
-    if (isDebuggable(editor) && editor->file())
-        toggleBreakpoint(editor->file()->fileName(), lineNumber);
+    if (editor->property("DisassemblerView").toBool()) {
+        QString line = editor->contents()
+            .section('\n', lineNumber - 1, lineNumber - 1);
+        quint64 address = DisassemblerViewAgent::addressFromDisassemblyLine(line);
+        toggleBreakpointByAddress(address);
+    } else if (editor->file()) {
+        toggleBreakpointByFileAndLine(editor->file()->fileName(), lineNumber);
+    }
 }
 
 void DebuggerPluginPrivate::showToolTip(ITextEditor *editor,
-- 
GitLab