Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
331 lines (296 loc) · 10.7 KB

File metadata and controls

331 lines (296 loc) · 10.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
unit uIDEExpert;
// Delphi IDE Expert: registriert "Static Code Analysis Tool for Delphi"
// * Tools-Menu-Eintrag mit Icon (via INTAServices.AddImages + TAction)
// * Splash-Screen-Eintrag waehrend IDE-Start
// * Help -> About -> Plugins-Eintrag
//
// Tools-Menu-Pattern (canonical Embarcadero, OTAPI-Docs Kap. 15):
// IOTAMenuWizard wird absichtlich NICHT verwendet - die kann keinen Icon.
// Stattdessen direkt INTAServices.MainMenu manipulieren: BMP laden ->
// in TImageList -> AddImages liefert ImageIndex in der IDE-Shared-Liste ->
// TAction mit OnExecute + ImageIndex -> TMenuItem mit Action -> in das
// ToolsMenu einhaengen.
interface
uses
ToolsAPI;
type
TStaticCodeAnalyserExpert = class(TNotifierObject, IOTAWizard)
public
destructor Destroy; override;
{ IOTAWizard }
function GetIDString: string;
function GetName: string;
function GetState: TWizardState;
procedure Execute;
end;
procedure Register;
implementation
// noinspection-file ClassPerFile, ConcatToFormat, FreeAndNilHint, LowercaseKeyword, NestedRoutine, NestedTry, NilComparison, PublicMemberWithoutDoc, RedundantJump, TooLongLine, UnsortedUses, UnusedLocalVar, UnusedParameter, UnusedPublicMember
// Self-scan Stil-Cluster - im jeweiligen File idiomatisch oder Hot-Path-bedingt.
uses
Winapi.Windows, System.SysUtils,
Vcl.Graphics, Vcl.ImgList, Vcl.Controls, Vcl.Menus, Vcl.ActnList,
uIDEAnalyserForm,
uIDEFindingsPropertiesForm;
const
PLUGIN_TITLE = 'Static Code Analysis';
PLUGIN_DESC = 'Static Code Analysis Tool for Delphi - findings, ' +
'metrics, leak detection, Sonar export.';
PLUGIN_VERSION = 'v0.9.8';
PLUGIN_LICENSE = 'Freeware / Open Source';
// Resource-Namen aus branding\sca_branding.rc (BITMAP-Type).
// 24x24 fuer Splash + About-Box (groesserer Header-Space).
SCA_APP_BMP_RES = 'SCA_APP_BMP';
// 16x16 fuer das IDE-Tools-Menu (Standard-Menu-Icon-Groesse).
SCA_APP_BMP16_RES = 'SCA_APP_BMP16';
type
// Tiny Wrapper-Klasse weil TAction.OnExecute eine Methode (procedure of
// object) erwartet, KEINE freie Prozedur. Lebt waehrend der gesamten
// BPL-Laufzeit und wird in finalization freigegeben.
TToolsMenuHandler = class
public
procedure MenuClick(Sender: TObject);
end;
var
// Index in der About-Box; gebraucht zum Unregister beim BPL-Unload.
// -1 = nicht registriert (z.B. wenn IDE keine AboutBoxServices liefert).
GAboutBoxIndex : Integer = -1;
// Branding-HBITMAP fuer Splash + About-Box, gecached fuer BPL-Laufzeit.
// 0 = nicht geladen (Resource fehlt oder LoadBitmap failed). Lebensdauer
// an die BPL gekoppelt - die IDE haelt das Handle waehrend Splash + About-
// Box, ein verfruehter DeleteObject wuerde dangling references erzeugen.
GBrandingHBmp : HBITMAP = 0;
// Tools-Menu-Eintrag (TMenuItem in INTAServices.MainMenu) + die zugrunde
// liegende TAction. Beide bei BPL-Unload freigeben sonst Dangling-Eintrag
// im IDE-Menue bis IDE-Neustart.
GToolsMenuItem : TMenuItem = nil;
GToolsAction : TAction = nil;
GToolsHandler : TToolsMenuHandler = nil;
procedure TToolsMenuHandler.MenuClick(Sender: TObject);
// OnExecute-Methode der TAction - identisch zum vormaligen IOTAMenuWizard.
// Execute-Pfad: zeigt das Dockable-Form.
begin
ShowAnalyserDockableForm;
end;
function BrandingHBitmap: HBITMAP;
// Canonical Embarcadero-Pattern (OTAPI-Docs Kap. 9): LoadBitmap(HInstance,
// '<resname>') liefert HBITMAP direkt aus der BITMAP-Resource. Null wenn
// Resource fehlt - dann fallen Splash/About auf Text-only zurueck.
begin
if GBrandingHBmp = 0 then
GBrandingHBmp := LoadBitmap(HInstance, SCA_APP_BMP_RES);
Result := GBrandingHBmp;
end;
procedure RegisterSplashScreen;
// Erscheint waehrend des IDE-Starts unter "Loaded plugins" im Splash.
// HBITMAP = 0: kein Icon - der Eintrag wird trotzdem als Text gerendert.
begin
if not Assigned(SplashScreenServices) then Exit;
SplashScreenServices.AddPluginBitmap(
PLUGIN_TITLE + ' ' + PLUGIN_VERSION,
BrandingHBitmap,
False, // IsUnregistered
PLUGIN_LICENSE,
''); // SKUBuild
end;
procedure RegisterAboutBox;
// Eintrag unter Help -> About -> Plugins.
var
Svc : IOTAAboutBoxServices;
begin
if not Supports(BorlandIDEServices, IOTAAboutBoxServices, Svc) then Exit;
GAboutBoxIndex := Svc.AddPluginInfo(
PLUGIN_TITLE + ' ' + PLUGIN_VERSION,
PLUGIN_DESC,
BrandingHBitmap,
False,
PLUGIN_LICENSE,
'');
end;
procedure UnregisterAboutBox;
var
Svc : IOTAAboutBoxServices;
begin
if (GAboutBoxIndex >= 0)
and Supports(BorlandIDEServices, IOTAAboutBoxServices, Svc) then
Svc.RemovePluginInfo(GAboutBoxIndex);
GAboutBoxIndex := -1;
end;
// ---------------------------------------------------------------------------
// Tools-Menu-Eintrag mit Icon
// ---------------------------------------------------------------------------
function FindIDEToolsMenu(MainMenu: TMainMenu): TMenuItem;
// Suche rekursiv nach dem ToolsMenu-Item im IDE-Hauptmenue. Embarcadero
// benennt es 'ToolsMenu' (siehe OTAPI-Docs Kap. 15). Fallback bei sehr
// alten Delphi-Versionen: name='Tools1Item' o.ae. - im Zweifel via
// Top-Level-Walk nach Caption '&Tools'.
var
i : Integer;
M : TMenuItem;
function WalkSub(AParent: TMenuItem): TMenuItem;
var
k : Integer;
begin
Result := nil;
for k := 0 to AParent.Count - 1 do
begin
if SameText(AParent[k].Name, 'ToolsMenu') then Exit(AParent[k]);
Result := WalkSub(AParent[k]);
if Result <> nil then Exit;
end;
end;
begin
Result := nil;
if not Assigned(MainMenu) then Exit;
// Erste Welle: rekursiver Name-Match auf 'ToolsMenu'.
for i := 0 to MainMenu.Items.Count - 1 do
begin
M := MainMenu.Items[i];
if SameText(M.Name, 'ToolsMenu') then Exit(M);
Result := WalkSub(M);
if Result <> nil then Exit;
end;
// Fallback: Top-Level nach Caption '&Tools' / 'Tools' suchen.
for i := 0 to MainMenu.Items.Count - 1 do
begin
M := MainMenu.Items[i];
if SameText(M.Caption, '&Tools') or SameText(M.Caption, 'Tools') then
Exit(M);
end;
end;
procedure RegisterToolsMenuItem;
// 1) BMP-Resource laden + in TImageList -> INTAServices.AddImages liefert
// den IDE-globalen ImageIndex.
// 2) TAction mit OnExecute + ImageIndex erzeugen.
// 3) TMenuItem mit der Action ans Ende des ToolsMenu haengen.
// Fehlerpfade still: wenn AddImages oder FindIDEToolsMenu nichts liefert,
// kein Menue-Eintrag - das Plugin bleibt aber ueber den Splash erreichbar.
var
NTAS : INTAServices;
IL : TImageList;
BM : TBitmap;
ImageIndex : Integer;
ToolsMenu : TMenuItem;
begin
if not Supports(BorlandIDEServices, INTAServices, NTAS) then Exit;
ToolsMenu := FindIDEToolsMenu(NTAS.MainMenu);
if not Assigned(ToolsMenu) then Exit;
ImageIndex := -1;
// ImageList nur als Transport-Vehikel - AddImages kopiert intern.
IL := TImageList.Create(nil);
try
BM := TBitmap.Create;
try
try
BM.LoadFromResourceName(HInstance, SCA_APP_BMP16_RES);
// clWhite -> transparent (24-bit BMP hat keinen Alpha-Channel,
// wir mappen den weissen Hintergrund per Mask zu Transparenz).
IL.AddMasked(BM, clWhite);
ImageIndex := NTAS.AddImages(IL);
except
// Resource-Load oder AddImages fehlgeschlagen - ImageIndex bleibt
// -1, MenuItem laeuft ohne Icon (immer noch besser als kein Eintrag).
end;
finally
BM.Free;
end;
finally
IL.Free;
end;
// TAction in der IDE-eigenen ActionList registrieren (Wichtig - sonst
// gilt der ShortCut-Code nicht und das ImageIndex-Mapping greift nicht).
// OnExecute ist TNotifyEvent (procedure of object) - braucht eine
// Methode an einer Instanz, nicht eine freie Prozedur. Daher der
// GToolsHandler-Wrapper.
if GToolsHandler = nil then
GToolsHandler := TToolsMenuHandler.Create;
GToolsAction := TAction.Create(nil);
GToolsAction.ActionList := NTAS.ActionList;
GToolsAction.Caption := PLUGIN_TITLE;
GToolsAction.OnExecute := GToolsHandler.MenuClick;
GToolsAction.Hint := PLUGIN_DESC;
GToolsAction.Category := 'SCA';
if ImageIndex >= 0 then
GToolsAction.ImageIndex := ImageIndex;
GToolsMenuItem := TMenuItem.Create(NTAS.MainMenu);
GToolsMenuItem.Action := GToolsAction;
GToolsMenuItem.Name := 'SCAToolsMenuItem';
ToolsMenu.Add(GToolsMenuItem);
end;
procedure UnregisterToolsMenuItem;
// MenuItem zuerst (sonst Dangling-Reference im IDE-Menue), dann Action,
// dann Handler. Action darf NUR freigegeben werden NACHDEM MenuItem keine
// Referenz mehr haelt - sonst AV beim naechsten IDE-Repaint.
begin
if Assigned(GToolsMenuItem) then
begin
GToolsMenuItem.Free; // entfernt aus dem Parent automatisch
GToolsMenuItem := nil;
end;
if Assigned(GToolsAction) then
begin
GToolsAction.Free;
GToolsAction := nil;
end;
if Assigned(GToolsHandler) then
begin
GToolsHandler.Free;
GToolsHandler := nil;
end;
end;
procedure Register;
begin
RegisterAnalyserDockableForm;
// Findings-Properties-Panel (Phase 6 Integration): registriert NACH dem
// Analyser-Form, damit RegisterWatchMode darin schon gelaufen ist - der
// Wrapper-FrameCreated subscribed GWatchMode.SubscribeFindings beim
// ersten Dock-Open.
RegisterFindingsPropertiesDockableForm;
RegisterPackageWizard(TStaticCodeAnalyserExpert.Create);
RegisterAboutBox;
RegisterToolsMenuItem;
end;
{ TStaticCodeAnalyserExpert }
destructor TStaticCodeAnalyserExpert.Destroy;
begin
// Properties-Panel zuerst loesen - sein EditServices-Notifier soll
// gehen bevor das Analyser-Form seine eigene Cleanup-Sequenz startet.
UnregisterFindingsPropertiesDockableForm;
UnregisterAnalyserDockableForm;
inherited;
end;
function TStaticCodeAnalyserExpert.GetIDString: string;
begin
Result := 'StaticCodeAnalyser';
end;
function TStaticCodeAnalyserExpert.GetName: string;
begin
Result := 'Static Code Analysis';
end;
function TStaticCodeAnalyserExpert.GetState: TWizardState;
begin
Result := [wsEnabled];
end;
procedure TStaticCodeAnalyserExpert.Execute;
begin
ShowAnalyserDockableForm;
end;
initialization
// Splash-Screen-Eintrag: muss in initialization stehen, NICHT in Register -
// Register feuert nach Komponenten-Registrierung, da ist der Splash i.d.R.
// schon weg. initialization feuert beim BPL-Load - bei "Load on Startup"-
// Packages waehrend der IDE-Start-Sequenz, also rechtzeitig fuer den Splash.
RegisterSplashScreen;
finalization
// Tools-Menue zuerst (haengt vom Action ab, das die IDE bei UnregisterAboutBox
// noch nicht beruehrt - aber Reihenfolge schadet nicht).
UnregisterToolsMenuItem;
UnregisterAboutBox;
// Branding-HBITMAP erst NACH Unregister freigeben - die IDE haelt das
// Handle waehrend des AboutBox/Splash-Lifecycle.
if GBrandingHBmp <> 0 then
begin
DeleteObject(GBrandingHBmp);
GBrandingHBmp := 0;
end;
end.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.