-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWvN.GitLens.pas
338 lines (281 loc) · 8.56 KB
/
WvN.GitLens.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
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
unit WvN.GitLens;
interface
uses
System.IOUtils;
{$IFNDEF CONDITIONALEXPRESSIONS}
{$MESSAGE ERROR '10 Seattle or higher is required'}
{$ELSE}
{$IF RTLVersion < 30.0}
{$MESSAGE ERROR '10 Seattle or higher is required'}
{$IFEND}
{$ENDIF}
procedure Register;
implementation
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Classes, System.IniFiles,
System.UITypes, System.UIConsts, System.RTLConsts, System.Rtti, System.TypInfo,
System.Generics.Collections, System.Generics.Defaults, Vcl.Graphics,
Vcl.Controls, ToolsAPI,
WvN.GitLens.Console.Capture,
WvN.GitLens.Git.Blame,
WvN.GitLens.Git.BlameParser,
WvN.GitLens.Git.User,
WvN.GitLens.Options,
WvN.GitLens.Options.Frame,
WvN.GitLens.Utils
;
type
TIDENotifier = class(TNotifierObject, IOTAIDENotifier)
private
FEditorNotifiers: TList<IOTAEditorNotifier>;
public
constructor Create;
destructor Destroy; override;
{ IOTAIDENotifier }
procedure FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload;
procedure AfterCompile(Succeeded: Boolean); overload;
end;
TEditorNotifier = class(TNotifierObject, IOTANotifier, IOTAEditorNotifier)
private
FSourceEditor: IOTASourceEditor;
FEditViewNotifiers: TList<INTAEditViewNotifier>;
FNotifierIndex: Integer;
procedure RemoveNotifiers;
public
constructor Create(ASourceEditor: IOTASourceEditor);
destructor Destroy; override;
{ IOTANotifier }
procedure Destroyed;
{ IOTAEditorNotifier }
procedure ViewNotification(const View: IOTAEditView; Operation: TOperation);
procedure ViewActivated(const View: IOTAEditView);
end;
TEditViewNotifier = class(TNotifierObject, IOTANotifier, INTAEditViewNotifier)
private
FEditView: IOTAEditView;
FNotifierIndex: Integer;
procedure RemoveNotifier;
public
constructor Create(AEditView: IOTAEditView);
destructor Destroy; override;
{ IOTANotifier }
procedure Destroyed;
{ INTAEditViewNotifier }
procedure EditorIdle(const View: IOTAEditView);
procedure BeginPaint(const View: IOTAEditView; var FullRepaint: Boolean);
procedure PaintLine(const View: IOTAEditView; LineNumber: Integer;
const LineText: PAnsiChar; const TextWidth: Word; const LineAttributes: TOTAAttributeArray;
const Canvas: TCanvas; const TextRect: TRect; const LineRect: TRect; const CellSize: TSize);
procedure EndPaint(const View: IOTAEditView);
end;
var
FCurrentFile:string;
FWvNGitLensEnabled: Boolean;
FIDENotifierIndex: Integer = -1;
FWvNGitLensAddInOptions: TWvNGitLensAddInOptions = nil;
FCurrentBuffer: IOTAEditBuffer;
FRepaintAll: Boolean;
FLeftGutterProp: PPropInfo;
FGitBlame:TGitBlame;
{ TIDENotifier }
constructor TIDENotifier.Create;
var
moduleServices: IOTAModuleServices;
module: IOTAModule;
editor: IOTASourceEditor;
begin
inherited;
FEditorNotifiers := TList<IOTAEditorNotifier>.Create;
moduleServices := BorlandIDEServices as IOTAModuleServices;
for var i := 0 to moduleServices.ModuleCount-1 do
begin
module := moduleServices.Modules[i];
for var j := 0 to module.ModuleFileCount-1 do
if Supports(module.ModuleFileEditors[j], IOTASourceEditor, editor) then
FEditorNotifiers.Add(TEditorNotifier.Create(editor));
end;
end;
destructor TIDENotifier.Destroy;
var
i: Integer;
begin
for i := 0 to FEditorNotifiers.Count-1 do
FEditorNotifiers[i].Destroyed;
FEditorNotifiers.Free;
inherited;
end;
procedure TIDENotifier.FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
begin
end;
procedure TIDENotifier.BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
begin
end;
procedure TIDENotifier.AfterCompile(Succeeded: Boolean);
begin
end;
{ TEditorNotifier }
constructor TEditorNotifier.Create(ASourceEditor: IOTASourceEditor);
var
i: Integer;
begin
inherited Create;
FEditViewNotifiers := TList<INTAEditViewNotifier>.Create;
FSourceEditor := ASourceEditor;
FNotifierIndex := FSourceEditor.AddNotifier(Self);
for i := 0 to FSourceEditor.EditViewCount-1 do
begin
FEditViewNotifiers.Add(TEditViewNotifier.Create(FSourceEditor.EditViews[i]));
end;
end;
destructor TEditorNotifier.Destroy;
begin
RemoveNotifiers;
FEditViewNotifiers.Free;
inherited;
end;
procedure TEditorNotifier.Destroyed;
begin
RemoveNotifiers;
end;
procedure TEditorNotifier.ViewNotification(const View: IOTAEditView; Operation: TOperation);
begin
if Operation = opInsert then
FEditViewNotifiers.Add(TEditViewNotifier.Create(View));
end;
procedure TEditorNotifier.ViewActivated(const View: IOTAEditView);
begin
FCurrentFile := View.Buffer.FileName;
var repoPath := ExtractFilePath(FCurrentFile);
SetCurrentDir(repoPath);
CaptureConsoleOutputASync('git --no-pager blame --line-porcelain "' + FCurrentFile+'"',
procedure(s:string)
begin
var CurrentUser : TGitUser;
CurrentUser.Name := 'Wouter van Nifterick';
CurrentUser.Email := '[email protected]';
FGitBlame := TGitBlameParser.Parse(s, repoPath, CurrentUser);
end)
end;
procedure TEditorNotifier.RemoveNotifiers;
var
i: Integer;
begin
for i := 0 to FEditViewNotifiers.Count-1 do
FEditViewNotifiers[i].Destroyed;
FEditViewNotifiers.Clear;
if Assigned(FSourceEditor) and (FNotifierIndex >= 0) then
begin
FSourceEditor.RemoveNotifier(FNotifierIndex);
FNotifierIndex := -1;
FSourceEditor := nil;
end;
end;
{ TEditViewNotifier }
constructor TEditViewNotifier.Create(AEditView: IOTAEditView);
begin
inherited Create;
FEditView := AEditView;
FNotifierIndex := FEditView.AddNotifier(Self);
end;
destructor TEditViewNotifier.Destroy;
begin
RemoveNotifier;
inherited;
end;
procedure TEditViewNotifier.Destroyed;
begin
RemoveNotifier;
end;
procedure TEditViewNotifier.EditorIdle(const View: IOTAEditView);
begin
end;
procedure TEditViewNotifier.BeginPaint(const View: IOTAEditView; var FullRepaint: Boolean);
begin
end;
procedure TEditViewNotifier.PaintLine(
const View: IOTAEditView;
LineNumber: Integer;
const LineText: PAnsiChar;
const TextWidth: Word; const LineAttributes: TOTAAttributeArray;
const Canvas: TCanvas; const TextRect: TRect; const LineRect: TRect; const CellSize: TSize);
begin
if LineNumber < 1 then
Exit;
if LineNumber>Length(FGitBlame.lines) then
Exit;
if LineNumber <> View.CursorPos.Line then
Exit;
var line := FGitBlame.lines[LineNumber-1];
for var c in FGitBlame.commits do
if c.ShortSha = line.Sha then
begin
var author := c.Author.name;
var age := now - c.AuthorDate;
var ageStr := TimeSpanToShortStr(age);
var msg := c.Summary;
var txt := format('%s, %s ago • %s',[author, ageStr, msg]);
Canvas.Brush.Style := bsClear;
Canvas.Font.Color := $666666;
Canvas.TextOut(TextRect.Right + (CellSize.cx * 8), TextRect.Top, txt);
end;
end;
procedure TEditViewNotifier.EndPaint(const View: IOTAEditView);
begin
end;
procedure TEditViewNotifier.RemoveNotifier;
begin
if Assigned(FEditView) and (FNotifierIndex >= 0) then
begin
FEditView.RemoveNotifier(FNotifierIndex);
FNotifierIndex := -1;
FEditView := nil;
end;
end;
procedure DoRepaint;
var
i: Integer;
begin
FRepaintAll := True;
if FCurrentBuffer <> nil then
for i := 0 to FCurrentBuffer.EditViewCount-1 do
FCurrentBuffer.EditViews[i].Paint;
end;
procedure Register;
function GetPropInfo(const QualifiedClassName, PropName: string): PPropInfo;
var
ctx: TRttiContext;
typ: TRttiType;
prop: TRttiProperty;
begin
typ := ctx.FindType(QualifiedClassName);
if typ = nil then Exit(nil);
prop := typ.GetProperty(PropName);
if prop = nil then Exit(nil);
Result := TRttiInstanceProperty(prop).PropInfo;
end;
begin
FIDENotifierIndex := (BorlandIDEServices as IOTAServices).AddNotifier(TIDENotifier.Create);
FWvNGitLensAddInOptions := TWvNGitLensAddInOptions.Create;
FWvNGitLensAddinOptions.OnPaint :=
procedure
begin
if FWvNGitLensEnabled then
DoRepaint;
end;
(BorlandIDEServices as INTAEnvironmentOptionsServices).RegisterAddInOptions(FWvNGitLensAddInOptions);
FLeftGutterProp := GetPropInfo('EditorControl.TEditControl', 'LeftGutter');
end;
procedure Unregister;
begin
if FIDENotifierIndex >= 0 then
(BorlandIDEServices as IOTAServices).RemoveNotifier(FIDENotifierIndex);
(BorlandIDEServices as INTAEnvironmentOptionsServices).UnregisterAddInOptions(FWvNGitLensAddInOptions);
FWvNGitLensAddInOptions := nil;
end;
initialization
finalization
Unregister;
end.