forked from bruderstein/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleDialog.cpp
More file actions
1100 lines (926 loc) · 28.9 KB
/
ConsoleDialog.cpp
File metadata and controls
1100 lines (926 loc) · 28.9 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 "stdafx.h"
#include "ConsoleDialog.h"
#include "Scintilla.h"
#include "SciLexer.h"
#include "resource.h"
#include "PythonConsole.h"
#include "Notepad_plus_msgs.h"
#include "PluginInterface.h"
#include "Docking.h"
#include "WcharMbcsConverter.h"
#include "MenuManager.h"
#include "PythonScript.h"
#include "ConfigFile.h"
namespace NppPythonScript
{
ConsoleDialog::ConsoleDialog() :
DockingDlgInterface(IDD_CONSOLE),
m_data(new tTbData),
m_scintilla(NULL),
m_hInput(NULL),
m_hCombo(NULL),
m_console(NULL),
m_currentPrompt(">>> "),
m_standardPrompt(">>> "),
m_continuePrompt("... "),
m_originalInputWndProc(NULL),
m_hTabIcon(NULL),
m_runButtonIsRun(true),
m_hContext(NULL),
m_nppData{0,0,0},
m_colorOutput(false),
m_user_color(-1),
m_customizeConsoleErrorColor(false),
m_customConsoleErrorColor(-1)
{}
ConsoleDialog::~ConsoleDialog()
{
if (m_scintilla)
{
::SendMessage(_hParent, NPPM_DESTROYSCINTILLAHANDLE_DEPRECATED, 0, reinterpret_cast<LPARAM>(m_scintilla));
m_scintilla = NULL;
}
if (m_data)
{
delete m_data;
m_data = NULL;
}
if (m_hTabIcon)
{
::DestroyIcon(m_hTabIcon);
m_hTabIcon = NULL;
}
if (m_hContext)
{
::DestroyMenu(m_hContext);
m_hContext = NULL;
}
// To please Lint, let's NULL these handles and pointers
m_hInput = NULL;
m_hCombo = NULL;
m_console = NULL;
}
WNDPROC ConsoleDialog::s_originalScintillaWndProc;
void ConsoleDialog::initDialog(HINSTANCE hInst, NppData& nppData, ConsoleInterface* console)
{
DockingDlgInterface::init(hInst, nppData._nppHandle);
auto getColorSetting = [](const TCHAR* settingName, int& output) {
try {
output = stoi(ConfigFile::getInstance()->getSetting(settingName));
return true;
}
catch (const std::exception&) {
return false;
}
};
m_colorOutput = getColorSetting(_T("COLORIZEOUTPUT"), m_user_color) && m_user_color > -1;
m_customizeConsoleErrorColor = getColorSetting(_T("CUSTOMCONSOLEERRORCOLOR"), m_customConsoleErrorColor) && m_customConsoleErrorColor > -1;
m_standardPrompt = ConfigFile::getInstance()->getSetting(_T("ADDEXTRALINETOOUTPUT")) == _T("1") ? m_standardPrompt.insert(0, "\n") : m_standardPrompt;
m_currentPrompt = m_standardPrompt;
//Window::init(hInst, nppData._nppHandle);
createOutputWindow(nppData._nppHandle);
m_console = console;
m_hContext = CreatePopupMenu();
MENUITEMINFO mi{};
mi.cbSize = sizeof(mi);
mi.fMask = MIIM_ID | MIIM_STRING;
mi.fType = MFT_STRING;
mi.fState = MFS_ENABLED;
mi.wID = 1;
mi.dwTypeData = _T("Select all");
InsertMenuItem(m_hContext, 0, TRUE, &mi);
mi.wID = 2;
mi.dwTypeData = _T("Copy");
InsertMenuItem(m_hContext, 1, TRUE, &mi);
mi.wID = 3;
mi.dwTypeData = _T("Clear");
InsertMenuItem(m_hContext, 3, TRUE, &mi);
m_nppData = nppData;
}
INT_PTR CALLBACK ConsoleDialog::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
{
SetParent(m_scintilla, _hSelf);
ShowWindow(m_scintilla, SW_SHOW);
m_hCombo = ::GetDlgItem(_hSelf, IDC_COMBO1);
m_hInput = FindWindowEx(m_hCombo, NULL, L"Edit", NULL);
HFONT hCourier = CreateFont(14,0,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, FIXED_PITCH, _T("Courier New"));
if (hCourier != NULL)
{
SendMessage(m_hInput, WM_SETFONT, reinterpret_cast<WPARAM>(hCourier), TRUE);
SendMessage(::GetDlgItem(_hSelf, IDC_PROMPT), WM_SETFONT, reinterpret_cast<WPARAM>(hCourier), TRUE);
}
// Subclass the Input box from the combobox
::SetWindowLongPtr(m_hInput, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
m_originalInputWndProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(m_hInput, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(ConsoleDialog::inputWndProc)));
// Subclass Scintilla
s_originalScintillaWndProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(m_scintilla, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&ConsoleDialog::scintillaWndProc)));
::SetFocus(m_hInput);
return FALSE;
}
case WM_SIZE:
MoveWindow(m_scintilla, 0, 0, LOWORD(lParam), HIWORD(lParam)-30, TRUE);
MoveWindow(::GetDlgItem(_hSelf, IDC_PROMPT), 0, HIWORD(lParam)-22, 30, 25, TRUE);
MoveWindow(m_hCombo, 30, HIWORD(lParam)-25, LOWORD(lParam)-85, 50, TRUE);
MoveWindow(::GetDlgItem(_hSelf, IDC_RUN), LOWORD(lParam)-50, HIWORD(lParam)-25, 50, 21, TRUE);
// ::SendMessage(m_scintilla, WM_SIZE, 0, MAKEWORD(LOWORD(lParam) - 10, HIWORD(lParam) - 30));
return FALSE;
case WM_CONTEXTMENU:
{
MENUITEMINFO mi{};
mi.cbSize = sizeof(mi);
mi.fMask = MIIM_STATE;
if (0 == (callScintilla(SCI_GETSELECTIONSTART) - callScintilla(SCI_GETSELECTIONEND)))
{
mi.fState = MFS_DISABLED;
}
else
{
mi.fState = MFS_ENABLED;
}
SetMenuItemInfo(m_hContext, 2, FALSE, &mi);
// Thanks MS for corrupting the value of BOOL. :-/
// From the documentation (http://msdn.microsoft.com/en-us/library/ms648002.aspx):
//
// If you specify TPM_RETURNCMD in the uFlags parameter, the return value is the menu-item
// identifier of the item that the user selected. If the user cancels the menu without making
// a selection, or if an error occurs, then the return value is zero.
INT cmdID = (INT)TrackPopupMenu(m_hContext, TPM_RETURNCMD, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, _hSelf, NULL);
switch(cmdID)
{
case 1: // Select All
callScintilla(SCI_SELECTALL);
break;
case 2: // Copy
callScintilla(SCI_COPY);
break;
case 3: // Clear
clearText();
break;
default:
break;
}
}
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_RUN)
{
if (m_runButtonIsRun)
{
runStatement();
}
else
{
assert(m_console != NULL);
if (m_console)
{
m_console->stopStatement();
}
}
//MessageBox(NULL, _T("Command") , _T("Python Command"), 0);
return FALSE;
}
else if (LOWORD(wParam) == IDCANCEL)
{
::SetFocus(getCurrScintilla());
return FALSE;
}
break;
case WM_SETFOCUS:
//giveInputFocus();
OutputDebugString(_T("ConsoleDialog SetFocus\r\n"));
return FALSE;
case WM_ACTIVATE:
if (wParam == WA_ACTIVE)
{
OutputDebugString(_T("ConsoleDialog WM_ACTIVATE WA_ACTIVE\r\n"));
giveInputFocus();
}
break;
case WM_CHILDACTIVATE:
OutputDebugString(_T("ConsoleDialog WM_CHILDACTIVATE\r\n"));
giveInputFocus();
break;
case WM_NOTIFY:
{
LPNMHDR nmhdr = reinterpret_cast<LPNMHDR>(lParam);
if (m_scintilla == nmhdr->hwndFrom)
{
switch(nmhdr->code)
{
case SCN_STYLENEEDED:
onStyleNeeded(reinterpret_cast<SCNotification*>(lParam));
return FALSE;
case SCN_HOTSPOTCLICK:
onHotspotClick(reinterpret_cast<SCNotification*>(lParam));
return FALSE;
default:
break;
}
}
break;
}
case WM_SHOWWINDOW:
{
MenuManager::getInstance()->checkShowConsole(wParam);
}
default:
break;
}
return DockingDlgInterface::run_dlgProc(message, wParam, lParam);
}
void ConsoleDialog::historyAdd(const TCHAR *line)
{
if (line && line[0])
{
auto i = ::SendMessage(m_hCombo, CB_FINDSTRINGEXACT, static_cast<WPARAM>(-1), reinterpret_cast<LPARAM>(line));
if (i != CB_ERR) // found
{
::SendMessage(m_hCombo, CB_DELETESTRING, i, 0);
}
i = ::SendMessage(m_hCombo, CB_INSERTSTRING, 0, reinterpret_cast<LPARAM>(line));
::SendMessage(m_hCombo, CB_SETCURSEL, i, 0);
}
}
LRESULT ConsoleDialog::inputWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
ConsoleDialog *dlg = reinterpret_cast<ConsoleDialog*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
return dlg->run_inputWndProc(hWnd, message, wParam, lParam);
}
LRESULT ConsoleDialog::run_inputWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_GETDLGCODE:
if (wParam == VK_RETURN)
return DLGC_WANTMESSAGE;
break;
case WM_KEYDOWN:
// Checking the previous key state in lParam to avoid repeated execution
if (wParam == VK_RETURN && !(lParam & 0x40000000))
{
runStatement();
return FALSE;
}
break;
case WM_SETFOCUS:
OutputDebugString(_T("Input SetFocus\r\n"));
break;
}
return CallWindowProc(m_originalInputWndProc, hWnd, message, wParam, lParam);
}
void ConsoleDialog::runStatement()
{
assert(m_console != NULL);
if (m_console)
{
size_t length = GetWindowTextLength(m_hInput);
TCHAR *buffer = new TCHAR[length + 1];
GetWindowText(m_hInput, buffer, (int)length + 1);
historyAdd(buffer);
std::shared_ptr<char> charBuffer = WcharMbcsConverter::tchar2char(buffer);
delete [] buffer;
writeCmdText(m_currentPrompt.size(), m_currentPrompt.c_str());
writeCmdText(strlen(charBuffer.get()), charBuffer.get());
writeCmdText(1, "\n");
m_console->runStatement(charBuffer.get());
}
}
void ConsoleDialog::stopStatement()
{
assert(m_console != NULL);
if (m_console)
{
m_console->stopStatement();
}
}
void ConsoleDialog::setPrompt(std::string prompt)
{
m_currentPrompt = prompt;
::SetWindowTextA(::GetDlgItem(_hSelf, IDC_PROMPT), (prompt.rfind('\n', 0) == 0) ? prompt.substr(1, prompt.size() - 1).c_str() : prompt.c_str());
}
std::string ConsoleDialog::getStandardPrompt(){ return m_standardPrompt;}
std::string ConsoleDialog::getContinuePrompt(){ return m_continuePrompt;}
void ConsoleDialog::createOutputWindow(HWND hParentWindow)
{
m_scintilla = (HWND)::SendMessage(_hParent, NPPM_CREATESCINTILLAHANDLE, 0, reinterpret_cast<LPARAM>(hParentWindow));
LONG_PTR currentStyle = GetWindowLongPtr(m_scintilla, GWL_STYLE);
SetWindowLongPtr(m_scintilla, GWL_STYLE, currentStyle | WS_TABSTOP);
LONG_PTR exstyles = GetWindowLongPtr(m_scintilla, GWL_EXSTYLE);
if ((exstyles & WS_EX_LAYOUTRTL) == WS_EX_LAYOUTRTL)
{
SetWindowLongPtr(m_scintilla, GWL_EXSTYLE, exstyles & (~WS_EX_LAYOUTRTL));
}
callScintilla(SCI_SETREADONLY, 1, 0);
/* Style bits
* LSB 0 - stderr = 1
* 1 - hotspot
* 2 - warning
* ... to be continued
*/
// Set the codepage to UTF-8
callScintilla(SCI_SETCODEPAGE, 65001);
// 0 is stdout, black text
callScintilla(SCI_STYLESETSIZE, 0 /* = style number */, 8 /* = size in points */);
// 1 is stderr, red text
callScintilla(SCI_STYLESETSIZE, 1 /* = style number */, 8 /* = size in points */);
COLORREF consoleErrorColor = m_customizeConsoleErrorColor ? m_customConsoleErrorColor : RGB(250, 0, 0);
callScintilla(SCI_STYLESETFORE, 1, consoleErrorColor);
// 2 is stdout, black text, underline hotspot
callScintilla(SCI_STYLESETSIZE, 2 /* = style number */, 8 /* = size in points */);
callScintilla(SCI_STYLESETUNDERLINE, 2 /* = style number */, 1 /* = underline */);
callScintilla(SCI_STYLESETHOTSPOT, 2, 1);
// 3 is stderr, red text, underline hotspot
callScintilla(SCI_STYLESETSIZE, 3 /* = style number */, 8 /* = size in points */);
callScintilla(SCI_STYLESETFORE, 3, RGB(250, 0, 0));
callScintilla(SCI_STYLESETUNDERLINE, 3 /* = style number */, 1 /* = underline */);
callScintilla(SCI_STYLESETHOTSPOT, 3, 1);
// 4 stdout warning without hotspot
callScintilla(SCI_STYLESETSIZE, 4 /* = style number */, 8 /* = size in points */);
callScintilla(SCI_STYLESETFORE, 4, RGB(199, 175, 7)); // mucky yellow
// 5 stderr warning without hotspot
callScintilla(SCI_STYLESETSIZE, 5 /* = style number */, 8 /* = size in points */);
callScintilla(SCI_STYLESETFORE, 5, RGB(255, 128, 64)); // orange
// 6 is hotspot, stdout, warning
callScintilla(SCI_STYLESETSIZE, 6 /* = style number */, 8 /* = size in points */);
callScintilla(SCI_STYLESETFORE, 6, RGB(199, 175, 7)); // mucky yellow
callScintilla(SCI_STYLESETUNDERLINE, 6 /* = style number */, 1 /* = underline */);
callScintilla(SCI_STYLESETHOTSPOT, 6, 1);
// 7 is hotspot, stderr, warning
callScintilla(SCI_STYLESETSIZE, 7 /* = style number */, 8 /* = size in points */);
callScintilla(SCI_STYLESETFORE, 7, RGB(255, 128, 64)); // orange
callScintilla(SCI_STYLESETUNDERLINE, 7 /* = style number */, 1 /* = underline */);
callScintilla(SCI_STYLESETHOTSPOT, 7, 1);
// 8 is colored stdout indicator
intptr_t defaultColor = callScintilla(SCI_STYLEGETFORE, 0, 0);
callScintilla(SCI_INDICSETSTYLE, 8 /* = indicator number */, INDIC_TEXTFORE);
callScintilla(SCI_INDICSETFORE, 8, m_colorOutput ? m_user_color : defaultColor); // green
callScintilla(SCI_USEPOPUP, 0);
//set container lexer mode for ilexer5*, before scintilla5 SCLEX_CONTAINER was used
callScintilla(SCI_SETILEXER, 0, 0);
}
LRESULT ConsoleDialog::scintillaWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_GETDLGCODE:
return DLGC_WANTARROWS | DLGC_WANTCHARS;
case WM_SETFOCUS:
OutputDebugString(_T("Scintilla SetFocus\r\n"));
break;
default:
break;
}
return CallWindowProc(s_originalScintillaWndProc, hWnd, message, wParam, lParam);
}
void ConsoleDialog::writeCmdText(size_t length, const char *text)
{
::SendMessage(m_scintilla, SCI_SETREADONLY, 0, 0);
for (idx_t i = 0; i < length; ++i)
{
if (text[i] == '\r')
{
::SendMessage(m_scintilla, SCI_APPENDTEXT, i, reinterpret_cast<LPARAM>(text));
text += i + 1;
length -= i + 1;
i = 0;
}
}
if (length > 0)
{
::SendMessage(m_scintilla, SCI_APPENDTEXT, length, reinterpret_cast<LPARAM>(text));
}
::SendMessage(m_scintilla, SCI_SETREADONLY, 1, 0);
::SendMessage(m_scintilla, SCI_GOTOPOS, ::SendMessage(m_scintilla, SCI_GETLENGTH, 0, 0), 0);
}
void ConsoleDialog::writeText(size_t length, const char *text)
{
::SendMessage(m_scintilla, SCI_SETREADONLY, 0, 0);
for (idx_t i = 0; i < length; ++i)
{
if (text[i] == '\r')
{
::SendMessage(m_scintilla, SCI_APPENDTEXT, i, reinterpret_cast<LPARAM>(text));
text += i + 1;
length -= i + 1;
i = 0;
}
}
if (length > 0)
{
::SendMessage(m_scintilla, SCI_APPENDTEXT, length, reinterpret_cast<LPARAM>(text));
}
::SendMessage(m_scintilla, SCI_SETREADONLY, 1, 0);
::SendMessage(m_scintilla, SCI_GOTOPOS, ::SendMessage(m_scintilla, SCI_GETLENGTH, 0, 0), 0);
}
void ConsoleDialog::writeColoredText(size_t length, const char *text)
{
size_t docLength = (size_t)callScintilla(SCI_GETLENGTH);
callScintilla(SCI_SETREADONLY, 0);
if (length > 0)
{
callScintilla(SCI_APPENDTEXT, length, reinterpret_cast<LPARAM>(text));
}
callScintilla(SCI_SETREADONLY, 1);
callScintilla(SCI_SETINDICATORCURRENT, 8);
callScintilla(SCI_INDICATORFILLRANGE, docLength, length);
callScintilla(SCI_GOTOPOS, docLength+length);
}
void ConsoleDialog::writeError(size_t length, const char *text)
{
size_t docLength = (size_t)callScintilla(SCI_GETLENGTH);
size_t realLength = length;
callScintilla(SCI_SETREADONLY, 0);
for (idx_t i = 0; i < length; ++i)
{
if (text[i] == '\r')
{
if (i)
{
callScintilla(SCI_APPENDTEXT, i, reinterpret_cast<LPARAM>(text));
}
text += i + 1;
length -= i + 1;
realLength--;
i = 0;
}
}
if (length > 0)
{
callScintilla(SCI_APPENDTEXT, length, reinterpret_cast<LPARAM>(text));
}
callScintilla(SCI_SETREADONLY, 1);
callScintilla(SCI_STARTSTYLING, docLength, 0x01);
callScintilla(SCI_SETSTYLING, realLength, 1);
callScintilla(SCI_COLOURISE, docLength, -1);
callScintilla(SCI_GOTOPOS, docLength + realLength);
}
void ConsoleDialog::doDialog()
{
if (!isCreated())
{
create(m_data);
assert(m_data);
if (m_data)
{
// define the default docking behaviour
m_data->uMask = DWS_DF_CONT_BOTTOM | DWS_ICONTAB;
m_data->pszName = _T("Python");
RECT rc{};
rc.bottom = 0;
rc.top = 0;
rc.left = 0;
rc.right = 0;
m_hTabIcon = (HICON)::LoadImage(_hInst, MAKEINTRESOURCE(IDI_PYTHON8), IMAGE_ICON, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADTRANSPARENT);
m_data->hIconTab = m_hTabIcon;
m_data->pszModuleName = PLUGIN_MODULE_NAME; // the plugin filename
m_data->dlgID = 1; // zero based index of the plugin's published funcs array command (here the "Show Console" in the getGeneratedFuncItemArray exported func)
m_data->pszAddInfo = NULL; //_pExProp->szCurrentPath;
m_data->iPrevCont = -1;
m_data->hClient = _hSelf;
m_data->rcFloat = rc;
::SendMessage(_hParent, NPPM_DMMREGASDCKDLG, 0, reinterpret_cast<LPARAM>(m_data));
// Parse the whole doc, in case we've had errors that haven't been parsed yet
callScintilla(SCI_COLOURISE, 0, -1);
}
}
display(true);
}
void ConsoleDialog::hide()
{
display(false);
HWND current_HWND = ::GetFocus();
if (m_hInput == current_HWND || m_scintilla == current_HWND)
{
intptr_t currentView = MAIN_VIEW;
::SendMessage(m_nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tView);
HWND sci = (currentView == MAIN_VIEW) ? m_nppData._scintillaMainHandle : m_nppData._scintillaSecondHandle;
DWORD currentThreadId = GetCurrentThreadId();
DWORD otherThreadId = GetWindowThreadProcessId(sci, NULL);
AttachThreadInput(currentThreadId, otherThreadId, TRUE);
SetFocus(sci);
AttachThreadInput(currentThreadId, otherThreadId, FALSE);
}
}
void ConsoleDialog::runEnabled(bool enabled)
{
//EnableWindow(GetDlgItem(_hSelf, IDC_RUN), enabled);
::SetWindowText(GetDlgItem(_hSelf, IDC_RUN), enabled ? _T("Run") : _T("Stop"));
m_runButtonIsRun = enabled;
if (enabled)
{
::SetForegroundWindow(_hSelf);
//::SetActiveWindow(_hSelf);
::SetFocus(m_hInput);
}
}
void ConsoleDialog::clearText()
{
::SendMessage(m_scintilla, SCI_SETREADONLY, 0, 0);
::SendMessage(m_scintilla, SCI_CLEARALL, 0, 0);
::SendMessage(m_scintilla, SCI_SETREADONLY, 1, 0);
}
void ConsoleDialog::onStyleNeeded(SCNotification* notification)
{
idx_t startPos = (idx_t)callScintilla(SCI_GETENDSTYLED);
idx_t startLine = (idx_t)callScintilla(SCI_LINEFROMPOSITION, startPos);
idx_t endPos = (idx_t)notification->position;
idx_t endLine = (idx_t)callScintilla(SCI_LINEFROMPOSITION, endPos);
LineDetails lineDetails{};
for(idx_t lineNumber = startLine; lineNumber <= endLine; ++lineNumber)
{
lineDetails.lineLength = (size_t)callScintilla(SCI_GETLINE, lineNumber);
if (lineDetails.lineLength > 0)
{
lineDetails.line = new char[lineDetails.lineLength + 1];
callScintilla(SCI_GETLINE, lineNumber, reinterpret_cast<LPARAM>(lineDetails.line));
lineDetails.line[lineDetails.lineLength] = '\0';
lineDetails.errorLevel = EL_UNSET;
if (parseLine(&lineDetails))
{
startPos = (idx_t)callScintilla(SCI_POSITIONFROMLINE, lineNumber);
// Check that it's not just a file called '<console>'
if (strncmp(lineDetails.line + lineDetails.filenameStart, "<console>", lineDetails.filenameEnd - lineDetails.filenameStart))
{
int mask, style;
switch(lineDetails.errorLevel)
{
case EL_WARNING:
mask = 0x04;
style = 0x04;
break;
case EL_ERROR:
mask = 0x01;
style = 0x01;
break;
case EL_UNSET:
case EL_INFO:
default:
mask = 0x00;
style = 0x00;
break;
}
if (lineDetails.filenameStart > 0)
{
callScintilla(SCI_STARTSTYLING, startPos, mask);
callScintilla(SCI_SETSTYLING, lineDetails.filenameStart, style);
}
callScintilla(SCI_STARTSTYLING, startPos + lineDetails.filenameStart, mask | 0x02);
callScintilla(SCI_SETSTYLING, lineDetails.filenameEnd - lineDetails.filenameStart, style | 0x02);
if (lineDetails.lineLength > lineDetails.filenameEnd)
{
callScintilla(SCI_STARTSTYLING, startPos + lineDetails.filenameEnd, mask);
callScintilla(SCI_SETSTYLING, lineDetails.lineLength - lineDetails.filenameEnd, style);
}
}
}
delete[] lineDetails.line;
}
}
// ensure that everything is set as styled (just move the endStyled variable on to the requested position)
callScintilla(SCI_STARTSTYLING, static_cast<WPARAM>(notification->position), 0x0);
}
bool ConsoleDialog::parseLine(LineDetails *lineDetails)
{
// Eg.
// File "C:\Users\Dave\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\fourth.py", line 2, in <module>
if (0 == strncmp(lineDetails->line, " File \"", 8)
&& parsePythonErrorLine(lineDetails))
{
return true;
}
// Eg.
// e:\work\pythonscript\pythonscript\src\consoledialog.cpp(523): error C2065: 'ee' : undeclared identifier
// Potentially with spaces in front if MSBUILD used
// Line number can contain "," for column (523,5)
if (parseVSErrorLine(lineDetails))
{
return true;
}
// Eg.
// C:/PalmDev/sdk-4/include/Core/System/NetMgr.h:550: warning: ignoring pragma: ;
if (parseGCCErrorLine(lineDetails))
{
return true;
}
return false;
}
bool ConsoleDialog::parseVSErrorLine(LineDetails *lineDetails)
{
enum StyleState
{
SS_BEGIN,
SS_FILENAME,
SS_LINENUMBER,
SS_ERRORTYPE,
SS_EXIT
} styleState;
bool retVal = false;
styleState = SS_BEGIN;
idx_t pos = 0;
lineDetails->errorLineNo = IDX_MAX;
while (styleState != SS_EXIT)
{
switch(styleState)
{
case SS_BEGIN:
while(lineDetails->line[pos] == ' ')
{
++pos;
}
lineDetails->filenameStart = pos;
styleState = SS_FILENAME;
break;
case SS_FILENAME:
while(lineDetails->line[pos] != '(' && pos < lineDetails->lineLength)
{
if (pos - lineDetails->filenameStart == 1)
{
if (lineDetails->line[pos] != ':')
{
styleState = SS_EXIT;
break;
}
}
else if (!isValidFilenameChar(lineDetails->line[pos]))
{
styleState = SS_EXIT;
break;
}
++pos;
}
if (lineDetails->line[pos] == '(') // Found the opening bracket for line no
{
++pos;
styleState = SS_LINENUMBER;
}
else
{
styleState = SS_EXIT;
}
break;
case SS_LINENUMBER:
{
idx_t startLineNoPos = pos;
idx_t endLineNoPos;
while(lineDetails->line[pos] >= '0' && lineDetails->line[pos] <= '9' && pos < lineDetails->lineLength)
{
++pos;
}
endLineNoPos = pos;
if (pos < (lineDetails->lineLength + 1)
&& lineDetails->line[pos] == ',')
{
++pos;
while(lineDetails->line[pos] >= '0' && lineDetails->line[pos] <= '9' && pos < lineDetails->lineLength)
{
++pos;
}
}
if (pos < (lineDetails->lineLength + 1)
&& lineDetails->line[pos] == ')'
&& lineDetails->line[pos+1] == ':')
{
// If no line number, jump out
if (endLineNoPos == startLineNoPos)
{
styleState = SS_EXIT;
break;
}
char *lineNumber = new char[(endLineNoPos - startLineNoPos) + 2];
strncpy_s(lineNumber, (endLineNoPos - startLineNoPos) + 2, lineDetails->line + startLineNoPos, endLineNoPos - startLineNoPos);
lineDetails->errorLineNo = strtoul(lineNumber, NULL, 0) - 1;
delete[] lineNumber;
lineDetails->filenameEnd = startLineNoPos - 1;
retVal = true;
pos += 3; // jump past "): " to either error or warning
styleState = SS_ERRORTYPE;
}
else
{
pos = startLineNoPos + 1;
styleState = SS_FILENAME;
}
break;
}
case SS_ERRORTYPE:
if (0 == strncmp(lineDetails->line + pos, "error", 5))
{
lineDetails->errorLevel = EL_ERROR;
}
else if (0 == strncmp(lineDetails->line + pos, "warning", 7))
{
lineDetails->errorLevel = EL_WARNING;
}
styleState = SS_EXIT;
break;
case SS_EXIT:
break;
default:
styleState = SS_EXIT;
break;
}
}
return retVal;
}
bool ConsoleDialog::parseGCCErrorLine(LineDetails *lineDetails)
{
enum StyleState
{
SS_FILENAME,
SS_LINENUMBER,
SS_ERRORTYPE,
SS_EXIT
} styleState;
bool retVal = false;
styleState = SS_FILENAME;
idx_t pos = 0;
lineDetails->filenameStart = 0;
lineDetails->errorLineNo = IDX_MAX;
while (styleState != SS_EXIT)
{
switch(styleState)
{
case SS_FILENAME:
{
bool isEscaped = false;
while((lineDetails->line[pos] != ':' || pos == 1) && pos < lineDetails->lineLength)
{
// Unescaped space, so leave - this isn't a gcc error
if (lineDetails->line[pos] == ' ' && !isEscaped)
{
styleState = SS_EXIT;
break;
}
// Unescaped invalid char, so it's not a gcc error
if (!(isEscaped || lineDetails->line[pos] == '/' || isValidFilenameChar(lineDetails->line[pos]) || (lineDetails->line[pos] == ':' && pos == 1)))
{
styleState = SS_EXIT;
break;
}
if (isEscaped)
isEscaped = false;
if (lineDetails->line[pos] == '\\')
isEscaped = true;
++pos;
}
if (lineDetails->line[pos] == ':') // Found the colon for line no
{
++pos;
styleState = SS_LINENUMBER;
}
else
{
styleState = SS_EXIT;
}
}
break;
case SS_LINENUMBER:
{
idx_t startLineNoPos = pos;
while(lineDetails->line[pos] >= '0' && lineDetails->line[pos] <= '9' && pos < lineDetails->lineLength)
{
++pos;
}
if (pos < (lineDetails->lineLength + 1)
&& lineDetails->line[pos] == ':')
{
lineDetails->errorLineNo = strtoul(lineDetails->line + startLineNoPos, NULL, 0) - 1;
// If the line number came out as 0, ie. there wasn't any,
// then the line is not a gcc error
if (lineDetails->errorLineNo == IDX_MAX)
{
styleState = SS_EXIT;
}
else
{
lineDetails->filenameEnd = startLineNoPos - 1;
retVal = true;
pos += 2;
styleState = SS_ERRORTYPE;
}
}
else
{
// If we've found the end of the number, and it isn't followed with a colon:
// then it's not a gcc error.
styleState = SS_EXIT;
}
break;
}
case SS_ERRORTYPE:
if (0 == strncmp(lineDetails->line + pos, "error", 5))
{
lineDetails->errorLevel = EL_ERROR;
}
else if (0 == strncmp(lineDetails->line + pos, "warning", 7))
{
lineDetails->errorLevel = EL_WARNING;
}
styleState = SS_EXIT;
break;
case SS_EXIT:
break;
default:
styleState = SS_EXIT;
break;
}
}
return retVal;
}
bool ConsoleDialog::parsePythonErrorLine(LineDetails *lineDetails)
{
enum StyleState