-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
UChatGptMain.pas
1218 lines (1083 loc) · 38 KB
/
UChatGptMain.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
{***************************************************}
{ }
{ This unit is the main unit of the package }
{ including register function. }
{ Auhtor: Ali Dehbansiahkarbon(adehban@gmail.com) }
{ GitHub: https://github.com/AliDehbansiahkarbon }
{ }
{***************************************************}
unit UChatGptMain;
interface
uses
System.Classes, System.SysUtils, Vcl.Controls, Vcl.Menus, Vcl.Dialogs, Winapi.Windows,
{$IFNDEF NEXTGEN}System.StrUtils{$ELSE}AnsiStrings{$ENDIF !NEXTGEN}, ToolsAPI, StructureViewAPI,
DockForm, System.Generics.Collections, Vcl.Forms, System.IOUtils, UConsts, UAbout, Vcl.Imaging.pngimage,
UChatGPTMenuHook, UChatGPTSetting, UChatGPTQFrame, UChatGPTQuestion, UEditorHelpers, Vcl.Graphics, Vcl.ImgList;
type
{$REGION 'Types and definitions'}
TTStringListHelper = class helper for TStringList
function TrimLineText: string;
end;
TChatGPTOnCliskType = procedure(Sender: TObject) of Object;
TMsgType = (mtNone, mtNormalQuestion, mtAddTest, mtFindBugs, mtAddComment,
mtOptimize, mtCompleteCode, mtExplain, mtRefactor, mtASM);
{*********************************************************}
{ }
{ This menu item will be added }
{ into the main menu of the IDE. }
{ }
{*********************************************************}
TChatGptMenuWizard = class(TInterfacedObject, IOTAWizard)
private
FRoot, FAskMenu,
FSettingMenu, FAskMenuDockable, FAboutMenu: TMenuItem;
FSetting: TSingletonSettingObj;
FAskSubMenuH, FAddTestH, FFindBugsH, //These hidden menues usre used for better Ux expreince with shortcuts.
FOptimizeH, FAddCommentsH, FCompleteCodeH, FAsmConvert: TMenuItem;
procedure AskMenuClick(Sender: TObject);
procedure ChatGPTDockableMenuClick(Sender: TObject);
procedure ChatGPTSettingMenuClick(Sender: TObject);
procedure ChatGPTAboutMenuClick(Sender: TObject);
procedure AskSubmenuHiddenOnClick(Sender: TObject);
procedure RenewUI(AForm: TCustomForm);
protected
{ protected declarations }
public
constructor Create;
function GetIDString: string;
function GetName: string;
function GetState: TWizardState; { Launch the AddIn }
procedure Execute;
procedure AfterSave; { This function is called immediately before the item is saved. This is not called for IOTAWizard }
procedure BeforeSave;
procedure Destroyed; { The associated item is being destroyed so all references should be dropped.Exceptions are ignored. }
procedure Modified; { This associated item was modified in some way. This is not called for IOTAWizards }
procedure LoadSubMenuIcons(AMainMenu: TMainMenu);
end;
{************************************************************************************************}
{ }
{ The editor is not immediately available upon startup of the IDE, }
{ therefore we can’t get access to the editor in the plugin initialization code. }
{ What we needed is to register a class that implements the INTAEditServicesNotifier interface.}
{ The IDE calls this interface when the editor is activated in the IDE. }
{ }
{************************************************************************************************}
{$IF CompilerVersion >= 32.0}
TStylingNotifier = class(TNotifierObject, IOTANotifier, INTAIDEThemingServicesNotifier)
procedure ChangingTheme();
procedure ChangedTheme();
end;
{$ENDIF}
TEditNotifierHelper = class(TNotifierObject, IOTANotifier, INTAEditServicesNotifier)
procedure OnChatGPTSubMenuClick(Sender: TObject; MenuItem: TMenuItem);
procedure OnChatGPTContextMenuFixedQuestionClick(Sender: TObject);
private
FMenuHook: TCpMenuHook;
class function GetQuestion(AStr: string): string;
class function CreateMsg(AType: TMsgType): string;
class procedure FormatSource;
function GetCurrentUnitPath: string;
class function GetSelectedText: string;
class procedure RunInlineQuestion(AQuestion: string; AMsgType: TMsgType);
class procedure DoAskSubMenu;
class procedure DoAddTest;
class procedure DoFindBugs;
class procedure DoOptimize;
class procedure DoAddComments;
class procedure DoCompleteCode;
class procedure DoExplain;
class procedure DoRefactor;
class procedure DoConvertToAssembly;
class function RefineText(AInput: TStringList; AMsgType: TMsgType = mtNone): string;
class procedure WriteIntoEditor(AText: string);
public
constructor Create;
destructor Destroy; override;
procedure WindowShow(const EditWindow: INTAEditWindow; Show, LoadedFromDesktop: Boolean);
procedure WindowNotification(const EditWindow: INTAEditWindow; Operation: TOperation);
procedure WindowActivated(const EditWindow: INTAEditWindow);
procedure WindowCommand(const EditWindow: INTAEditWindow; Command, Param: Integer; var Handled: Boolean);
procedure EditorViewActivated(const EditWindow: INTAEditWindow; const EditView: IOTAEditView);
procedure EditorViewModified(const EditWindow: INTAEditWindow; const EditView: IOTAEditView);
procedure DockFormVisibleChanged(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
procedure DockFormUpdated(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
procedure DockFormRefresh(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
procedure AddEditorContextMenu;
function AddMenuItem(AParentMenu: TMenuItem; AName, ACaption: string; AOnClick: TChatGPTOnCliskType; AShortCut: string; ATag: NativeInt): TMenuItem;
end;
{*******************************************************}
{ }
{ This is the dockable form of the plugin. }
{ It is a singleton class which means there will }
{ be just one instance as long as the IDE is alive! }
{ Could be activate/deactive in main menu. }
{ }
{*******************************************************}
TChatGPTDockForm = class(TDockableForm)
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
FDockFormClassListObj: TClassList;
public
Fram_Question: TFram_Question;
constructor Create(AOwner: TComponent);
destructor Destroy; override;
end;
const
WizardFail = -1;
var
FMainMenuIndex: Integer = WizardFail;
FNotifierIndex: Integer = WizardFail;
FStylingNotifierIndex: Integer = WizardFail;
FChatGPTSubMenu: TCpMenuItemDef;
FChatGPTDockForm: TChatGPTDockForm;
FChatGptMenuWizard: TChatGptMenuWizard;
FShouldApplyTheme: Boolean = False;
procedure RemoveAferUnInstall;
procedure RegisterAboutBox;
procedure register;
{$ENDREGION}
implementation
uses
UChatGPTProgress;
{$REGION 'public methods'}
procedure register;
begin
FChatGptMenuWizard := TChatGptMenuWizard.Create;
FMainMenuIndex := (BorlandIDEServices as IOTAWizardServices).AddWizard(FChatGptMenuWizard);
FNotifierIndex := (BorlandIDEServices as IOTAEditorServices).AddNotifier(TEditNotifierHelper.Create);
{$IF CompilerVersion >= 32.0}
FStylingNotifierIndex := (BorlandIDEServices as IOTAIDEThemingServices).AddNotifier(TStylingNotifier.Create);
{$ENDIF}
end;
procedure RemoveAferUnInstall;
var
LvRootMenu: TMainMenu;
begin
if Assigned(FChatGPTDockForm) then
begin
FChatGPTDockForm.Close;
FreeAndNil(FChatGPTDockForm);
end;
LvRootMenu := (BorlandIDEServices as INTAServices).MainMenu;
LvRootMenu.Items.Delete(TSingletonSettingObj.Instance.RootMenuIndex);
if FMainMenuIndex <> WizardFail then
(BorlandIDEServices as IOTAWizardServices).RemoveWizard(FMainMenuIndex);
if FNotifierIndex <> WizardFail then
(BorlandIDEServices as IOTAEditorServices).RemoveNotifier(FNotifierIndex);
{$IF CompilerVersion >= 32.0}
if FStylingNotifierIndex <> WizardFail then
(BorlandIDEServices as IOTAIDEThemingServices).RemoveNotifier(FStylingNotifierIndex);
{$ENDIF}
end;
procedure RegisterAboutBox;
var
LvSplashService : IOTASplashScreenServices;
LvResStream: TResourceStream;
LvPngImage: TPngImage;
LvBmp: TBitmap;
begin
if Supports(SplashScreenServices, IOTASplashScreenServices, LvSplashService) then
begin
LvResStream := TResourceStream.Create(HInstance, 'GPT24', RT_RCDATA);
try
LvPngImage := TPngImage.Create;
try
LvPngImage.LoadFromStream(LvResStream);
LvBmp := TBitmap.Create;
try
LvPngImage.Transparent := False;
LvPngImage.AssignTo(LvBmp);
LvSplashService.AddPluginBitmap(CVersionedName, LvBmp.Handle, False, 'MIT License');
finally
LvBmp.Free;
end;
finally
LvPngImage.Free;
end;
finally
LvResStream.Free;
end;
end;
end;
{$ENDREGION}
{$REGION 'TChatGptMenuWizard'}
procedure TChatGptMenuWizard.ChatGPTAboutMenuClick(Sender: TObject);
var
Frm_About: TFrm_About;
begin
Frm_About := TFrm_About.Create(nil);
TSingletonSettingObj.RegisterFormClassForTheming(TFrm_About, Frm_About); //Apply Theme
try
Frm_About.ShowModal;
finally
Frm_About.Free;
end;
end;
procedure TChatGptMenuWizard.ChatGPTDockableMenuClick(Sender: TObject);
var
LvSettingObj: TSingletonSettingObj;
begin
LvSettingObj := TSingletonSettingObj.Instance;
LvSettingObj.ReadRegistry;
if LvSettingObj.ApiKey = '' then
begin
if (LvSettingObj.IsOffline) and (LvSettingObj.GetSetting.Trim.IsEmpty) then
Exit;
end;
if not Assigned(FChatGPTDockForm) then
FChatGPTDockForm := TChatGPTDockForm.Create(Application);
TSingletonSettingObj.RegisterFormClassForTheming(TChatGPTDockForm, FChatGPTDockForm); //Apply Theme
RenewUI(FChatGPTDockForm);
FChatGPTDockForm.Show;
end;
procedure TChatGptMenuWizard.ChatGPTSettingMenuClick(Sender: TObject);
begin
FSetting := TSingletonSettingObj.Instance;
FSetting.ReadRegistry;
Frm_Setting := TFrm_Setting.Create(nil);
try
TSingletonSettingObj.RegisterFormClassForTheming(TFrm_Setting, Frm_Setting); //Apply Theme
with Frm_Setting do
begin
Edt_ApiKey.Text := FSetting.ApiKey;
Edt_Url.Text := FSetting.URL;
Edt_MaxToken.Text := FSetting.MaxToken.ToString;
Edt_Temperature.Text := FSetting.Temperature.ToString;
cbbModel.ItemIndex := Frm_Setting.cbbModel.Items.IndexOf(FSetting.Model);
Edt_SourceIdentifier.Text := FSetting.Identifier;
chk_CodeFormatter.Checked := FSetting.CodeFormatter;
chk_Rtl.Checked := FSetting.RighToLeft;
chk_History.Checked := FSetting.HistoryEnabled;
lbEdt_History.Text := FSetting.HistoryPath;
lbEdt_History.Enabled := FSetting.HistoryEnabled;
Btn_HistoryPathBuilder.Enabled := FSetting.HistoryEnabled;
ColorBox_Highlight.Selected := FSetting.HighlightColor;
chk_AnimatedLetters.Checked := FSetting.AnimatedLetters;
lbEdt_Timeout.Text := FSetting.TimeOut.ToString;
chk_Offline.Checked := FSetting.IsOffline;
cbbModel.Enabled := not FSetting.IsOffline;
edt_MaxToken.Enabled := not FSetting.IsOffline;
edt_OfflineModel.Enabled := FSetting.IsOffline;
if FSetting.IsOffline then
edt_OfflineModel.Text := FSetting.Model;
AddAllDefinedQuestions;
chk_WriteSonic.Checked := FSetting.EnableWriteSonic;
lbEdt_WriteSonicAPIKey.Text := FSetting.WriteSonicAPIKey;
lbEdt_WriteSonicBaseURL.Text := FSetting.WriteSonicBaseURL;
lbEdt_WriteSonicAPIKey.Enabled := FSetting.EnableWriteSonic;
lbEdt_WriteSonicBaseURL.Enabled := FSetting.EnableWriteSonic;
chk_YouChat.Checked := FSetting.EnableYouChat;
lbEdt_YouChatAPIKey.Text := FSetting.YouChatAPIKey;
lbEdt_YouChatBaseURL.Text := FSetting.YouChatBaseURL;
lbEdt_YouChatAPIKey.Enabled := FSetting.EnableYouChat;
lbEdt_YouChatBaseURL.Enabled := FSetting.EnableYouChat;
ShowModal;
end;
if Frm_Setting.HasChanges then
RenewUI(FChatGPTDockForm);
finally
Frm_Setting.Free;
FSetting.ReadRegistry; // In case something changed in setting that must reload.
end;
end;
constructor TChatGptMenuWizard.Create;
var
LvMainMenu: TMainMenu;
begin
if not Assigned(FAskMenu) then
begin
FAskMenu := TMenuItem.Create(nil);
FAskMenu.Name := 'Mnu_ChatGPT';
FAskMenu.Caption := 'Ask ChatGPT';
FAskMenu.OnClick := AskMenuClick;
FAskMenu.ShortCut := TextToShortCut('Ctrl+Alt+Shift+C');
end;
if not Assigned(FSettingMenu) then
begin
FSettingMenu := TMenuItem.Create(nil);
FSettingMenu.Name := 'Mnu_ChatGPTSetting';
FSettingMenu.Caption := 'ChatGPT Setting';
FSettingMenu.OnClick := ChatGPTSettingMenuClick;
FSettingMenu.ImageIndex := 36;
end;
{$REGION 'Hidden menus, to use shortcuts before creation of the real menu inside the Editor'}
if not Assigned(FAskSubMenuH) then
begin
FAskSubMenuH := TMenuItem.Create(nil);
FAskSubMenuH.Tag := 0;
FAskSubMenuH.ShortCut := TextToShortCut('Ctrl+Alt+Shift+A');
FAskSubMenuH.OnClick := AskSubmenuHiddenOnClick;
FAskSubMenuH.Visible := False;
end;
if not Assigned(FAddTestH) then
begin
FAddTestH := TMenuItem.Create(nil);
FAddTestH.Tag := 1;
FAddTestH.ShortCut := TextToShortCut('Ctrl+Alt+Shift+T');
FAddTestH.OnClick := AskSubmenuHiddenOnClick;
FAddTestH.Visible := False;
end;
if not Assigned(FFindBugsH) then
begin
FFindBugsH := TMenuItem.Create(nil);
FFindBugsH.Tag := 2;
FFindBugsH.ShortCut := TextToShortCut('Ctrl+Alt+Shift+B');
FFindBugsH.OnClick := AskSubmenuHiddenOnClick;
FFindBugsH.Visible := False;
end;
if not Assigned(FOptimizeH) then
begin
FOptimizeH := TMenuItem.Create(nil);
FOptimizeH.Tag := 3;
FOptimizeH.ShortCut := TextToShortCut('Ctrl+Alt+Shift+O');
FOptimizeH.OnClick := AskSubmenuHiddenOnClick;
FOptimizeH.Visible := False;
end;
if not Assigned(FAddCommentsH) then
begin
FAddCommentsH := TMenuItem.Create(nil);
FAddCommentsH.Tag := 4;
FAddCommentsH.ShortCut := TextToShortCut('Ctrl+Alt+Shift+M');
FAddCommentsH.OnClick := AskSubmenuHiddenOnClick;
FAddCommentsH.Visible := False;
end;
if not Assigned(FCompleteCodeH) then
begin
FCompleteCodeH := TMenuItem.Create(nil);
FCompleteCodeH.Tag := 5;
FCompleteCodeH.ShortCut := TextToShortCut('Ctrl+Alt+Shift+K');
FCompleteCodeH.OnClick := AskSubmenuHiddenOnClick;
FCompleteCodeH.Visible := False;
end;
if not Assigned(FAsmConvert) then
begin
FCompleteCodeH := TMenuItem.Create(nil);
FCompleteCodeH.Tag := 8;
FCompleteCodeH.ShortCut := TextToShortCut('Ctrl+Alt+Shift+S');
FCompleteCodeH.OnClick := AskSubmenuHiddenOnClick;
FCompleteCodeH.Visible := False;
end;
{$ENDREGION}
if not Assigned(FAskMenuDockable) then
begin
FAskMenuDockable := TMenuItem.Create(nil);
FAskMenuDockable.Name := 'Mnu_ChatGPTDockable';
FAskMenuDockable.Caption := 'ChatGPT Dockabale';
FAskMenuDockable.OnClick := ChatGPTDockableMenuClick;
end;
if not Assigned(FAboutMenu) then
begin
FAboutMenu := TMenuItem.Create(nil);
FAboutMenu.Name := 'Mnu_ChatGPTAbout';
FAboutMenu.Caption := 'About';
FAboutMenu.OnClick := ChatGPTAboutMenuClick;
FAboutMenu.ImageIndex := 50;
end;
if not Assigned(FRoot) then
begin
FRoot := TMenuItem.Create(nil);
FRoot.Caption := 'ChatGPTWizard';
FRoot.Name := 'ChatGPTRootMenu';
FRoot.Add(FAskMenu);
FRoot.Add(FAskMenuDockable);
FRoot.Add(FSettingMenu);
FRoot.Add(FAskSubMenuH);
FRoot.Add(FAddTestH);
FRoot.Add(FFindBugsH);
FRoot.Add(FOptimizeH);
FRoot.Add(FAddCommentsH);
FRoot.Add(FCompleteCodeH);
FRoot.Add(FAboutMenu);
end;
if not Assigned((BorlandIDEServices as INTAServices).MainMenu.Items.Find('ChatGPTRootMenu')) then
begin
LvMainMenu := (BorlandIDEServices as INTAServices).MainMenu;
LvMainMenu.Items.Insert(LvMainMenu.Items.Count - 1, FRoot);
LoadSubMenuIcons(LvMainMenu);
end;
TSingletonSettingObj.Instance.RootMenuIndex := LvMainMenu.Items.IndexOf(FRoot);
TSingletonSettingObj.Instance.ReadRegistry;
end;
procedure TChatGptMenuWizard.AskMenuClick(Sender: TObject);
var
LvSettingObj: TSingletonSettingObj;
FrmChatGPTMain: TFrmChatGPT;
begin
LvSettingObj := TSingletonSettingObj.Instance;
LvSettingObj.ReadRegistry;
if LvSettingObj.ApiKey = '' then
begin
if (LvSettingObj.IsOffline) and (LvSettingObj.GetSetting.Trim.IsEmpty) then
Exit;
end;
if LvSettingObj.ApiKey <> EmptyStr then
begin
FrmChatGPTMain := TFrmChatGPT.Create(Application);
try
LvSettingObj.RegisterFormClassForTheming(TFrmChatGPT, FrmChatGPTMain); //Apply Theme
RenewUI(FrmChatGPTMain);
FrmChatGPTMain.ShowModal;
finally
FreeAndNil(FrmChatGPTMain);
end;
end;
end;
procedure TChatGptMenuWizard.AskSubmenuHiddenOnClick(Sender: TObject);
begin
if Sender is TMenuItem then
begin
with TEditNotifierHelper do
begin
case TMenuItem(Sender).Tag of
0: DoAskSubMenu;
1: DoAddTest;
2: DoFindBugs;
3: DoOptimize;
4: DoAddComments;
5: DoCompleteCode;
6: DoExplain;
7: DoRefactor;
8: DoConvertToAssembly;
end;
end;
end;
end;
procedure TChatGptMenuWizard.BeforeSave;
begin
//Do noting yet, its created by interface force!
end;
procedure TChatGptMenuWizard.Destroyed;
begin
if Assigned(FAskMenu) then
FreeAndNil(FAskMenu);
if Assigned(FAskMenuDockable) then
FreeAndNil(FAskMenuDockable);
if Assigned(FSettingMenu) then
FreeAndNil(FSettingMenu);
if Assigned(FRoot) then
FreeAndNil(FRoot);
end;
procedure TChatGptMenuWizard.Execute;
begin
//Do noting yet, its created by interface force!
end;
function TChatGptMenuWizard.GetIDString: string;
begin
Result := 'ChatGptIDString';
end;
function TChatGptMenuWizard.GetName: string;
begin
Result := 'ChatGptName';
end;
function TChatGptMenuWizard.GetState: TWizardState;
begin
Result := [wsEnabled];
end;
procedure TChatGptMenuWizard.LoadSubMenuIcons(AMainMenu: TMainMenu);
var
LvResStream: TResourceStream;
LvImgList: TCustomImageList;
LvPngImage: TPngImage;
LvRootMenu: TMenuItem;
LvBmp: TBitmap;
I: Integer;
begin
LvResStream := TResourceStream.Create(HInstance, 'GPT24', RT_RCDATA);
try
LvPngImage := TPngImage.Create;
try
LvPngImage.LoadFromStream(LvResStream);
LvBmp := TBitmap.Create;
try
LvPngImage.Transparent := False;
LvPngImage.AssignTo(LvBmp);
LvBmp.SetSize(16, 16);
LvBmp.Canvas.Draw((16 - LvPngImage.Width) div 2, (16 - LvPngImage.Height) div 2, LvPngImage);
for I := AMainMenu.Items.Count - 1 downto 0 do
begin
LvRootMenu := AMainMenu.Items[I];
if LvRootMenu.Name = 'ChatGPTRootMenu' then
begin
LvImgList := LvRootMenu.GetImageList;
if Assigned(LvImgList) then
begin
try
FAskMenu.ImageIndex := LvImgList.Add(LvBmp, nil);
FAskMenuDockable.ImageIndex := FAskMenu.ImageIndex;
except
FAskMenu.ImageIndex := 1;
FAskMenuDockable.ImageIndex := 1;
end;
end;
Break;
end;
end;
finally
LvBmp.Free;
end;
finally
LvPngImage.Free;
end;
finally
LvResStream.Free;
end;
try
RegisterAboutBox;
except
end;
end;
procedure TChatGptMenuWizard.Modified;
begin
//Do noting yet, its created by interface force!
end;
procedure TChatGptMenuWizard.RenewUI(AForm: TCustomForm);
begin
if not Assigned(AForm) then
Exit;
if TSingletonSettingObj.Instance.RighToLeft then
AForm.BiDiMode := bdRightToLeft
else
AForm.BiDiMode := bdLeftToRight;
Cs.Enter;
if AForm.FindChildControl('Fram_Question') <> nil then
with AForm.FindComponent('Fram_Question') as TFram_Question do
begin
Btn_Ask.Enabled := True;
Align := alClient;
pgcMain.ActivePageIndex := 0;
tsWriteSonicAnswer.TabVisible := (CompilerVersion >= 32) and (TSingletonSettingObj.Instance.EnableWriteSonic);
tsYouChat.TabVisible := (CompilerVersion >= 32) and (TSingletonSettingObj.Instance.EnableYouChat);
if TSingletonSettingObj.Instance.RighToLeft then
begin
Btn_Ask.Left := pnlTop.Width - Btn_Ask.Width - 5;
Btn_Clipboard.Left := Btn_Ask.Left - Btn_Clipboard.Width - 5;
Btn_Clear.Left := Btn_Clipboard.Left - Btn_Clear.Width - 5;
Btn_Ask.Anchors := [TAnchorKind.akTop, TAnchorKind.akRight];
Btn_Clipboard.Anchors := [TAnchorKind.akTop, TAnchorKind.akRight];
Btn_Clear.Anchors := [TAnchorKind.akTop, TAnchorKind.akRight];
end
else
begin
Btn_Ask.Left := 15;
Btn_Clipboard.Left := Btn_Ask.Left + Btn_Ask.Width + 5;
Btn_Clear.Left := Btn_Clipboard.Left + Btn_Clipboard.Width + 5;
Btn_Ask.Anchors := [TAnchorKind.akTop, TAnchorKind.akLeft];
Btn_Clipboard.Anchors := [TAnchorKind.akTop, TAnchorKind.akLeft];
Btn_Clear.Anchors := [TAnchorKind.akTop, TAnchorKind.akLeft];
end;
if TSingletonSettingObj.Instance.IsOffline then
tsChatGPTAnswer.Caption := 'Ollama(Offline)'
else
tsChatGPTAnswer.Caption := 'OpenAI(ChatGPT)';
end;
Cs.Leave;
end;
{$ENDREGION}
{$REGION 'TEditNotifierHelper'}
function TEditNotifierHelper.AddMenuItem(AParentMenu: TMenuItem; AName, ACaption: string; AOnClick: TChatGPTOnCliskType; AShortCut: string; ATag: NativeInt): TMenuItem;
var
LvItem: TMenuItem;
begin
LvItem := TMenuItem.Create(nil);
LvItem.Name := AName;
LvItem.Tag := ATag;
LvItem.Caption := ACaption + IfThen(AShortCut.IsEmpty, '', ' (' + AShortCut + ')');
LvItem.OnClick := AOnClick;
AParentMenu.Add(LvItem);
Result := LvItem;
end;
procedure TEditNotifierHelper.OnChatGPTContextMenuFixedQuestionClick(Sender: TObject);
begin
if Sender is TMenuItem then
begin
case TMenuItem(Sender).Tag of
0: DoAskSubMenu;
1: DoAddTest;
2: DoFindBugs;
3: DoOptimize;
4: DoAddComments;
5: DoCompleteCode;
6: DoExplain;
7: DoRefactor;
8: DoConvertToAssembly;
end;
end;
end;
procedure TEditNotifierHelper.OnChatGPTSubMenuClick(Sender: TObject; MenuItem: TMenuItem);
begin
TSingletonSettingObj.Instance.ReadRegistry;
if not Assigned(MenuItem.Find('Ask')) then
begin
AddMenuItem(MenuItem, 'ChatGPTAskSubMenu', 'Ask', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+A', 0);
AddMenuItem(MenuItem, 'ChatGPTAddTest', 'Add Test', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+T', 1);
AddMenuItem(MenuItem, 'ChatGPTFindBugs', 'Find Bugs', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+B', 2);
AddMenuItem(MenuItem, 'ChatGPTOptimize', 'Optimize', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+O', 3);
AddMenuItem(MenuItem, 'ChatGPTAddComments', 'Add Comments', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+M', 4);
AddMenuItem(MenuItem, 'ChatGPTCompleteCode', 'Complete Code', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+k', 5);
AddMenuItem(MenuItem, 'ChatGPTExplain', 'Explain Code', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+E', 6);
AddMenuItem(MenuItem, 'ChatGPTRefactor', 'Refactor Code', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+R', 7);
AddMenuItem(MenuItem, 'ChatGPTAsm', 'Convert to Assembly', OnChatGPTContextMenuFixedQuestionClick, 'Ctrl+Alt+Shift+S', 8);
end;
end;
class function TEditNotifierHelper.RefineText(AInput: TStringList; AMsgType: TMsgType): string;
const LcLeftComment = '//************ '; LcRightComment = ' ***************';
var
LvComment: string;
begin
case AMsgType of
mtNone:
LvComment := '';
mtNormalQuestion:
LvComment := '';
mtAddTest:
LvComment := 'Add Test';
mtFindBugs:
LvComment := 'Find Bugs';
mtAddComment:
LvComment := 'Add Comment';
mtOptimize:
LvComment := 'Optimize Code';
mtCompleteCode:
LvComment := 'Complete Code';
mtExplain:
LvComment := 'Explanation';
mtRefactor:
LvComment := 'Refactor';
mtASM:
LvComment := 'Convert to Assembly'
end;
Result := CRLF + '{' + LcLeftComment + LvComment + LcRightComment + CRLF +
AInput.Text + '}';
end;
class procedure TEditNotifierHelper.RunInlineQuestion(AQuestion: string; AMsgType: TMsgType);
begin
if not TSingletonSettingObj.Instance.ApiKey.Trim.IsEmpty then
begin
Frm_Progress := TFrm_Progress.Create(nil);
try
Frm_Progress.SelectedText := AQuestion;
TSingletonSettingObj.RegisterFormClassForTheming(TFrm_Progress, Frm_Progress); //Apply Theme
Frm_Progress.ShowModal;
if not Frm_Progress.Answer.Text.Trim.IsEmpty then
begin
WriteIntoEditor(RefineText(Frm_Progress.Answer, AMsgType));
if not (Frm_Progress.HasError) and (TSingletonSettingObj.Instance.CodeFormatter) then
TEditNotifierHelper.FormatSource;
end;
finally
FreeAndNil(Frm_Progress);
end;
end;
end;
procedure TEditNotifierHelper.AddEditorContextMenu;
var
LvEditorPopUpMenu: TPopupMenu;
begin
LvEditorPopUpMenu := TPopupMenu((BorlandIDEServices as IOTAEditorServices).TopView.GetEditWindow.Form.FindComponent('EditorLocalMenu'));
FMenuHook.HookMenu(LvEditorPopUpMenu);
if FMenuHook.IsHooked(LvEditorPopUpMenu) then
begin
FChatGPTSubMenu := TCpMenuItemDef.Create('ChatGPTSubMenu', 'AI Assistant(ChatGPTWizard)', nil, ipAfter, 'ChatGPTSubMenu');
FChatGPTSubMenu.OnCreated := OnChatGPTSubMenuClick;
FMenuHook.AddMenuItemDef(FChatGPTSubMenu);
end;
end;
constructor TEditNotifierHelper.Create;
begin
inherited Create;
FMenuHook := TCpMenuHook.Create(nil);
end;
class function TEditNotifierHelper.CreateMsg(AType: TMsgType): string;
var
LeftIdentifier: string;
RightIdentifier: string;
begin
case AType of
mtNormalQuestion:
begin
Cs.Enter;
LeftIdentifier := TSingletonSettingObj.Instance.LeftIdentifier;
RightIdentifier := TSingletonSettingObj.Instance.RightIdentifier;
Cs.Leave;
Result := 'There is no selected text with the ChatGPT Plug-in''s desired format, follow the below sample, please.' +
#13 + LeftIdentifier + ' Any Question... ' + RightIdentifier;
end;
mtAddTest,
mtFindBugs,
mtOptimize,
mtCompleteCode,
mtExplain, mtASM: Result := 'There is no selected text.';
end;
end;
destructor TEditNotifierHelper.Destroy;
begin
FMenuHook.Free;
inherited;
end;
class procedure TEditNotifierHelper.DoAskSubMenu;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
begin
//If it is a ChatGPT question
if (SameStr(LeftStr(LvSelectedText, 4).ToLower, TSingletonSettingObj.Instance.LeftIdentifier)) and (SameStr(RightStr(LvSelectedText, 4).ToLower, TSingletonSettingObj.Instance.RightIdentifier)) then
RunInlineQuestion(GetQuestion(LvSelectedText), mtNormalQuestion)
else
ShowMessage(CreateMsg(mtNormalQuestion));
end
else
ShowMessage(CreateMsg(mtNormalQuestion));
end;
class procedure TEditNotifierHelper.DoAddTest;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuAddTest + #13 + LvSelectedText, mtAddTest)
else
ShowMessage(CreateMsg(mtAddTest));
end;
class procedure TEditNotifierHelper.DoFindBugs;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuFindBugs + #13 + LvSelectedText, mtFindBugs)
else
ShowMessage(CreateMsg(mtFindBugs));
end;
class procedure TEditNotifierHelper.DoOptimize;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuOptimize + #13 + LvSelectedText, mtOptimize)
else
ShowMessage(CreateMsg(mtOptimize));
end;
class procedure TEditNotifierHelper.DoAddComments;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuAddComments + #13 + LvSelectedText, mtAddComment)
else
ShowMessage(CreateMsg(mtAddTest));
end;
class procedure TEditNotifierHelper.DoCompleteCode;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuCompleteCode + #13 + LvSelectedText, mtCompleteCode)
else
ShowMessage(CreateMsg(mtCompleteCode));
end;
class procedure TEditNotifierHelper.DoConvertToAssembly;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuConvertToAsm + #13 + LvSelectedText, mtASM)
else
ShowMessage(CreateMsg(mtASM));
end;
class procedure TEditNotifierHelper.DoExplain;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuExplain + #13 + LvSelectedText, mtExplain)
else
ShowMessage(CreateMsg(mtExplain));
end;
class procedure TEditNotifierHelper.DoRefactor;
var
LvSelectedText: string;
begin
LvSelectedText := GetSelectedText;
if not LvSelectedText.IsEmpty then
RunInlineQuestion(ContextMenuRefactor + #13 + LvSelectedText, mtRefactor)
else
ShowMessage(CreateMsg(mtOptimize));
end;
procedure TEditNotifierHelper.DockFormRefresh(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
begin
end;
procedure TEditNotifierHelper.DockFormUpdated(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
begin
end;
procedure TEditNotifierHelper.DockFormVisibleChanged(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
begin
end;
procedure TChatGptMenuWizard.AfterSave;
begin
//Do noting yet, its created by interface force!
end;
procedure TEditNotifierHelper.WindowActivated(const EditWindow: INTAEditWindow);
begin
end;
procedure TEditNotifierHelper.WindowCommand(const EditWindow: INTAEditWindow; Command, Param: Integer; var Handled: Boolean);
begin
end;
procedure TEditNotifierHelper.WindowNotification(const EditWindow: INTAEditWindow; Operation: TOperation);
begin
end;
procedure TEditNotifierHelper.WindowShow(const EditWindow: INTAEditWindow; Show, LoadedFromDesktop: Boolean);
begin
end;
procedure TEditNotifierHelper.EditorViewModified(const EditWindow: INTAEditWindow; const EditView: IOTAEditView);
begin
end;
function TEditNotifierHelper.GetCurrentUnitPath: string;
var
ModuleServices: IOTAModuleServices;
CurrentModule: IOTAModule;
begin
Result := '';
if Supports(BorlandIDEServices, IOTAModuleServices) then
begin
try
ModuleServices := BorlandIDEServices as IOTAModuleServices;
CurrentModule := ModuleServices.CurrentModule;
Result := ExtractFileName(CurrentModule.CurrentEditor.FileName);
except
Result := '';
end;
end;
end;
procedure TEditNotifierHelper.EditorViewActivated(const EditWindow: INTAEditWindow; const EditView: IOTAEditView);
var
LvCurrentUnitName: string;
begin
if not Assigned(FChatGPTSubMenu) then
AddEditorContextMenu;
LvCurrentUnitName := GetCurrentUnitPath;
Cs.Enter;
if Assigned(FChatGPTDockForm) and (FChatGPTDockForm.Showing) and (FChatGPTDockForm.Fram_Question.pgcMain.ActivePageIndex = 1) and
(not LvCurrentUnitName.Equals(TSingletonSettingObj.Instance.CurrentActiveViewName)) then
begin
TSingletonSettingObj.Instance.CurrentActiveViewName := LvCurrentUnitName;
Cs.Leave;
if Assigned(FChatGPTDockForm) then
begin
with FChatGPTDockForm.Fram_Question do
begin
if (pgcMain.ActivePage = tsClassView) and (not ClassViewIsBusy) then
ReloadClassList(FChatGPTDockForm.FDockFormClassListObj);
end;
end;
end;
end;
class procedure TEditNotifierHelper.FormatSource;
var
LvEditorPopUpMenu: TPopupMenu;
LvFormatSource: TMenuItem;
begin
LvEditorPopUpMenu := TPopupMenu((BorlandIDEServices as IOTAEditorServices).TopView.GetEditWindow.Form.FindComponent('EditorLocalMenu'));
if LvEditorPopUpMenu <> nil then
begin
LvFormatSource := LvEditorPopUpMenu.Items.Find('Format Source');
if Assigned(LvFormatSource) then
LvFormatSource.Click;
end;
end;