-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticHighlight.cpp
More file actions
619 lines (527 loc) · 20.7 KB
/
SemanticHighlight.cpp
File metadata and controls
619 lines (527 loc) · 20.7 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/***************************************************************
* Name: SemanticHighlight.cpp
* Purpose: Code::Blocks plugin
* Author: mistar (mistar@tcs.uj.edu.pl)
* Created: 2012-04-27
* Copyright: mistar
* License: GPL
**************************************************************/
#include <memory> // std::shared_ptr
#include <vector> // std::make_pair
// Code::Blocks
#include <sdk.h>
#include <configurationpanel.h>
#include <logmanager.h>
#include <editormanager.h>
#include <cbeditor.h>
#include <cbstyledtextctrl.h>
#include <projectmanager.h>
#include <cbproject.h>
// wxWidgets
#include <wx/tokenzr.h>
#include <wx/string.h>
#include <wx/thread.h>
// clang
#include <clang-c/Index.h>
// local
#include "GlobalVariables.hpp"
#include "SemanticHighlight.hpp"
#include "CompileOptions.hpp"
#include "UnsavedFiles.hpp"
#include "SHThread.hpp"
#include "Styles.hpp"
#include <cstdio>
// ugly hack as Scintilla.h is not includeable
#ifndef SCI_GETLEXERLANGUAGE
#define SCI_GETLEXERLANGUAGE 4012
#endif
#ifndef SCI_GETLENGTH
#define SCI_GETLENGTH 2006
#endif
#ifndef SCI_GETTEXT
#define SCI_GETTEXT 2182
#endif
// keywords being type names
wxArrayString typeKeywords;
// map CXCursorKind -> styles
int identifierStyle[512];
// Register the plugin with Code::Blocks.
// We are using an anonymous namespace so we don't litter the global one.
namespace
{
PluginRegistrant<SemanticHighlight> reg(_T("SemanticHighlight"));
}
/*
* SHTimer
*/
class SHTimer : public wxTimer
{
cbEditor * editor;
public:
explicit SHTimer(cbEditor * ed) : editor(ed) {}
virtual void Notify();
};
/*
* EditorData
*/
struct EditorData
{
SHTimer timer;
SHThread thread;
SHSharedData sharedData;
cbEditor * editor; // debug
unsigned version;
EditorData(cbEditor * ed, wxEvtHandler * handler);
~EditorData();
};
inline EditorData::EditorData(cbEditor * ed, wxEvtHandler * handler) :
timer(ed),
thread(sharedData, ed, handler),
editor(ed),
version(0)
{
thread.Create();
}
static void Perform(cbEditor * editor, Action action);
inline EditorData::~EditorData()
{
// stop the thread; we don't use Perform function as it uses editorMap
{
wxMutexLocker locker(sharedData.mutex);
sharedData.action = EXIT;
sharedData.condition.Signal();
}
thread.Wait();
}
typedef std::unordered_map<cbEditor *, std::shared_ptr<EditorData>> EditorMap;
// maps cbEditor to EditorData
EditorMap editorMap;
static void Perform(cbEditor * editor, Action action)
{
SHSharedData & sharedData = editorMap[editor]->sharedData;
wxMutexLocker locker(sharedData.mutex);
sharedData.action = action;
sharedData.condition.Signal();
}
static cbProject * GetProject(cbEditor * ed)
{
ProjectFile * pf = ed->GetProjectFile();
return pf ? pf->GetParentProject() : 0;
}
/*
* SemanticHighlight
*/
// constructor
SemanticHighlight::SemanticHighlight()
{
// Make sure our resources are available.
// In the generated boilerplate code we have no resources but when
// we add some, it will be nice that this code is in place already ;)
if(!Manager::LoadResource(_T("SemanticHighlight.zip")))
{
NotifyMissingFile(_T("SemanticHighlight.zip"));
}
for (int i = 0; i < 512; ++i)
identifierStyle[i] = stDEFAULT;
identifierStyle[CXCursor_ClassDecl] = stTYPE;
identifierStyle[CXCursor_StructDecl] = stTYPE;
identifierStyle[CXCursor_UnionDecl] = stTYPE;
identifierStyle[CXCursor_EnumDecl] = stTYPE;
identifierStyle[CXCursor_FieldDecl] = stVARIABLE;
identifierStyle[CXCursor_EnumConstantDecl] = stCONSTANT;
identifierStyle[CXCursor_FunctionDecl] = stFUNCTION;
identifierStyle[CXCursor_VarDecl] = stVARIABLE;
identifierStyle[CXCursor_ParmDecl] = stVARIABLE;
identifierStyle[CXCursor_TypedefDecl] = stTYPE;
identifierStyle[CXCursor_CXXMethod] = stFUNCTION;
identifierStyle[CXCursor_Namespace] = stNAMESPACE;
identifierStyle[CXCursor_Constructor] = stFUNCTION;
identifierStyle[CXCursor_Destructor] = stFUNCTION;
identifierStyle[CXCursor_TemplateTypeParameter] = stTEMPLATE_PARAMETER;
identifierStyle[CXCursor_NonTypeTemplateParameter] = stTEMPLATE_PARAMETER;
identifierStyle[CXCursor_TemplateTemplateParameter] = stTEMPLATE_PARAMETER;
identifierStyle[CXCursor_FunctionTemplate] = stFUNCTION;
identifierStyle[CXCursor_ClassTemplate] = stTYPE;
identifierStyle[CXCursor_ClassTemplatePartialSpecialization] = stTYPE;
identifierStyle[CXCursor_TypeAliasDecl] = stTYPE;
identifierStyle[CXCursor_TypeRef] = stTYPE;
identifierStyle[CXCursor_TemplateRef] = stTYPE;
identifierStyle[CXCursor_NamespaceRef] = stNAMESPACE;
typeKeywords.Add(_("void"));
typeKeywords.Add(_("bool"));
typeKeywords.Add(_("char"));
typeKeywords.Add(_("int"));
typeKeywords.Add(_("float"));
typeKeywords.Add(_("double"));
typeKeywords.Add(_("short"));
typeKeywords.Add(_("long"));
typeKeywords.Add(_("signed"));
typeKeywords.Add(_("unsigned"));
typeKeywords.Add(_("auto"));
typeKeywords.Add(_("char16_t"));
typeKeywords.Add(_("char32_t"));
typeKeywords.Add(_("wchar_t"));
}
void SemanticHighlight::SetStyles(cbEditor * editor)
{
cbStyledTextCtrl * ctrl = editor->GetControl();
wxColour bkgcolor(0x20, 0x20, 0x20);
//ctrl->ClearDocumentStyle();
ctrl->SetCaretForeground(wxColour(0xff, 0xff, 0xff));
ctrl->StyleSetBackground(32, bkgcolor);
ctrl->SetCaretLineVisible(true);
ctrl->SetCaretLineBackground(wxColour(0x28, 0x28, 0x28));
// stDEFAULT
ctrl->StyleSetForeground(stDEFAULT, wxColour(0xc0, 0xc0, 0xc0));
ctrl->StyleSetBackground(stDEFAULT, bkgcolor);
ctrl->StyleSetBold(stDEFAULT, false);
ctrl->StyleSetItalic(stDEFAULT, false);
// stPUNCTUATION
ctrl->StyleSetForeground(stPUNCTUATION, wxColour(0xff, 0xff, 0xff));
ctrl->StyleSetBackground(stPUNCTUATION, bkgcolor);
ctrl->StyleSetBold(stPUNCTUATION, false);
ctrl->StyleSetItalic(stPUNCTUATION, false);
// stCONSTANT
ctrl->StyleSetForeground(stCONSTANT, wxColour(0xff, 0x80, 0xb0));
ctrl->StyleSetBackground(stCONSTANT, bkgcolor);
ctrl->StyleSetBold(stCONSTANT, false);
ctrl->StyleSetItalic(stCONSTANT, false);
// stKEYWORD
ctrl->StyleSetForeground(stKEYWORD, wxColour(0x80, 0x90, 0xa0));
ctrl->StyleSetBackground(stKEYWORD, bkgcolor);
ctrl->StyleSetBold(stKEYWORD, false);
ctrl->StyleSetItalic(stKEYWORD, false);
// stTYPE
ctrl->StyleSetForeground(stTYPE, wxColour(0x30, 0x80, 0x20));
ctrl->StyleSetBackground(stTYPE, bkgcolor);
ctrl->StyleSetBold(stTYPE, true);
ctrl->StyleSetItalic(stTYPE, false);
// stVARIABLE
ctrl->StyleSetForeground(stVARIABLE, wxColour(0xc0, 0xc0, 0xc0));
ctrl->StyleSetBackground(stVARIABLE, bkgcolor);
ctrl->StyleSetBold(stVARIABLE, true);
ctrl->StyleSetItalic(stVARIABLE, false);
// stFUNCTION
ctrl->StyleSetForeground(stFUNCTION, wxColour(0xe0, 0xe0, 0x80));
ctrl->StyleSetBackground(stFUNCTION, bkgcolor);
ctrl->StyleSetBold(stFUNCTION, true);
ctrl->StyleSetItalic(stFUNCTION, false);
// stTEMPLATE_PARAMETER
ctrl->StyleSetForeground(stTEMPLATE_PARAMETER, wxColour(0xb0, 0xd0, 0xf0));
ctrl->StyleSetBackground(stTEMPLATE_PARAMETER, bkgcolor);
ctrl->StyleSetBold(stTEMPLATE_PARAMETER, true);
ctrl->StyleSetItalic(stTEMPLATE_PARAMETER, false);
// stNAMESPACE
ctrl->StyleSetForeground(stNAMESPACE, wxColour(0x00, 0xff, 0x00));
ctrl->StyleSetBackground(stNAMESPACE, bkgcolor);
ctrl->StyleSetBold(stNAMESPACE, true);
ctrl->StyleSetItalic(stNAMESPACE, false);
// stPREPROCESSOR
ctrl->StyleSetForeground(stPREPROCESSOR, wxColour(0x90, 0x70, 0x50));
ctrl->StyleSetBackground(stPREPROCESSOR, bkgcolor);
ctrl->StyleSetBold(stPREPROCESSOR, false);
ctrl->StyleSetItalic(stPREPROCESSOR, false);
// stCOMMENT
ctrl->StyleSetForeground(stCOMMENT, wxColour(0x60, 0x60, 0x60));
ctrl->StyleSetBackground(stCOMMENT, bkgcolor);
ctrl->StyleSetBold(stCOMMENT, false);
ctrl->StyleSetItalic(stCOMMENT, true);
// stINVALID
ctrl->StyleSetForeground(stINVALID, wxColour(0x00, 0x00, 0x00));
ctrl->StyleSetBackground(stINVALID, wxColour(0xff, 0x20, 0x20));
ctrl->StyleSetBold(stINVALID, true);
ctrl->StyleSetItalic(stINVALID, false);
// stWEIRD
ctrl->StyleSetForeground(stWEIRD, wxColour(0x00, 0x00, 0x00));
ctrl->StyleSetBackground(stWEIRD, wxColour(0xff, 0xff, 0x00));
ctrl->StyleSetBold(stWEIRD, false);
ctrl->StyleSetItalic(stWEIRD, false);
// INDICATORS
// indERROR
ctrl->IndicatorSetForeground(indERROR, wxColour(0xff, 0x00, 0x00));
ctrl->IndicatorSetStyle(indERROR, wxSCI_INDIC_SQUIGGLELOW);
ctrl->IndicatorSetUnder(indERROR, true);
// indWARNING
ctrl->IndicatorSetForeground(indWARNING, wxColour(0xff, 0xff, 0x00));
ctrl->IndicatorSetStyle(indWARNING, wxSCI_INDIC_SQUIGGLELOW);
ctrl->IndicatorSetUnder(indWARNING, true);
ctrl->SetLexer(wxSCI_LEX_NULL);
}
static void GetCompilerOptions(CompileOptionsBase * co, wxArrayString & options)
{
options.Empty();
if (!co)
return;
Manager::Get()->GetLogManager()->Log(_("Non-zero argument"));
const wxArrayString & includeDirsArray = co->GetIncludeDirs();
for (int i = 0; i < includeDirsArray.GetCount(); ++i)
{
// Manager::Get()->GetLogManager()->Log(wxT("Adding directory: ") + includeDirsArray[i]);
options.Add(_("-I") + includeDirsArray[i]);
}
wxArrayString compilerOptsArray = co->GetCompilerOptions();
compilerOptsArray.Add(wxT(" ")); // ensure that compilerOpts below is non-empty
wxString compilerOpts = GetStringFromArray(compilerOptsArray, _(" "), true);
wxStringTokenizer commandsTokenizer(compilerOpts, wxT("`"), wxTOKEN_RET_EMPTY_ALL);
wxStringTokenizer whitespaceTokenizer;
wxString token = commandsTokenizer.GetNextToken();
token.Trim().Trim(true);
wxString option;
if (!token.IsEmpty())
{
// split on whitespaces
whitespaceTokenizer.SetString(token);
while (whitespaceTokenizer.HasMoreTokens())
{
option = whitespaceTokenizer.GetNextToken();
option.Trim().Trim(true);
// Manager::Get()->GetLogManager()->Log(wxT("Adding option: ") + option);
options.Add(option);
}
}
while (commandsTokenizer.HasMoreTokens())
{
token = commandsTokenizer.GetNextToken();
token.Trim().Trim(true);
wxArrayString resultsArray;
// Manager::Get()->GetLogManager()->Log(wxT("Executing: ") + token);
wxCharBuffer aux = token.utf8_str();
fprintf(stderr, "\t\t\t\tExecuting: '%s'\n", aux.data());
wxExecute(token, resultsArray, wxEXEC_SYNC | wxEXEC_NODISABLE);
fprintf(stderr, "\t\t\t\tDone.\n");
// Manager::Get()->GetLogManager()->Log(wxT("Done."));
wxString results = GetStringFromArray(resultsArray, _(" "));
whitespaceTokenizer.SetString(results);
while (whitespaceTokenizer.HasMoreTokens())
{
option = whitespaceTokenizer.GetNextToken();
option.Trim().Trim(true);
// Manager::Get()->GetLogManager()->Log(wxT("Adding option: ") + option);
options.Add(option);
}
token = commandsTokenizer.GetNextToken(); // should be there
token.Trim().Trim(true);
whitespaceTokenizer.SetString(token);
while (whitespaceTokenizer.HasMoreTokens())
{
option = whitespaceTokenizer.GetNextToken();
option.Trim().Trim(true);
// Manager::Get()->GetLogManager()->Log(wxT("Adding option: ") + option);
options.Add(option);
}
}
}
void InitializeProjectData(cbProject * project)
{
wxArrayString options;
GetCompilerOptions(project, options);
CompileOptions::current.SetCompileOptions(project, options);
// for all build targets
int n = project->GetBuildTargetsCount();
for (int i = 0; i < n; ++i)
{
ProjectBuildTarget * bt = project->GetBuildTarget(i);
GetCompilerOptions(bt, options);
CompileOptions::current.SetCompileOptions(bt, options);
}
ProjectBuildTarget * abt = project->GetBuildTarget(project->GetActiveBuildTarget());
CompileOptions::current.SetActiveBuildTarget(project, abt);
}
static void SetUnsavedFileData(cbEditor * editor)
{
unsigned long length = editor->GetControl()->SendMsg(SCI_GETLENGTH);
char * text = new char[length + 1];
editor->GetControl()->SendMsg(SCI_GETTEXT, length + 1, (wxIntPtr)text); // always with trailing 0
wxCharBuffer buffer = editor->GetFilename().utf8_str();
char * filename = new char[strlen(buffer.data())+1];
strcpy(filename, buffer.data());
UnsavedFiles::current.Set(
editor,
std::shared_ptr<char>(filename, [](char * arr){ delete [] arr; }),
std::shared_ptr<char>(text, [](char * arr){ delete [] arr; }),
length,
editorMap[editor]->version,
GetProject(editor)
);
}
void SemanticHighlight::InitializeEditorData(cbEditor * editor)
{
SetStyles(editor);
editorMap[editor] = std::shared_ptr<EditorData>(new EditorData(editor, this));
SetUnsavedFileData(editor);
//editorMap[editor]->thread.Run();
}
void SemanticHighlight::OnAttach()
{
// do whatever initialization you need for your plugin
// NOTE: after this function, the inherited member variable
// m_IsAttached will be TRUE...
// You should check for it in other functions, because if it
// is FALSE, it means that the application did *not* "load"
// (see: does not need) this plugin...
lastActiveEditor = 0;
EditorHooks::HookFunctorBase * hook = new EditorHooks::HookFunctor<SemanticHighlight>(this, &SemanticHighlight::OnEditorTextModified);
hookId = EditorHooks::RegisterHook(hook);
Manager::Get()->RegisterEventSink(cbEVT_APP_STARTUP_DONE, new cbEventFunctor<SemanticHighlight, CodeBlocksEvent>(this, &SemanticHighlight::OnAppStartupDone));
Manager::Get()->RegisterEventSink(cbEVT_EDITOR_ACTIVATED, new cbEventFunctor<SemanticHighlight, CodeBlocksEvent>(this, &SemanticHighlight::OnEditorActivated));
Manager::Get()->RegisterEventSink(cbEVT_EDITOR_CLOSE, new cbEventFunctor<SemanticHighlight, CodeBlocksEvent>(this, &SemanticHighlight::OnEditorClose));
Manager::Get()->RegisterEventSink(cbEVT_PROJECT_OPEN, new cbEventFunctor<SemanticHighlight, CodeBlocksEvent>(this, &SemanticHighlight::OnProjectOpen));
Manager::Get()->RegisterEventSink(cbEVT_PROJECT_CLOSE, new cbEventFunctor<SemanticHighlight, CodeBlocksEvent>(this, &SemanticHighlight::OnProjectClose));
// Manager::Get()->RegisterEventSink(cbEVT_PROJECT_OPTIONS_CHANGED, new cbEventFunctor<SemanticHighlight, CodeBlocksEvent>(this, &SemanticHighlight::OnOptionsChanged));
// Manager::Get()->RegisterEventSink(cbEVT_PROJECT_TARGETS_MODIFIED, new cbEventFunctor<SemanticHighlight, CodeBlocksEvent>(this, &SemanticHighlight::OnOptionsChanged));
Connect(shEVT_HIGHLIGHT, (wxObjectEventFunction)(&SemanticHighlight::ProcessSHData));
// check all projects (and build targets)
const ProjectsArray & projects = *(Manager::Get()->GetProjectManager()->GetProjects());
for (int i = 0; i < projects.GetCount(); ++i)
InitializeProjectData(projects.Item(i));
// initialize all opened editors
EditorBase * eb;
unsigned num_editors = Manager::Get()->GetEditorManager()->GetEditorsCount();
for (unsigned i = 0; i < num_editors; ++i)
{
eb = Manager::Get()->GetEditorManager()->GetEditor(i);
if (!eb->IsBuiltinEditor())
continue;
cbEditor * editor = static_cast<cbEditor *>(eb);
InitializeEditorData(editor);
}
// run all editor threads
for (EditorMap::iterator it = editorMap.begin(); it != editorMap.end(); ++it)
it->second->thread.Run();
}
void SemanticHighlight::OnRelease(bool appShutDown)
{
// do de-initialization for your plugin
// if appShutDown is true, the plugin is unloaded because Code::Blocks is being shut down,
// which means you must not use any of the SDK Managers
// NOTE: after this function, the inherited member variable
// m_IsAttached will be FALSE...
EditorHooks::UnregisterHook(hookId, true);
Manager::Get()->RemoveAllEventSinksFor(this);
// for all editors:
for (EditorMap::iterator it = editorMap.begin(); it != editorMap.end(); ++it)
it->first->GetControl()->SetLexer(wxSCI_LEX_CPP);
editorMap.clear();
}
// ugly hack to prevent crashing on startup because of weird event order...
void SemanticHighlight::OnAppStartupDone(CodeBlocksEvent & evt)
{
evt.Skip();
EditorBase * eb;
std::vector<cbEditor *> editors;
unsigned num_editors = Manager::Get()->GetEditorManager()->GetEditorsCount();
for (unsigned i = 0; i < num_editors; ++i)
{
eb = Manager::Get()->GetEditorManager()->GetEditor(i);
if (!eb->IsBuiltinEditor())
continue;
cbEditor * editor = static_cast<cbEditor *>(eb);
if (editorMap.find(editor) != editorMap.end())
continue;
InitializeEditorData(editor);
editors.push_back(editor);
//editorMap[editor]->thread.Run();
}
for (auto & editor : editors)
editorMap[editor]->thread.Run();
}
void SemanticHighlight::OnEditorTextModified(cbEditor * editor, wxScintillaEvent & evt)
{
evt.Skip();
// process only changes...
if (evt.GetEventType() != wxEVT_SCI_MODIFIED)
return;
// ... which are either inserts or deletes
const int mt = evt.GetModificationType();
if (mt & 0x0003) // after modification
{
editorMap[editor]->timer.Start(200, wxTIMER_ONE_SHOT); // wait 200 miliseconds for other keystrokes
editorMap[editor]->version++;
}
}
void SHTimer::Notify()
{
SetUnsavedFileData(editor);
Perform(editor, REPARSE);
}
void SemanticHighlight::OnProjectOpen(CodeBlocksEvent & evt)
{
evt.Skip();
cbProject * project = evt.GetProject();
if (project)
InitializeProjectData(project);
}
void SemanticHighlight::OnProjectClose(CodeBlocksEvent & evt)
{
evt.Skip();
cbProject * project = evt.GetProject();
CompileOptions::current.Erase(project);
// for all build targets
//Manager::Get()->GetLogManager()->Log(_("SemanticHighlight: cbEVT_PROJECT_CLOSE; project: ") + project->GetTitle() + wxString::Format(_(" (%p)"), project));
for (int i = 0; i < project->GetBuildTargetsCount(); ++i)
{
ProjectBuildTarget * bt = project->GetBuildTarget(i);
CompileOptions::current.Erase(bt);
//Manager::Get()->GetLogManager()->Log(_("SemanticHighlight: \ttarget: ") + bt->GetTitle() + wxString::Format(_(" (%p)"), bt));
}
}
void SemanticHighlight::OnEditorActivated(CodeBlocksEvent & evt)
{
evt.Skip();
EditorBase * eb = evt.GetEditor();
if (!eb->IsBuiltinEditor())
return;
cbEditor * editor = static_cast<cbEditor *>(eb);
if (editor == lastActiveEditor)
return;
lastActiveEditor = editor;
cbProject * project = GetProject(editor);
// workaround for C::B order of events (PROJECT_OPENED after EDITOR_ACTIVATED)
// to initialize project's options before parsing editor's text
if (!CompileOptions::current.Exists(project))
InitializeProjectData(project);
EditorMap::iterator it = editorMap.find(editor);
if (it != editorMap.end())
// resume editor's thread
Perform(editor, REPARSE);
else
{
InitializeEditorData(editor);
editorMap[editor]->thread.Run();
}
}
void SemanticHighlight::OnEditorClose(CodeBlocksEvent & evt)
{
EditorBase * eb = evt.GetEditor();
if (!eb->IsBuiltinEditor())
return;
cbEditor * editor = static_cast<cbEditor *>(eb);
EditorMap::iterator it = editorMap.find(editor);
if (it == editorMap.end())
return;
// clean up editor data
editorMap.erase(it);
UnsavedFiles::current.Remove(editor);
evt.Skip();
}
void SemanticHighlight::ProcessSHData(SHEvent & evt)
{
//Manager::Get()->GetLogManager()->Log(wxString::Format(wxT("Received message from a parsing thread (editor = %p).\n"), evt.editor));
if (editorMap[evt.editor]->version != evt.version)
return;
//Manager::Get()->GetLogManager()->Log(wxString::Format(wxT("Processing SH data (editor = %p).\n"), evt.editor));
auto length = evt.styles.size();
cbStyledTextCtrl * ctrl = evt.editor->GetControl();
ctrl->StartStyling(0);
ctrl->SetStyleBytes(length, &evt.styles[0]);
ctrl->SetIndicatorCurrent(indERROR);
ctrl->IndicatorClearRange(0, length);
ctrl->SetIndicatorCurrent(indWARNING);
ctrl->IndicatorClearRange(0, length);
for (auto & diag : evt.diagnostics)
{
ctrl->SetIndicatorCurrent(diag.kind == CXDiagnostic_Warning ? indWARNING : indERROR);
ctrl->IndicatorFillRange(diag.offset, 3);
Manager::Get()->GetLogManager()->Log(diag.text);
}
}