Repository navigation
Expand file tree
/
Copy pathDelphiFormatter.pas
More file actions
418 lines (341 loc) · 11.7 KB
/
Copy pathDelphiFormatter.pas
File metadata and controls
418 lines (341 loc) · 11.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
unit DelphiFormatter;
interface
procedure Register;
implementation
uses
Winapi.Windows, System.Classes, System.SysUtils, System.IniFiles, System.IOUtils, VCL.Menus, ToolsAPI;
const
SETTINGS_FILE = 'DelphiFormatterRedux.ini';
DISPLAY_NAME = 'Delphi Formatter Redux';
DEFAULT_LOG_MESSAGES : Boolean = False;
LOG_DATE_FORMAT = 'yyyy/mm/dd HH:mm:ss.zzz';
type
EFormatterError = class(Exception);
TLogType = (ltInfo, ltWarning, ltError);
TLogTypeHelper = record helper for TLogType
function Name : string;
end;
TDelphiFormatterRedux = class
private
class var SettingsLoaded : Boolean;
class var LogMessages : Boolean;
class var FormatterExe : string;
class var ConfigPath : string;
class var MessageFormatSettings : TFormatSettings;
class var SettingsLock : TObject;
class function GetFormatterExe : string; static;
class function GetConfigPath : string; static;
class function GetActiveEditorBuffer : IOTAEditBuffer; static;
class function GetSettingsFolder : string; static;
class function GetPackageFolder : string; static;
class function GetDefaultFormatterPath : string; static;
class function GetDefaultConfigPath : string; static;
class function GetSettingsIniPath : string; static;
class procedure EnsureSettingsIniExists; static;
class procedure LoadSettings; static;
class procedure RunFormatter(const AFormatterExe, AConfigPath, AFileName : string); static;
class procedure SaveFileViaOTA(const AFileName : string); static;
class procedure ReloadFileViaOTA(const AFileName : string); static;
class procedure LogToMessages(AType : TLogType; const AMessage : string); static;
class procedure OutputMessage(const AText : string); static;
public
class procedure Execute; static;
end;
TDelphiFormatterReduxKeyBinding = class(TInterfacedObject, IOTAKeyboardBinding)
private
procedure ExecuteBinding(const AContext : IOTAKeyContext; AKeyCode : TShortcut; var ABindingResult : TKeyBindingResult);
public
procedure AfterSave;
procedure BeforeSave;
procedure Destroyed;
procedure Modified;
procedure BindKeyboard(const ABindingServices : IOTAKeyBindingServices);
function GetBindingType : TBindingType;
function GetDisplayName : string;
function GetName : string;
end;
class function TDelphiFormatterRedux.GetDefaultFormatterPath : string;
begin
Result := TPath.GetFullPath(TPath.Combine(GetPackageFolder, '..\..\Formatter.exe'));
end;
class function TDelphiFormatterRedux.GetDefaultConfigPath : string;
begin
Result := TPath.GetFullPath(TPath.Combine(GetPackageFolder, '..\..\Formatter.config'));
end;
class function TDelphiFormatterRedux.GetSettingsIniPath : string;
begin
Result := TPath.Combine(GetSettingsFolder, SETTINGS_FILE);
end;
class function TDelphiFormatterRedux.GetSettingsFolder : string;
begin
Result := TPath.Combine(TPath.GetHomePath, 'DelphiFormatterRedux');
end;
class function TDelphiFormatterRedux.GetPackageFolder : string;
var
sModuleName : array [0 .. MAX_PATH - 1] of Char;
begin
SetString(Result, sModuleName, GetModuleFileName(HInstance, sModuleName, Length(sModuleName)));
Result := ExtractFilePath(Result);
end;
class procedure TDelphiFormatterRedux.EnsureSettingsIniExists;
var
sIniPath : string;
sSettingsFolder : string;
begin
sSettingsFolder := GetSettingsFolder;
if not TDirectory.Exists(sSettingsFolder) then
TDirectory.CreateDirectory(sSettingsFolder);
sIniPath := GetSettingsIniPath;
if not FileExists(sIniPath) then
begin
OutputMessage(DISPLAY_NAME + ' - Settings file does not exist. Creating defaults.');
var
oIni := TIniFile.Create(sIniPath);
try
oIni.WriteString('Formatter', 'Exe', GetDefaultFormatterPath);
oIni.WriteString('Formatter', 'Config', GetDefaultConfigPath);
oIni.WriteBool('Formatter', 'LogMessages', DEFAULT_LOG_MESSAGES);
finally
oIni.Free;
end;
OutputMessage(DISPLAY_NAME + ' - Settings file created: ' + sIniPath);
OutputMessage(DISPLAY_NAME +
' - Defaults will be used for this session. You can edit the file and re-open Delphi to apply the changes.');
end;
end;
class procedure TDelphiFormatterRedux.LoadSettings;
var
sFormatterFile : string;
sConfigFile : string;
sIniPath : string;
bNeedLoad : Boolean;
begin
bNeedLoad := not SettingsLoaded;
if bNeedLoad then
begin
TMonitor.Enter(SettingsLock);
try
bNeedLoad := not SettingsLoaded;
if bNeedLoad then
begin
MessageFormatSettings := TFormatSettings.Create;
EnsureSettingsIniExists;
sIniPath := GetSettingsIniPath;
var
oIni := TIniFile.Create(sIniPath);
try
sFormatterFile := Trim(oIni.ReadString('Formatter', 'Exe', GetDefaultFormatterPath));
sConfigFile := Trim(oIni.ReadString('Formatter', 'Config', GetDefaultConfigPath));
LogMessages := oIni.ReadBool('Formatter', 'LogMessages', DEFAULT_LOG_MESSAGES);
finally
oIni.Free;
end;
sFormatterFile := ExpandFileName(sFormatterFile);
if (sFormatterFile = '') or not FileExists(sFormatterFile) then
raise EFormatterError.Create('Formatter.exe was not found: ' + sFormatterFile);
sConfigFile := ExpandFileName(sConfigFile);
if (sConfigFile = '') or not FileExists(sConfigFile) then
raise EFormatterError.Create('Formatter config was not found: ' + sConfigFile);
FormatterExe := sFormatterFile;
ConfigPath := sConfigFile;
SettingsLoaded := True;
OutputMessage(DISPLAY_NAME + ' - Settings loaded. Log info messages: ' + BoolToStr(LogMessages, True));
end;
finally
TMonitor.Exit(SettingsLock);
end;
end;
end;
class function TDelphiFormatterRedux.GetFormatterExe : string;
begin
LoadSettings;
Result := FormatterExe;
end;
class function TDelphiFormatterRedux.GetConfigPath : string;
begin
LoadSettings;
Result := ConfigPath;
end;
class procedure TDelphiFormatterRedux.LogToMessages(AType : TLogType; const AMessage : string);
var
sStamp : string;
begin
if (AType > ltInfo) or LogMessages then
begin
sStamp := FormatDateTime(LOG_DATE_FORMAT, Now, MessageFormatSettings);
OutputMessage(Format('%s [%s] %s - %s', [sStamp, DISPLAY_NAME, AType.Name, AMessage]));
end;
end;
class procedure TDelphiFormatterRedux.OutputMessage(const AText : string);
var
oMsgSvc : IOTAMessageServices;
begin
if Supports(BorlandIDEServices, IOTAMessageServices, oMsgSvc) then
oMsgSvc.AddTitleMessage(AText);
end;
class function TDelphiFormatterRedux.GetActiveEditorBuffer : IOTAEditBuffer;
var
oEditorSvc : IOTAEditorServices;
oView : IOTAEditView;
begin
Result := nil;
if Supports(BorlandIDEServices, IOTAEditorServices, oEditorSvc) then
begin
oView := oEditorSvc.TopView;
if oView <> nil then
Result := oView.Buffer;
end;
end;
class procedure TDelphiFormatterRedux.SaveFileViaOTA(const AFileName : string);
var
oActionSvc : IOTAActionServices;
begin
if Supports(BorlandIDEServices, IOTAActionServices, oActionSvc) then
begin
try
oActionSvc.SaveFile(AFileName);
except
on E : Exception do
raise EFormatterError.CreateFmt('Failed to save file via OTA: %s (%s)', [AFileName, E.Message]);
end;
end
else
raise EFormatterError.Create('IOTAActionServices unavailable.');
end;
class procedure TDelphiFormatterRedux.ReloadFileViaOTA(const AFileName : string);
var
oActionSvc : IOTAActionServices;
begin
if Supports(BorlandIDEServices, IOTAActionServices, oActionSvc) then
begin
try
oActionSvc.ReloadFile(AFileName);
except
on E : Exception do
raise EFormatterError.CreateFmt('Failed to reload file via OTA: %s (%s)', [AFileName, E.Message]);
end;
end
else
raise EFormatterError.Create('IOTAActionServices unavailable.');
end;
class procedure TDelphiFormatterRedux.RunFormatter(const AFormatterExe, AConfigPath, AFileName : string);
var
sCmd : string;
sCmdLine : UnicodeString;
oSI : TStartupInfoW;
oPI : TProcessInformation;
dwWaitResult : Cardinal;
dwExitCode : Cardinal;
begin
sCmd := '"' + AFormatterExe + '"' + ' -config "' + AConfigPath + '"' + ' -b -silent "' + AFileName + '"';
sCmdLine := sCmd;
ZeroMemory(@oSI, SizeOf(oSI));
oSI.cb := SizeOf(oSI);
oSI.dwFlags := STARTF_USESHOWWINDOW;
oSI.wShowWindow := SW_HIDE;
ZeroMemory(@oPI, SizeOf(oPI));
if not CreateProcessW(nil, PWideChar(sCmdLine), nil, nil, False, CREATE_NO_WINDOW, nil, nil, oSI, oPI) then
raise EFormatterError.CreateFmt('Failed to run formatter. Win32 error %d', [GetLastError]);
try
dwWaitResult := WaitForSingleObject(oPI.hProcess, INFINITE);
if dwWaitResult <> WAIT_OBJECT_0 then
raise EFormatterError.CreateFmt('Failed waiting for formatter (WaitForSingleObject=%d).', [dwWaitResult]);
dwExitCode := 0;
if GetExitCodeProcess(oPI.hProcess, dwExitCode) then
begin
if dwExitCode <> 0 then
raise EFormatterError.CreateFmt('Formatter returned exit code %d.', [dwExitCode]);
end
else
raise EFormatterError.CreateFmt('Failed to get formatter exit code. Win32 error %d', [GetLastError]);
finally
CloseHandle(oPI.hThread);
CloseHandle(oPI.hProcess);
end;
end;
class procedure TDelphiFormatterRedux.Execute;
var
sFile : string;
oBuffer : IOTAEditBuffer;
begin
oBuffer := GetActiveEditorBuffer;
if oBuffer = nil then
raise EFormatterError.Create('No active editor buffer.');
sFile := oBuffer.FileName;
if sFile = '' then
raise EFormatterError.Create('Active file has not been saved yet. Please save it first.');
LogToMessages(ltInfo, 'Saving: ' + sFile);
SaveFileViaOTA(sFile);
if not FileExists(sFile) then
raise EFormatterError.Create('File does not exist on disk after saving: ' + sFile);
LogToMessages(ltInfo, 'Running: ' + GetFormatterExe);
RunFormatter(GetFormatterExe, GetConfigPath, sFile);
LogToMessages(ltInfo, 'Reloading: ' + sFile);
ReloadFileViaOTA(sFile);
LogToMessages(ltInfo, 'Done.');
end;
procedure TDelphiFormatterReduxKeyBinding.AfterSave;
begin
end;
procedure TDelphiFormatterReduxKeyBinding.BeforeSave;
begin
end;
procedure TDelphiFormatterReduxKeyBinding.Destroyed;
begin
end;
procedure TDelphiFormatterReduxKeyBinding.Modified;
begin
end;
procedure TDelphiFormatterReduxKeyBinding.BindKeyboard(const ABindingServices : IOTAKeyBindingServices);
begin
ABindingServices.AddKeyBinding([TextToShortCut('Ctrl+D')], ExecuteBinding, nil);
end;
procedure TDelphiFormatterReduxKeyBinding.ExecuteBinding(const AContext : IOTAKeyContext; AKeyCode : TShortcut;
var ABindingResult : TKeyBindingResult);
begin
try
TDelphiFormatterRedux.Execute;
except
on E : Exception do
TDelphiFormatterRedux.LogToMessages(ltError, E.Message);
end;
ABindingResult := krHandled;
end;
function TDelphiFormatterReduxKeyBinding.GetBindingType : TBindingType;
begin
Result := btPartial;
end;
function TDelphiFormatterReduxKeyBinding.GetDisplayName : string;
begin
Result := DISPLAY_NAME;
end;
function TDelphiFormatterReduxKeyBinding.GetName : string;
begin
Result := 'DelphiFormatterReduxKeyBinding';
end;
procedure Register;
var
KeySvc : IOTAKeyboardServices;
begin
if Supports(BorlandIDEServices, IOTAKeyboardServices, KeySvc) then
KeySvc.AddKeyboardBinding(TDelphiFormatterReduxKeyBinding.Create);
end;
{ TLogTypeHelper }
function TLogTypeHelper.Name : string;
begin
case Self of
ltInfo :
Result := 'Information';
ltWarning :
Result := 'Warning';
ltError :
Result := 'Error';
end;
end;
initialization
TDelphiFormatterRedux.SettingsLoaded := False;
TDelphiFormatterRedux.LogMessages := DEFAULT_LOG_MESSAGES;
TDelphiFormatterRedux.SettingsLock := TObject.Create;
finalization
TDelphiFormatterRedux.SettingsLock.Free;
end.