forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynCompletionProposal.pas
3768 lines (3310 loc) · 113 KB
/
SynCompletionProposal.pas
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
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: SynCompletionProposal.pas, released 2000-04-11.
The Original Code is based on mwCompletionProposal.pas by Cyrille de Brebisson,
part of the mwEdit component suite.
Portions created by Cyrille de Brebisson are Copyright (C) 1999
Cyrille de Brebisson.
Unicode translation by Maël Hörz.
All Rights Reserved.
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: SynCompletionProposal.pas,v 1.80.1.1 2013/06/25 10:31:19 codehunterworks Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Last Changes:
1.80.1.1 - Removed TProposalColumn.BiggestWord and
added TProposalColumn.ColumnWidth (Static Column Width in Pixels)
Modified by KV to
- apply FormatParams to only the first line of the ItemList
- to show the commas between parameters in parameter completion
-------------------------------------------------------------------------------}
unit SynCompletionProposal;
{$I SynEdit.inc}
interface
uses
Windows,
Messages,
Graphics,
Forms,
Controls,
StdCtrls,
ExtCtrls,
Menus,
SynEditTypes,
SynEditKeyCmds,
SynEdit,
SynUnicode,
SysUtils,
Classes;
type
SynCompletionType = (ctCode, ctHint, ctParams);
TSynForm = {$IFDEF SYN_COMPILER_3_UP}TCustomForm{$ELSE}TForm{$ENDIF};
TSynBaseCompletionProposalPaintItem = procedure(Sender: TObject;
Index: Integer; TargetCanvas: TCanvas; ItemRect: TRect;
var CustomDraw: Boolean) of object;
TSynBaseCompletionProposalMeasureItem = procedure(Sender: TObject;
Index: Integer; TargetCanvas: TCanvas; var ItemWidth: Integer) of object;
TCodeCompletionEvent = procedure(Sender: TObject; var Value: UnicodeString;
Shift: TShiftState; Index: Integer; EndToken: WideChar) of object;
TAfterCodeCompletionEvent = procedure(Sender: TObject; const Value: UnicodeString;
Shift: TShiftState; Index: Integer; EndToken: WideChar) of object;
TValidateEvent = procedure(Sender: TObject; Shift: TShiftState;
EndToken: WideChar) of object;
TCompletionParameter = procedure(Sender: TObject; CurrentIndex: Integer;
var Level, IndexToDisplay: Integer; var Key: WideChar;
var DisplayString: UnicodeString) of object;
TCompletionExecute = procedure(Kind: SynCompletionType; Sender: TObject;
var CurrentInput: UnicodeString; var x, y: Integer; var CanExecute: Boolean) of object;
TCompletionChange = procedure(Sender: TObject; AIndex: Integer) of object;
TCodeItemInfo = procedure(Sender: TObject; AIndex: Integer; var Info : string) of object;
TSynCompletionOption = (scoCaseSensitive, //Use case sensitivity to do matches
scoLimitToMatchedText, //Limit the matched text to only what they have typed in
scoTitleIsCentered, //Center the title in the box if you choose to use titles
scoUseInsertList, //Use the InsertList to insert text instead of the ItemList (which will be displayed)
scoUsePrettyText, //Use the PrettyText function to output the words
scoUseBuiltInTimer, //Use the built in timer and the trigger keys to execute the proposal as well as the shortcut
scoEndCharCompletion, //When an end char is pressed, it triggers completion to occur (like the Delphi IDE)
scoConsiderWordBreakChars,//Use word break characters as additional end characters
scoCompleteWithTab, //Use the tab character for completion
scoCompleteWithEnter); //Use the Enter character for completion
TSynCompletionOptions = set of TSynCompletionOption;
const
DefaultProposalOptions = [scoLimitToMatchedText, scoEndCharCompletion, scoCompleteWithTab, scoCompleteWithEnter];
DefaultEndOfTokenChr = '()[]. ';
type
TProposalColumns = class;
TSynBaseCompletionProposalForm = class(TSynForm)
private
FCurrentString: UnicodeString;
FOnKeyPress: TKeyPressWEvent;
FOnPaintItem: TSynBaseCompletionProposalPaintItem;
FOnMeasureItem: TSynBaseCompletionProposalMeasureItem;
FOnChangePosition: TCompletionChange;
FOnCodeItemInfo: TCodeItemInfo;
FItemList: TUnicodeStrings;
FInsertList: TUnicodeStrings;
FAssignedList: TUnicodeStrings;
FPosition: Integer;
FLinesInWindow: Integer;
FTitleFontHeight: Integer;
FFontHeight: integer;
FScrollbar: TScrollBar;
FOnValidate: TValidateEvent;
FOnCancel: TNotifyEvent;
FClSelect: TColor;
fClSelectText: TColor;
FClTitleBackground: TColor;
fClBackGround: TColor;
Bitmap: TBitmap; // used for drawing
TitleBitmap: TBitmap; // used for title-drawing
FCurrentEditor: TCustomSynEdit;
FTitle: UnicodeString;
FTitleFont: TFont;
FFont: TFont;
FResizeable: Boolean;
FItemHeight: Integer;
FMargin: Integer;
FEffectiveItemHeight: Integer;
FImages: TImageList;
//These are the reflections of the Options property of the CompletionProposal
FCase: boolean;
FMatchText: Boolean;
FFormattedText: Boolean;
FCenterTitle: Boolean;
FUseInsertList: boolean;
FCompleteWithTab: Boolean;
FCompleteWithEnter: Boolean;
FMouseWheelAccumulator: integer;
FDisplayKind: SynCompletionType;
FParameterToken: TCompletionParameter;
FCurrentIndex: Integer;
FCurrentLevel: Integer;
FDefaultKind: SynCompletionType;
FEndOfTokenChr: UnicodeString;
FTriggerChars: UnicodeString;
OldShowCaret: Boolean;
FHeightBuffer: Integer;
FColumns: TProposalColumns;
procedure SetCurrentString(const Value: UnicodeString);
procedure MoveLine(cnt: Integer);
procedure ScrollbarOnChange(Sender: TObject);
procedure ScrollbarOnScroll(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
procedure ScrollbarOnEnter(Sender: TObject);
procedure SetItemList(const Value: TUnicodeStrings);
procedure SetInsertList(const Value: TUnicodeStrings);
procedure SetPosition(const Value: Integer);
procedure SetResizeable(const Value: Boolean);
procedure SetItemHeight(const Value: Integer);
procedure SetImages(const Value: TImageList);
procedure StringListChange(Sender: TObject);
procedure DoDoubleClick(Sender : TObject);
procedure DoFormShow(Sender: TObject);
procedure DoFormHide(Sender: TObject);
procedure AdjustScrollBarPosition;
procedure AdjustMetrics;
procedure SetTitle(const Value: UnicodeString);
procedure SetFont(const Value: TFont);
procedure SetTitleFont(const Value: TFont);
procedure SetColumns(Value: TProposalColumns);
procedure TitleFontChange(Sender: TObject);
procedure FontChange(Sender: TObject);
procedure RecalcItemHeight;
function IsWordBreakChar(AChar: WideChar): Boolean;
protected
FCodeItemInfoWindow : THintWindow;
procedure DoKeyPressW(Key: WideChar);
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure KeyPressW(var Key: WideChar); virtual;
procedure Paint; override;
procedure Activate; override;
procedure Deactivate; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure Resize; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure WMChar(var Msg: TWMChar); message WM_CHAR;
procedure WMMouseWheel(var Msg: TMessage); message WM_MOUSEWHEEL;
procedure WMActivate (var Message: TWMActivate); message WM_ACTIVATE;
procedure WMEraseBackgrnd(var Message: TMessage); message WM_ERASEBKGND;
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWnd; override;
{$IFDEF SYN_DELPHI_4_UP}
function CanResize(var NewWidth, NewHeight: Integer): Boolean; override;
{$ENDIF}
procedure ShowCodeItemInfo(Info: string);
public
constructor Create(AOwner: Tcomponent); override;
destructor Destroy; override;
function LogicalToPhysicalIndex(Index: Integer): Integer;
function PhysicalToLogicalIndex(Index: Integer): Integer;
property DisplayType: SynCompletionType read FDisplayKind write FDisplayKind;
property DefaultType: SynCompletionType read FDefaultKind write FDefaultKind default ctCode;
property CurrentString: UnicodeString read FCurrentString write SetCurrentString;
property CurrentIndex: Integer read FCurrentIndex write FCurrentIndex;
property CurrentLevel: Integer read FCurrentLevel write FCurrentLevel;
property OnParameterToken: TCompletionParameter read FParameterToken write FParameterToken;
property OnKeyPress: TKeyPressWEvent read FOnKeyPress write FOnKeyPress;
property OnPaintItem: TSynBaseCompletionProposalPaintItem read FOnPaintItem write FOnPaintItem;
property OnMeasureItem: TSynBaseCompletionProposalMeasureItem read FOnMeasureItem write FOnMeasureItem;
property OnValidate: TValidateEvent read FOnValidate write FOnValidate;
property OnCancel: TNotifyEvent read FOnCancel write FOnCancel;
property ItemList: TUnicodeStrings read FItemList write SetItemList;
property InsertList: TUnicodeStrings read FInsertList write SetInsertList;
property AssignedList: TUnicodeStrings read FAssignedList write FAssignedList;
property Position: Integer read FPosition write SetPosition;
property Title: UnicodeString read fTitle write SetTitle;
property ClSelect: TColor read FClSelect write FClSelect default clHighlight;
property ClSelectedText: TColor read FClSelectText write FClSelectText default clHighlightText;
property ClBackground: TColor read FClBackGround write FClBackGround default clWindow;
property ClTitleBackground: TColor read FClTitleBackground write FClTitleBackground default clBtnFace;
property ItemHeight: Integer read FItemHeight write SetItemHeight default 0;
property Margin: Integer read FMargin write FMargin default 2;
property UsePrettyText: boolean read FFormattedText write FFormattedText default False;
property UseInsertList: boolean read FUseInsertList write FUseInsertList default False;
property CenterTitle: boolean read FCenterTitle write FCenterTitle default True;
property CaseSensitive: Boolean read fCase write fCase default False;
property CurrentEditor: TCustomSynEdit read fCurrentEditor write fCurrentEditor;
property MatchText: Boolean read fMatchText write fMatchText;
property EndOfTokenChr: UnicodeString read FEndOfTokenChr write FEndOfTokenChr;
property TriggerChars: UnicodeString read FTriggerChars write FTriggerChars;
property CompleteWithTab: Boolean read FCompleteWithTab write FCompleteWithTab;
property CompleteWithEnter: Boolean read FCompleteWithEnter write FCompleteWithEnter;
property TitleFont: TFont read fTitleFont write SetTitleFont;
property Font: TFont read fFont write SetFont;
property Columns: TProposalColumns read FColumns write SetColumns;
property Resizeable: Boolean read FResizeable write SetResizeable default True;
property Images: TImageList read FImages write SetImages;
end;
TSynBaseCompletionProposal = class(TComponent)
private
FForm: TSynBaseCompletionProposalForm;
FOnExecute: TCompletionExecute;
FOnClose: TNotifyEvent;
FOnShow: TNotifyEvent;
FWidth: Integer;
FPreviousToken: UnicodeString;
FDotOffset: Integer;
FOptions: TSynCompletionOptions;
FNbLinesInWindow: Integer;
FFormatParams : Boolean;
FCanExecute: Boolean;
function GetClSelect: TColor;
procedure SetClSelect(const Value: TColor);
function GetCurrentString: UnicodeString;
function GetItemList: TUnicodeStrings;
function GetInsertList: TUnicodeStrings;
function GetOnCancel: TNotifyEvent;
function GetOnKeyPress: TKeyPressWEvent;
function GetOnPaintItem: TSynBaseCompletionProposalPaintItem;
function GetOnMeasureItem: TSynBaseCompletionProposalMeasureItem;
function GetOnValidate: TValidateEvent;
function GetPosition: Integer;
procedure SetCurrentString(const Value: UnicodeString);
procedure SetItemList(const Value: TUnicodeStrings);
procedure SetInsertList(const Value: TUnicodeStrings);
procedure SetNbLinesInWindow(const Value: Integer);
procedure SetOnCancel(const Value: TNotifyEvent);
procedure SetOnKeyPress(const Value: TKeyPressWEvent);
procedure SetOnPaintItem(const Value: TSynBaseCompletionProposalPaintItem);
procedure SetOnMeasureItem(const Value: TSynBaseCompletionProposalMeasureItem);
procedure SetPosition(const Value: Integer);
procedure SetOnValidate(const Value: TValidateEvent);
procedure SetWidth(Value: Integer);
procedure SetImages(const Value: TImageList);
function GetDisplayKind: SynCompletionType;
procedure SetDisplayKind(const Value: SynCompletionType);
function GetParameterToken: TCompletionParameter;
procedure SetParameterToken(const Value: TCompletionParameter);
function GetDefaultKind: SynCompletionType;
procedure SetDefaultKind(const Value: SynCompletionType);
function GetClBack: TColor;
procedure SetClBack(const Value: TColor);
function GetClSelectedText: TColor;
procedure SetClSelectedText(const Value: TColor);
function GetEndOfTokenChar: UnicodeString;
procedure SetEndOfTokenChar(const Value: UnicodeString);
function GetClTitleBackground: TColor;
procedure SetClTitleBackground(const Value: TColor);
procedure SetTitle(const Value: UnicodeString);
function GetTitle: UnicodeString;
function GetFont: TFont;
function GetTitleFont: TFont;
procedure SetFont(const Value: TFont);
procedure SetTitleFont(const Value: TFont);
function GetOptions: TSynCompletionOptions;
function GetTriggerChars: UnicodeString;
procedure SetTriggerChars(const Value: UnicodeString);
function GetOnChange: TCompletionChange;
procedure SetOnChange(const Value: TCompletionChange);
function GetOnCodeItemInfo: TCodeItemInfo;
procedure SetOnCodeItemInfo(const Value: TCodeItemInfo);
procedure SetColumns(const Value: TProposalColumns);
function GetColumns: TProposalColumns;
function GetResizeable: Boolean;
procedure SetResizeable(const Value: Boolean);
function GetItemHeight: Integer;
procedure SetItemHeight(const Value: Integer);
function GetMargin: Integer;
procedure SetMargin(const Value: Integer);
function GetImages: TImageList;
function IsWordBreakChar(AChar: WideChar): Boolean;
protected
procedure DefineProperties(Filer: TFiler); override;
procedure SetOptions(const Value: TSynCompletionOptions); virtual;
procedure EditorCancelMode(Sender: TObject); virtual;
procedure HookedEditorCommand(Sender: TObject; AfterProcessing: Boolean;
var Handled: Boolean; var Command: TSynEditorCommand; var AChar: WideChar;
Data: Pointer; HandlerData: Pointer); virtual;
public
constructor Create(Aowner: TComponent); override;
procedure Execute(s: UnicodeString; x, y: Integer);
procedure ExecuteEx(s: UnicodeString; x, y: Integer; Kind: SynCompletionType
{$IFDEF SYN_COMPILER_4_UP} = ctCode {$ENDIF}); virtual;
procedure Activate;
procedure Deactivate;
procedure ClearList;
function DisplayItem(AIndex: Integer): UnicodeString;
function InsertItem(AIndex: Integer): UnicodeString;
procedure AddItemAt(Where: Integer; ADisplayText, AInsertText: UnicodeString);
procedure AddItem(ADisplayText, AInsertText: UnicodeString);
procedure ResetAssignedList;
property OnKeyPress: TKeyPressWEvent read GetOnKeyPress write SetOnKeyPress;
property OnValidate: TValidateEvent read GetOnValidate write SetOnValidate;
property OnCancel: TNotifyEvent read GetOnCancel write SetOnCancel;
property CurrentString: UnicodeString read GetCurrentString write SetCurrentString;
property DotOffset: Integer read FDotOffset write FDotOffset;
property DisplayType: SynCompletionType read GetDisplayKind write SetDisplayKind;
property Form: TSynBaseCompletionProposalForm read FForm;
property PreviousToken: UnicodeString read FPreviousToken;
property Position: Integer read GetPosition write SetPosition;
property FormatParams : boolean read fFormatParams write fFormatParams;
published
property DefaultType: SynCompletionType read GetDefaultKind write SetDefaultKind default ctCode;
property Options: TSynCompletionOptions read GetOptions write SetOptions default DefaultProposalOptions;
property ItemList: TUnicodeStrings read GetItemList write SetItemList;
property InsertList: TUnicodeStrings read GetInsertList write SetInsertList;
property NbLinesInWindow: Integer read FNbLinesInWindow write SetNbLinesInWindow default 8;
property ClSelect: TColor read GetClSelect write SetClSelect default clHighlight;
property ClSelectedText: TColor read GetClSelectedText write SetClSelectedText default clHighlightText;
property ClBackground: TColor read GetClBack write SetClBack default clWindow;
property ClTitleBackground: TColor read GetClTitleBackground write SetClTitleBackground default clBtnFace;
property Width: Integer read FWidth write SetWidth default 260;
property EndOfTokenChr: UnicodeString read GetEndOfTokenChar write SetEndOfTokenChar;
property TriggerChars: UnicodeString read GetTriggerChars write SetTriggerChars;
property Title: UnicodeString read GetTitle write SetTitle;
property Font: TFont read GetFont write SetFont;
property TitleFont: TFont read GetTitleFont write SetTitleFont;
property Columns: TProposalColumns read GetColumns write SetColumns;
property Resizeable: Boolean read GetResizeable write SetResizeable default True;
property ItemHeight: Integer read GetItemHeight write SetItemHeight default 0;
property Images: TImageList read GetImages write SetImages default nil;
property Margin: Integer read GetMargin write SetMargin default 2;
property OnChange: TCompletionChange read GetOnChange write SetOnChange;
property OnCodeItemInfo: TCodeItemInfo read GetOnCodeItemInfo write SetOnCodeItemInfo;
property OnClose: TNotifyEvent read FOnClose write FOnClose;
property OnExecute: TCompletionExecute read FOnExecute write FOnExecute;
property OnMeasureItem: TSynBaseCompletionProposalMeasureItem read GetOnMeasureItem write SetOnMeasureItem;
property OnPaintItem: TSynBaseCompletionProposalPaintItem read GetOnPaintItem write SetOnPaintItem;
property OnParameterToken: TCompletionParameter read GetParameterToken write SetParameterToken;
property OnShow: TNotifyEvent read FOnShow write FOnShow;
end;
TSynCompletionProposal = class(TSynBaseCompletionProposal)
private
fEditors: TList;
FShortCut: TShortCut;
FNoNextKey: Boolean;
FCompletionStart: Integer;
FAdjustCompletionStart: Boolean;
FOnCodeCompletion: TCodeCompletionEvent;
FTimer: TTimer;
FTimerInterval: Integer;
FEditor: TCustomSynEdit;
FOnAfterCodeCompletion: TAfterCodeCompletionEvent;
FOnCancelled: TNotifyEvent;
procedure SetEditor(const Value: TCustomSynEdit);
procedure HandleOnCancel(Sender: TObject);
procedure HandleOnValidate(Sender: TObject; Shift: TShiftState; EndToken: WideChar);
procedure HandleOnKeyPress(Sender: TObject; var Key: WideChar);
procedure HandleDblClick(Sender: TObject);
procedure EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure EditorKeyPress(Sender: TObject; var Key: WideChar);
procedure TimerExecute(Sender: TObject);
function GetPreviousToken(AEditor: TCustomSynEdit): UnicodeString;
function GetCurrentInput(AEditor: TCustomSynEdit): UnicodeString;
function GetTimerInterval: Integer;
procedure SetTimerInterval(const Value: Integer);
function GetEditor(i: Integer): TCustomSynEdit;
procedure InternalCancelCompletion;
protected
procedure DoExecute(AEditor: TCustomSynEdit); virtual;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SetShortCut(Value: TShortCut);
procedure SetOptions(const Value: TSynCompletionOptions); override;
procedure EditorCancelMode(Sender: TObject); override;
procedure HookedEditorCommand(Sender: TObject; AfterProcessing: Boolean;
var Handled: Boolean; var Command: TSynEditorCommand; var AChar: WideChar;
Data: Pointer; HandlerData: Pointer); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AddEditor(AEditor: TCustomSynEdit);
function RemoveEditor(AEditor: TCustomSynEdit): boolean;
function EditorsCount: integer;
procedure ExecuteEx(s: UnicodeString; x, y: Integer; Kind : SynCompletionType
{$IFDEF SYN_COMPILER_4_UP} = ctCode {$ENDIF}); override;
procedure ActivateCompletion;
procedure CancelCompletion;
procedure ActivateTimer(ACurrentEditor: TCustomSynEdit);
procedure DeactivateTimer;
property Editors[i: Integer]: TCustomSynEdit read GetEditor;
property CompletionStart: Integer read FCompletionStart write FCompletionStart; // ET 04/02/2003
published
property ShortCut: TShortCut read FShortCut write SetShortCut;
property Editor: TCustomSynEdit read FEditor write SetEditor;
property TimerInterval: Integer read GetTimerInterval write SetTimerInterval default 1000;
property OnAfterCodeCompletion: TAfterCodeCompletionEvent read FOnAfterCodeCompletion write FOnAfterCodeCompletion;
property OnCancelled: TNotifyEvent read FOnCancelled write FOnCancelled;
property OnCodeCompletion: TCodeCompletionEvent read FOnCodeCompletion write FOnCodeCompletion;
end;
TSynAutoComplete = class(TComponent)
private
FShortCut: TShortCut;
fEditor: TCustomSynEdit;
fAutoCompleteList: TUnicodeStrings;
fNoNextKey : Boolean;
FEndOfTokenChr: UnicodeString;
FOnBeforeExecute: TNotifyEvent;
FOnAfterExecute: TNotifyEvent;
FInternalCompletion: TSynCompletionProposal;
FDoLookup: Boolean;
FOptions: TSynCompletionOptions;
procedure SetAutoCompleteList(List: TUnicodeStrings);
procedure SetEditor(const Value: TCustomSynEdit);
procedure SetDoLookup(const Value: Boolean);
procedure CreateInternalCompletion;
function GetOptions: TSynCompletionOptions;
procedure SetOptions(const Value: TSynCompletionOptions);
procedure DoInternalAutoCompletion(Sender: TObject;
const Value: UnicodeString; Shift: TShiftState; Index: Integer;
EndToken: WideChar);
function GetExecuting: Boolean;
protected
procedure SetShortCut(Value: TShortCut);
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
procedure EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
virtual;
procedure EditorKeyPress(Sender: TObject; var Key: WideChar); virtual;
function GetPreviousToken(Editor: TCustomSynEdit): UnicodeString;
public
function GetCompletionProposal : TSynCompletionProposal;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Execute(Token: UnicodeString; Editor: TCustomSynEdit);
procedure ExecuteEx(Token: UnicodeString; Editor: TCustomSynEdit; LookupIfNotExact: Boolean);
function GetTokenList: UnicodeString;
function GetTokenValue(Token: UnicodeString): UnicodeString;
procedure CancelCompletion;
property Executing: Boolean read GetExecuting;
published
property AutoCompleteList: TUnicodeStrings read fAutoCompleteList
write SetAutoCompleteList;
property EndOfTokenChr: UnicodeString read FEndOfTokenChr write FEndOfTokenChr;
property Editor: TCustomSynEdit read fEditor write SetEditor;
property ShortCut: TShortCut read FShortCut write SetShortCut;
property OnBeforeExecute: TNotifyEvent read FOnBeforeExecute write FOnBeforeExecute;
property OnAfterExecute: TNotifyEvent read FOnAfterExecute write FOnAfterExecute;
property DoLookupWhenNotExact: Boolean read FDoLookup write SetDoLookup default true;
property Options: TSynCompletionOptions read GetOptions write SetOptions default DefaultProposalOptions;
end;
TProposalColumn = class(TCollectionItem)
private
FColumnWidth: Integer;
FInternalWidth: Integer;
FFontStyle: TFontStyles;
protected
procedure DefineProperties(Filer: TFiler); override;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
property ColumnWidth: Integer read FColumnWidth write FColumnWidth;
property DefaultFontStyle: TFontStyles read FFontStyle write FFontStyle default [];
end;
TProposalColumns = class(TCollection)
private
FOwner: TPersistent;
function GetItem(Index: Integer): TProposalColumn;
procedure SetItem(Index: Integer; Value: TProposalColumn);
protected
function GetOwner: TPersistent; {$IFDEF SYN_COMPILER_3_UP} override; {$ENDIF}
public
constructor Create(AOwner: TPersistent; ItemClass: TCollectionItemClass);
function Add: TProposalColumn;
{$IFDEF SYN_COMPILER_3_UP}
function FindItemID(ID: Integer): TProposalColumn;
{$ENDIF}
{$IFDEF SYN_COMPILER_4_UP}
function Insert(Index: Integer): TProposalColumn;
{$ENDIF}
property Items[Index: Integer]: TProposalColumn read GetItem write SetItem; default;
end;
procedure FormattedTextOut(TargetCanvas: TCanvas; const Rect: TRect;
const Text: UnicodeString; Selected: Boolean; Columns: TProposalColumns; Images: TImageList);
function FormattedTextWidth(TargetCanvas: TCanvas; const Text: UnicodeString;
Columns: TProposalColumns; Images: TImageList): Integer;
function PrettyTextToFormattedString(const APrettyText: UnicodeString;
AlternateBoldStyle: Boolean {$IFDEF SYN_COMPILER_4_UP} = False {$ENDIF}): UnicodeString;
implementation
uses
{$IFDEF SYN_COMPILER_4_UP}
Math,
{$ENDIF}
SynEditTextBuffer,
SynEditMiscProcs,
SynEditKeyConst,
Vcl.Themes;
const
TextHeightString = 'CompletionProposal';
//------------------------- Formatted painting stuff ---------------------------
type
TFormatCommand = (fcNoCommand, fcColor, fcStyle, fcColumn, fcHSpace, fcImage);
TFormatCommands = set of TFormatCommand;
PFormatChunk = ^TFormatChunk;
TFormatChunk = record
Str: UnicodeString;
Command: TFormatCommand;
Data: Pointer;
end;
PFormatStyleData = ^TFormatStyleData;
TFormatStyleData = record
Style: WideChar;
Action: Integer; // -1 = Reset, +1 = Set, 0 = Toggle
end;
TFormatChunkList = class
private
FChunks: TList;
function GetCount: Integer;
function GetChunk(Index: Integer): PFormatChunk;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(AChunk: PFormatChunk);
property Count: Integer read GetCount;
property Chunks[Index: Integer]: PFormatChunk read GetChunk; default;
end;
const
AllCommands = [fcColor..High(TFormatCommand)];
function TFormatChunkList.GetCount: Integer;
begin
Result := FChunks.Count;
end;
function TFormatChunkList.GetChunk(Index: Integer): PFormatChunk;
begin
Result := FChunks[Index];
end;
procedure TFormatChunkList.Clear;
var
C: PFormatChunk;
StyleFormatData: PFormatStyleData;
begin
while FChunks.Count > 0 do
begin
C := FChunks.Last;
FChunks.Delete(FChunks.Count-1);
case C^.Command of
fcStyle:
begin
StyleFormatData := C^.Data;
Dispose(StyleFormatData);
end;
end;
Dispose(C);
end;
end;
constructor TFormatChunkList.Create;
begin
inherited Create;
FChunks := TList.Create;
end;
destructor TFormatChunkList.Destroy;
begin
Clear;
FChunks.Free;
inherited Destroy;
end;
procedure TFormatChunkList.Add(AChunk: PFormatChunk);
begin
FChunks.Add(AChunk);
end;
function ParseFormatChunks(const FormattedString: UnicodeString; ChunkList: TFormatChunkList;
const StripCommands: TFormatCommands): Boolean;
var
CurChar: WideChar;
CurPos: Integer;
CurrentChunk: UnicodeString;
PossibleErrorPos: Integer;
ErrorFound: Boolean;
procedure NextChar;
begin
inc(CurPos);
{$IFOPT R+}
// Work-around Delphi's annoying behaviour of failing the RangeCheck when
// reading the final #0 char
if CurPos = Length(FormattedString) +1 then
CurChar := #0
else
{$ENDIF}
CurChar := FormattedString[CurPos];
end;
procedure AddStringChunk;
var
C: PFormatChunk;
begin
C := New(PFormatChunk);
C^.Str := CurrentChunk;
C^.Command := fcNoCommand;
C^.Data := nil;
ChunkList.Add(C);
CurrentChunk := '';
end;
procedure AddCommandChunk(ACommand: TFormatCommand; Data: Pointer);
var
C: PFormatChunk;
begin
C := New(PFormatChunk);
C^.Str := '';
C^.Command := ACommand;
C^.Data := Data;
ChunkList.Add(C);
end;
procedure ParseEscapeSequence;
var
Command: UnicodeString;
Parameter: UnicodeString;
CommandType: TFormatCommand;
Data: Pointer;
begin
Assert(CurChar = '\');
NextChar;
if CurChar = '\' then
begin
CurrentChunk := CurrentChunk + '\';
NextChar;
exit;
end;
if CurrentChunk <> '' then
AddStringChunk;
Command := '';
while (CurChar <> '{') and (CurPos <= Length(FormattedString)) do
begin
Command := Command +CurChar;
NextChar;
end;
if CurChar = '{' then
begin
PossibleErrorPos := CurPos;
NextChar;
Parameter := '';
while (CurChar <> '}') and (CurPos <= Length(FormattedString)) do
begin
Parameter := Parameter + CurChar;
NextChar;
end;
if CurChar = '}' then
begin
Command := SynWideUpperCase(Command);
Data := nil;
CommandType := fcNoCommand;
if Command = 'COLOR' then
begin
try
Data := Pointer(StringToColor(Parameter));
CommandType := fcColor;
except
CommandType := fcNoCommand;
ErrorFound := True;
end;
end else
if Command = 'COLUMN' then
begin
if Parameter <> '' then
begin
CommandType := fcNoCommand;
ErrorFound := True;
end else
CommandType := fcColumn;
end else
if Command = 'HSPACE' then
begin
try
Data := Pointer(StrToInt(Parameter));
CommandType := fcHSpace;
except
CommandType := fcNoCommand;
ErrorFound := True;
end;
end else
if Command = 'IMAGE' then
begin
try
Data := Pointer(StrToInt(Parameter));
CommandType := fcImage;
except
CommandType := fcNoCommand;
ErrorFound := True;
end;
end else
if Command = 'STYLE' then
begin
if (Length(Parameter) = 2)
and CharInSet(Parameter[1], ['+', '-', '~'])
and CharInSet(SynWideUpperCase(Parameter[2])[1],
['B', 'I', 'U', 'S']) then
begin
CommandType := fcStyle;
if not (fcStyle in StripCommands) then
begin
Data := New(PFormatStyleData);
PFormatStyleData(Data)^.Style := SynWideUpperCase(Parameter[2])[1];
case Parameter[1] of
'+': PFormatStyleData(Data)^.Action := 1;
'-': PFormatStyleData(Data)^.Action := -1;
'~': PFormatStyleData(Data)^.Action := 0;
end;
end;
end else
begin
CommandType := fcNoCommand;
ErrorFound := True;
end;
end else
ErrorFound := True;
if (CommandType <> fcNoCommand) and (not (CommandType in StripCommands)) then
AddCommandChunk(CommandType, Data);
NextChar;
end;
end;
Result := not ErrorFound;
end;
procedure ParseString;
begin
Assert(CurChar <> '\');
while (CurChar <> '\') and (CurPos <= Length(FormattedString)) do
begin
CurrentChunk := CurrentChunk +CurChar;
NextChar;
end;
end;
begin
Assert(Assigned(ChunkList));
if FormattedString = '' then
exit;
ErrorFound := False;
CurrentChunk := '';
CurPos := 1;
CurChar := FormattedString[1];
while CurPos <= Length(FormattedString) do
begin
if CurChar = '\' then
ParseEscapeSequence
else
ParseString;
end;
if CurrentChunk <> '' then
AddStringChunk;
end;
function StripFormatCommands(const FormattedString: UnicodeString): UnicodeString;
var
Chunks: TFormatChunkList;
i: Integer;
begin
Chunks := TFormatChunkList.Create;
try
ParseFormatChunks(FormattedString, Chunks, AllCommands);
Result := '';
for i := 0 to Chunks.Count -1 do
Result := Result + Chunks[i]^.Str;
finally
Chunks.Free;
end;
end;
function PaintChunks(TargetCanvas: TCanvas; const Rect: TRect;
ChunkList: TFormatChunkList; Columns: TProposalColumns; Images: TImageList;
Invisible: Boolean): Integer;
var
i: Integer;
X: Integer;
C: PFormatChunk;
CurrentColumn: TProposalColumn;
CurrentColumnIndex: Integer;
LastColumnStart: Integer;
Style: TFontStyles;
OldFont: TFont;
begin
OldFont := TFont.Create;
try
OldFont.Assign(TargetCanvas.Font);
if Assigned(Columns) and (Columns.Count > 0) then
begin
CurrentColumnIndex := 0;
CurrentColumn := TProposalColumn(Columns.Items[0]);
TargetCanvas.Font.Style := CurrentColumn.FFontStyle;
end else
begin
CurrentColumnIndex := -1;
CurrentColumn := nil;
end;
LastColumnStart := Rect.Left;
X := Rect.Left;
TargetCanvas.Brush.Style := bsClear;
for i := 0 to ChunkList.Count -1 do
begin
C := ChunkList[i];
case C^.Command of
fcNoCommand:
begin
if not Invisible then
TextOut(TargetCanvas, X, Rect.Top, C^.Str);
inc(X, TextWidth(TargetCanvas, C^.Str));
if X > Rect.Right then
break;
end;
fcColor:
if not Invisible then
TargetCanvas.Font.Color := StyleServices.GetSystemColor(TColor(C^.Data));
fcStyle:
begin
case PFormatStyleData(C^.Data)^.Style of
'I': Style := [fsItalic];
'B': Style := [fsBold];
'U': Style := [fsUnderline];
'S': Style := [fsStrikeout];
else Assert(False);
end;
case PFormatStyleData(C^.Data)^.Action of
-1: TargetCanvas.Font.Style := TargetCanvas.Font.Style - Style;
0: if TargetCanvas.Font.Style * Style = [] then
TargetCanvas.Font.Style := TargetCanvas.Font.Style + Style
else
TargetCanvas.Font.Style := TargetCanvas.Font.Style - Style;
1: TargetCanvas.Font.Style := TargetCanvas.Font.Style + Style;
else Assert(False);
end;
end;
fcColumn:
if Assigned(Columns) and (Columns.Count > 0) then
begin
if CurrentColumnIndex <= Columns.Count -1 then
begin
inc(LastColumnStart, CurrentColumn.FColumnWidth);
X := LastColumnStart;
inc(CurrentColumnIndex);
if CurrentColumnIndex <= Columns.Count -1 then
begin
CurrentColumn := TProposalColumn(Columns.Items[CurrentColumnIndex]);
TargetCanvas.Font.Style := CurrentColumn.FFontStyle;
end else
CurrentColumn := nil;
end;
end;
fcHSpace:
begin
inc(X, Integer(C^.Data));
if X > Rect.Right then
break;
end;
fcImage:
begin
Assert(Assigned(Images));
Images.Draw(TargetCanvas, X, Rect.Top, Integer(C^.Data));
inc(X, Images.Width);
if X > Rect.Right then
break;
end;
end;
end;
Result := X;
TargetCanvas.Font.Assign(OldFont);
finally
OldFont.Free;
TargetCanvas.Brush.Style := bsSolid;
end;
end;