Debian Codegraph @

DebugAdapterClient/DAPBreakpointsView.cpp

raw
1 #include "DAPBreakpointsView.h"
2
3 #include "DebugAdapterClient.hpp"
4 #include "UIBreakpoint.hpp"
5 #include "dap/dap.hpp"
6
7 #include <algorithm>
8 #include <unordered_map>
9 #include <wx/msgdlg.h>
10
11 DAPBreakpointsView::DAPBreakpointsView(wxWindow* parent, DebugAdapterClient* plugin, clModuleLogger& log)
12 : DAPBreakpointsViewBase(parent)
13 , m_plugin(plugin)
14 , LOG(log)
15 {
16 m_dvListCtrl->SetSortFunction(
17 [](const clRowEntry* a, const clRowEntry* b) { return a->GetLabel().CmpNoCase(b->GetLabel()); });
18 m_dvListCtrl->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &DAPBreakpointsView::OnBreakpointActivated, this);
19 clBitmapList* bitmaps = new clBitmapList;
20 m_toolbar->AddTool(XRCID("dap-new-source-breakpoint"), _("New source breakpoint"), bitmaps->Add("file_new"));
21 m_toolbar->AddTool(XRCID("dap-new-function-breakpoint"), _("New function breakpoint"), bitmaps->Add("json"));
22 m_toolbar->AddTool(XRCID("dap-delete-all-breakpoints"), _("Delete All"), bitmaps->Add("clear"));
23 m_toolbar->AssignBitmaps(bitmaps);
24 m_toolbar->Realize();
25
26 m_toolbar->Bind(wxEVT_TOOL, &DAPBreakpointsView::OnNewFunctionBreakpoint, this,
27 XRCID("dap-new-function-breakpoint"));
28 m_toolbar->Bind(wxEVT_TOOL, &DAPBreakpointsView::OnNewSourceBreakpoint, this, XRCID("dap-new-source-breakpoint"));
29 m_toolbar->Bind(wxEVT_TOOL, &DAPBreakpointsView::OnDeleteAllBreakpoints, this, XRCID("dap-delete-all-breakpoints"));
30
31 m_toolbar->Bind(
32 wxEVT_UPDATE_UI, [&](wxUpdateUIEvent& e) { e.Enable(m_plugin->GetClient().CanInteract()); },
33 XRCID("dap-new-function-breakpoint"));
34
35 m_toolbar->Bind(
36 wxEVT_UPDATE_UI, [&](wxUpdateUIEvent& e) { e.Enable(m_plugin->GetClient().CanInteract()); },
37 XRCID("dap-new-source-breakpoint"));
38
39 m_toolbar->Bind(
40 wxEVT_UPDATE_UI,
41 [&](wxUpdateUIEvent& e) { e.Enable(m_plugin->GetClient().CanInteract() && !m_dvListCtrl->IsEmpty()); },
42 XRCID("dap-delete-all-breakpoints"));
43 }
44
45 DAPBreakpointsView::~DAPBreakpointsView()
46 {
47 m_dvListCtrl->Unbind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &DAPBreakpointsView::OnBreakpointActivated, this);
48 m_dvListCtrl->DeleteAllItems([](wxUIntPtr d) {
49 BreakpointClientData* ptr = reinterpret_cast<BreakpointClientData*>(d);
50 delete ptr;
51 });
52 }
53
54 void DAPBreakpointsView::RefreshView(const SessionBreakpoints& breakpoints)
55 {
56 // keep the previous breakpoints
57 std::unordered_map<int, dap::Breakpoint> old_breakpoints;
58 for(size_t i = 0; i < m_dvListCtrl->GetItemCount(); ++i) {
59 auto cd = GetItemData(m_dvListCtrl->RowToItem(i));
60 if(!cd || cd->m_breapoint.id <= 0) {
61 continue;
62 }
63 old_breakpoints.insert({ cd->m_breapoint.id, cd->m_breapoint });
64 }
65
66 m_dvListCtrl->Begin();
67 m_dvListCtrl->DeleteAllItems();
68
69 for(auto bp : breakpoints.get_breakpoints()) {
70
71 wxString& path = bp.source.path;
72 if(path.empty()) {
73 // use the path from the previous time we saw it
74 if(old_breakpoints.count(bp.id)) {
75 path = old_breakpoints[bp.id].source.path;
76 }
77 }
78
79 // still empty?
80 if(path.empty()) {
81 if(!bp.source.name.empty()) {
82 path = bp.source.name;
83 } else if(bp.source.sourceReference > 0) {
84 path << "source ref: " << bp.source.sourceReference;
85 }
86 }
87
88 // the order is:
89 // #,?,File,Line,Fullpath
90 wxVector<wxVariant> cols;
91 cols.reserve(m_dvListCtrl->GetHeader()->GetCount());
92 cols.push_back(wxString() << bp.id);
93 cols.push_back(bp.verified ? wxString(wxT("\u2713")) : wxString(wxT("\u2715")));
94 cols.push_back(wxFileName(path).GetFullName());
95 cols.push_back(wxString() << bp.line);
96 cols.push_back(path); // the longest field, is shown last
97 m_dvListCtrl->AppendItem(cols, (wxUIntPtr) new BreakpointClientData(bp));
98 }
99 m_dvListCtrl->Commit();
100 }
101
102 BreakpointClientData* DAPBreakpointsView::GetItemData(const wxDataViewItem& item)
103 {
104 BreakpointClientData* ptr = reinterpret_cast<BreakpointClientData*>(m_dvListCtrl->GetItemData(item));
105 return ptr;
106 }
107
108 void DAPBreakpointsView::OnBreakpointActivated(wxDataViewEvent& event)
109 {
110 auto item = event.GetItem();
111 CHECK_ITEM_RET(item);
112
113 auto cd = GetItemData(item);
114 CHECK_PTR_RET(cd);
115
116 m_plugin->LoadFile(cd->m_breapoint.source, cd->m_breapoint.line - 1);
117 }
118
119 void DAPBreakpointsView::OnBreakpointsContextMenu(wxDataViewEvent& event)
120 {
121 wxMenu menu;
122 menu.Append(XRCID("dap-new-function-breakpoint"), _("New function breakppoint"));
123 menu.Bind(wxEVT_MENU, &DAPBreakpointsView::OnNewFunctionBreakpoint, this, XRCID("dap-new-function-breakpoint"));
124 m_dvListCtrl->PopupMenu(&menu);
125 }
126
127 void DAPBreakpointsView::OnNewFunctionBreakpoint(wxCommandEvent& event)
128 {
129 wxUnusedVar(event);
130 wxString funcname = clGetTextFromUser(_("Set breakpoint in function"), _("Function name"));
131 if(funcname.empty()) {
132 return;
133 }
134
135 dap::FunctionBreakpoint new_bp;
136 new_bp.name = funcname;
137 auto iter = std::find_if(m_functionBreakpoints.begin(), m_functionBreakpoints.end(),
138 [&funcname](const dap::FunctionBreakpoint& bp) { return bp.name == funcname; });
139 if(iter != m_functionBreakpoints.end()) {
140 return;
141 }
142 m_functionBreakpoints.push_back(new_bp);
143 m_plugin->GetClient().SetFunctionBreakpoints(m_functionBreakpoints);
144 }
145
146 void DAPBreakpointsView::OnNewSourceBreakpoint(wxCommandEvent& event)
147 {
148 wxUnusedVar(event);
149 wxString location = clGetTextFromUser(_("Set breakpoint in source file"), _("Location (source:line)"));
150 if(location.empty()) {
151 return;
152 }
153
154 wxString source;
155 long line_numner;
156 source = location.BeforeFirst(':');
157 if(!location.AfterLast(':').ToCLong(&line_numner)) {
158 wxMessageBox(_("Invalid line number"), "CodeLite", wxOK | wxICON_ERROR | wxOK_DEFAULT);
159 return;
160 }
161
162 // get all breakpoints for the requested source file
163 std::vector<dap::SourceBreakpoint> source_breakpoints;
164 for(size_t i = 0; i < m_dvListCtrl->GetItemCount(); ++i) {
165 auto cd = GetItemData(m_dvListCtrl->RowToItem(i));
166 if(!cd) {
167 continue;
168 }
169 if(cd->m_breapoint.source.path == source) {
170 source_breakpoints.push_back({ cd->m_breapoint.line, "" });
171 }
172 }
173 source_breakpoints.push_back({ static_cast<int>(line_numner), "" });
174 m_plugin->GetClient().SetBreakpointsFile(source, source_breakpoints);
175 }
176
177 void DAPBreakpointsView::OnDeleteAllBreakpoints(wxCommandEvent& event)
178 {
179 wxUnusedVar(event);
180 // collect all source file
181 std::unordered_set<wxString> paths;
182 for(size_t i = 0; i < m_dvListCtrl->GetItemCount(); ++i) {
183 auto cd = GetItemData(m_dvListCtrl->RowToItem(i));
184 if(!cd) {
185 continue;
186 }
187 LOG_DEBUG(LOG) << "Will delete breakpoint:" << cd->m_breapoint.source.sourceReference << ","
188 << cd->m_breapoint.source.path << endl;
189 if(cd->m_breapoint.source.path.empty()) {
190 continue;
191 }
192
193 paths.insert(cd->m_breapoint.source.path);
194 }
195
196 for(const wxString& path : paths) {
197 LOG_DEBUG(LOG) << "Deleting breakpoints with path:" << path << endl;
198 m_plugin->GetClient().SetBreakpointsFile(path, {});
199 }
200 }