forked from groehner/JavaEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSynEditDiff.pas
More file actions
596 lines (551 loc) · 14.9 KB
/
USynEditDiff.pas
File metadata and controls
596 lines (551 loc) · 14.9 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
{ -------------------------------------------------------------------------------
Unit: USynEditDiff
Author: Gerhard Röhner
Date: 2011
Purpose: SynEdit descendent for TextDiff
------------------------------------------------------------------------------- }
unit USynEditDiff;
interface
uses
Classes,
Graphics,
ExtCtrls,
Forms,
SynEdit,
SynEditTextBuffer;
type
TLineObj = class
private
FBackClr: TColor;
FSpezial: Boolean;
FTag: LongInt;
public
property BackClr: TColor read FBackClr write FBackClr;
property Spezial: Boolean read FSpezial write FSpezial;
property Tag: LongInt read FTag write FTag;
end;
TSynEditDiff = class(TSynEdit)
private
FMyOwner: TComponent;
FCurrLongestLineLen: Integer; // needed for horizontal scrollbar
FEditFormNr: Integer;
FLineModClr: TColor;
FYellowGray: TColor;
FSilveryGray: TColor;
FModifiedStrs: array [Boolean] of string;
FInsertModeStrs: array [Boolean] of string;
FEncoding: string; // ANSI, UTF-8, UTF-16
FLineBreak: string; // Windows, Unix, Mac
FNumber: Integer;
FPathname: string;
FPCaption: TPanel;
FWithColoredLines: Boolean;
procedure DeleteObjects(From, Till: Integer);
procedure DoExit1(Sender: TObject);
procedure SynEditorReplaceText(Sender: TObject;
const ASearch, AReplace: string; Line, Column: Integer;
var AAction: TSynReplaceAction);
procedure CodeEditKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure EditorStatusChange(Sender: TObject; Changes: TSynStatusChanges);
procedure SetHighlighter;
procedure CreateObjects(From, Till, Tag: Integer); overload;
function EncodingAsString(const Encoding: string): string;
function LinebreakAsString: string;
procedure UpdateState;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DoUndo;
procedure DoRedo;
procedure Enter(Sender: TObject);
function GetLineObj(Index: Integer): TLineObj;
procedure InsertItem(Index: Integer; const Str: string;
LineObject: TLineObj);
procedure SynEditorSpecialLineColors(Sender: TObject; Line: Integer;
var Special: Boolean; var Foreground, Background: TColor);
procedure GutterTextEvent(Sender: TObject; ALine: Integer;
var AText: string);
procedure Load(Lines12: TSynEditStringList; const APathname: string);
procedure Save(WithBackup: Boolean);
procedure LinesClearAll;
procedure CreateObjects; overload;
procedure ShowFilename;
procedure SetModified(Value: Boolean);
function CopyToClipboardFT(From, Till: Integer): Boolean;
procedure PasteFromClipboardFT(EmptyClipboard: Boolean; From, Till, Num: Integer);
procedure ChangeStyle;
procedure SyncScroll(Sender: TObject; ScrollBar: TScrollBarKind);
property Number: Integer read FNumber write FNumber;
property Pathname: string read FPathname;
property PCaption: TPanel read FPCaption write FPCaption;
property WithColoredLines: Boolean read FWithColoredLines
write FWithColoredLines;
end;
implementation
uses
SysUtils,
Windows,
Controls,
Clipbrd,
FileCtrl,
JvGnugettext,
UStringRessources,
UTextDiffForm,
UDlgConfirmReplace,
UUtils,
SynEditTypes,
UConfiguration,
UJava;
constructor TSynEditDiff.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyOwner := AOwner;
FCurrLongestLineLen := 60;
FWithColoredLines := False;
FNumber := 0;
FPathname := '';
FEditFormNr := -1;
FModifiedStrs[False] := '';
FModifiedStrs[True] := _(LNGModified);
FInsertModeStrs[False] := _(LNGModusOverwrite);
FInsertModeStrs[True] := _(LNGModusInsert);
SearchEngine := FJava.SynEditSearch;
Gutter.ShowLineNumbers := True;
Gutter.DigitCount := 0;
Gutter.AutoSize := True;
MaxUndo := 300;
TabWidth := 2;
WantTabs := True;
Options := [eoAutoIndent, eoDragDropEditing, eoSmartTabs, eoTabIndent,
eoTabsToSpaces, eoTrimTrailingSpaces, eoSmartTabDelete, eoGroupUndo,
eoKeepCaretX, eoEnhanceHomeKey];
ScrollOptions := [eoScrollPastEol, eoShowScrollHint];
Font.Assign(FConfiguration.Font);
Gutter.Font.Assign(FConfiguration.Font);
Gutter.Font.Height := Font.Height + 2;
OnKeyUp := CodeEditKeyUp;
OnStatusChange := EditorStatusChange;
OnReplaceText := SynEditorReplaceText;
OnEnter := Enter;
OnExit := DoExit1;
OnScroll := SyncScroll;
ChangeStyle;
end;
destructor TSynEditDiff.Destroy;
begin
OnStatusChange := nil;
OnEnter := nil;
OnExit := nil;
TSynEditStringList(Lines).OnCleared := nil;
LinesClearAll;
inherited;
end;
procedure TSynEditDiff.DoUndo;
begin
Undo;
CreateObjects;
end;
procedure TSynEditDiff.DoRedo;
begin
Redo;
CreateObjects;
end;
procedure TSynEditDiff.Enter(Sender: TObject);
begin
Gutter.Color := FYellowGray;
EditorStatusChange(Sender, [scAll]);
if FWithColoredLines then
TFTextDiff(FMyOwner).ShowDiffState;
end;
procedure TSynEditDiff.DoExit1(Sender: TObject);
begin
Gutter.Color := FSilveryGray;
end;
procedure TSynEditDiff.CodeEditKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
with TFTextDiff(FMyOwner) do
if FilesCompared and (Key in [VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT]) then
SyncScroll(Self, sbVertical)
else if (Key = VK_RETURN) or ([ssCtrl] = Shift) and (Key = Ord('V')) then
CreateObjects;
end;
function TSynEditDiff.GetLineObj(Index: Integer): TLineObj;
begin
if Lines.Count = 0 then
Result := nil
else
begin
if (Index < 0) or (Index >= Lines.Count) then
raise Exception.Create('TLines.GetLineObj() - index out of bounds.');
Result := TLineObj(Lines.Objects[Index]);
end;
end;
procedure TSynEditDiff.InsertItem(Index: Integer; const Str: string;
LineObject: TLineObj);
begin
inherited Lines.InsertObject(Index, Str, LineObject);
end;
procedure TSynEditDiff.SynEditorSpecialLineColors(Sender: TObject;
Line: Integer; var Special: Boolean; var Foreground, Background: TColor);
begin
if (Line > Lines.Count) or not FWithColoredLines then
Exit;
var
Lo1 := GetLineObj(Line - 1);
if not Assigned(Lo1) then
begin
// Background:= clRed; // good for debugging
Background := FLineModClr;
Special := True;
end
else
with Lo1 do
if Spezial then
begin
Background := BackClr;
Special := True;
end;
end;
procedure TSynEditDiff.UpdateState;
begin
with FJava do
begin
SetEnabledMI(MIUndo, CanUndo);
SetEnabledTB(TBUndo, MIUndo.Enabled);
SetEnabledMI(MIRedo, CanRedo);
SetEnabledTB(TBRedo, MIRedo.Enabled);
var
Selection := SelAvail;
SetEnabledMI(MICut, Selection);
SetEnabledMI(MICopy, Selection);
SetEnabledMI(MICopyRTF, Selection);
SetEnabledMI(MICopyHTML, Selection);
SetEnabledMI(MICopyHTMLAsText, Selection);
SetEnabledMI(MICopyNumbered, Selection);
SetEnabledMI(MICopyRtfNumbered, Selection);
SetEnabledMI(MIPaste, CanPaste);
end;
with TFTextDiff(FMyOwner) do
begin
SetEnabledMI(MIUndo, CanUndo);
SetEnabledMI(MIRedo, CanRedo);
SetEnabledMI(MICut, SelAvail);
SetEnabledMI(MICopy, SelAvail);
SetEnabledMI(MIPaste, CanPaste);
end;
end;
procedure TSynEditDiff.EditorStatusChange(Sender: TObject;
Changes: TSynStatusChanges);
begin
if not Assigned(FMyOwner) then
Exit;
TFTextDiff(FMyOwner).CodeEditNumber := FNumber;
TFTextDiff(FMyOwner).liLineColumn.Caption :=
Format(' %4d : %3d ', [CaretY, CaretX]);
if Changes * [scModified] <> [] then
begin
TFTextDiff(FMyOwner).liModified.Caption := FModifiedStrs[Modified];
ShowFilename;
if FEditFormNr = -1 then
begin
var
AForm := FJava.GetTDIWindowType(FPathname, '%E%');
if Assigned(AForm) then
FEditFormNr := AForm.Number;
end;
if FEditFormNr > -1 then
FJava.TabModified(FEditFormNr, Modified);
end;
TFTextDiff(FMyOwner).liInsOvr.Caption := FInsertModeStrs[InsertMode];
TFTextDiff(FMyOwner).liEncoding.Caption := ' ' + FEncoding + '/' +
LinebreakAsString + ' ';
UpdateState;
end;
procedure TSynEditDiff.GutterTextEvent(Sender: TObject; ALine: Integer;
var AText: string);
begin
if FWithColoredLines then
begin
var
LineObject := GetLineObj(ALine - 1);
if not Assigned(LineObject) then
Exit;
with LineObject do
if Tag = 0 then
AText := ''
else
AText := IntToStr(Tag);
end
else
AText := IntToStr(ALine);
end;
procedure TSynEditDiff.Load(Lines12: TSynEditStringList;
const APathname: string);
begin
if not FileExists(APathname) then
Exit;
Lines12.LoadFromFile(APathname);
Lines.BeginUpdate;
LinesClearAll;
Lines.Assign(Lines12);
Lines.EndUpdate;
Self.FPathname := APathname;
FEncoding := EncodingAsString(Lines12.Encoding.EncodingName);
FLineBreak := Lines12.LineBreak;
FWithColoredLines := False;
SetHighlighter;
SetModified(False);
end;
procedure TSynEditDiff.Save(WithBackup: Boolean);
var
BackupName, Ext: string;
Line: TLineObj;
begin
if FWithColoredLines then
begin
BeginUpdate;
for var I := Lines.Count - 1 downto 0 do
begin
Line := GetLineObj(I);
if Assigned(Line) then
if Line.FTag = 0 then
begin
FreeAndNil(Line);
Lines.Delete(I);
end
else
Line.FSpezial := False;
end;
FWithColoredLines := False;
EndUpdate;
Invalidate;
end;
if Modified then begin
if WithBackup then
begin
BackupName := FPathname;
Ext := ExtractFileExt(FPathname);
if Length(Ext) > 1 then
Ext[2] := '~';
BackupName := ChangeFileExt(BackupName, Ext);
if FileExists(BackupName) then
SysUtils.DeleteFile(BackupName);
if FileExists(FPathname) then
RenameFile(FPathname, BackupName);
end;
try
Lines.SaveToFile(FPathname);
SetModified(False);
except
on E: Exception do
ErrorMsg(Format(_(LNGCanNotWrite), [FPathname]));
end;
end;
end;
procedure TSynEditDiff.SetHighlighter;
begin
var Str := LowerCase(ExtractFileExt(FPathname));
Highlighter := FConfiguration.GetHighlighter(Str);
if Highlighter = nil then
OnPaintTransient := nil;
end;
function TSynEditDiff.EncodingAsString(const Encoding: string): string;
begin
Result := Encoding;
if Pos('ANSI', Result) > 0 then
Result := 'ANSI'
else if Pos('ASCII', Result) > 0 then
Result := 'ASCII'
else if Pos('UTF-8', Result) > 0 then
Result := 'UTF-8'
else if Pos('UTF-16', Result) > 0 then
Result := 'UTF-16'
else if Pos('Unicode', Result) > 0 then
Result := 'UTF-16';
end;
function TSynEditDiff.LinebreakAsString: string;
begin
if FLineBreak = #13#10 then
Result := 'Windows'
else if FLineBreak = #10 then
Result := 'Unix'
else
Result := 'Mac';
end;
procedure TSynEditDiff.LinesClearAll;
begin
DeleteObjects(0, Lines.Count - 1);
Lines.Clear;
OnSpecialLineColors := nil;
OnGutterGetText := nil;
end;
procedure TSynEditDiff.DeleteObjects(From, Till: Integer);
var
LineObject: TLineObj;
begin
IncPaintLock;
for var I := From to Till do
begin
LineObject := GetLineObj(I);
if Assigned(LineObject) then
begin
LineObject.Free;
Lines.Objects[I] := nil;
end;
end;
DecPaintLock;
end;
procedure TSynEditDiff.CreateObjects;
var
LineObject: TLineObj;
begin
if not FWithColoredLines then
Exit;
Lines.BeginUpdate;
var Int := 0;
while (Int < Lines.Count) and Assigned(GetLineObj(Int)) do
Inc(Int);
while (Int < Lines.Count) and (GetLineObj(Int) = nil) do
begin
LineObject := TLineObj.Create;
LineObject.FSpezial := True;
LineObject.FBackClr := FLineModClr;
LineObject.FTag := Int + 1;
Lines.Objects[Int] := LineObject;
Inc(Int);
end;
Lines.EndUpdate;
Invalidate;
end;
procedure TSynEditDiff.CreateObjects(From, Till, Tag: Integer);
var
LineObject: TLineObj;
begin
for var I := From to Till do
if GetLineObj(I) = nil then
begin
LineObject := TLineObj.Create;
LineObject.FSpezial := True;
LineObject.FBackClr := FLineModClr;
LineObject.FTag := Tag;
if Tag > 0 then
Inc(Tag);
Lines.Objects[I] := LineObject;
end;
end;
procedure TSynEditDiff.SynEditorReplaceText(Sender: TObject;
const ASearch, AReplace: string; Line, Column: Integer;
var AAction: TSynReplaceAction);
function PointToDisplay(Posi: TPoint): TDisplayCoord;
begin
Result.Column := Posi.X;
Result.Row := Posi.Y;
end;
begin
if ASearch = AReplace then
AAction := raSkip
else
begin
var
APos := DisplayCoord(Column, Line);
APos := PointToDisplay(ClientToScreen(RowColumnToPixels(APos)));
var
EditRect := ClientRect;
EditRect.TopLeft := ClientToScreen(EditRect.TopLeft);
EditRect.BottomRight := ClientToScreen(EditRect.BottomRight);
with TFConfirmReplace.Create(Application) do
begin
PrepareShow(EditRect, APos.Column, APos.Row,
APos.Row + LineHeight, ASearch);
case ShowModal of
mrYes:
AAction := raReplace;
mrYesToAll:
AAction := raReplaceAll;
mrNo:
AAction := raSkip;
else
AAction := raCancel;
end;
Free;
end;
end;
end;
procedure TSynEditDiff.ShowFilename;
var
Str: string;
ACanvas: TCanvas;
begin
ACanvas := TFTextDiff(FMyOwner).Canvas;
ACanvas.Font.Assign(FPCaption.Font);
Str := ' ' + FPathname;
if Modified then
Str := Str + '*';
FPCaption.Caption := MinimizeName(Str, ACanvas, FPCaption.Width);
end;
procedure TSynEditDiff.SetModified(Value: Boolean);
begin
Modified := Value;
ShowFilename;
end;
function TSynEditDiff.CopyToClipboardFT(From, Till: Integer): Boolean;
begin
SelStart := RowColToCharIndex(BufferCoord(1, From + 1));
if Till + 2 > Lines.Count then
SelEnd := RowColToCharIndex(BufferCoord(Length(Lines[Lines.Count - 1]) + 1,
Till + 1))
else
SelEnd := RowColToCharIndex(BufferCoord(1, Till + 2)) - 2;
Result := (SelLength = 0);
if Result then
Clipboard.Clear
else
CopyToClipboard;
SelLength := 0;
end;
procedure TSynEditDiff.PasteFromClipboardFT(EmptyClipboard: Boolean;
From, Till, Num: Integer);
begin
DeleteObjects(From, Till);
SelStart := RowColToCharIndex(BufferCoord(1, From + 1));
if Till + 2 > Lines.Count then
SelEnd := RowColToCharIndex(BufferCoord(Length(Lines[Lines.Count - 1]) + 1,
Till + 1))
else
SelEnd := RowColToCharIndex(BufferCoord(1, Till + 2)) - 2;
try
if EmptyClipboard then
begin
CutToClipboard;
Clipboard.Clear;
end
else
PasteFromClipboard;
except on E: Exception do
ErrorMsg(E.Message);
end;
CreateObjects(From, Till, Num);
SetModified(True);
Invalidate;
end;
procedure TSynEditDiff.ChangeStyle;
begin
if FConfiguration.IsDark then
begin
FLineModClr := $878787;
FYellowGray := $519595;
FSilveryGray := $777777;
end
else
begin
FLineModClr := $E0E0E0;
FYellowGray := $97734F;
FSilveryGray := $16A231;
end;
end;
procedure TSynEditDiff.SyncScroll(Sender: TObject; ScrollBar: TScrollBarKind);
begin
TFTextDiff(FMyOwner).SyncScroll(Self, ScrollBar);
end;
end.