-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGDK.ToolsAPI.Helper.pas
738 lines (593 loc) · 18.2 KB
/
GDK.ToolsAPI.Helper.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
unit GDK.ToolsAPI.Helper;
interface
uses
ToolsAPI,
GDK.ToolsAPI.Helper.Interfaces,
System.SysUtils,
GDK.ToolsAPI.CustomMessage;
type
TToolsApiHelper = class(TInterfacedObject, IToolsApiHelper)
private
FEditView: IToolsApiEditView;
public
function Logger: IToolsApiLogger; overload;
function Logger(const GroupName: string): IToolsApiLogger; overload;
function ProjectGroup: IOTAProjectGroup;
function Project: IToolsApiProject; overload;
function Project(const Project: IOTAProject): IToolsApiProject; overload;
function ProjectContextMenu: IToolsApiProjectContextMenu;
function BuildConfigurations: IToolsApiBuildConfigurations;
function ModuleCount: Integer;
function Module: IToolsApiModule; overload;
function Module(const Module: IOTAModule): IToolsApiModule; overload;
function Module(const Index: Integer): IToolsApiModule; overload;
function SourceEditor: IToolsApiSourceEditor;
function EditorReader: IToolsApiEditReader;
function EditorWriter: IToolsApiEditWriter;
procedure EditorInsertText(const Text: TArray<string>; const Position: TOTAEditPos); overload;
procedure EditorInsertText(const Text: TArray<string>; const AtLine: Integer); overload;
function EditorPosition: TOTAEditPos;
function EditorContent: string;
function EditView: IToolsApiEditView;
end;
TToolsApiLogger = class(TInterfacedObject, IToolsApiLogger)
private
FGroupName: string;
public
constructor Create(const GroupName: string);
function UsesCustomGroup: Boolean;
function GetGroup: IOTAMessageGroup;
procedure Clear;
procedure Log(const Text: string); overload;
procedure Log(const Text: string; const Params: array of const); overload;
function Custom: TCustomMessage;
end;
TToolsApiProject = class(TInterfacedObject, IToolsApiProject)
private
FProject: IOTAProject;
procedure Guard;
public
constructor Create; overload;
constructor Create(const Project: IOTAProject); overload;
function Get: IOTAProject;
function ProjectConfigurations: IOTAProjectOptionsConfigurations;
function BuildConfigurations: IToolsApiBuildConfigurations;
end;
TToolsApiModule = class(TInterfacedObject, IToolsApiModule)
private
FModule: IOTAModule;
procedure Guard;
public
constructor Create; overload;
constructor Create(const Module: IOTAModule); overload;
constructor Create(const Index: Integer); overload;
function Get: IOTAModule;
function FileCount: Integer;
function FileName: string;
procedure Refresh(const ForceRefresh: Boolean);
function Editor(const Predicate: TFunc<IOTAEditor, Boolean>): IOTAEditor;
function SourceEditor(const Predicate: TFunc<IOTASourceEditor, Boolean> = nil): IToolsApiSourceEditor;
function FormEditor(const Predicate: TFunc<IOTAFormEditor, Boolean> = nil): IOTAFormEditor;
end;
TToolsApiEditView = class(TInterfacedObject, IToolsApiEditView)
private
FEditorServices: IOTAEditorServices;
public
constructor Create;
function TopView: IOTAEditView;
function CursorPosition: TOTAEditPos;
procedure InsertText(const Text: TArray<string>; const Position: TOTAEditPos);
end;
TToolsApiSourceEditor = class(TInterfacedObject, IToolsApiSourceEditor)
private
FEditor: IOTASourceEditor;
procedure AddToUses(const UnitNames: TArray<string>; const AddToImplementation: Boolean);
public
constructor Create(const Editor: IOTASourceEditor);
function Get: IOTASourceEditor;
function Reader: IToolsApiEditReader;
function Writer: IToolsApiEditWriter;
function UndoableWriter: IToolsApiEditWriter;
procedure AddToInterfaceUses(const UnitName: string); overload;
procedure AddToInterfaceUses(const UnitNames: TArray<string>); overload;
procedure AddToImplementationUses(const UnitName: string); overload;
procedure AddToImplementationUses(const UnitNames: TArray<string>); overload;
end;
TToolsApiEditReader = class(TInterfacedObject, IToolsApiEditReader)
private
FReader: IOTAEditReader;
public
constructor Create(const Reader: IOTAEditReader);
function Get: IOTAEditReader;
function Content: string;
end;
TToolsApiEditWriter = class(TInterfacedObject, IToolsApiEditWriter)
private
FWriter: IOTAEditWriter;
public
constructor Create(const Writer: IOTAEditWriter);
function Get: IOTAEditWriter;
procedure InsertText(const Text: string; const Position: Integer); overload;
procedure InsertText(const Text: string; const Position: TOTAEditPos); overload;
end;
TToolsApiBuildConfigurations = class(TInterfacedObject, IToolsApiBuildConfigurations)
private
FProject: IOTAProject;
FConfigurations: IOTAProjectOptionsConfigurations;
public
constructor Create(const Project: IOTAProject);
function Base: IToolsApiBuildConfiguration;
function Active: IToolsApiBuildConfiguration;
end;
TToolsApiBuildConfiguration = class(TInterfacedObject, IToolsApiBuildConfiguration)
private
FConfiguration: IOTABuildConfiguration;
function GetSearchPaths: TArray<string>;
procedure SetSearchPaths(const Paths: TArray<string>);
public
constructor Create(const Configuration: IOTABuildConfiguration);
function Get: IOTABuildConfiguration;
property SearchPaths: TArray<string> read GetSearchPaths write SetSearchPaths;
end;
implementation
uses
System.Classes,
DCCStrs,
GDK.ToolsAPI.ProjectManagerContextMenu,
GDK.ToolsAPI.UsesManager,
GDK.ToolsAPI.UsesBuilder;
{ TToolsApiHelper }
function TToolsApiHelper.Project: IToolsApiProject;
begin
Result := TToolsApiProject.Create;
end;
function TToolsApiHelper.Project(const Project: IOTAProject): IToolsApiProject;
begin
Result := TToolsApiProject.Create(Project);
end;
function TToolsApiHelper.ProjectContextMenu: IToolsApiProjectContextMenu;
begin
Result := TToolsApiProjectContextMenu.Create;
end;
function TToolsApiHelper.ProjectGroup: IOTAProjectGroup;
var
ModuleServices: IOTAModuleServices;
Module: IOTAModule;
ProjectGroup: IOTAProjectGroup;
i: Integer;
Begin
ProjectGroup := nil;
ModuleServices := (BorlandIDEServices as IOTAModuleServices);
for i := 0 to ModuleServices.ModuleCount - 1 do
begin
Module := ModuleServices.Modules[i];
if (Module.QueryInterface(IOTAProjectGroup, ProjectGroup) = S_OK) Then
Break;
end;
Result := ProjectGroup;
end;
function TToolsApiHelper.Module: IToolsApiModule;
begin
Result := TToolsApiModule.Create;
end;
function TToolsApiHelper.Module(const Module: IOTAModule): IToolsApiModule;
begin
Result := TToolsApiModule.Create(Module);
end;
function TToolsApiHelper.Module(const Index: Integer): IToolsApiModule;
begin
Result := TToolsApiModule.Create(Index);
end;
function TToolsApiHelper.ModuleCount: Integer;
var
ModuleServices: IOTAModuleServices;
begin
ModuleServices := (BorlandIDEServices as IOTAModuleServices);
Result := ModuleServices.ModuleCount;
end;
function TToolsApiHelper.SourceEditor: IToolsApiSourceEditor;
begin
Result := Self.Module.SourceEditor;
end;
function TToolsApiHelper.BuildConfigurations: IToolsApiBuildConfigurations;
var
ActiveProject: IOTAProject;
begin
ActiveProject := Self.Project.Get;
Result := TToolsApiBuildConfigurations.Create(ActiveProject);
end;
function TToolsApiHelper.EditorReader: IToolsApiEditReader;
begin
Result := Self.SourceEditor.Reader;
end;
function TToolsApiHelper.EditorWriter: IToolsApiEditWriter;
begin
Self.SourceEditor.Writer;
end;
function TToolsApiHelper.EditView: IToolsApiEditView;
begin
if not Assigned(FEditView) then
FEditView := TToolsApiEditView.Create;
Result := FEditView;
end;
function TToolsApiHelper.Logger: IToolsApiLogger;
begin
Result := Self.Logger('');
end;
function TToolsApiHelper.Logger(const GroupName: string): IToolsApiLogger;
begin
Result := TToolsApiLogger.Create(GroupName);
end;
procedure TToolsApiHelper.EditorInsertText(const Text: TArray<string>; const Position: TOTAEditPos);
begin
Self.EditView.InsertText(Text, Position);
end;
procedure TToolsApiHelper.EditorInsertText(const Text: TArray<string>; const AtLine: Integer);
begin
var Lines := string.Join(sLineBreak, Text);
Self.EditorWriter.InsertText(Lines, AtLine);
end;
function TToolsApiHelper.EditorPosition: TOTAEditPos;
begin
Result := Self.EditView.CursorPosition;
end;
function TToolsApiHelper.EditorContent: string;
begin
Result := EditorReader.Content;
end;
{ TToolsApiProject }
constructor TToolsApiProject.Create;
begin
inherited Create;
FProject := GetActiveProject;
Guard;
end;
constructor TToolsApiProject.Create(const Project: IOTAProject);
begin
inherited Create;
FProject := Project;
Guard;
end;
function TToolsApiProject.Get: IOTAProject;
begin
Result := FProject;
end;
function TToolsApiProject.ProjectConfigurations: IOTAProjectOptionsConfigurations;
begin
Result := FProject.ProjectOptions as IOTAProjectOptionsConfigurations;
end;
function TToolsApiProject.BuildConfigurations: IToolsApiBuildConfigurations;
begin
Result := TToolsApiBuildConfigurations.Create(FProject);
end;
procedure TToolsApiProject.Guard;
begin
if not Assigned(FProject) then
raise EToolsApiNoProjectFound.Create('No active project found');
end;
{ TToolsApiModule }
constructor TToolsApiModule.Create;
begin
inherited Create;
FModule := (BorlandIDEServices as IOTAModuleServices).CurrentModule;
Guard;
end;
constructor TToolsApiModule.Create(const Module: IOTAModule);
begin
inherited Create;
FModule := Module;
Guard;
end;
constructor TToolsApiModule.Create(const Index: Integer);
begin
inherited Create;
FModule := (BorlandIDEServices as IOTAModuleServices).Modules[Index];
Guard;
end;
function TToolsApiModule.Get: IOTAModule;
begin
Result := FModule;
end;
function TToolsApiModule.FileCount: Integer;
begin
Result := FModule.GetModuleFileCount;
end;
function TToolsApiModule.FileName: string;
begin
Result := FModule.FileName;
end;
procedure TToolsApiModule.Refresh(const ForceRefresh: Boolean);
begin
FModule.Refresh(ForceRefresh);
end;
function TToolsApiModule.Editor(const Predicate: TFunc<IOTAEditor, Boolean>): IOTAEditor;
var
i: Integer;
Editor: IOTAEditor;
begin
Result := nil;
for i := 0 to FileCount - 1 do
begin
Editor := FModule.GetModuleFileEditor(i);
if Assigned(Predicate) and not Predicate(Editor) then
Continue;
Result := Editor;
Break;
end;
if not Assigned(Result) then
raise EToolsApiNoEditorFound.Create('No editor found.');
end;
function TToolsApiModule.SourceEditor(const Predicate: TFunc<IOTASourceEditor, Boolean>): IToolsApiSourceEditor;
var
Found: IOTAEditor;
SourceEditor: IOTASourceEditor;
begin
Found := Editor(
function(Editor: IOTAEditor): Boolean
begin
Result := Supports(Editor, IOTASourceEditor);
end);
SourceEditor := Found as IOTASourceEditor;
Result := TToolsApiSourceEditor.Create(SourceEditor);
end;
function TToolsApiModule.FormEditor(const Predicate: TFunc<IOTAFormEditor, Boolean>): IOTAFormEditor;
var
Found: IOTAEditor;
begin
Found := Editor(
function(Editor: IOTAEditor): Boolean
begin
Result := Supports(Editor, IOTAFormEditor);
end);
Result := Found as IOTAFormEditor;
end;
procedure TToolsApiModule.Guard;
begin
if not Assigned(FModule) then
raise EToolsApiNoModuleFound.Create('No active module found.');
end;
{ TToolsApiSourceEditor }
constructor TToolsApiSourceEditor.Create(const Editor: IOTASourceEditor);
begin
inherited Create;
FEditor := Editor;
end;
function TToolsApiSourceEditor.Get: IOTASourceEditor;
begin
Result := FEditor;
end;
function TToolsApiSourceEditor.Reader: IToolsApiEditReader;
var
Reader: IOTAEditReader;
begin
Reader := FEditor.CreateReader;
Result := TToolsApiEditReader.Create(Reader);
end;
function TToolsApiSourceEditor.Writer: IToolsApiEditWriter;
begin
var Writer := FEditor.CreateWriter;
Result := TToolsApiEditWriter.Create(Writer);
end;
function TToolsApiSourceEditor.UndoableWriter: IToolsApiEditWriter;
begin
var Writer := FEditor.CreateUndoableWriter;
Result := TToolsApiEditWriter.Create(Writer);
end;
procedure TToolsApiSourceEditor.AddToInterfaceUses(const UnitName: string);
begin
AddToUses([UnitName], False);
end;
procedure TToolsApiSourceEditor.AddToInterfaceUses(const UnitNames: TArray<string>);
begin
AddToUses(UnitNames, False);
end;
procedure TToolsApiSourceEditor.AddToImplementationUses(const UnitName: string);
begin
AddToUses([UnitName], True);
end;
procedure TToolsApiSourceEditor.AddToImplementationUses(const UnitNames: TArray<string>);
begin
AddToUses(UnitNames, True);
end;
procedure TToolsApiSourceEditor.AddToUses(const UnitNames: TArray<string>; const AddToImplementation: Boolean);
begin
var UsesBuilder := TToolsAPIUsesBuilder.Use;
if AddToImplementation then
UsesBuilder.InImplementation
else
UsesBuilder.InInterface;
var EditorContent := Self.Reader.Content;
var UsesToWrite := UsesBuilder
.WithSource(EditorContent)
.Build(UnitNames);
if UsesToWrite.CanWrite then
Self.UndoableWriter.InsertText(UsesToWrite.Text, UsesToWrite.Position);
end;
{ TToolsApiEditReader }
constructor TToolsApiEditReader.Create(const Reader: IOTAEditReader);
begin
inherited Create;
FReader := Reader;
end;
function TToolsApiEditReader.Get: IOTAEditReader;
begin
Result := FReader;
end;
function TToolsApiEditReader.Content: string;
const
BufferSize: Integer = 1024;
var
Read: Integer;
Position: Integer;
Buffer: AnsiString;
begin
Result := '';
Position := 0;
repeat
SetLength(Buffer, BufferSize);
Read := FReader.GetText(Position, PAnsiChar(Buffer), BufferSize);
SetLength(Buffer, Read);
Result := Result + string(Buffer);
Inc(Position, Read);
until (Read < BufferSize);
end;
{ TToolsApiBuildConfiguration }
constructor TToolsApiBuildConfiguration.Create(const Configuration: IOTABuildConfiguration);
begin
inherited Create;
FConfiguration := Configuration;
end;
function TToolsApiBuildConfiguration.Get: IOTABuildConfiguration;
begin
Result := FConfiguration;
end;
function TToolsApiBuildConfiguration.GetSearchPaths: TArray<string>;
var
Paths: TStringList;
begin
Paths := TStringList.Create;
try
FConfiguration.GetValues(DCCStrs.sUnitSearchPath, Paths, True);
Result := Paths.ToStringArray;
finally
Paths.Free;
end;
end;
procedure TToolsApiBuildConfiguration.SetSearchPaths(const Paths: TArray<string>);
var
SearchPaths: TStringList;
begin
SearchPaths := TStringList.Create;
try
SearchPaths.AddStrings(Paths);
FConfiguration.SetValues(sUnitSearchPath, SearchPaths);
finally
SearchPaths.Free;
end;
end;
{ TToolsApiEditWriter }
constructor TToolsApiEditWriter.Create(const Writer: IOTAEditWriter);
begin
inherited Create;
FWriter := Writer;
end;
function TToolsApiEditWriter.Get: IOTAEditWriter;
begin
Result := FWriter;
end;
procedure TToolsApiEditWriter.InsertText(const Text: string; const Position: Integer);
begin
FWriter.CopyTo(Position);
FWriter.Insert(PAnsiChar(AnsiString(Text)));
end;
procedure TToolsApiEditWriter.InsertText(const Text: string; const Position: TOTAEditPos);
var
EditView: IToolsApiEditView;
begin
EditView := TToolsApiEditView.Create;
EditView.InsertText([Text], Position);
end;
{ TToolsApiEditView }
constructor TToolsApiEditView.Create;
begin
inherited Create;
FEditorServices := (BorlandIDEServices as IOTAEditorServices);
end;
function TToolsApiEditView.TopView: IOTAEditView;
begin
Result := FEditorServices.TopView;
if not Assigned(Result) then
raise EToolsApiNoEditViewFound.Create('No edit view found.');
end;
function TToolsApiEditView.CursorPosition: TOTAEditPos;
begin
Result := TopView.CursorPos;
end;
procedure TToolsApiEditView.InsertText(const Text: TArray<string>; const Position: TOTAEditPos);
var
Line: string;
EditView: IOTAEditView;
LineNumber, Col: Integer;
begin
EditView := TopView;
LineNumber := Position.Line;
Col := Position.Col;
for Line in Text do
begin
EditView.Buffer.EditPosition.Move(LineNumber, Col);
EditView.Buffer.EditPosition.InsertText(Line + sLineBreak);
Inc(LineNumber);
end;
end;
{ TToolsApiBuildConfigurations }
constructor TToolsApiBuildConfigurations.Create(const Project: IOTAProject);
begin
inherited Create;
FProject := Project;
FConfigurations := FProject.ProjectOptions as IOTAProjectOptionsConfigurations;
end;
function TToolsApiBuildConfigurations.Active: IToolsApiBuildConfiguration;
var
Config: IOTABuildConfiguration;
begin
Config := FConfigurations.ActiveConfiguration;
Result := TToolsApiBuildConfiguration.Create(Config);
end;
function TToolsApiBuildConfigurations.Base: IToolsApiBuildConfiguration;
var
Config: IOTABuildConfiguration;
begin
Config := FConfigurations.BaseConfiguration;
Result := TToolsApiBuildConfiguration.Create(Config);
end;
{ TToolsApiLogger }
constructor TToolsApiLogger.Create(const GroupName: string);
begin
inherited Create;
FGroupName := GroupName;
end;
function TToolsApiLogger.Custom: TCustomMessage;
var
CustomGroup: IOTAMessageGroup;
begin
CustomGroup := GetGroup;
Result := TCustomMessage.Create(CustomGroup);
end;
function TToolsApiLogger.UsesCustomGroup: Boolean;
begin
Result := (not FGroupName.IsEmpty);
end;
function TToolsApiLogger.GetGroup: IOTAMessageGroup;
begin
if not Self.UsesCustomGroup then
raise EToolsApiNoCustomLogGroupUsed.Create('No custom logging group is used');
Result := (BorlandIDEServices As IOTAMessageServices).AddMessageGroup(FGroupName);
end;
procedure TToolsApiLogger.Clear;
var
CustomGroup: IOTAMessageGroup;
begin
if not Self.UsesCustomGroup then
Exit;
CustomGroup := (BorlandIDEServices As IOTAMessageServices).GetGroup(FGroupName);
(BorlandIDEServices As IOTAMessageServices).ClearMessageGroup(CustomGroup);
end;
procedure TToolsApiLogger.Log(const Text: string; const Params: array of const);
var
FormattedText: string;
begin
FormattedText := Format(Text, Params);
Log(FormattedText);
end;
procedure TToolsApiLogger.Log(const Text: string);
var
LineRef: Pointer;
CustomGroup: IOTAMessageGroup;
begin
if Self.UsesCustomGroup then
CustomGroup := GetGroup
else
CustomGroup := nil;
(BorlandIDEServices As IOTAMessageServices).AddToolMessage('', Text, '', 1, 1, nil, LineRef, CustomGroup);
end;
end.