Repository navigation
Expand file tree
/
Copy patheditor.cpp
More file actions
1559 lines (1370 loc) · 45.4 KB
/
Copy patheditor.cpp
File metadata and controls
1559 lines (1370 loc) · 45.4 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
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
#include "editor.h"
#include "properties.h"
/* C and Spin highlighters prove more languages can be added easily.
*/
#include "highlightC.h"
#include "highlightSpin.h"
#include "mainwindow.h"
#define MAINWINDOW MainWindow
Editor::Editor(QWidget *parent) : QPlainTextEdit(parent)
{
mainwindow = parent;
propDialog = static_cast<MAINWINDOW*>(mainwindow)->propDialog;
spinParser = 0;
ctrlPressed = false;
isSpin = false;
expectAutoComplete = false;
lineNumberArea = new LineNumberArea(this);
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
connect(parent, SIGNAL(highlightCurrentLine(QColor)), this, SLOT(highlightCurrentLine(QColor)));
updateLineNumberAreaWidth(0);
highlighter = 0;
setHighlights();
setMouseTracking(true);
setCenterOnScroll(true);
setWordWrapMode(QTextOption::NoWrap);
QPalette p = this->palette();
QColor pal(255,248,192);
p.setColor(QPalette::Active, QPalette::Base, pal);
p.setColor(QPalette::Inactive, QPalette::Base, pal);
this->setPalette(p);
// this must be a pointer otherwise we can't control the position.
cbAuto = new QComboBox(this);
cbAuto->hide();
}
Editor::~Editor()
{
cbAuto->clear();
delete cbAuto;
delete highlighter;
delete lineNumberArea;
}
void Editor::initSpin(SpinParser *parser)
{
spinParser = parser;
}
SpinParser *Editor::getSpinParser()
{
return spinParser;
}
void Editor::setHighlights(QString filename)
{
fileName = filename;
if(highlighter) {
delete highlighter;
highlighter = 0;
}
if(filename.isEmpty() == false) {
if(filename.contains(".spin",Qt::CaseInsensitive)) {
highlighter = new SpinHighlighter(this->document(), propDialog);
isSpin = true;
}
else {
highlighter = new HighlightC(this->document(), propDialog);
isSpin = false;
}
}
}
void Editor::setLineNumber(int num)
{
QTextCursor cur = textCursor();
cur.setPosition(0);
cur.movePosition(QTextCursor::Down,QTextCursor::MoveAnchor,num-1);
setTextCursor(cur);
}
#undef SPIN_AUTOCON
void Editor::keyPressEvent (QKeyEvent *e)
{
if((QApplication::keyboardModifiers() & Qt::CTRL)) {
if(e->key() == Qt::Key_S) {
emit saveEditorFile();
return;
}
}
/* source browser */
if((QApplication::keyboardModifiers() & Qt::CTRL) && ctrlPressed == false) {
ctrlPressed = true;
QTextCursor tcur = this->textCursor();
QTextCursor cur = this->cursorForPosition(mousepos);
tcur.select(QTextCursor::WordUnderCursor);
cur.select(QTextCursor::WordUnderCursor);
QString text = cur.selectedText();
if(tcur.selectedText().compare(text)) {
QPlainTextEdit::keyPressEvent(e);
return;
}
#if 0 // highlighting like this causes editor trouble. don't do it.
if(text.length() > 0) {
//qDebug() << "keyPressEvent ctrlPressed " << text;
if(static_cast<MAINWINDOW*>(mainwindow)->isTagged(text))
this->setTextCursor(cur);
}
#endif
QPlainTextEdit::keyPressEvent(e);
return;
}
/* if F1 do help.
* if tab or shift tab, do block shift
* if "." do auto complete
*/
int key = e->key();
if((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
if(QToolTip::isVisible() && propDialog->getSpinSuggestEnable()) {
useSpinSuggestion(key);
}
else {
#ifdef ENABLE_AUTO_ENTER
if(autoEnterColumn() == 0)
QPlainTextEdit::keyPressEvent(e);
#endif
}
}
#ifdef ENABLE_AUTO_ENTER
else if(key == Qt::Key_BraceRight) {
if(braceMatchColumn() == 0)
QPlainTextEdit::keyPressEvent(e);;
}
#endif
/* if F1 get word under mouse and pass to findSymbolHelp. no word is ok too. */
else if(key == Qt::Key_F1) {
contextHelp();
QPlainTextEdit::keyPressEvent(e);
}
#if defined(SPIN_AUTOCOMPLETE)
#ifdef SPIN_AUTOCON
/* #-auto complete */
else if(key == Qt::Key_NumberSign) {
if(isSpin) {
if(spinAutoCompleteCON() == 0)
QPlainTextEdit::keyPressEvent(e);
}
else {
QPlainTextEdit::keyPressEvent(e);
}
}
#endif
/* dot-auto complete */
else if(key == Qt::Key_Period && propDialog->getAutoCompleteEnable()) {
if(isSpin) {
if(spinAutoComplete() == 0)
QPlainTextEdit::keyPressEvent(e);
}
else {
QPlainTextEdit::keyPressEvent(e);
}
}
#endif
/* if TAB key do block move */
else if(key == Qt::Key_Tab || key == Qt::Key_Backtab) {
if(QToolTip::isVisible() && propDialog->getSpinSuggestEnable()) {
useSpinSuggestion(key);
}
else {
tabBlockShift();
}
}
/* if key up/down*/
else if(key == Qt::Key_Up || key == Qt::Key_Down) {
/* if tooltip, higlight the selected entry */
if(QToolTip::isVisible() && propDialog->getSpinSuggestEnable()) {
selectSpinSuggestion(key);
}
else {
QPlainTextEdit::keyPressEvent(e);
}
}
else {
QPlainTextEdit::keyPressEvent(e);
// If key is escape, don't show info.
if(key == Qt::Key_Escape) {
QToolTip::hideText();
return;
}
else if(key == Qt::Key_Backspace) {
QToolTip::hideText();
return;
}
else if(key == Qt::Key_Space) {
QToolTip::hideText();
return;
}
if(propDialog->getSpinSuggestEnable()) {
spinSuggest(key);
}
}
}
void Editor::spinSuggest(int key)
{
QTextCursor cur = textCursor();
int oldpos = cur.position();
cur.movePosition(QTextCursor::StartOfWord,QTextCursor::QTextCursor::KeepAnchor);
QString text = cur.selectedText();
if(text.length() < 1) {
return;
}
cur.select(QTextCursor::WordUnderCursor);
QString word = cur.selectedText();
if(text.compare(word)) {
return;
}
QPoint popPoint = mapToGlobal(keyPopPoint(cur));
int newpos = cur.position();
cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, oldpos-newpos);
QStringList toolTextList;
if(text.length() > 2) {
int added = 0;
QStringList list = spinParser->spinSymbols(fileName,"");
foreach(QString s, list) {
s = s.mid(s.indexOf("\t")+1); // skip type field
if(s.contains(text,Qt::CaseInsensitive)) {
QRegExp rx("([ \t]+)");
if(s.contains(rx)) // replace multiple space/tab with space
s = s.replace(rx, " ");
int pos = s.indexOf(text,0,Qt::CaseInsensitive);
while(pos > 0 && s[pos-1] != ' ' && s[pos-1] != '\t')
pos--; // find beginning of word
s = s.mid(pos);
s = s.trimmed();
// We can't really depend on the pub/pri tags for deciding what to do
// with var names. Treat everything as a simple symbol.
s = s.mid(0, s.indexOf(QRegExp("[ \[,()<>:=+-*/!@#$%^&|\\t\\r\\n]")));
s = s.trimmed();
if(s.compare(word) != 0) {
toolTextList.append(s);
added++;
}
}
}
if(!added) {
QToolTip::hideText();
}
if(toolTextList.count() > 0) {
QString tooltext("");
#ifdef QT5
toolTextList.sort(Qt::CaseInsensitive);
#else
toolTextList.sort();
#endif
for(int n = 0; n < toolTextList.count(); n++) {
if(n) {
if(QString(toolTextList[n-1]).compare(toolTextList[n],Qt::CaseInsensitive))
tooltext += "<br/>"+toolTextList[n];
} else {
tooltext = "<b>"+toolTextList[n]+"</b>";
}
}
QToolTip::showText(popPoint, tooltext, this, this->rect());
}
}
}
void Editor::selectSpinSuggestion(int key)
{
QTextCursor cur = textCursor();
QString s = QToolTip::text();
QStringList list = s.split(QRegExp("<br/>"));
int len = list.count();
int bnum = 0;
for(int n = 0; n < len && len > 1; n++) {
s = list[n];
if(s.contains("<b>")) {
s = s.replace(QRegExp("<b>|</b>"),"");
list[n] = s;
if(key == Qt::Key_Up) {
if(!n) n = len;
bnum = (n-1) % len;
}
else if(key == Qt::Key_Down) {
bnum = (n+1) % len;
}
}
}
QString tooltext;
for(int n = 0; n < len && len > 1; n++) {
if(n) {
tooltext += "<br/>";
}
if(n == bnum) {
tooltext += "<b>"+list[n]+"</b>";
}
else {
tooltext += list[n];
}
}
cur.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
QPoint pop = mapToGlobal(keyPopPoint(cur));
QToolTip::showText(pop,tooltext,this,rect());
}
void Editor::useSpinSuggestion(int key)
{
QTextCursor cur = textCursor();
QString s = QToolTip::text();
QStringList list = s.split(QRegExp("<br/>"));
if(list.count() < 2) { // don't insert multi-line
cur.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
s = s.replace(QRegExp("<b>|</b>"),"");
cur.insertText(s);
setTextCursor(cur);
}
else {
int n;
int len = list.count();
for(n = 0; n < len && len > 1; n++) {
s = list[n];
if(s.contains("<b>")) {
s = s.replace(QRegExp("<b>|</b>"),"");
break;
}
}
if(n <= len) {
cur.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
cur.insertText(s);
setTextCursor(cur);
}
}
// if the key is enter and end of line, insert a block
if(key == Qt::Key_Enter || key == Qt::Key_Return) {
if(cur.atBlockEnd()) {
cur.insertBlock();
setTextCursor(cur);
}
}
// if the key is a tab, insert a space
if(key == Qt::Key_Tab) {
int spaces = propDialog->getTabSpaces();
int n = cur.columnNumber();
int pos = cur.position();
QChar ch = this->toPlainText().at(pos);
if(ch == Qt::Key_Space || ch == Qt::Key_Tab) {
n++;
}
for(; n % spaces; n++) {
cur.insertText(" ");
}
//cur.insertText(" "); // insert a single space
setTextCursor(cur);
}
QToolTip::hideText();
}
QPoint Editor::keyPopPoint(QTextCursor cursor)
{
int ht = fontMetrics().height();
int wd = fontMetrics().width(QLatin1Char('9'));
int col = cursor.columnNumber();
int row = cursor.blockNumber()+2; // show just below line
QTextBlock block = firstVisibleBlock();
int top = block.firstLineNumber();
QPoint pt = QPoint(lineNumberAreaWidth()+col*wd,(row-top)*ht);
//qDebug() << "keyPopPoint" << pt.x() << pt.y();
return pt;
}
void Editor::keyReleaseEvent (QKeyEvent *e)
{
QTextCursor cur = this->textCursor();
if(ctrlPressed) {
ctrlPressed = false;
cur.setPosition(cur.anchor());
//qDebug() << "keyReleaseEvent ctrlReleased";
}
QPlainTextEdit::keyReleaseEvent(e);
if(expectAutoComplete) {
#if defined(Q_OS_MAC)
if(e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)
this->undo();
#endif
expectAutoComplete = false;
}
}
void Editor::clearCtrlPressed()
{
ctrlPressed = false;
}
void Editor::mouseMoveEvent (QMouseEvent *e)
{
mousepos = e->pos();
//qDebug() << "mouseMoveEvent " << mousepos.x() << "," << mousepos.y();
QPlainTextEdit::mouseMoveEvent(e);
}
void Editor::mousePressEvent (QMouseEvent *e)
{
if(ctrlPressed) {
qDebug() << "mousePressEvent ctrlPressed (not implemented yet)";
//static_cast<MAINWINDOW*>(mainwindow)->findDeclaration(e->pos());
ctrlPressed = false;
}
QPlainTextEdit::mousePressEvent(e);
}
void Editor::mouseDoubleClickEvent (QMouseEvent *e)
{
QPlainTextEdit::mouseDoubleClickEvent(e);
static_cast<MAINWINDOW*>(mainwindow)->findMultilineComment(e->pos());
mousepos = e->pos();
}
int Editor::autoEnterColumn()
{
if(fileName.endsWith(".spin", Qt::CaseInsensitive)) {
return autoEnterColumnSpin();
}
else if(fileName.endsWith(".c", Qt::CaseInsensitive) ||
fileName.endsWith(".cpp", Qt::CaseInsensitive) ||
fileName.endsWith(".h", Qt::CaseInsensitive)
) {
return autoEnterColumnC();
}
return 0;
}
int Editor::autoEnterColumnC()
{
QTextCursor cur = this->textCursor();
if(cur.selectedText().length() > 0) {
return 0;
}
int line = cur.blockNumber();
int col = cur.columnNumber();
cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,col);
QString text = cur.selectedText();
cur.clearSelection();
if(text.length() == 0) return 0;
int stop = -1;
int indent = -1;
int star = -1;
int slcm = text.indexOf("//");
// don't indent under closed comment
if(text.indexOf("*/") > -1) {
return 0;
}
if(isCommentOpen(line)) {
star = stop = text.indexOf("*");
}
if(stop < 0 && slcm > -1) {
stop = slcm;
}
if(stop < 0 && text.lastIndexOf("{") == text.length()-1) {
indent = propDialog->getTabSpaces();
}
qDebug() << text;
/* start a single undo/redo operation */
cur.beginEditBlock();
cur.insertBlock();
for(int n = 0; n <= stop || isspace(text[n].toLatin1()); n++) {
if(n == star) {
cur.insertText("*");
}
else if(n == slcm) {
cur.insertText("// ");
}
else {
cur.insertText(" ");
}
}
if(indent > 0) {
for(int n = 0; n < indent; n++) {
cur.insertText(" ");
}
}
this->setTextCursor(cur);
/* end a single undo/redo operation */
cur.endEditBlock();
return 1;
}
int Editor::autoEnterColumnSpin()
{
QTextCursor cur = this->textCursor();
if(cur.selectedText().length() > 0) {
return 0;
}
int col = cur.columnNumber();
cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,col);
QString text = cur.selectedText();
cur.clearSelection();
QRegExp sections("\\b(con|dat|pub|pri|obj|var)\\b");
sections.setCaseSensitivity(Qt::CaseInsensitive);
if((text.length() == 0 && col == 0) || text.indexOf(sections) == 0) {
cur.beginEditBlock();
cur.insertBlock();
for(int n = propDialog->getTabSpaces(); n > 0; n--) {
cur.insertText(" ");
}
this->setTextCursor(cur);
/* end a single undo/redo operation */
cur.endEditBlock();
return 1;
}
// handle indent for spin
int stop = -1;
int indent = -1;
int slcm = text.indexOf("'");
if(slcm > -1) {
stop = slcm;
}
//qDebug() << text;
/* start a single undo/redo operation */
cur.beginEditBlock();
cur.insertBlock();
//cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
for(int n = 0; n <= stop || isspace(text[n].toLatin1()); n++) {
if(n == slcm) {
if(text.indexOf("''") > -1)
cur.insertText("''");
else
cur.insertText("'");
}
else {
cur.insertText(" ");
}
}
if(indent > 0) {
for(int n = 0; n < indent; n++) {
cur.insertText(" ");
}
}
this->setTextCursor(cur);
/* end a single undo/redo operation */
cur.endEditBlock();
return 1;
}
bool Editor::isCommentOpen(int line)
{
// find out if there is a brace mismatch
QString s = this->toPlainText();
QStringList sl = s.split("\n",QString::SkipEmptyParts);
int length = sl.length();
int como = 0;
int comc = 0;
for(int n = 0; n < length; n++) {
QString s = sl.at(n);
if(s.contains("/*")) {
como++;
}
if(s.contains("*/")) {
comc++;
}
if(line == n && como > comc)
return true;
}
return !(como == comc);
}
bool Editor::isSpinCommentOpen(int line)
{
// find out if there is a brace mismatch
QString s = this->toPlainText();
QStringList sl = s.split("\n",QString::SkipEmptyParts);
int length = sl.length();
int como = 0;
int comc = 0;
for(int n = 0; n < length; n++) {
QString s = sl.at(n);
if(s.contains("{")) {
como++;
}
if(s.contains("}")) {
comc++;
}
if(line == n && como > comc)
return true;
}
return !(como == comc);
}
int Editor::braceMatchColumn()
{
int position = this->textCursor().columnNumber();
int indent = propDialog->getTabSpaces();
if(position <= indent)
return 0;
QTextCursor cur = this->textCursor();
/* if this line has a brace, ignore */
cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::EndOfLine,QTextCursor::KeepAnchor);
QString text = cur.selectedText();
cur.clearSelection();
if(text.contains("{"))
return 0;
// find out if there is a brace mismatch
QString s = this->toPlainText();
QStringList sl = this->toPlainText().split(QRegExp("/\\*.*\\*/"));
s = sl.join("\n");
sl = s.split("\n",QString::SkipEmptyParts);
int length = sl.length();
int braceo = 0;
int bracec = 0;
for(int n = 0; n < length; n++) {
QString s = sl.at(n);
if(s.contains("{")) {
braceo++;
}
if(s.contains("}")) {
bracec++;
}
}
if(braceo == bracec) {
return 0;
}
// if brace mismatch, set closing brace
cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::Up,QTextCursor::MoveAnchor,1);
cur.movePosition(QTextCursor::Down,QTextCursor::KeepAnchor,1);
text = cur.selectedText();
cur.movePosition(QTextCursor::StartOfLine,QTextCursor::MoveAnchor);
if(text.length() == 0)
return 0;
//cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor,text.length());
setTextCursor(cur);
cur.clearSelection();
/* start a single undo/redo operation */
cur.beginEditBlock();
for(int n = 0; n < position-indent; n++) {
cur.insertText(" ");
}
cur.insertText("}");
this->setTextCursor(cur);
/* end a single undo/redo operation */
cur.endEditBlock();
return 1;
}
/**
* This is the beginning of the AUTO_COMPLETE section of the editor.
* It will only be enabled if SPIN_AUTOCOMPLETE is defined in the .pro file.
* SPIN_AUTOCOMPLETE is also known as "spinny-sense", thank you Peter Parker.
*
* SPIN_AUTOCOMPLETE works like this:
* - Press dot (.) in any non-comment area of the source to show
* OBJ, CON, PUB, PRI, VAR, and DAT references.
* If SPIN_AUTOCON is defined, press # in the editor to show CON values.
* - Press escape to not add any item listed.
* - Press dot (.) to add a dot.
* - Scroll to the item you want and press enter to add that item.
* Tab to select is not available at the moment. Later, "tab" may be used
* instead of enter if the cbAuto is a sub-class of QComboBox.
*/
QString Editor::spinPrune(QString s)
{
QRegExp re("\\b(byte|long|word|org)\\b");
re.setCaseSensitivity(Qt::CaseInsensitive);
s = s.mid(s.indexOf("\t")+1);
if(s.lastIndexOf(")") > 0)
s = s.mid(0,s.lastIndexOf(")")+1);
if(s.lastIndexOf(":") > 0)
s = s.mid(0,s.lastIndexOf(":"));
if(s.lastIndexOf("|") > 0)
s = s.mid(0,s.lastIndexOf("|"));
if(s.lastIndexOf("[") > 0)
s = s.mid(0,s.lastIndexOf("["));
if(s.lastIndexOf("=") > 0)
s = s.mid(0,s.lastIndexOf("="));
if(s.indexOf(re) > 0)
s = s.mid(0,s.indexOf(re));
return s.trimmed();
}
QString Editor::deletePrefix(QString s)
{
QRegExp re("\\b(byte|long|word)\\b");
re.setCaseSensitivity(Qt::CaseInsensitive);
QRegExp sections("\\b(con|dat|pub|pri|obj|var)\\b");
sections.setCaseSensitivity(Qt::CaseInsensitive);
if(s.indexOf(sections) == 0) {
s = s.mid(s.indexOf(sections)+4);
}
else if(s.indexOf(re) == 0) {
s = s.mid(s.indexOf(re)+5);
}
return s;
}
int Editor::addAutoItem(QString type, QString s)
{
int width = 0;
QIcon icon;
bool split = false;
QString ch = type.at(0);
if(ch.compare("c") == 0) {
icon.addFile(":/images/con2.png");
split = true;
} else if(ch.compare("e") == 0) {
icon.addFile(":/images/con2.png");
split = true;
} else if(ch.compare("o") == 0) {
icon.addFile(":/images/blks.png");
} else if(ch.compare("p") == 0) {
icon.addFile(":/images/pri2.png");
} else if(ch.compare("f") == 0) {
icon.addFile(":/images/pub2.png");
} else if(ch.compare("v") == 0) {
icon.addFile(":/images/var2.png");
split = true;
} else if(ch.compare("x") == 0) {
icon.addFile(":/images/dat2.png");
split = true;
}
QStringList lst;
if(deletePrefix(s).length() != 0) {
s = spinPrune(deletePrefix(s));
}
if(split)
lst = s.split(",", QString::SkipEmptyParts);
else
lst.append(s);
foreach (QString name, lst) {
int len = cbAuto->count();
int skip = 0;
for(int n = 0; n < len; n++) {
// linear search ok for short list ... maybe optimize with hash later.
if(cbAuto->itemText(n).compare(name) == 0)
skip++;
}
if(!skip) {
name = name.trimmed();
if(name.length() > width)
width = name.length();
cbAuto->addItem(icon,name);
}
}
return width;
}
void Editor::spinAutoShow(int width)
{
QPoint pt = keyPopPoint(textCursor());
/*
* Can't seem to control width without some strange after-effect.
* This is how it would be done though.
*/
int fw = cbAuto->fontMetrics().width(QLatin1Char('9'));
int fh = cbAuto->fontMetrics().height();
cbAuto->setGeometry(pt.x(), pt.y(), (width+10)*fw, fh*10);
//
expectAutoComplete = true;
cbAuto->move(pt.x(), pt.y());
cbAuto->setFrame(false);
cbAuto->setEditable(true);
cbAuto->setMaxVisibleItems(10);
cbAuto->setAutoCompletion(true);
cbAuto->showPopup();
}
bool Editor::isNotAutoComplete()
{
QTextCursor cur = this->textCursor();
cur.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
if(cur.selectedText().contains("'")) {
return true;
}
while(cur.atStart() == false) {
if(cur.selectedText().contains("{")) {
if(cur.selectedText().contains("}") != true)
return true;
else
return false;
}
cur.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
}
QTextCursor qcur = this->textCursor();
int column = qcur.columnNumber();
qcur.select(QTextCursor::LineUnderCursor);
QString text = qcur.selectedText();
if(text.contains("\"")) {
int instring = 0;
for(int n = 0; n < text.length(); n++) {
if(text.at(n) == '\"')
instring ^= 1;
if(instring && (n == column))
return true;
}
}
return false;
}
QString Editor::selectAutoComplete()
{
QTextCursor cur = this->textCursor();
int col;
char ch;
QString text = cur.selectedText();
cur.removeSelectedText();
while((col = cur.columnNumber()) > 0) {
cur.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor,1);
text = cur.selectedText();
ch = text.at(0).toLatin1();
if(isspace(ch))
break;
}
return cur.selectedText().trimmed();
}
int Editor::spinAutoComplete()
{
if(isNotAutoComplete())
return 0;
QString text = selectAutoComplete();
// if(text.compare("#") == 0) // would like to autocomplete on previous object
/*
* we have an object name. get object info
*/
if(text.length() > 0) {
qDebug() << "keyPressEvent object dot pressed" << text;
QStringList list = spinParser->spinSymbols(fileName,text);
if(list.count() == 0)
return 0;
cbAuto->clear();
// we depend on index item 0 to be the auto-start key
cbAuto->addItem(".");
if(list.count() > 0) {
int width = 0;
//list.sort(); // let the parser do this
for(int j = 0; j < list.count(); j++) {
QString s = list[j];
QString type = s;
#ifdef SPIN_AUTOCON
if(type.at(0) == 'c') // don't show con for object. - # shows con
continue;
if(type.at(0) == 'e') // don't show enum for object. - # shows enums
continue;
#endif
if(type.at(0) == 'o') // don't show obj for object.
continue;
if(type.at(0) == 'p') // don't show pri for object.
continue;
if(type.at(0) == 'v') // don't show var for object.
continue;
if(type.at(0) == 'x') // don't show dat for object.
continue;
s = spinPrune(s);
int w = addAutoItem(type, s);
if(w > width) width = w;
}
connect(cbAuto, SIGNAL(activated(int)), this, SLOT(cbAutoSelected0insert(int)));
spinAutoShow(width);
}
return 1;
}
/*
* no object name. get local info
*/
else {
//qDebug() << "keyPressEvent local dot pressed";
QStringList list = spinParser->spinSymbols(fileName,"");
if(list.count() == 0)
return 0;
cbAuto->clear();
cbAuto->addItem(".");
if(list.count() > 0) {
int width = 0;
// always put objects on top
for(int j = 0; j < list.count(); j++) {
QString s = list[j];
QString type = s;
if(type.at(0) == 'o') {
list.removeAt(j);
list.insert(0,s);
}
}
// add all elements
for(int j = 0; j < list.count(); j++) {
QString s = list[j];
QString type = s;
#ifdef SPIN_AUTOCON
if(type.at(0) == 'c')
continue;
if(type.at(0) == 'e')
continue;
#endif
s = spinPrune(s);
int w = addAutoItem(type, s);
if(w > width) width = w;
}
connect(cbAuto, SIGNAL(activated(int)), this, SLOT(cbAutoSelected(int)));
spinAutoShow(width);
}
return 1;
}
return 0;
}
int Editor::spinAutoCompleteCON()
{
#ifdef SPIN_AUTOCON
QString text = selectAutoComplete();
if(text.length() > 0) {
connect(cbAuto, SIGNAL(activated(int)), this, SLOT(cbAutoSelected0insert(int)));
qDebug() << "keyPressEvent # pressed" << text;
QStringList list = spinParser->spinConstants(fileName,text);
if(list.count() == 0)
return 0;
cbAuto->clear();
// we depend on index item 0 to be the auto-start key