-
Notifications
You must be signed in to change notification settings - Fork 8
/
clangplugin.cpp
1040 lines (963 loc) · 37.9 KB
/
clangplugin.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
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* A clang based plugin
*/
#include <sdk.h>
#include "clangplugin.h"
#include <cbcolourmanager.h>
#include <cbstyledtextctrl.h>
#include <compilercommandgenerator.h>
#include <editor_hooks.h>
#include <wx/tokenzr.h>
#ifndef CB_PRECOMP
#include <cbeditor.h>
#include <cbproject.h>
#include <compilerfactory.h>
#include <configmanager.h>
#include <editorcolourset.h>
#include <editormanager.h>
#include <logmanager.h>
#include <macrosmanager.h>
#include <projectfile.h>
#include <projectmanager.h>
#include <algorithm>
#include <wx/dir.h>
#endif // CB_PRECOMP
// this auto-registers the plugin
namespace
{
PluginRegistrant<ClangPlugin> reg(wxT("ClangLib"));
}
static const wxString g_InvalidStr(wxT("invalid"));
const int idEdOpenTimer = wxNewId();
const int idReparseTimer = wxNewId();
const int idDiagnosticTimer = wxNewId();
const int idHightlightTimer = wxNewId();
const int idGotoDeclaration = wxNewId();
// milliseconds
#define ED_OPEN_DELAY 1000
#define ED_ACTIVATE_DELAY 150
#define REPARSE_DELAY 900
#define DIAGNOSTIC_DELAY 3000
#define HIGHTLIGHT_DELAY 1700
ClangPlugin::ClangPlugin() :
m_Proxy(m_Database, m_CppKeywords),
m_ImageList(16, 16),
m_EdOpenTimer(this, idEdOpenTimer),
m_ReparseTimer(this, idReparseTimer),
m_DiagnosticTimer(this, idDiagnosticTimer),
m_HightlightTimer(this, idHightlightTimer),
m_pLastEditor(nullptr),
m_TranslUnitId(wxNOT_FOUND)
{
if (!Manager::LoadResource(_T("clanglib.zip")))
NotifyMissingFile(_T("clanglib.zip"));
}
ClangPlugin::~ClangPlugin()
{
}
void ClangPlugin::OnAttach()
{
wxBitmap bmp;
wxString prefix = ConfigManager::GetDataFolder() + wxT("/images/codecompletion/");
// bitmaps must be added by order of PARSER_IMG_* consts (which are also TokenCategory enums)
const char* imgs[] = {
"class_folder.png", // PARSER_IMG_CLASS_FOLDER
"class.png", // PARSER_IMG_CLASS
"class_private.png", // PARSER_IMG_CLASS_PRIVATE
"class_protected.png", // PARSER_IMG_CLASS_PROTECTED
"class_public.png", // PARSER_IMG_CLASS_PUBLIC
"ctor_private.png", // PARSER_IMG_CTOR_PRIVATE
"ctor_protected.png", // PARSER_IMG_CTOR_PROTECTED
"ctor_public.png", // PARSER_IMG_CTOR_PUBLIC
"dtor_private.png", // PARSER_IMG_DTOR_PRIVATE
"dtor_protected.png", // PARSER_IMG_DTOR_PROTECTED
"dtor_public.png", // PARSER_IMG_DTOR_PUBLIC
"method_private.png", // PARSER_IMG_FUNC_PRIVATE
"method_protected.png", // PARSER_IMG_FUNC_PRIVATE
"method_public.png", // PARSER_IMG_FUNC_PUBLIC
"var_private.png", // PARSER_IMG_VAR_PRIVATE
"var_protected.png", // PARSER_IMG_VAR_PROTECTED
"var_public.png", // PARSER_IMG_VAR_PUBLIC
"macro_def.png", // PARSER_IMG_MACRO_DEF
"enum.png", // PARSER_IMG_ENUM
"enum_private.png", // PARSER_IMG_ENUM_PRIVATE
"enum_protected.png", // PARSER_IMG_ENUM_PROTECTED
"enum_public.png", // PARSER_IMG_ENUM_PUBLIC
"enumerator.png", // PARSER_IMG_ENUMERATOR
"namespace.png", // PARSER_IMG_NAMESPACE
"typedef.png", // PARSER_IMG_TYPEDEF
"typedef_private.png", // PARSER_IMG_TYPEDEF_PRIVATE
"typedef_protected.png", // PARSER_IMG_TYPEDEF_PROTECTED
"typedef_public.png", // PARSER_IMG_TYPEDEF_PUBLIC
"symbols_folder.png", // PARSER_IMG_SYMBOLS_FOLDER
"vars_folder.png", // PARSER_IMG_VARS_FOLDER
"funcs_folder.png", // PARSER_IMG_FUNCS_FOLDER
"enums_folder.png", // PARSER_IMG_ENUMS_FOLDER
"macro_def_folder.png", // PARSER_IMG_MACRO_DEF_FOLDER
"others_folder.png", // PARSER_IMG_OTHERS_FOLDER
"typedefs_folder.png", // PARSER_IMG_TYPEDEF_FOLDER
"macro_use.png", // PARSER_IMG_MACRO_USE
"macro_use_private.png", // PARSER_IMG_MACRO_USE_PRIVATE
"macro_use_protected.png", // PARSER_IMG_MACRO_USE_PROTECTED
"macro_use_public.png", // PARSER_IMG_MACRO_USE_PUBLIC
"macro_use_folder.png", // PARSER_IMG_MACRO_USE_FOLDER
"cpp_lang.png", // tcLangKeyword
nullptr
};
for (const char** itr = imgs; *itr; ++itr)
m_ImageList.Add(cbLoadBitmap(prefix + wxString::FromUTF8(*itr), wxBITMAP_TYPE_PNG));
EditorColourSet* theme = Manager::Get()->GetEditorManager()->GetColourSet();
wxStringTokenizer tokenizer(theme->GetKeywords(theme->GetHighlightLanguage(wxT("C/C++")), 0));
while (tokenizer.HasMoreTokens())
m_CppKeywords.push_back(tokenizer.GetNextToken());
std::sort(m_CppKeywords.begin(), m_CppKeywords.end());
wxStringVec(m_CppKeywords).swap(m_CppKeywords);
typedef cbEventFunctor<ClangPlugin, CodeBlocksEvent> ClEvent;
Manager::Get()->RegisterEventSink(cbEVT_EDITOR_OPEN, new ClEvent(this, &ClangPlugin::OnEditorOpen));
Manager::Get()->RegisterEventSink(cbEVT_EDITOR_ACTIVATED, new ClEvent(this, &ClangPlugin::OnEditorActivate));
Manager::Get()->RegisterEventSink(cbEVT_PROJECT_ACTIVATE, new ClEvent(this, &ClangPlugin::OnProjectActivate));
Connect(idEdOpenTimer, wxEVT_TIMER, wxTimerEventHandler(ClangPlugin::OnTimer));
Connect(idReparseTimer, wxEVT_TIMER, wxTimerEventHandler(ClangPlugin::OnTimer));
Connect(idDiagnosticTimer, wxEVT_TIMER, wxTimerEventHandler(ClangPlugin::OnTimer));
Connect(idHightlightTimer, wxEVT_TIMER, wxTimerEventHandler(ClangPlugin::OnTimer));
Connect(idGotoDeclaration, wxEVT_COMMAND_MENU_SELECTED, /*wxMenuEventHandler*/wxCommandEventHandler(ClangPlugin::OnGotoDeclaration), nullptr, this);
m_EditorHookId = EditorHooks::RegisterHook(new EditorHooks::HookFunctor<ClangPlugin>(this, &ClangPlugin::OnEditorHook));
}
void ClangPlugin::OnRelease(bool WXUNUSED(appShutDown))
{
EditorHooks::UnregisterHook(m_EditorHookId);
Disconnect(idGotoDeclaration);
Disconnect(idHightlightTimer);
Disconnect(idDiagnosticTimer);
Disconnect(idReparseTimer);
Disconnect(idEdOpenTimer);
Manager::Get()->RemoveAllEventSinksFor(this);
m_ImageList.RemoveAll();
}
ClangPlugin::CCProviderStatus ClangPlugin::GetProviderStatusFor(cbEditor* ed)
{
if (ed->GetLanguage() == ed->GetColourSet()->GetHighlightLanguage(wxT("C/C++")))
return ccpsActive;
return ccpsInactive;
}
struct PrioritySorter
{
bool operator()(const ClangPlugin::CCToken& a, const ClangPlugin::CCToken& b)
{
return a.weight < b.weight;
}
};
static wxString GetActualName(const wxString& name)
{
const int idx = name.Find(wxT(':'));
if (idx == wxNOT_FOUND)
return name;
return name.Mid(0, idx);
}
std::vector<ClangPlugin::CCToken> ClangPlugin::GetAutocompList(bool isAuto, cbEditor* ed,
int& tknStart, int& tknEnd)
{
std::vector<CCToken> tokens;
if (ed != m_pLastEditor)
{
m_TranslUnitId = m_Proxy.GetTranslationUnitId(ed->GetFilename());
m_pLastEditor = ed;
}
if (m_TranslUnitId == wxNOT_FOUND)
{
Manager::Get()->GetLogManager()->LogWarning(wxT("ClangLib: m_TranslUnitId == wxNOT_FOUND, "
"cannot complete in file ") + ed->GetFilename());
return tokens;
}
cbStyledTextCtrl* stc = ed->GetControl();
const int style = stc->GetStyleAt(tknEnd);
const wxChar curChar = stc->GetCharAt(tknEnd - 1);
if (isAuto) // filter illogical cases of auto-launch
{
if ( ( curChar == wxT(':') // scope operator
&& stc->GetCharAt(tknEnd - 2) != wxT(':') )
|| ( curChar == wxT('>') // '->'
&& stc->GetCharAt(tknEnd - 2) != wxT('-') )
|| ( wxString(wxT("<\"/")).Find(curChar) != wxNOT_FOUND // #include directive (TODO: enumerate completable include files)
&& !stc->IsPreprocessor(style) ) )
{
return tokens;
}
}
std::vector<ClToken> tknResults;
const int line = stc->LineFromPosition(tknStart);
std::map<wxString, wxString> unsavedFiles;
EditorManager* edMgr = Manager::Get()->GetEditorManager();
for (int i = 0; i < edMgr->GetEditorsCount(); ++i)
{
cbEditor* editor = edMgr->GetBuiltinEditor(i);
if (editor && editor->GetModified())
unsavedFiles.insert(std::make_pair(editor->GetFilename(),
editor->GetControl()->GetText()));
}
const int lnStart = stc->PositionFromLine(line);
int column = tknStart - lnStart;
for (; column > 0; --column)
{
if ( !wxIsspace(stc->GetCharAt(lnStart + column - 1))
|| (column != 1 && !wxIsspace(stc->GetCharAt(lnStart + column - 2))) )
{
break;
}
}
m_Proxy.CodeCompleteAt(isAuto, ed->GetFilename(), line + 1, column + 1,
m_TranslUnitId, unsavedFiles, tknResults);
const wxString& prefix = stc->GetTextRange(tknStart, tknEnd).Lower();
bool includeCtors = true; // sometimes we get a lot of these
for (int i = tknStart - 1; i > 0; --i)
{
wxChar chr = stc->GetCharAt(i);
if (!wxIsspace(chr))
{
if (chr == wxT(';') || chr == wxT('}')) // last non-whitespace character
includeCtors = false; // filter out ctors (they are unlikely to be wanted in this situation)
break;
}
}
if (prefix.Length() > 3) // larger context, match the prefix at any point in the token
{
for (std::vector<ClToken>::const_iterator tknIt = tknResults.begin();
tknIt != tknResults.end(); ++tknIt)
{
if (tknIt->name.Lower().Find(prefix) != wxNOT_FOUND && (includeCtors || tknIt->category != tcCtorPublic))
tokens.push_back(CCToken(tknIt->id, tknIt->name, tknIt->name, tknIt->weight, tknIt->category));
}
}
else if (prefix.IsEmpty())
{
for (std::vector<ClToken>::const_iterator tknIt = tknResults.begin();
tknIt != tknResults.end(); ++tknIt)
{
// it is rather unlikely for an operator to be the desired completion
if (!tknIt->name.StartsWith(wxT("operator")) && (includeCtors || tknIt->category != tcCtorPublic))
tokens.push_back(CCToken(tknIt->id, tknIt->name, tknIt->name, tknIt->weight, tknIt->category));
}
}
else // smaller context, only allow matches of the prefix at the beginning of the token
{
for (std::vector<ClToken>::const_iterator tknIt = tknResults.begin();
tknIt != tknResults.end(); ++tknIt)
{
if (tknIt->name.Lower().StartsWith(prefix) && (includeCtors || tknIt->category != tcCtorPublic))
tokens.push_back(CCToken(tknIt->id, tknIt->name, tknIt->name, tknIt->weight, tknIt->category));
}
}
if (!tokens.empty())
{
if (prefix.IsEmpty() && tokens.size() > 1500) // reduce to give only top matches
{
std::partial_sort(tokens.begin(), tokens.begin() + 1000, tokens.end(), PrioritySorter());
tokens.erase(tokens.begin() + 1000, tokens.end());
}
const int imgCount = m_ImageList.GetImageCount();
for (int i = 0; i < imgCount; ++i)
stc->RegisterImage(i, m_ImageList.GetBitmap(i));
bool isPP = stc->GetLine(line).Strip(wxString::leading).StartsWith(wxT("#"));
std::set<int> usedWeights;
for (std::vector<CCToken>::iterator tknIt = tokens.begin();
tknIt != tokens.end(); ++tknIt)
{
usedWeights.insert(tknIt->weight);
switch (tknIt->category)
{
case tcNone:
if (isPP)
tknIt->category = tcMacroDef;
else if (std::binary_search(m_CppKeywords.begin(), m_CppKeywords.end(), GetActualName(tknIt->name)))
tknIt->category = tcLangKeyword;
break;
case tcClass:
case tcCtorPublic:
case tcDtorPublic:
case tcFuncPublic:
case tcVarPublic:
case tcEnum:
case tcTypedef:
m_Proxy.RefineTokenType(m_TranslUnitId, tknIt->id, tknIt->category);
break;
default:
break;
}
}
// Clang sometimes gives many weight values, which can make completion more difficult
// because results are less alphabetical. Use a compression map on the lower priority
// values (higher numbers) to reduce the total number of weights used.
if (usedWeights.size() > 3)
{
std::vector<int> weightsVec(usedWeights.begin(), usedWeights.end());
std::map<int, int> weightCompr;
weightCompr[weightsVec[0]] = weightsVec[0];
weightCompr[weightsVec[1]] = weightsVec[1];
int factor = (weightsVec.size() > 7 ? 3 : 2);
for (size_t i = 2; i < weightsVec.size(); ++i)
weightCompr[weightsVec[i]] = weightsVec[(i - 2) / factor + 2];
for (std::vector<CCToken>::iterator tknIt = tokens.begin();
tknIt != tokens.end(); ++tknIt)
{
tknIt->weight = weightCompr[tknIt->weight];
}
}
}
return tokens;
}
wxString ClangPlugin::GetDocumentation(const CCToken& token)
{
if (token.id >= 0)
return m_Proxy.DocumentCCToken(m_TranslUnitId, token.id);
return wxEmptyString;
}
std::vector<ClangPlugin::CCCallTip> ClangPlugin::GetCallTips(int pos, int style, cbEditor* ed, int& argsPos)
{
std::vector<CCCallTip> tips;
if (ed != m_pLastEditor)
{
m_TranslUnitId = m_Proxy.GetTranslationUnitId(ed->GetFilename());
m_pLastEditor = ed;
}
if (m_TranslUnitId == wxNOT_FOUND)
return tips;
cbStyledTextCtrl* stc = ed->GetControl();
int nest = 0;
int commas = 0;
while (--pos > 0)
{
const int curStyle = stc->GetStyleAt(pos);
if ( stc->IsString(curStyle)
|| stc->IsCharacter(curStyle)
|| stc->IsComment(curStyle) )
{
continue;
}
const wxChar ch = stc->GetCharAt(pos);
if (ch == wxT(';'))
return tips; // error?
else if (ch == wxT(','))
{
if (nest == 0)
++commas;
}
else if (ch == wxT(')'))
--nest;
else if (ch == wxT('('))
{
++nest;
if (nest > 0)
break;
}
}
while (--pos > 0)
{
if ( stc->GetCharAt(pos) <= wxT(' ')
|| stc->IsComment(stc->GetStyleAt(pos)) )
{
continue;
}
break;
}
argsPos = stc->WordEndPosition(pos, true);
if (argsPos != m_LastCallTipPos)
{
m_LastCallTips.clear();
const int line = stc->LineFromPosition(pos);
const int column = pos - stc->PositionFromLine(line);
const wxString& tknText = stc->GetTextRange(stc->WordStartPosition(pos, true), argsPos);
if (!tknText.IsEmpty())
m_Proxy.GetCallTipsAt(ed->GetFilename(), line + 1, column + 1,
m_TranslUnitId, tknText, m_LastCallTips);
}
m_LastCallTipPos = argsPos;
for (std::vector<wxStringVec>::const_iterator strVecItr = m_LastCallTips.begin();
strVecItr != m_LastCallTips.end(); ++strVecItr)
{
int strVecSz = strVecItr->size();
if (commas != 0 && strVecSz < commas + 3)
continue;
wxString tip;
int hlStart = wxSCI_INVALID_POSITION;
int hlEnd = wxSCI_INVALID_POSITION;
for (int i = 0; i < strVecSz; ++i)
{
if (i == commas + 1 && strVecSz > 2)
{
hlStart = tip.Length();
hlEnd = hlStart + (*strVecItr)[i].Length();
}
tip += (*strVecItr)[i];
if (i > 0 && i < (strVecSz - 2))
tip += wxT(", ");
}
tips.push_back(CCCallTip(tip, hlStart, hlEnd));
}
return tips;
}
std::vector<ClangPlugin::CCToken> ClangPlugin::GetTokenAt(int pos, cbEditor* ed, bool& allowCallTip)
{
std::vector<CCToken> tokens;
if (ed != m_pLastEditor)
{
m_TranslUnitId = m_Proxy.GetTranslationUnitId(ed->GetFilename());
m_pLastEditor = ed;
}
if (m_TranslUnitId == wxNOT_FOUND)
return tokens;
cbStyledTextCtrl* stc = ed->GetControl();
if (stc->GetTextRange(pos - 1, pos + 1).Strip().IsEmpty())
return tokens;
const int line = stc->LineFromPosition(pos);
const int column = pos - stc->PositionFromLine(line);
wxStringVec names;
m_Proxy.GetTokensAt(ed->GetFilename(), line + 1, column + 1, m_TranslUnitId, names);
for (wxStringVec::const_iterator nmIt = names.begin(); nmIt != names.end(); ++nmIt)
tokens.push_back(CCToken(-1, *nmIt));
return tokens;
}
wxString ClangPlugin::OnDocumentationLink(wxHtmlLinkEvent& event, bool& dismissPopup)
{
return wxEmptyString;
}
void ClangPlugin::DoAutocomplete(const CCToken& token, cbEditor* ed)
{
wxString tknText = token.name;
int idx = tknText.Find(wxT(':'));
if (idx != wxNOT_FOUND)
tknText.Truncate(idx);
std::pair<int, int> offsets = std::make_pair(0, 0);
cbStyledTextCtrl* stc = ed->GetControl();
wxString suffix = m_Proxy.GetCCInsertSuffix(m_TranslUnitId, token.id,
GetEOLStr(stc->GetEOLMode())
+ ed->GetLineIndentString(stc->GetCurrentLine()),
offsets);
int pos = stc->GetCurrentPos();
int startPos = std::min(stc->WordStartPosition(pos, true), std::min(stc->GetSelectionStart(),
stc->GetSelectionEnd()));
int moveToPos = startPos + tknText.Length();
stc->SetTargetStart(startPos);
int endPos = stc->WordEndPosition(pos, true);
if (tknText.EndsWith(stc->GetTextRange(pos, endPos)))
{
if (!suffix.IsEmpty())
{
if (stc->GetCharAt(endPos) == suffix[0])
{
if (suffix.Length() != 2 || stc->GetCharAt(endPos + 1) != suffix[1])
offsets = std::make_pair(1, 1);
}
else
tknText += suffix;
}
}
else
{
endPos = pos;
tknText += suffix;
}
stc->SetTargetEnd(endPos);
stc->AutoCompCancel(); // so (wx)Scintilla does not insert the text as well
if (stc->GetTextRange(startPos, endPos) != tknText)
stc->ReplaceTarget(tknText);
stc->SetSelectionVoid(moveToPos + offsets.first, moveToPos + offsets.second);
stc->ChooseCaretX();
if ( token.category != tcLangKeyword
&& (offsets.first != offsets.second || offsets.first == 1) )
{
int tooltipMode = Manager::Get()->GetConfigManager(wxT("ccmanager"))->ReadInt(wxT("/tooltip_mode"), 1);
if (tooltipMode != 3) // keybound only
{
CodeBlocksEvent evt(cbEVT_SHOW_CALL_TIP);
Manager::Get()->ProcessEvent(evt);
}
}
}
void ClangPlugin::BuildMenu(wxMenuBar* menuBar)
{
int idx = menuBar->FindMenu(_("Sea&rch"));
if (idx != wxNOT_FOUND)
{
menuBar->GetMenu(idx)->Append(idGotoDeclaration, _("Resolve token (clang)"));
}
}
void ClangPlugin::BuildModuleMenu(const ModuleType type, wxMenu* menu,
const FileTreeData* WXUNUSED(data))
{
if (type != mtEditorManager)
return;
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed)
return;
if (ed != m_pLastEditor)
{
m_TranslUnitId = m_Proxy.GetTranslationUnitId(ed->GetFilename());
m_pLastEditor = ed;
}
if (m_TranslUnitId == wxNOT_FOUND)
return;
cbStyledTextCtrl* stc = ed->GetControl();
const int pos = stc->GetCurrentPos();
if (stc->GetTextRange(pos - 1, pos + 1).Strip().IsEmpty())
return;
menu->Insert(0, idGotoDeclaration, _("Resolve token (clang)"));
}
void ClangPlugin::OnEditorOpen(CodeBlocksEvent& event)
{
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinEditor(event.GetEditor());
if (ed)
{
UpdateCompileCommand(ed);
m_EdOpenTimer.Start(ED_OPEN_DELAY, wxTIMER_ONE_SHOT);
}
event.Skip();
}
void ClangPlugin::OnEditorActivate(CodeBlocksEvent& event)
{
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinEditor(event.GetEditor());
if (ed)
{
UpdateCompileCommand(ed);
if(!m_EdOpenTimer.IsRunning())
m_EdOpenTimer.Start(ED_ACTIVATE_DELAY, wxTIMER_ONE_SHOT);
}
event.Skip();
}
void ClangPlugin::OnProjectActivate(CodeBlocksEvent& event)
{
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinEditor(event.GetEditor());
if (ed)
{
UpdateCompileCommand(ed);
if (!m_EdOpenTimer.IsRunning())
m_EdOpenTimer.Start(ED_ACTIVATE_DELAY, wxTIMER_ONE_SHOT);
}
event.Skip();
}
void ClangPlugin::OnGotoDeclaration(wxCommandEvent& WXUNUSED(event))
{
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed || m_TranslUnitId == wxNOT_FOUND)
return;
cbStyledTextCtrl* stc = ed->GetControl();
const int pos = stc->GetCurrentPos();
wxString filename = ed->GetFilename();
int line = stc->LineFromPosition(pos);
int column = pos - stc->PositionFromLine(line) + 1;
if (stc->GetLine(line).StartsWith(wxT("#include")))
column = 2;
++line;
m_Proxy.ResolveTokenAt(filename, line, column, m_TranslUnitId);
ed = Manager::Get()->GetEditorManager()->Open(filename);
if (ed)
ed->GotoTokenPosition(line - 1, stc->GetTextRange(stc->WordStartPosition(pos, true),
stc->WordEndPosition(pos, true)));
}
wxString ClangPlugin::GetCompilerInclDirs(const wxString& compId)
{
std::map<wxString, wxString>::const_iterator idItr = m_compInclDirs.find(compId);
if (idItr != m_compInclDirs.end())
return idItr->second;
Compiler* comp = CompilerFactory::GetCompiler(compId);
wxFileName fn(wxEmptyString, comp->GetPrograms().CPP);
wxString masterPath = comp->GetMasterPath();
Manager::Get()->GetMacrosManager()->ReplaceMacros(masterPath);
fn.SetPath(masterPath);
if (!fn.FileExists())
fn.AppendDir(wxT("bin"));
#ifdef __WXMSW__
wxString command = fn.GetFullPath() + wxT(" -v -E -x c++ nul");
#else
wxString command = fn.GetFullPath() + wxT(" -v -E -x c++ /dev/null");
#endif // __WXMSW__
wxArrayString output, errors;
wxExecute(command, output, errors, wxEXEC_NODISABLE);
wxArrayString::const_iterator errItr = errors.begin();
for (; errItr != errors.end(); ++errItr)
{
if (errItr->IsSameAs(wxT("#include <...> search starts here:")))
{
++errItr;
break;
}
}
wxString includeDirs;
for (; errItr != errors.end(); ++errItr)
{
if (errItr->IsSameAs(wxT("End of search list.")))
break;
includeDirs += wxT(" -I") + errItr->Strip(wxString::both);
}
return m_compInclDirs.insert(std::pair<wxString, wxString>(compId, includeDirs)).first->second;
}
wxString ClangPlugin::GetSourceOf(cbEditor* ed)
{
cbProject* project = nullptr;
ProjectFile* opf = ed->GetProjectFile();
if (opf)
project = opf->GetParentProject();
if (!project)
project = Manager::Get()->GetProjectManager()->GetActiveProject();
wxFileName theFile(ed->GetFilename());
wxFileName candidateFile;
bool isCandidate;
wxArrayString fileArray;
wxDir::GetAllFiles(theFile.GetPath(wxPATH_GET_VOLUME), &fileArray,
theFile.GetName() + wxT(".*"), wxDIR_FILES | wxDIR_HIDDEN);
wxFileName currentCandidateFile = FindSourceIn(fileArray, theFile, isCandidate);
if (isCandidate)
candidateFile = currentCandidateFile;
else if (currentCandidateFile.IsOk())
return currentCandidateFile.GetFullPath();
fileArray.Clear();
EditorManager* edMgr = Manager::Get()->GetEditorManager();
for (int i = 0; i < edMgr->GetEditorsCount(); ++i)
{
cbEditor* edit = edMgr->GetBuiltinEditor(i);
if (!edit)
continue;
ProjectFile* pf = edit->GetProjectFile();
if (!pf)
continue;
fileArray.Add(pf->file.GetFullPath());
}
currentCandidateFile = FindSourceIn(fileArray, theFile, isCandidate);
if (!isCandidate && currentCandidateFile.IsOk())
return currentCandidateFile.GetFullPath();
if (project)
{
fileArray.Clear();
for (FilesList::const_iterator it = project->GetFilesList().begin();
it != project->GetFilesList().end(); ++it)
{
ProjectFile* pf = *it;
if (!pf)
continue;
fileArray.Add(pf->file.GetFullPath());
}
currentCandidateFile = FindSourceIn(fileArray, theFile, isCandidate);
if (isCandidate && !candidateFile.IsOk())
candidateFile = currentCandidateFile;
else if (currentCandidateFile.IsOk())
return currentCandidateFile.GetFullPath();
wxArrayString dirs = project->GetIncludeDirs();
for (int i = 0; i < project->GetBuildTargetsCount(); ++i)
{
ProjectBuildTarget* target = project->GetBuildTarget(i);
if (target)
{
for (size_t ti = 0; ti < target->GetIncludeDirs().GetCount(); ++ti)
{
wxString dir = target->GetIncludeDirs()[ti];
if (dirs.Index(dir) == wxNOT_FOUND)
dirs.Add(dir);
}
}
}
for (size_t i = 0; i < dirs.GetCount(); ++i)
{
wxString dir = dirs[i];
Manager::Get()->GetMacrosManager()->ReplaceMacros(dir);
wxFileName dname(dir);
if (!dname.IsAbsolute())
dname.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, project->GetBasePath());
fileArray.Clear();
wxDir::GetAllFiles(dname.GetPath(), &fileArray, theFile.GetName() + wxT(".*"), wxDIR_FILES | wxDIR_HIDDEN);
currentCandidateFile = FindSourceIn(fileArray, theFile, isCandidate);
if (isCandidate)
candidateFile = currentCandidateFile;
else if (currentCandidateFile.IsOk())
return currentCandidateFile.GetFullPath();
}
}
if (candidateFile.IsOk())
return candidateFile.GetFullPath();
return wxEmptyString;
}
wxFileName ClangPlugin::FindSourceIn(const wxArrayString& candidateFilesArray,
const wxFileName& activeFile, bool& isCandidate)
{
bool extStartsWithCapital = wxIsupper(activeFile.GetExt()[0]);
wxFileName candidateFile;
for (size_t i = 0; i < candidateFilesArray.GetCount(); ++i)
{
wxFileName currentCandidateFile(candidateFilesArray[i]);
if (IsSourceOf(currentCandidateFile, activeFile, isCandidate))
{
bool isUpper = wxIsupper(currentCandidateFile.GetExt()[0]);
if (isUpper == extStartsWithCapital && !isCandidate)
return currentCandidateFile;
else
candidateFile = currentCandidateFile;
}
}
isCandidate = true;
return candidateFile;
}
bool ClangPlugin::IsSourceOf(const wxFileName& candidateFile,
const wxFileName& activeFile, bool& isCandidate)
{
if (candidateFile.GetName().CmpNoCase(activeFile.GetName()) == 0)
{
isCandidate = (candidateFile.GetName() != activeFile.GetName());
if (FileTypeOf(candidateFile.GetFullName()) == ftSource)
{
if (candidateFile.GetPath() != activeFile.GetPath())
{
wxArrayString fileArray;
wxDir::GetAllFiles(candidateFile.GetPath(wxPATH_GET_VOLUME), &fileArray,
candidateFile.GetName() + wxT(".*"), wxDIR_FILES | wxDIR_HIDDEN);
for (size_t i = 0; i < fileArray.GetCount(); ++i)
if (wxFileName(fileArray[i]).GetFullName() == activeFile.GetFullName())
return false;
}
return candidateFile.FileExists();
}
}
return false;
}
// Don't call this function from within:
// ClangPlugin::OnEditorHook
// ClangPlugin::OnTimer
void ClangPlugin::UpdateCompileCommand(cbEditor* ed)
{
wxString compileCommand;
ProjectFile* pf = ed->GetProjectFile();
ProjectBuildTarget* target = nullptr;
Compiler* comp = nullptr;
if (pf && pf->GetParentProject() && !pf->GetBuildTargets().IsEmpty())
{
target = pf->GetParentProject()->GetBuildTarget(pf->GetBuildTargets()[0]);
comp = CompilerFactory::GetCompiler(target->GetCompilerID());
}
cbProject* proj = (pf ? pf->GetParentProject() : nullptr);
if (!comp && proj)
comp = CompilerFactory::GetCompiler(proj->GetCompilerID());
if (!comp)
{
cbProject* tmpPrj = Manager::Get()->GetProjectManager()->GetActiveProject();
if (tmpPrj)
comp = CompilerFactory::GetCompiler(tmpPrj->GetCompilerID());
}
if (!comp)
comp = CompilerFactory::GetDefaultCompiler();
if (pf && (!pf->GetBuildTargets().IsEmpty()))
{
target = pf->GetParentProject()->GetBuildTarget(pf->GetBuildTargets()[0]);
if (pf->GetUseCustomBuildCommand(target->GetCompilerID()))
compileCommand = pf->GetCustomBuildCommand(target->GetCompilerID()).AfterFirst(wxT(' '));
}
if (compileCommand.IsEmpty())
compileCommand = wxT("$options $includes");
CompilerCommandGenerator* gen = comp->GetCommandGenerator(proj);
gen->GenerateCommandLine(compileCommand, target, pf, ed->GetFilename(),
g_InvalidStr, g_InvalidStr, g_InvalidStr);
delete gen;
wxStringTokenizer tokenizer(compileCommand);
compileCommand.Empty();
wxString pathStr;
while (tokenizer.HasMoreTokens())
{
wxString flag = tokenizer.GetNextToken();
// make all include paths absolute, so clang does not choke if Code::Blocks switches directories
if (flag.StartsWith(wxT("-I"), &pathStr))
{
wxFileName path(pathStr);
if (path.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE))
flag = wxT("-I") + path.GetFullPath();
}
compileCommand += flag + wxT(" ");
}
compileCommand += GetCompilerInclDirs(comp->GetID());
m_CompileCommand = compileCommand;
}
void ClangPlugin::OnTimer(wxTimerEvent& event)
{
if (!IsAttached())
return;
const int evId = event.GetId();
if (evId == idEdOpenTimer) // m_EdOpenTimer
{
if (m_CompileCommand.IsEmpty())
{
m_EdOpenTimer.Start(ED_OPEN_DELAY, wxTIMER_ONE_SHOT); // retry later
return;
}
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed || !IsProviderFor(ed))
return;
else if (m_Proxy.GetTranslationUnitId(ed->GetFilename()) != wxNOT_FOUND)
{
m_DiagnosticTimer.Start(DIAGNOSTIC_DELAY, wxTIMER_ONE_SHOT);
return;
}
if (FileTypeOf(ed->GetFilename()) == ftHeader) // try to find the associated source
{
const wxString& source = GetSourceOf(ed);
if (!source.IsEmpty())
{
m_Proxy.CreateTranslationUnit(source, m_CompileCommand);
if (m_Proxy.GetTranslationUnitId(ed->GetFilename()) != wxNOT_FOUND)
return; // got it
}
}
m_Proxy.CreateTranslationUnit(ed->GetFilename(), m_CompileCommand);
m_DiagnosticTimer.Start(DIAGNOSTIC_DELAY, wxTIMER_ONE_SHOT);
}
else if (evId == idReparseTimer) // m_ReparseTimer
{
EditorManager* edMgr = Manager::Get()->GetEditorManager();
cbEditor* ed = edMgr->GetBuiltinActiveEditor();
if (!ed)
return;
if (ed != m_pLastEditor)
{
m_TranslUnitId = m_Proxy.GetTranslationUnitId(ed->GetFilename());
m_pLastEditor = ed;
}
if (m_TranslUnitId == wxNOT_FOUND)
return;
std::map<wxString, wxString> unsavedFiles;
for (int i = 0; i < edMgr->GetEditorsCount(); ++i)
{
ed = edMgr->GetBuiltinEditor(i);
if (ed && ed->GetModified())
unsavedFiles.insert(std::make_pair(ed->GetFilename(), ed->GetControl()->GetText()));
}
m_Proxy.Reparse(m_TranslUnitId, unsavedFiles);
DiagnoseEd(m_pLastEditor, dlMinimal);
}
// m_DiagnosticTimer, m_HightlightTime
else if (evId == idDiagnosticTimer || evId == idHightlightTimer)
{
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed)
return;
if (ed != m_pLastEditor)
{
m_TranslUnitId = m_Proxy.GetTranslationUnitId(ed->GetFilename());
m_pLastEditor = ed;
}
if (m_TranslUnitId == wxNOT_FOUND)
return;
if (evId == idDiagnosticTimer)
DiagnoseEd(ed, dlFull);
else
HighlightOccurrences(ed);
}
else
event.Skip();
}
void ClangPlugin::OnEditorHook(cbEditor* ed, wxScintillaEvent& event)
{
event.Skip();
if (!IsProviderFor(ed))
return;
if (event.GetEventType() == wxEVT_SCI_MODIFIED)
{
if (event.GetModificationType() & (wxSCI_MOD_INSERTTEXT | wxSCI_MOD_DELETETEXT))
{
m_ReparseTimer.Start(REPARSE_DELAY, wxTIMER_ONE_SHOT);
m_DiagnosticTimer.Start(DIAGNOSTIC_DELAY, wxTIMER_ONE_SHOT);
}
}
else if (event.GetEventType() == wxEVT_SCI_UPDATEUI)
{
if (event.GetUpdated() & wxSCI_UPDATE_SELECTION)
{
m_HightlightTimer.Stop();
m_HightlightTimer.Start(HIGHTLIGHT_DELAY, wxTIMER_ONE_SHOT);
}
}
}
void ClangPlugin::DiagnoseEd(cbEditor* ed, DiagnosticLevel diagLv)
{
std::vector<ClDiagnostic> diagnostics;
m_Proxy.GetDiagnostics(m_TranslUnitId, diagnostics);
cbStyledTextCtrl* stc = ed->GetControl();
if (diagLv == dlFull)
stc->AnnotationClearAll();
const int warningIndicator = 0; // predefined
const int errorIndicator = 15; // hopefully we do not clash with someone else...
stc->SetIndicatorCurrent(warningIndicator);
stc->IndicatorClearRange(0, stc->GetLength());
stc->IndicatorSetStyle(errorIndicator, wxSCI_INDIC_SQUIGGLE);
stc->IndicatorSetForeground(errorIndicator, *wxRED);
stc->SetIndicatorCurrent(errorIndicator);
stc->IndicatorClearRange(0, stc->GetLength());
const wxString& fileNm = ed->GetFilename();
for ( std::vector<ClDiagnostic>::const_iterator dgItr = diagnostics.begin();
dgItr != diagnostics.end(); ++dgItr )
{
//Manager::Get()->GetLogManager()->Log(dgItr->file + wxT(" ") + dgItr->message + F(wxT(" %d, %d"), dgItr->range.first, dgItr->range.second));
if (dgItr->file != fileNm)
continue;
if (diagLv == dlFull)
{
wxString str = stc->AnnotationGetText(dgItr->line - 1);
if (!str.IsEmpty())
str += wxT('\n');
stc->AnnotationSetText(dgItr->line - 1, str + dgItr->message);
stc->AnnotationSetStyle(dgItr->line - 1, 50);
}
int pos = stc->PositionFromLine(dgItr->line - 1) + dgItr->range.first - 1;
int range = dgItr->range.second - dgItr->range.first;
if (range == 0)
{
range = stc->WordEndPosition(pos, true) - pos;
if (range == 0)
{
pos = stc->WordStartPosition(pos, true);
range = stc->WordEndPosition(pos, true) - pos;
}
}
if (dgItr->severity == sError)
stc->SetIndicatorCurrent(errorIndicator);
else if ( dgItr != diagnostics.begin()
&& dgItr->line == (dgItr - 1)->line
&& dgItr->range.first <= (dgItr - 1)->range.second )
{
continue; // do not overwrite the last indicator
}
else
stc->SetIndicatorCurrent(warningIndicator);
stc->IndicatorFillRange(pos, range);
}
if (diagLv == dlFull)
stc->AnnotationSetVisible(wxSCI_ANNOTATION_BOXED);
}
void ClangPlugin::HighlightOccurrences(cbEditor* ed)