-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
RegionExpertImpl.pas
286 lines (251 loc) · 8.17 KB
/
RegionExpertImpl.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
unit RegionExpertImpl;
interface
{$IF CompilerVersion>=16.0}
uses
SysUtils, Classes, ToolsAPI;
type
TRegionExpertBinding = class(TNotifierObject, IOTAKeyboardBinding)
public
procedure GotoRegion(const Context: IOTAKeyContext;
KeyCode: TShortCut; var BindingResult: TKeyBindingResult);
// IOTAKeyBoardBinding
function GetBindingType: TBindingType;
function GetDisplayName: string;
function GetName: string;
procedure BindKeyboard(const BindingServices: IOTAKeyBindingServices);
end;
{$IFEND}
implementation
{$IF CompilerVersion>=16.0}
uses
Menus, OTAUtils, Windows, Forms, frmGotoRegionImpl, Controls, TextUtils,
DelphiParserImpl;
type
TModuleRegion = class
protected
FModule: IOTAModule;
FRegion: TOTARegion;
public
constructor Create(const AModule: IOTAModule; const ARegion: TOTARegion);
destructor Destroy; override;
property Module: IOTAModule read FModule;
property Region: TOTARegion read FRegion;
end;
TRegionList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
end;
constructor TModuleRegion.Create(const AModule: IOTAModule; const ARegion: TOTARegion);
begin
FModule := AModule;
FRegion := ARegion;
end;
destructor TModuleRegion.Destroy;
begin
FModule := nil;
inherited;
end;
procedure TRegionList.Notify(Ptr: Pointer; Action: TListNotification);
var
P: TModuleRegion;
begin
if Action = lnDeleted then
begin
P := Ptr;
P.Free;
end;
end;
{$REGION 'TRegionExpert implementation'}
{ TRegionExpertBinding }
procedure TRegionExpertBinding.BindKeyboard(
const BindingServices: IOTAKeyBindingServices);
begin
BindingServices.AddKeyBinding([Shortcut(Ord('O'), [ssCtrl]),
Shortcut(Ord('X'), [ssCtrl])], GotoRegion, nil);
end;
function TRegionExpertBinding.GetBindingType: TBindingType;
begin
Result := btPartial;
end;
function TRegionExpertBinding.GetName: string;
begin
Result := 'chuacw.Productivity Experts.RegionExpert';
end;
function TRegionExpertBinding.GetDisplayName: string;
begin
Result := 'Chee Wee''s Region Expert';
end;
{$REGION 'GotoRegion implementation'}
procedure TRegionExpertBinding.GotoRegion(const Context: IOTAKeyContext;
KeyCode: TShortCut; var BindingResult: TKeyBindingResult);
var
MS: IOTAMessageServices;
MG: IOTAMessageGroup;
procedure InternalGotoRegion(const AModuleRegion: TModuleRegion);
var
SourceEditor: IOTASourceEditor;
EditView: IOTAEditView;
ElideAction: IOTAElideActions;
Module: IOTAModule;
Region: TOTARegion;
begin
Module := AModuleRegion.Module;
Region := AModuleRegion.Region;
if Supports(Module.CurrentEditor, IOTASourceEditor, SourceEditor) then
begin
EditView := SourceEditor.EditViews[0];
SourceEditor.Show;
SourceEditor.SwitchToView(0);
EditView.Position.Move(Region.Start.Line, 1);
if Supports(EditView, IOTAElideActions, ElideAction) then
begin
if frmGotoRegion.ExpandRegion then
ElideAction.UnElideNearestBlock;
if frmGotoRegion.GoRegionTop then
EditView.SetTopLeft(Region.Start.Line, 1);
end;
end;
end;
var
List: TRegionList;
procedure CheckModule(Module: IOTAModule);
const
sRegion: array[rkRegion..rkGlobal] of string = (
'Region', 'If', 'Namespace', 'Type', 'Method', 'Nested method', 'Global'
);
var
I, J: Integer;
ModuleRegions: IOTAModuleRegions;
Regions: TOTARegions;
Region: TOTARegion;
SourceEditor: IOTASourceEditor;
EditView: IOTAEditView;
ElideAction: IOTAElideActions;
P: Pointer;
LModuleRegion: TModuleRegion;
Parent, Child: Pointer;
LPosition: IOTAEditPosition;
MethodName, Text: string;
begin
if Module = nil then exit;
{$IFDEF DEBUG}
MS.AddTitleMessage(Format('Module: %s', [Module.FileName]), MG);
{$ENDIF}
if Supports(Module, IOTAModuleRegions, ModuleRegions) then
begin
Parent := nil; Child := nil;
{$IFDEF DEBUG}
MS.AddTitleMessage(Format('IOTAModuleRegions found for %s...',
[Module.CurrentEditor.FileName]), MG);
MS.AddToolMessage(Module.CurrentEditor.FileName, Module.CurrentEditor.FileName,
'', 1, 1, Parent, Child, MG);
Parent := Child;
{$ENDIF}
Regions := ModuleRegions.GetRegions('ProductivityExpertImpl.pas');
for I := Low(Regions) to High(Regions) do
begin
Region := Regions[I];
{$IFDEF DEBUG}
MS.AddToolMessage(Module.CurrentEditor.FileName, Format('Region: "%s" Kind: %s',
[Region.Name, sRegion[Region.RegionKind]]),
'', Region.Start.Line, 1, Parent, Child, MG);
{$ENDIF}
if Region.RegionKind in [rkRegion, rkMethod] then
begin
{$IFDEF DEBUG}
MS.AddTitleMessage(Format('Region Container: %s', [Module.CurrentEditor.FileName]), MG);
{$ENDIF}
if Supports(Module.CurrentEditor, IOTASourceEditor, SourceEditor) then
begin
{$IFDEF DEBUG}
MS.AddTitleMessage(Format('Source Editor supported: %s', [Module.CurrentEditor.FileName]), MG);
{$ENDIF}
EditView := SourceEditor.EditViews[0];
if Region.RegionKind = rkMethod then
begin
LPosition := EditView.Position;
LPosition.Save;
try
LPosition.Move(Region.Start.Line, 1);
Text := LPosition.Read(1024);
for J := 1 to 3 do
begin
if ParseDelphiMethodName(Text, MethodName) then Break;
LPosition.Move(Region.Start.Line-J, 1);
Text := LPosition.Read(1024);
end;
Region.Name := MethodName;
MS.AddToolMessage(Module.CurrentEditor.FileName,
Format('Method: %s', [Region.Name]),
'', Region.Start.Line, 1, Parent, Child, MG);
finally
LPosition.Restore;
end;
end;
if Supports(EditView, IOTAElideActions, ElideAction) then
begin
LModuleRegion := TModuleRegion.Create(Module, Region);
P := LModuleRegion;
List.Add(P);
end;
end;
end;
end;
end;
end;
var
I: Integer;
Project: IOTAProject;
Module: IOTAModule;
ModuleServices: IOTAModuleServices;
LModuleRegion: TModuleRegion;
LCursor: TCursor;
begin
BindingResult := krHandled;
List := TRegionList.Create;
if not Assigned(frmGotoRegion) then
begin
frmGotoRegion := TfrmGotoRegion.Create(Application);
frmGotoRegion.CaptionPrefix := GetDisplayName;
end;
LCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
try
MS := BorlandIDEServices as IOTAMessageServices;
MG := MS.AddMessageGroup(GetDisplayName);
MG.AutoScroll := True;
{$IFDEF DEBUG}
MS.AddTitleMessage('Looking for regions...', MG);
{$ENDIF}
ModuleServices := BorlandIDEServices as IOTAModuleServices;
for I := 0 to ModuleServices.ModuleCount-1 do
CheckModule(ModuleServices.Modules[I]);
for I := 0 to List.Count-1 do
begin
LModuleRegion := List[I];
frmGotoRegion.AddRegion(Format('%s: %s',
[ExtractFileName(LModuleRegion.Module.FileName),
LModuleRegion.Region.Name]));
end;
if (List.Count>0) then
begin
if (frmGotoRegion.ShowModal = mrOk) then
begin
I := frmGotoRegion.Region;
LModuleRegion := List.Items[I];
InternalGotoRegion(LModuleRegion);
end;
end else
begin
MS.AddTitleMessage('No regions found.', MG);
end;
FreeAndNil(frmGotoRegion);
finally
Screen.Cursor := LCursor;
List.Free;
end;
end;
{$ENDREGION 'GotoRegion implementation'}
{$ENDREGION 'TRegionExpert implementation'}
{$IFEND}
end.