-
Notifications
You must be signed in to change notification settings - Fork 444
/
DAPTextView.cpp
213 lines (183 loc) · 6.13 KB
/
DAPTextView.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "DAPTextView.h"
#include "ColoursAndFontsManager.h"
#include "bookmark_manager.h"
#include "editor_config.h"
#include "event_notifier.h"
#include "globals.h"
#include "imanager.h"
#include "clAuiBook.hpp"
namespace
{
constexpr int NUMBER_MARGIN_ID = 0;
constexpr int EDIT_TRACKER_MARGIN_ID = 1;
constexpr int SYMBOLS_MARGIN_ID = 2;
constexpr int SYMBOLS_MARGIN_SEP_ID = 3;
constexpr int FOLD_MARGIN_ID = 4;
} // namespace
DAPTextView::DAPTextView(wxWindow* parent)
: DAPTextViewBase(parent)
{
MSWSetWindowDarkTheme(this);
EventNotifier::Get()->Bind(wxEVT_SYS_COLOURS_CHANGED, &DAPTextView::OnColourChanged, this);
m_stcTextView->Bind(wxEVT_STC_MARGINCLICK, &DAPTextView::OnMarginClick, this);
m_stcTextView->SetEditable(false);
ApplyTheme();
// define the debugger marker
auto options = EditorConfigST::Get()->GetOptions();
if(options->HasOption(OptionsConfig::Opt_Mark_Debugger_Line)) {
m_stcTextView->MarkerDefine(smt_indicator, wxSTC_MARK_BACKGROUND, wxNullColour,
options->GetDebuggerMarkerLine());
m_stcTextView->MarkerSetAlpha(smt_indicator, 50);
} else {
m_stcTextView->MarkerDefine(smt_indicator, wxSTC_MARK_SHORTARROW);
wxColour debuggerMarkerColour(136, 170, 0);
m_stcTextView->MarkerSetBackground(smt_indicator, debuggerMarkerColour);
m_stcTextView->MarkerSetForeground(smt_indicator, debuggerMarkerColour.ChangeLightness(50));
}
// margins
// line numbers
m_stcTextView->SetMarginType(NUMBER_MARGIN_ID, wxSTC_MARGIN_NUMBER);
// symbol margin
m_stcTextView->SetMarginType(SYMBOLS_MARGIN_ID, wxSTC_MARGIN_SYMBOL);
m_stcTextView->SetMarginWidth(SYMBOLS_MARGIN_ID, FromDIP(16));
// breakpoints
m_stcTextView->MarkerDefine(smt_breakpoint, wxSTC_MARK_CIRCLE);
m_stcTextView->MarkerSetBackground(smt_breakpoint, "RED");
m_stcTextView->MarkerSetAlpha(smt_breakpoint, 30);
}
DAPTextView::~DAPTextView()
{
EventNotifier::Get()->Unbind(wxEVT_SYS_COLOURS_CHANGED, &DAPTextView::OnColourChanged, this);
m_stcTextView->Unbind(wxEVT_STC_MARGINCLICK, &DAPTextView::OnMarginClick, this);
}
void DAPTextView::OnColourChanged(clCommandEvent& event)
{
event.Skip();
ApplyTheme();
}
void DAPTextView::ApplyTheme()
{
wxString file_name = "file.text";
if(!m_current_source.path.empty()) {
file_name = wxFileName(m_current_source.path).GetFullName();
} else if(!m_mimeType.empty()) {
// try the mime type
if(m_mimeType == "text/x-lldb.disassembly") {
file_name = "file.asm"; // assembly
}
}
auto lexer = ColoursAndFontsManager::Get().GetLexerForFile(file_name);
lexer->Apply(m_stcTextView);
}
void DAPTextView::ClearMarker(wxStyledTextCtrl* ctrl) { ctrl->MarkerDeleteAll(smt_indicator); }
void DAPTextView::SetMarker(wxStyledTextCtrl* ctrl, int line_number)
{
ctrl->MarkerDeleteAll(smt_indicator);
ctrl->MarkerAdd(line_number, smt_indicator);
int caretPos = ctrl->PositionFromLine(line_number);
ctrl->SetSelection(caretPos, caretPos);
ctrl->SetCurrentPos(caretPos);
ctrl->EnsureCaretVisible();
}
void DAPTextView::Clear()
{
m_stcTextView->MarkerDeleteAll(smt_indicator);
m_stcTextView->ClearAll();
m_filepath.clear();
}
void DAPTextView::SetText(const dap::Source& source, const wxString& text, const wxString& path,
const wxString& mimeType)
{
m_stcTextView->SetEditable(true);
m_stcTextView->SetText(text);
m_stcTextView->SetEditable(false);
m_current_source = source;
m_mimeType = mimeType;
SetFilePath(path);
UpdateLineNumbersMargin();
ApplyTheme();
}
void DAPTextView::LoadFile(const dap::Source& source, const wxString& filepath)
{
m_stcTextView->SetEditable(true);
bool ok = m_stcTextView->LoadFile(filepath);
m_stcTextView->SetEditable(false);
m_mimeType.clear();
if(ok) {
SetFilePath(filepath);
m_current_source = source;
} else {
Clear();
}
UpdateLineNumbersMargin();
ApplyTheme();
}
void DAPTextView::SetFilePath(const wxString& filepath)
{
m_filepath = filepath;
auto book = clGetManager()->GetMainNotebook();
int where = book->FindPage(this);
if(where == wxNOT_FOUND) {
return;
}
book->SetPageText(where, "[dap]: " + m_filepath);
}
void DAPTextView::UpdateLineNumbersMargin()
{
int newLineCount = m_stcTextView->GetLineCount();
int newWidthCount = log10(newLineCount) + 2;
int size = FromDIP(newWidthCount * m_stcTextView->TextWidth(wxSTC_STYLE_LINENUMBER, "X"));
m_stcTextView->SetMarginWidth(NUMBER_MARGIN_ID, size);
}
void DAPTextView::OnMarginClick(wxStyledTextEvent& event)
{
int nLine = m_stcTextView->LineFromPosition(event.GetPosition());
switch(event.GetMargin()) {
case SYMBOLS_MARGIN_ID:
if(HasBreakpointMarker(nLine)) {
DeleteBreakpointMarkers(nLine);
} else {
SetBreakpointMarker(nLine, wxEmptyString);
}
break;
default:
break;
}
// TODO: let the plugin know that it needs to apply breakpoints for this source
}
bool DAPTextView::HasBreakpointMarker(int line_number)
{
int markers_bit_mask = m_stcTextView->MarkerGet(line_number);
int mask = (1 << smt_breakpoint);
return markers_bit_mask & mask;
}
size_t DAPTextView::GetBreakpointMarkers(std::vector<int>* lines)
{
int mask = (1 << smt_breakpoint);
int line = m_stcTextView->MarkerNext(0, mask);
while(line != wxNOT_FOUND) {
lines->push_back(line);
line = m_stcTextView->MarkerNext(line + 1, mask);
}
return lines->size();
}
void DAPTextView::DeleteBreakpointMarkers(int line_number)
{
// get a list of lines to work on
std::vector<int> lines;
if(line_number == wxNOT_FOUND) {
GetBreakpointMarkers(&lines);
} else {
lines.push_back(line_number);
}
for(int line : lines) {
m_stcTextView->MarkerDelete(line, smt_breakpoint);
}
}
void DAPTextView::SetBreakpointMarker(int line_number, const wxString& tooltip)
{
if(HasBreakpointMarker(line_number)) {
return;
}
m_stcTextView->MarkerAdd(line_number, smt_breakpoint);
}