Repository navigation
Expand file tree
/
Copy pathAutoCodeTemplatesImpl.pas
More file actions
398 lines (363 loc) · 12.7 KB
/
Copy pathAutoCodeTemplatesImpl.pas
File metadata and controls
398 lines (363 loc) · 12.7 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
// Productivity Experts by Chua Chee Wee, Singapore
//
// Chua Chee Wee,
// Singapore
// 1 May 2005.
//
// 1) Region Expert - marks a block of selected text as a region with a
// keystroke in C# and Delphi.
// 2) Comment Expert - marks a block of selected text as comments with a
// keystroke in C# and Delphi.
// 3) generates XML-styled comments for code declarations with a key
// sequence in Delphi.
// 4) auto comments extension expert - automatically inserts // in between
// blocks of lines in Delphi.
// 5) Goto Region Expert - iterates through all open files and presents a list
// of regions, allowing you to select what region to go to, optionally expanding
// the region, and displaying the selected region at the top of the editor.
// Currently only supports Delphi as there's a bug in the OTAPI which doesn't
// allow it to iterate C# regions. Activate using Ctrl O + Ctrl X ( NOT Ctrl O + X ).
// 6) Code Template Expert - supports C# and Delphi
// Automatically expands any matched code templates. Code Template Names
// may not differ by 1 letter, eg, tryc, trycf. If that happens, the first
// match is always taken, ie, the name of a code template should not be the prefix
// of another. Instead, use something like tryc1, trycf. To try in Delphi, try
// typing arrayc. To try in C#, try typing tryc.
//
// Blog: http://chuacw.ath.cx/chuacw/category/78.aspx
unit AutoCodeTemplatesImpl;
interface
procedure Register;
implementation
uses
Classes, Forms, Menus, Messages, ToolsAPI, IniFiles, CodeTemplatesImpl,
SysUtils, OTAUtils, CodeTemplateInsightImpl, Controls;
type
TIDENotifier = class(TNotifierObject, IOTAIDENotifier)
protected
SL: TStringList;
FCodeTemplates: ICodeTemplates;
public
constructor Create;
procedure AfterCompile(Succeeded: Boolean);
procedure BeforeCompile(const Project: IOTAProject;
var Cancel: Boolean);
procedure FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
destructor Destroy; override;
end;
TEditorNotifier = class(TNotifierObject, IOTANotifier, IOTAEditorNotifier,
IOTAModuleNotifier)
protected
FEditorNotifierIndex,
FModuleNotifierIndex: Integer;
FModule: IOTAModule;
FSourceEditor: IOTASourceEditor;
FCodeTemplates: ICodeTemplates;
SLI: Integer;
FStringList: TStringList;
procedure UpdateIndices;
public
constructor Create(AStringList: TStringList;
AModule: IOTAModule; ASourceEditor: IOTASourceEditor;
const ACodeTemplates: ICodeTemplates);
destructor Destroy; override;
procedure Modified;
procedure ViewActivated(const AView: IOTAEditView);
procedure ViewNotification(const AView: IOTAEditView;
Operation: TOperation);
// IOTAModuleNotifier
function CheckOverwrite: Boolean;
procedure ModuleRenamed(const NewName: string);
property CodeTemplates: ICodeTemplates read FCodeTemplates write FCodeTemplates;
property EditorNotifierIndex: Integer read FEditorNotifierIndex write FEditorNotifierIndex;
property ModuleNotifierIndex: Integer read FModuleNotifierIndex write FModuleNotifierIndex;
property SourceEditor: IOTASourceEditor read FSourceEditor write FSourceEditor;
property Module: IOTAModule read FModule write FModule;
property StringList: TStringList read FStringList write FStringList;
end;
var
NotifierIndex: Integer;
procedure Register;
var
LServices: IOTAServices;
Notifier: IOTAIDENotifier;
begin
LServices := BorlandIDEServices as IOTAServices;
Notifier := TIDENotifier.Create;
NotifierIndex := LServices.AddNotifier(Notifier);
end;
procedure RemoveNotifier;
var
LServices: IOTAServices;
begin
if NotifierIndex <> -1 then
begin
LServices := BorlandIDEServices as IOTAServices;
LServices.RemoveNotifier(NotifierIndex);
end;
end;
{ TIDENotifier }
procedure TIDENotifier.AfterCompile(Succeeded: Boolean);
begin
end;
procedure TIDENotifier.BeforeCompile(const Project: IOTAProject;
var Cancel: Boolean);
begin
end;
constructor TIDENotifier.Create;
var
Services: IOTAServices;
FileName: string;
Path, CodeTemplateName: string;
begin
inherited Create;
SL := THashedStringList.Create;
Services := BorlandIDEServices as IOTAServices;
Path := IncludeTrailingPathDelimiter(Services.GetTemplateDirectory);
FileName := ChangeFileExt(ExtractFileName(Application.ExeName), '.dci');
CodeTemplateName := Path + FileName;
if not FileExists(CodeTemplateName) then
begin
Path := IncludeTrailingPathDelimiter(Services.GetBinDirectory);
CodeTemplateName := Path + FileName;
end;
(BorlandIDEServices as IOTAMessageServices).AddTitleMessage(CodeTemplateName);
FCodeTemplates := TCodeTemplates.Create(CodeTemplateName);
end;
destructor TIDENotifier.Destroy;
var
I: Integer;
LEditorNotifier: TEditorNotifier;
begin
for I := SL.Count - 1 downto 0 do
begin
LEditorNotifier := SL.Objects[I] as TEditorNotifier;
LEditorNotifier.SourceEditor.RemoveNotifier(LEditorNotifier.EditorNotifierIndex);
end;
SL.Free;
inherited;
end;
procedure TIDENotifier.FileNotification(
NotifyCode: TOTAFileNotification; const FileName: string;
var Cancel: Boolean);
procedure RemoveNotifier(ASourceEditor: IOTASourceEditor);
var
LEditorNotifier: TEditorNotifier;
I: Integer;
begin
I := SL.IndexOf(ASourceEditor.FileName);
if I <> -1 then
begin
LEditorNotifier := SL.Objects[I] as TEditorNotifier;
LEditorNotifier.Module.RemoveNotifier(LEditorNotifier.ModuleNotifierIndex);
LEditorNotifier.SourceEditor.RemoveNotifier(LEditorNotifier.EditorNotifierIndex);
if SL.Count > 0 then
TEditorNotifier(SL.Objects[0]).UpdateIndices;
end;
end;
procedure AddNotifier(Module: IOTAModule; ASourceEditor: IOTASourceEditor);
var
I: Integer;
ProjectGroup: IOTAProjectGroup;
begin
ProjectGroup := GetCurrentProjectGroup;
if (not Assigned(ProjectGroup)) or
(ProjectGroup.FileName = ASourceEditor.FileName) then exit;
I := SL.IndexOf(ASourceEditor.FileName);
if I = -1 then
begin
// No need to keep track of instance to free, as it'll be freed automatically.
TEditorNotifier.Create(SL, Module, ASourceEditor, FCodeTemplates);
end;
end;
var
ModuleServices: IOTAModuleServices;
Module: IOTAModule;
Editor: IOTAEditor;
SourceEditor: IOTASourceEditor;
I: Integer;
Project: IOTAProject;
begin
case NotifyCode of
ofnFileOpened:
begin
// When modules are opened
ModuleServices := BorlandIDEServices as IOTAModuleServices;
Module := ModuleServices.FindModule(FileName);
if Supports(Module, IOTAProject, Project) then
begin
Editor := Project.CurrentEditor;
if Supports(Editor, IOTASourceEditor, SourceEditor) then
AddNotifier(Module, SourceEditor);
end;
for I := 0 to Module.ModuleFileCount - 1 do
begin
if Supports(Module.ModuleFileEditors[I], IOTASourceEditor, SourceEditor) and
(SourceEditor.EditViewCount >= 1) then
AddNotifier(Module, SourceEditor) else
// C# support
if Supports(Module.ModuleFileEditors[I], IOTAEditor, Editor) then
if Supports(Editor, IOTASourceEditor, SourceEditor) then
AddNotifier(Module, SourceEditor);
end;
end;
ofnFileClosing:
begin
ModuleServices := BorlandIDEServices as IOTAModuleServices;
Module := ModuleServices.FindModule(FileName);
if Supports(Module, IOTAProject, Project) then
begin
Editor := Project.CurrentEditor;
if Supports(Editor, IOTASourceEditor, SourceEditor) then
RemoveNotifier(SourceEditor);
end;
if Assigned(Module) then
begin
for I := 0 to Module.ModuleFileCount - 1 do
begin
if Supports(Module.ModuleFileEditors[I], IOTASourceEditor, SourceEditor) and
(SourceEditor.EditViewCount >= 1) then
RemoveNotifier(SourceEditor) else
// C# support
if Supports(Module.ModuleFileEditors[I], IOTAEditor, Editor) and
Supports(Editor, IOTASourceEditor, SourceEditor) then
RemoveNotifier(SourceEditor);
end;
end;
end;
end;
end;
{ TEditorNotifier }
function TEditorNotifier.CheckOverwrite: Boolean;
begin
Result := not FileExists(FSourceEditor.FileName);
end;
constructor TEditorNotifier.Create(AStringList: TStringList;
AModule: IOTAModule; ASourceEditor: IOTASourceEditor;
const ACodeTemplates: ICodeTemplates);
begin
inherited Create;
FStringList := AStringList;
AStringList.AddObject(ASourceEditor.FileName, Self);
UpdateIndices;
FSourceEditor := ASourceEditor;
FModule := AModule;
FEditorNotifierIndex := ASourceEditor.AddNotifier(Self);
FModuleNotifierIndex := AModule.AddNotifier(Self);
FCodeTemplates := ACodeTemplates;
end;
destructor TEditorNotifier.Destroy;
var
I: Integer;
begin
I := FStringList.IndexOf(SourceEditor.FileName);
if I <> -1 then
FStringList.Delete(I);
FSourceEditor := nil;
inherited;
end;
procedure TEditorNotifier.Modified;
var
LView: IOTAEditView;
I, Element, Column, LineFlag, PipeIndex: Integer;
Identifier, Template, Template1, Template2: string;
ep: IOTAEditPosition;
CursorPos: TOTAEditPos;
CodeTemplate: TCodeTemplate;
SL: TStringList;
begin
if (SourceEditor.EditViewCount = 0) or
Assigned(CodeTemplateInsight) then
exit;
LView := SourceEditor.EditViews[0];
CursorPos := LView.CursorPos;
if CursorPos.Col > 1 then
Dec(CursorPos.Col);
LView.GetAttributeAtPos(CursorPos, False, Element, LineFlag);
if Element in [atIdentifier, atReservedWord] then
begin
ep := LView.Position;
Identifier := ep.RipText('_', rfBackward or rfIncludeAlphaChars or rfIncludeNumericChars);
if FCodeTemplates.Exists(Identifier, I) then
begin
CodeTemplate := FCodeTemplates.Item[I];
if LView.Buffer.EditOptions.IDString = CodeTemplate.IDString then
begin
CodeTemplateInsight := TCodeTemplateInsight.Create(ep, CodeTemplate,
LView, Length(Identifier));
CodeTemplateInsight.Show;
{
ep.MoveRelative(0, -Length(Identifier));
ep.Delete(Length(Identifier));
Template := CodeTemplate.Template;
SL := TStringList.Create;
try
SL.Text := Template;
if (SL.Count > 1) and (LView.Buffer.EditOptions.BufferOptions.AutoIndent) then
begin
Column := ep.Column;
Template2 := #13#10;
for I := 0 to SL.Count - 1 do
begin
ep.Move(ep.Row, Column);
Template1 := SL[I];
PipeIndex := Pos('|', Template1);
if PipeIndex > 0 then
begin
CursorPos.Line := ep.Row;
CursorPos.Col := PipeIndex + Column - 1;
Delete(Template1, PipeIndex, 1);
end;
if I = SL.Count - 1 then
Template2 := '';
Template1 := Format('%s%s', [Template1, Template2]);
ep.InsertText(Template1);
end;
ep.Move(CursorPos.Line, CursorPos.Col);
end else
begin
I := Pos('|', Template);
Template1 := Copy(Template, 1, I - 1);
Template2 := Copy(Template, I + 1, Length(Template));
ep.InsertText(Template1);
CursorPos := LView.CursorPos;
ep.InsertText(Template2);
LView.CursorPos := CursorPos;
end;
finally
SL.Free;
end;
LView.Paint;
}
end;
end;
end;
end;
procedure TEditorNotifier.ModuleRenamed(const NewName: string);
begin
FStringList[SLI] := NewName;
end;
procedure TEditorNotifier.UpdateIndices;
var
I: Integer;
LEditorNotifier: TEditorNotifier;
begin
for I := 0 to FStringList.Count - 1 do
begin
LEditorNotifier := TEditorNotifier(FStringList.Objects[I]);
LEditorNotifier.SLI := I;
end;
end;
procedure TEditorNotifier.ViewActivated(const AView: IOTAEditView);
begin
end;
procedure TEditorNotifier.ViewNotification(const AView: IOTAEditView;
Operation: TOperation);
begin
end;
initialization
NotifierIndex := -1;
finalization
RemoveNotifier;
end.