forked from alpha0010/ClangLib
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclangcc.cpp
823 lines (736 loc) · 28.9 KB
/
clangcc.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
#include "clangcc.h"
#include <cbstyledtextctrl.h>
#include <editor_hooks.h>
#include <cbcolourmanager.h>
#include <infowindow.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 <vector>
#include <wx/dir.h>
#include <wx/tokenzr.h>
#include <wx/choice.h>
//#endif // CB_PRECOMP
#include "cclogger.h"
const int idHighlightTimer = wxNewId();
#define HIGHLIGHT_DELAY 700
const wxString ClangCodeCompletion::SettingName = _T("/code_completion");
ClangCodeCompletion::ClangCodeCompletion() :
ClangPluginComponent(),
m_TranslUnitId(-1),
m_EditorHookId(-1),
m_HighlightTimer(this, idHighlightTimer),
m_CCOutstanding(0),
m_CCOutstandingLastMessageTime(0),
m_CCOutstandingTokenStart(-1),
m_CCOutstandingLoc(0,0)
{
}
ClangCodeCompletion::~ClangCodeCompletion()
{
}
void ClangCodeCompletion::OnAttach(IClangPlugin* pClangPlugin)
{
ClangPluginComponent::OnAttach(pClangPlugin);
ColourManager *pColours = Manager::Get()->GetColourManager();
pColours->RegisterColour(_("Code completion"), _("Documentation popup scope text"), wxT("cc_docs_scope_fore"), *wxBLUE);
typedef cbEventFunctor<ClangCodeCompletion, CodeBlocksEvent> CBCCEvent;
Manager::Get()->RegisterEventSink(cbEVT_EDITOR_ACTIVATED, new CBCCEvent(this, &ClangCodeCompletion::OnEditorActivate));
Manager::Get()->RegisterEventSink(cbEVT_EDITOR_CLOSE, new CBCCEvent(this, &ClangCodeCompletion::OnEditorClose));
Connect(idHighlightTimer, wxEVT_TIMER, wxTimerEventHandler(ClangCodeCompletion::OnTimer));
typedef cbEventFunctor<ClangCodeCompletion, ClangEvent> ClCCEvent;
pClangPlugin->RegisterEventSink(clEVT_TRANSLATIONUNIT_CREATED, new ClCCEvent(this, &ClangCodeCompletion::OnTranslationUnitCreated));
pClangPlugin->RegisterEventSink(clEVT_GETCODECOMPLETE_FINISHED, new ClCCEvent(this, &ClangCodeCompletion::OnCodeCompleteFinished));
pClangPlugin->RegisterEventSink(clEVT_GETOCCURRENCES_FINISHED, new ClCCEvent(this, &ClangCodeCompletion::OnGetOccurrencesFinished));
m_EditorHookId = EditorHooks::RegisterHook(new EditorHooks::HookFunctor<ClangCodeCompletion>(this, &ClangCodeCompletion::OnEditorHook));
}
void ClangCodeCompletion::OnRelease(IClangPlugin* pClangPlugin)
{
pClangPlugin->RemoveAllEventSinksFor(this);
Disconnect(idHighlightTimer);
EditorHooks::UnregisterHook(m_EditorHookId);
Manager::Get()->RemoveAllEventSinksFor(this);
ClangPluginComponent::OnRelease(pClangPlugin);
}
void ClangCodeCompletion::OnEditorActivate(CodeBlocksEvent& event)
{
event.Skip();
if (!IsAttached())
return;
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinEditor(event.GetEditor());
if (ed)
{
wxString fn = ed->GetFilename();
ClTranslUnitId id = m_pClangPlugin->GetTranslationUnitId(fn);
m_TranslUnitId = id;
m_CCOutstanding = 0;
m_CCOutstandingLastMessageTime = 0;
m_CCOutstandingTokenStart = 0;
m_CCOutstandingResults.clear();
cbStyledTextCtrl* stc = ed->GetControl();
#ifndef __WXMSW__
stc->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(ClangCodeCompletion::OnKeyDown));
stc->Connect(wxID_ANY, wxEVT_KEY_DOWN, wxKeyEventHandler(ClangCodeCompletion::OnKeyDown), (wxObject*)nullptr, this);
#endif
const int imgCount = m_pClangPlugin->GetImageList(id).GetImageCount();
for (int i = 0; i < imgCount; ++i)
stc->RegisterImage(i, m_pClangPlugin->GetImageList(id).GetBitmap(i));
}
}
void ClangCodeCompletion::OnEditorClose(CodeBlocksEvent& event)
{
event.Skip();
if (!IsAttached())
return;
EditorManager* edm = Manager::Get()->GetEditorManager();
if (!edm)
return;
}
void ClangCodeCompletion::OnEditorHook(cbEditor* ed, wxScintillaEvent& event)
{
event.Skip();
if (!IsAttached())
return;
bool clearIndicator = false;
//if (!m_pClangPlugin->IsProviderFor(ed))
// return;
cbStyledTextCtrl* stc = ed->GetControl();
if (event.GetEventType() == wxEVT_SCI_MODIFIED)
{
if (event.GetModificationType() & (wxSCI_MOD_INSERTTEXT | wxSCI_MOD_DELETETEXT))
{
m_HighlightTimer.Stop();
clearIndicator = true;
}
}
else if (event.GetEventType() == wxEVT_SCI_UPDATEUI)
{
if (event.GetUpdated() & wxSCI_UPDATE_SELECTION)
{
m_HighlightTimer.Stop();
m_HighlightTimer.Start(HIGHLIGHT_DELAY, wxTIMER_ONE_SHOT);
clearIndicator = true;
}
}
else if (event.GetEventType() == wxEVT_SCI_CHANGE)
{
//fprintf(stdout,"wxEVT_SCI_CHANGE\n");
}
else if (event.GetEventType() == wxEVT_SCI_KEY)
{
//fprintf(stdout,"wxEVT_SCI_KEY\n");
}
if (clearIndicator)
{
const int theIndicator = 16;
stc->SetIndicatorCurrent(theIndicator);
stc->IndicatorClearRange(0, stc->GetLength());
}
}
void ClangCodeCompletion::OnTimer(wxTimerEvent& event)
{
if (!IsAttached())
return;
const int evId = event.GetId();
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed)
return;
if (evId == idHighlightTimer)
BeginHighlightOccurrences(ed);
else
event.Skip();
}
void ClangCodeCompletion::OnKeyDown(wxKeyEvent& event)
{
if (event.GetKeyCode() == WXK_TAB)
{
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (ed)
{
cbStyledTextCtrl* stc = ed->GetControl();
if (!stc->AutoCompActive())
{
int pos = stc->PositionFromLine(stc->GetCurrentLine());
int maxPos = stc->PositionFromLine(stc->GetCurrentLine() + 1);
for (std::vector<wxString>::iterator it = m_TabJumpArguments.begin(); it != m_TabJumpArguments.end(); ++it)
{
int argPos = stc->FindText(pos, maxPos, *it);
if (argPos != wxNOT_FOUND)
{
stc->SetSelectionVoid(argPos, argPos + it->Length() - 1);
wxString value = *it;
it = m_TabJumpArguments.erase(it);
m_TabJumpArguments.push_back(value);
stc->EnableTabSmartJump();
return;
}
}
}
}
}
event.Skip();
}
void ClangCodeCompletion::OnCompleteCode(CodeBlocksEvent &event)
{
event.Skip();
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed)
return;
//wxString filename = ed->GetFilename();
//m_pClangPlugin->RequestReparse( m_TranslUnitId, filename );
}
ClTranslUnitId ClangCodeCompletion::GetCurrentTranslationUnitId()
{
if (m_TranslUnitId == wxNOT_FOUND)
{
cbEditor* ed = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
if (!ed)
return wxNOT_FOUND;
wxString filename = ed->GetFilename();
m_TranslUnitId = m_pClangPlugin->GetTranslationUnitId( filename );
}
return m_TranslUnitId;
}
cbCodeCompletionPlugin::CCProviderStatus ClangCodeCompletion::GetProviderStatusFor(cbEditor* ed)
{
if (ed->GetLanguage() == ed->GetColourSet()->GetHighlightLanguage(wxT("C/C++")))
return cbCodeCompletionPlugin::ccpsActive;
return cbCodeCompletionPlugin::ccpsInactive;
}
struct PrioritySorter
{
bool operator()(const cbCodeCompletionPlugin::CCToken& a, const cbCodeCompletionPlugin::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<cbCodeCompletionPlugin::CCToken> ClangCodeCompletion::GetAutocompList(bool isAuto, cbEditor* ed, int& tknStart, int& tknEnd)
{
CCLogger::Get()->DebugLog( wxT("ClangCodeCompletion::GetAutocompList") );
std::vector<cbCodeCompletionPlugin::CCToken> tokens;
ConfigManager* cfg = Manager::Get()->GetConfigManager(wxT("ClangLib"));
size_t maxResultCount = cfg->ReadInt(wxT("/max_matches"), 1024);
cbStyledTextCtrl* stc = ed->GetControl();
const int style = stc->GetStyleAt( tknEnd );
const int lineIndentPos = stc->GetLineIndentPosition(stc->GetCurrentLine());
const wxChar lineFirstChar = stc->GetCharAt(lineIndentPos);
if (lineFirstChar == wxT('#'))
{
const int startPos = stc->WordStartPosition(lineIndentPos + 1, true);
const int endPos = stc->WordEndPosition(lineIndentPos + 1, true);
const wxString str = stc->GetTextRange(startPos, endPos);
if (str == wxT("include") && (tknEnd > endPos))
return GetAutocompListIncludes(isAuto, ed, tknStart, tknEnd);
}
ClTranslUnitId translUnitId = GetCurrentTranslationUnitId();
if (translUnitId == wxNOT_FOUND)
{
Manager::Get()->GetLogManager()->LogWarning(wxT("ClangLib: translUnitId == wxNOT_FOUND, "
"cannot complete in file ") + ed->GetFilename());
if (wxGetLocalTime() + 10 > m_CCOutstandingLastMessageTime)
{
InfoWindow::Display(_("Code completion"), _("Busy parsing the document"), 1000);
m_CCOutstandingLastMessageTime = wxGetLocalTime();
}
return tokens;
}
if (translUnitId != m_TranslUnitId)
return tokens;
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;
}
}
if (stc->IsString(style)||stc->IsComment(style)||stc->IsCharacter(style))
return tokens;
// TODO: The outstanding bits really have become a mess: It's complicated with CC that can come in with the same position from C::B, CC request that can come in from the result and an old request that might still have been in the pipe
//CCLogger::Get()->DebugLog( F(wxT("GetAutoCompList m_CCOutstanding=%d m_CCOutstandingTokenStart=%d"), m_CCOutstanding, m_CCOutstandingTokenStart ) );
int CCOutstanding = m_CCOutstanding;
if ((CCOutstanding > 0) && (m_CCOutstandingTokenStart == tknStart) && m_CCOutstandingResults.empty())
{
CCLogger::Get()->DebugLog(wxT("CC request allready requested"));
return tokens;
}
if (m_CCOutstandingTokenStart != tknStart)
{
m_CCOutstandingResults.clear();
}
if ((CCOutstanding > 0) && (m_CCOutstandingTokenStart != tknStart))
{
CCOutstanding = 0;
}
if ((CCOutstanding > 0) && m_CCOutstandingResults.empty())
{
CCLogger::Get()->DebugLog( wxT("CodeCompletion allready requested, wait until it's finished!") );
// This request is allready scheduled to be performed but is not ready yet...
return tokens;
}
const int line = stc->LineFromPosition(tknStart);
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;
}
}
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;
}
}
std::vector<ClToken> tknResults;
if (m_CCOutstandingResults.empty())
{
ClTokenPosition loc(line + 1, column + 1);
unsigned long timeout = 20;
if ( !isAuto )
{
timeout = 100;
}
if (wxCOND_TIMEOUT == m_pClangPlugin->GetCodeCompletionAt(translUnitId, ed->GetFilename(), loc, includeCtors, timeout, tknResults))
{
m_CCOutstanding++;
m_CCOutstandingTokenStart = tknStart;
m_CCOutstandingLoc = loc;
m_CCOutstandingResults.clear();
return tokens;
}
}
else
{
tknResults = m_CCOutstandingResults;
}
m_CCOutstanding = 0;
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(cbCodeCompletionPlugin::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(cbCodeCompletionPlugin::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(cbCodeCompletionPlugin::CCToken(tknIt->id, tknIt->name, tknIt->name, tknIt->weight, tknIt->category));
}
}
if (!tokens.empty())
{
if (prefix.IsEmpty() && (tokens.size() > maxResultCount)) // reduce to give only top matches
{
std::partial_sort(tokens.begin(), tokens.begin() + maxResultCount, tokens.end(), PrioritySorter());
tokens.erase(tokens.begin() + maxResultCount, tokens.end());
}
//const int imgCount = m_pClangPlugin->GetImageList(translUnitId).GetImageCount();
//for (int i = 0; i < imgCount; ++i)
// stc->RegisterImage(i, m_pClangPlugin->GetImageList(translUnitId).GetBitmap(i));
bool isPP = stc->GetLine(line).Strip(wxString::leading).StartsWith(wxT("#"));
wxStringVec keywords = m_pClangPlugin->GetKeywords(translUnitId);
std::set<int> usedWeights;
for (std::vector<cbCodeCompletionPlugin::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(keywords.begin(), keywords.end(), GetActualName(tknIt->name)))
tknIt->category = tcLangKeyword;
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)
{
CCLogger::Get()->DebugLog( wxT("Recalculating weights") );
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<cbCodeCompletionPlugin::CCToken>::iterator tknIt = tokens.begin();
tknIt != tokens.end(); ++tknIt)
{
tknIt->weight = weightCompr[tknIt->weight];
}
}
}
CCLogger::Get()->DebugLog( wxT("Delivering list of CC Tokens") );
return tokens;
}
std::vector<cbCodeCompletionPlugin::CCToken> ClangCodeCompletion::GetAutocompListIncludes(bool WXUNUSED(isAuto), cbEditor* WXUNUSED(ed), int& WXUNUSED(tknStart), int& WXUNUSED(tknEnd))
{
std::vector<cbCodeCompletionPlugin::CCToken> result;
return result;
}
bool ClangCodeCompletion::DoAutocomplete( const cbCodeCompletionPlugin::CCToken& token, cbEditor* ed)
{
wxString tknText = token.name;
int idx = tknText.Find(wxT(':'));
if (idx != wxNOT_FOUND)
tknText.Truncate(idx);
std::vector<std::pair<int, int> > offsetsList;
cbStyledTextCtrl* stc = ed->GetControl();
wxString suffix = m_pClangPlugin->GetCodeCompletionInsertSuffix(GetCurrentTranslationUnitId(),
token.id,
GetEOLStr(stc->GetEOLMode()) + ed->GetLineIndentString(stc->GetCurrentLine()),
offsetsList);
//if (offsetsList.size() == 0)
// offsetsList.push_back( std::make_pair<int,int>(0,0) );
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)))
{
// Inplace function renaming. We determine here if we insert the arguments or not
if (!suffix.IsEmpty())
{
if (stc->GetCharAt(endPos) == (int)suffix[0])
{
if ( (suffix.Length() != 2) || (stc->GetCharAt(endPos + 1) != (int)suffix[1]) )
{
offsetsList.clear();
}
}
else
{
tknText += suffix;
if (suffix.Length() == 2)
{
moveToPos += 2;
}
}
}
}
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);
if (offsetsList.size() > 0)
stc->SetSelectionVoid(moveToPos + offsetsList[0].first, moveToPos + offsetsList[0].second);
else
stc->SetSelectionVoid(moveToPos, moveToPos);
stc->ChooseCaretX();
if (m_TabJumpArguments.size() > 10 )
m_TabJumpArguments.clear();
for (std::vector< std::pair<int,int> >::const_iterator it = offsetsList.begin(); it != offsetsList.end(); ++it)
{
if (it->first != it->second)
{
m_TabJumpArguments.push_back( suffix.SubString(it->first, it->second) );
stc->EnableTabSmartJump();
}
}
if (offsetsList.size() > 0)
{
if (m_TabJumpArguments.size() > 0)
{
// Move the first to the last since the first is allready selected
wxString first = m_TabJumpArguments.front();
m_TabJumpArguments.erase(m_TabJumpArguments.begin());
m_TabJumpArguments.push_back(first);
}
if ( (token.category != tcLangKeyword)
&& ((offsetsList[0].first != offsetsList[0].second) || (offsetsList[0].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);
}
}
}
return true;
}
wxString ClangCodeCompletion::GetDocumentation(const cbCodeCompletionPlugin::CCToken &token)
{
EditorManager* edMgr = Manager::Get()->GetEditorManager();
cbEditor* ed = edMgr->GetBuiltinActiveEditor();
if (ed)
{
return m_pClangPlugin->GetCodeCompletionTokenDocumentation(m_TranslUnitId, ed->GetFilename(), ClTokenPosition(0,0), token.id);
}
return wxEmptyString;
}
void ClangCodeCompletion::BeginHighlightOccurrences(cbEditor* ed)
{
ClTranslUnitId translId = GetCurrentTranslationUnitId();
cbStyledTextCtrl* stc = ed->GetControl();
int pos = stc->GetCurrentPos();
const wxChar ch = stc->GetCharAt(pos);
if ( pos > 0
&& (wxIsspace(ch) || (ch != wxT('_') && wxIspunct(ch)))
&& !wxIsspace(stc->GetCharAt(pos - 1)) )
{
--pos;
}
// chosen a high value for indicator, hoping not to interfere with the indicators used by some lexers
// if they get updated from deprecated old style indicators someday.
const int theIndicator = 16;
stc->SetIndicatorCurrent(theIndicator);
// Set Styling:
// clear all style indications set in a previous run (is also done once after text gets unselected)
stc->IndicatorClearRange(0, stc->GetLength());
if (stc->GetTextRange(pos - 1, pos + 1).Strip().IsEmpty())
return;
const int line = stc->LineFromPosition(pos);
ClTokenPosition loc(line + 1, pos - stc->PositionFromLine(line) + 1);
m_pClangPlugin->GetOccurrencesOf( translId, ed->GetFilename(), loc );
}
void ClangCodeCompletion::OnTranslationUnitCreated( ClangEvent& event )
{
if (event.GetTranslationUnitId() != GetCurrentTranslationUnitId())
return;
m_CCOutstanding = 0;
m_CCOutstandingTokenStart = 0;
m_CCOutstandingResults.clear();
}
void ClangCodeCompletion::OnCodeCompleteFinished(ClangEvent& event)
{
if (event.GetTranslationUnitId() != m_TranslUnitId)
return;
if (m_CCOutstanding > 0)
{
if (event.GetLocation() != m_CCOutstandingLoc)
{
CCLogger::Get()->DebugLog(wxT("Discard old CodeCompletion request result"));
return;
}
EditorManager* edMgr = Manager::Get()->GetEditorManager();
cbEditor* ed = edMgr->GetBuiltinActiveEditor();
if (ed)
{
m_CCOutstandingResults = event.GetCodeCompletionResults();
if (m_CCOutstandingResults.size() > 0)
{
CodeBlocksEvent evt(cbEVT_COMPLETE_CODE);
evt.SetInt(1);
Manager::Get()->ProcessEvent(evt);
return;
}
}
m_CCOutstanding--;
}
}
void ClangCodeCompletion::OnGetOccurrencesFinished(ClangEvent& event)
{
if (event.GetTranslationUnitId() != m_TranslUnitId)
{
CCLogger::Get()->DebugLog( _T("Translation unit has switched") );
return;
}
EditorManager* edMgr = Manager::Get()->GetEditorManager();
cbEditor* ed = edMgr->GetBuiltinActiveEditor();
if (!ed)
return;
cbStyledTextCtrl* stc = ed->GetControl();
int pos = stc->GetCurrentPos();
const wxChar ch = stc->GetCharAt(pos);
if ( pos > 0
&& (wxIsspace(ch) || (ch != wxT('_') && wxIspunct(ch)))
&& !wxIsspace(stc->GetCharAt(pos - 1)) )
{
--pos;
}
const int line = stc->LineFromPosition(pos);
ClTokenPosition loc(line + 1, pos - stc->PositionFromLine(line) + 1);
if (event.GetLocation() != loc)
{
CCLogger::Get()->DebugLog( wxT("Location has changed since last GetOccurrences request") );
return; // Location has changed since the request
}
// chosen a high value for indicator, hoping not to interfere with the indicators used by some lexers
// if they get updated from deprecated old style indicators someday.
const int theIndicator = 16;
stc->SetIndicatorCurrent(theIndicator);
// Set Styling:
// clear all style indications set in a previous run (is also done once after text gets unselected)
stc->IndicatorClearRange(0, stc->GetLength());
// TODO: use independent key
wxColour highlightColour(Manager::Get()->GetColourManager()->GetColour(wxT("editor_highlight_occurrence")));
stc->IndicatorSetStyle(theIndicator, wxSCI_INDIC_HIGHLIGHT);
stc->IndicatorSetForeground(theIndicator, highlightColour);
stc->IndicatorSetUnder(theIndicator, true);
const std::vector< std::pair<int, int> >& occurrences = event.GetOccurrencesResults();
for (std::vector< std::pair<int, int> >::const_iterator tkn = occurrences.begin();
tkn != occurrences.end(); ++tkn)
{
stc->IndicatorFillRange(tkn->first, tkn->second);
}
}
// Sorting in GetLocalIncludeDirs()
static int CompareStringLen(const wxString& first, const wxString& second)
{
return second.Len() - first.Len();
}
wxArrayString ClangCodeCompletion::GetLocalIncludeDirs(cbProject* project, const wxArrayString& buildTargets)
{
wxArrayString dirs;
#if 0 //TODO
// Do not try to operate include directories if the project is not for this platform
if (m_CCEnablePlatformCheck && !project->SupportsCurrentPlatform())
return dirs;
#endif
const wxString prjPath = project->GetCommonTopLevelPath();
GetAbsolutePath(prjPath, project->GetIncludeDirs(), dirs);
for (size_t i = 0; i < buildTargets.GetCount(); ++i)
{
ProjectBuildTarget* tgt = project->GetBuildTarget(buildTargets[i]);
// Do not try to operate include directories if the target is not for this platform
#if 0 //TODO
if ( !m_CCEnablePlatformCheck
|| (m_CCEnablePlatformCheck && tgt->SupportsCurrentPlatform()) )
{
#endif
GetAbsolutePath(prjPath, tgt->GetIncludeDirs(), dirs);
#if 0 //TODO
}
#endif
}
// if a path has prefix with the project's path, it is a local include search dir
// other wise, it is a system level include search dir, we try to collect all the system dirs
wxArrayString sysDirs;
for (size_t i = 0; i < dirs.GetCount();)
{
if (dirs[i].StartsWith(prjPath))
++i;
else
{
#if 0 //TODO
wxCriticalSectionLocker locker(m_SystemHeadersThreadCS);
if (m_SystemHeadersMap.find(dirs[i]) == m_SystemHeadersMap.end())
sysDirs.Add(dirs[i]);
#endif
dirs.RemoveAt(i);
}
}
if (!sysDirs.IsEmpty())
{
#if 0 //TODO
SystemHeadersThread* thread = new SystemHeadersThread(this, &m_SystemHeadersThreadCS, m_SystemHeadersMap, sysDirs);
m_SystemHeadersThreads.push_back(thread);
if (!m_SystemHeadersThreads.front()->IsRunning() && m_NativeParser.Done())
thread->Run();
#endif
}
dirs.Sort(CompareStringLen);
return dirs;
}
wxArrayString& ClangCodeCompletion::GetSystemIncludeDirs(cbProject* project, bool force)
{
static cbProject* lastProject = nullptr;
static wxArrayString incDirs;
if (!force && project == lastProject) // force == false means we can use the cached dirs
return incDirs;
else
{
incDirs.Clear();
lastProject = project;
}
wxString prjPath;
if (project)
prjPath = project->GetCommonTopLevelPath();
#if 0
ParserBase* parser = m_NativeParser.GetParserByProject(project);
if (!parser)
return incDirs;
incDirs = parser->GetIncludeDirs();
// we try to remove the dirs which belong to the project
for (size_t i = 0; i < incDirs.GetCount();)
{
if (incDirs[i].Last() != wxFILE_SEP_PATH)
incDirs[i].Append(wxFILE_SEP_PATH);
// the dirs which have prjPath prefix are local dirs, so they should be removed
if (project && incDirs[i].StartsWith(prjPath))
incDirs.RemoveAt(i);
else
++i;
}
#endif
return incDirs;
}
void ClangCodeCompletion::GetAbsolutePath(const wxString& basePath, const wxArrayString& targets, wxArrayString& dirs)
{
for (size_t i = 0; i < targets.GetCount(); ++i)
{
wxString includePath = targets[i];
Manager::Get()->GetMacrosManager()->ReplaceMacros(includePath);
wxFileName fn(includePath, wxEmptyString);
if (fn.IsRelative())
{
const wxArrayString oldDirs = fn.GetDirs();
fn.SetPath(basePath);
for (size_t j = 0; j < oldDirs.GetCount(); ++j)
fn.AppendDir(oldDirs[j]);
}
const wxString path = fn.GetFullPath();
if (dirs.Index(path) == wxNOT_FOUND)
dirs.Add(path);
}
}