forked from integrated-application-development/pasfmt-rad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasfmt.OnSave.pas
250 lines (205 loc) · 7.36 KB
/
Pasfmt.OnSave.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
unit Pasfmt.OnSave;
interface
uses
ToolsAPI,
System.Classes,
DockForm,
System.Generics.Collections,
Pasfmt.FormatEditor;
type
TConfigureFormatterProc = reference to procedure(var Formatter: TEditBufferFormatter);
TFormatOnSaveInstallerNotifier = class(TNotifierObject, INTAEditServicesNotifier)
private
FFormatOnSaveModules: TDictionary<IOTAModule, Integer>;
FConfigureFormatterProc: TConfigureFormatterProc;
public
constructor Create;
destructor Destroy; override;
procedure OnFormatOnSaveNotifierDestroyed(Sender: TObject);
procedure UninstallAll;
procedure WindowShow(const EditWindow: INTAEditWindow; Show: Boolean; LoadedFromDesktop: Boolean);
procedure WindowNotification(const EditWindow: INTAEditWindow; Operation: TOperation);
procedure WindowActivated(const EditWindow: INTAEditWindow);
procedure WindowCommand(const EditWindow: INTAEditWindow; Command: Integer; Param: Integer; var Handled: Boolean);
procedure EditorViewModified(const EditWindow: INTAEditWindow; const EditView: IOTAEditView);
procedure EditorViewActivated(const EditWindow: INTAEditWindow; const EditView: IOTAEditView);
procedure DockFormVisibleChanged(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
procedure DockFormUpdated(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
procedure DockFormRefresh(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
property ConfigureFormatter: TConfigureFormatterProc read FConfigureFormatterProc write FConfigureFormatterProc;
end;
TFormatOnSaveModuleNotifier = class(TNotifierObject, IOTAModuleNotifier)
private
FModule: IOTAModule;
FOnDestroyed: TNotifyEvent;
FConfigureFormatterProc: TConfigureFormatterProc;
public
constructor Create(Module: IOTAModule);
procedure AfterSave;
procedure BeforeSave;
procedure Destroyed;
procedure Modified;
function CheckOverwrite: Boolean;
procedure ModuleRenamed(const NewName: string);
property Module: IOTAModule read FModule;
property OnDestroyed: TNotifyEvent read FOnDestroyed write FOnDestroyed;
property ConfigureFormatter: TConfigureFormatterProc read FConfigureFormatterProc write FConfigureFormatterProc;
end;
function OnSaveInstaller: TFormatOnSaveInstallerNotifier;
implementation
uses
System.SysUtils,
Pasfmt.Log,
Pasfmt.Settings;
var
// TFormatOnSaveInstallerNotifier
GOnSaveInstaller: INTAEditServicesNotifier;
function OnSaveInstaller: TFormatOnSaveInstallerNotifier;
begin
if not Assigned(GOnSaveInstaller) then begin
GOnSaveInstaller := TFormatOnSaveInstallerNotifier.Create;
end;
Result := TFormatOnSaveInstallerNotifier(GOnSaveInstaller);
end;
constructor TFormatOnSaveInstallerNotifier.Create;
begin
FFormatOnSaveModules := TDictionary<IOTAModule, Integer>.Create;
end;
//______________________________________________________________________________________________________________________
destructor TFormatOnSaveInstallerNotifier.Destroy;
begin
UninstallAll;
FreeAndNil(FFormatOnSaveModules);
inherited;
end;
//______________________________________________________________________________________________________________________
procedure TFormatOnSaveInstallerNotifier.OnFormatOnSaveNotifierDestroyed(Sender: TObject);
var
Notifier: TFormatOnSaveModuleNotifier;
begin
Notifier := Sender as TFormatOnSaveModuleNotifier;
FFormatOnSaveModules.Remove(Notifier.Module);
Log.Debug('Deregistered format-on-save for module "%s"', [Notifier.Module.FileName]);
end;
//______________________________________________________________________________________________________________________
procedure TFormatOnSaveInstallerNotifier.UninstallAll;
var
Pair: TPair<IOTAModule, Integer>;
Count: Integer;
begin
for Pair in FFormatOnSaveModules do
Pair.Key.RemoveNotifier(Pair.Value);
Count := FFormatOnSaveModules.Count;
FFormatOnSaveModules.Clear;
Log.Debug('Deregistered all %d format-on-saves', [Count]);
end;
//______________________________________________________________________________________________________________________
procedure TFormatOnSaveInstallerNotifier.DockFormRefresh(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
begin
// implement interface
end;
procedure TFormatOnSaveInstallerNotifier.DockFormUpdated(const EditWindow: INTAEditWindow; DockForm: TDockableForm);
begin
// implement interface
end;
procedure TFormatOnSaveInstallerNotifier.DockFormVisibleChanged(
const EditWindow: INTAEditWindow;
DockForm: TDockableForm
);
begin
// implement interface
end;
procedure TFormatOnSaveInstallerNotifier.EditorViewActivated(
const EditWindow: INTAEditWindow;
const EditView: IOTAEditView
);
var
Notifier: TFormatOnSaveModuleNotifier;
Module: IOTAModule;
Index: Integer;
begin
Module := EditView.Buffer.Module;
if PasfmtSettings.FormatOnSave and not FFormatOnSaveModules.ContainsKey(Module) then begin
Notifier := TFormatOnSaveModuleNotifier.Create(Module);
Notifier.OnDestroyed := OnFormatOnSaveNotifierDestroyed;
Notifier.ConfigureFormatter := ConfigureFormatter;
Index := Module.AddNotifier(Notifier);
FFormatOnSaveModules.Add(Module, Index);
Log.Debug('Registered format-on-save for module "%s"', [Module.FileName]);
end;
end;
procedure TFormatOnSaveInstallerNotifier.EditorViewModified(
const EditWindow: INTAEditWindow;
const EditView: IOTAEditView
);
begin
// implement interface
end;
procedure TFormatOnSaveInstallerNotifier.WindowActivated(const EditWindow: INTAEditWindow);
begin
// implement interface
end;
procedure TFormatOnSaveInstallerNotifier.WindowCommand(
const EditWindow: INTAEditWindow;
Command, Param: Integer;
var Handled: Boolean
);
begin
// implement interface
end;
procedure TFormatOnSaveInstallerNotifier.WindowNotification(const EditWindow: INTAEditWindow; Operation: TOperation);
begin
// implement interface
end;
procedure TFormatOnSaveInstallerNotifier.WindowShow(const EditWindow: INTAEditWindow; Show, LoadedFromDesktop: Boolean);
begin
// implement interface
end;
//______________________________________________________________________________________________________________________
constructor TFormatOnSaveModuleNotifier.Create(Module: IOTAModule);
begin
FModule := Module;
end;
procedure TFormatOnSaveModuleNotifier.AfterSave;
begin
inherited;
end;
procedure TFormatOnSaveModuleNotifier.BeforeSave;
var
Formatter: TEditBufferFormatter;
Buffer: IOTAEditBuffer;
begin
inherited;
if Supports(Module.CurrentEditor, IOTAEditBuffer, Buffer) then begin
Log.Debug('Format-on-save triggered for "%s"', [Module.FileName]);
Formatter := Default(TEditBufferFormatter);
if Assigned(FConfigureFormatterProc) then begin
FConfigureFormatterProc(Formatter);
end;
Formatter.Format(Buffer);
end;
end;
function TFormatOnSaveModuleNotifier.CheckOverwrite: Boolean;
begin
// implement interface
Result := True;
end;
procedure TFormatOnSaveModuleNotifier.Destroyed;
begin
inherited;
if Assigned(FOnDestroyed) then begin
FOnDestroyed(Self);
end;
end;
procedure TFormatOnSaveModuleNotifier.Modified;
begin
inherited;
// implement interface
// todo format on type
end;
procedure TFormatOnSaveModuleNotifier.ModuleRenamed(const NewName: string);
begin
// implement interface
end;
//______________________________________________________________________________________________________________________
end.