File: synhighlighterdiff.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (745 lines) | stat: -rw-r--r-- 22,357 bytes parent folder | download | duplicates (4)
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.

This file was added to the Lazarus branch of SynEdit.
The original Author is M Friebe
}

{ This unit provide Highlighting for diff files
  Currently supported formats: Context, Unified
  //Todo: support original diff
}

unit SynHighlighterDiff;

{$I SynEdit.inc}

interface

uses
  Classes, Graphics, math,
  SynEditHighlighter, SynEditHighlighterFoldBase;

type
  TtkTokenKind = (tkNull, tkUnknown, tkSpace,
                  // File Header
                  tkFileOrigMark, tkFileNewMark,
                  tkFileOrig, tkFileNew,
                  // Chunk Header
                  tkChunkSeparator,
                  tkChunkMixedMark, tkChunkOrigMark, tkChunkNewMark,
                  tkChunkMixed, tkChunkOrig, tkChunkNew,
                  // Lines
                  tkLineRemovedMark, tkLineAddedMark, tkLineChangedMark, tkLineContextMark,
                  tkLineRemoved, tkLineAdded, tkLineChanged, tkLineContext
                 );

  TRangeState = (rsUnknown,
                 // Context
                 rsCtxFirst,
                   rsCtxUnknown,
                   rsCtxFileOrig, rsCtxFileNew,
                   rsCtxChunkHeader, rsCtxChunkOrig, rsCtxChunkNew,
                   rsCtxLineRemoved, rsCtxLineAdded, rsCtxLineChanged,
                   rsCtxLineContext,
                 rsCtxLast,
                 // Unified
                 rsUniFirst,
                   rsUniUnknown,
                   rsUniFileOrig, rsUniFileNew,
                   rsUniChunkHeader,
                   rsUniLineRemoved, rsUniLineAdded,
                   rsUniLineContext,
                 rsUniLast
                );

  TDiffCodeFoldBlockType = (
    cfbtDiffFile,
    cfbtDiffChunk,
    cfbtDiffChunkSect,
    cfbtDiffNone
    );

  TProcTableProc = procedure of object;

const
  CountDiffCodeFoldBlockOffset: Pointer =
    Pointer(PtrInt(Integer(high(TDiffCodeFoldBlockType))+1));

type

  { TSynDiffSyn }

  TSynDiffSyn = class(TSynCustomFoldHighlighter)
  private
    fRange: TRangeState;
    fLine: PChar;
    fLineNumber: Integer;
    Run: LongInt;
    fTokenPos: Integer;
    FTokenID: TtkTokenKind;
  private
    fProcTable: array[#0..#255] of TProcTableProc;
    procedure MakeMethodTables;
    procedure CRProc;
    procedure LFProc;
    procedure NullProc;
    procedure LineProc;
    procedure UnknownProc;
    procedure AsteriskProc;
    procedure MinusProc;
    procedure PlusProc;
    procedure ExclamationProc;
    procedure AtProc;
    procedure SpaceProc;
  private
    FChunkMarkerAttri: TSynHighlighterAttributes;
    FChunkMixedAttri: TSynHighlighterAttributes;
    FChunkNewAttri: TSynHighlighterAttributes;
    FChunkOldAttri: TSynHighlighterAttributes;
    FLineAddedAttri: TSynHighlighterAttributes;
    FLineChangedAttri: TSynHighlighterAttributes;
    FLineContextAttri: TSynHighlighterAttributes;
    FLineRemovedAttri: TSynHighlighterAttributes;
    FNewFileAttri: TSynHighlighterAttributes;
    FOrigFileAttri: TSynHighlighterAttributes;
    FSpaceAttri: TSynHighlighterAttributes;
    FUnknownAttri: TSynHighlighterAttributes;
  protected
    function GetSampleSource : String; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;

    function  GetToken: String; override;
    procedure GetTokenEx(out TokenStart: PChar; out TokenLength: integer); override;
    function  GetTokenAttribute: TSynHighlighterAttributes; override;
    function  GetTokenKind: integer; override;
    function  GetTokenPos: Integer; override;
    function  GetTokenID: TtkTokenKind;

    procedure Next; override;
    function GetEol: Boolean; override;

    procedure SetLine(const NewValue: String;
      LineNumber: Integer); override;
    function GetRange: Pointer; override;
    procedure SetRange(Value: Pointer); override;
    procedure ResetRange; override;

    function GetDefaultAttribute(Index: integer): TSynHighlighterAttributes; override;
    class function GetLanguageName: string; override;
  protected
    // folding
    procedure CreateRootCodeFoldBlock; override;

    function StartDiffCodeFoldBlock(ABlockType: TDiffCodeFoldBlockType): TSynCustomCodeFoldBlock;
    procedure EndDiffCodeFoldBlock;
    function TopDiffCodeFoldBlockType(DownIndex: Integer = 0): TDiffCodeFoldBlockType;

    function GetFoldConfigInstance(Index: Integer): TSynCustomFoldConfig; override;
    function GetFoldConfigCount: Integer; override;
    function GetFoldConfigInternalCount: Integer; override;
  published
    property UnknownAttri: TSynHighlighterAttributes read FUnknownAttri write FUnknownAttri;
    property SpaceAttri: TSynHighlighterAttributes read FSpaceAttri write FSpaceAttri;
    property OrigFileAttri: TSynHighlighterAttributes read FOrigFileAttri write FOrigFileAttri;
    property NewFileAttri: TSynHighlighterAttributes read FNewFileAttri write FNewFileAttri;
    property ChunkMarkerAttri: TSynHighlighterAttributes read FChunkMarkerAttri write FChunkMarkerAttri;
    property ChunkNewAttri: TSynHighlighterAttributes read FChunkNewAttri write FChunkNewAttri;
    property ChunkOldAttri: TSynHighlighterAttributes read FChunkOldAttri write FChunkOldAttri;
    property ChunkMixedAttri: TSynHighlighterAttributes read FChunkMixedAttri write FChunkMixedAttri;
    property LineAddedAttri: TSynHighlighterAttributes read FLineAddedAttri write FLineAddedAttri;
    property LineRemovedAttri: TSynHighlighterAttributes read FLineRemovedAttri write FLineRemovedAttri;
    property LineChangedAttri: TSynHighlighterAttributes read FLineChangedAttri write FLineChangedAttri;
    property LineContextAttri: TSynHighlighterAttributes read FLineContextAttri write FLineContextAttri;
  end;

implementation

uses
  SynEditStrConst;


{ TSynDiffSyn }

procedure TSynDiffSyn.MakeMethodTables;
var
  I: Char;
begin
  for I := #0 to #255 do
    case I of
      #13: fProcTable[I] := @CRProc;
      #10: fProcTable[I] := @LFProc;
      #0: fProcTable[I] :=  @NullProc;
      '*': fProcTable[I] := @AsteriskProc;
      '-': fProcTable[I] := @MinusProc;
      '+': fProcTable[I] := @PlusProc;
      '!': fProcTable[I] := @ExclamationProc;
      '@': fProcTable[I] := @AtProc;
      ' ': fProcTable[I] := @SpaceProc;
    else fProcTable[I] := @UnknownProc;
    end;
end;

procedure TSynDiffSyn.CRProc;
begin
  fTokenID := tkSpace;
  Inc(Run);
  if (fLine[Run] = #10) then Inc(Run);
end;

procedure TSynDiffSyn.LFProc;
begin
  fTokenID := tkSpace;
  inc(Run);
end;

procedure TSynDiffSyn.NullProc;
begin
  fTokenID := tkNull;
end;

procedure TSynDiffSyn.LineProc;
var
  l: Integer;
begin
  case fRange of
    rsCtxFileOrig, rsUniFileOrig: FTokenID := tkFileOrig;
    rsCtxFileNew, rsUniFileNew:   FTokenID := tkFileNew;
    rsCtxChunkNew: begin
        l := length(fLine);
        if fLine[Run] = '-' then begin
          FTokenID := tkChunkNewMark;
          while (Run < l-1) and (fLine[Run] = '-') do inc(Run);
        end else begin
          FTokenID := tkChunkNew;
          while (Run < l-1) and (fLine[Run] <> '-') do inc(Run);
        end;
      end;
    rsCtxChunkOrig: begin
        l := length(fLine);
        if fLine[Run] = '*' then begin
          FTokenID := tkChunkOrigMark;
          while (Run < l-1) and (fLine[Run] = '*') do inc(Run);
        end else begin
          FTokenID := tkChunkOrig;
          while (Run < l-1) and (fLine[Run] <> '*') do inc(Run);
        end;
      end;
    rsUniChunkHeader: begin
        l := length(fLine);
        if fLine[Run] = '@' then begin
          FTokenID := tkChunkMixedMark;
          while (Run < l-1) and (fLine[Run] = '@') do inc(Run);
        end else begin
          FTokenID := tkChunkMixed;
          while (Run < l-1) and (fLine[Run] <> '@') do inc(Run);
        end;
      end;
    rsCtxLineRemoved, rsUniLineRemoved: FTokenID := tkLineRemoved;
    rsCtxLineAdded, rsUniLineAdded:     FTokenID := tkLineAdded;
    rsCtxLineChanged:                   FTokenID := tkLineChanged;
    rsCtxLineContext, rsUniLineContext: FTokenID := tkLineContext;
    else begin
      FTokenID := tkUnknown;
    end;
  end;
  Run := length(fLine); // #0 NullProc
end;

procedure TSynDiffSyn.UnknownProc;
begin
  if (fRange in [rsCtxFirst..rsCtxLast]) then
    fRange := rsCtxUnknown
  else if (fRange in [rsUniFirst..rsUniLast]) then
    fRange := rsUniUnknown
  else
    fRange := rsUnknown;
  FTokenID := tkUnknown;
  Run := length(fLine);
end;

procedure TSynDiffSyn.AsteriskProc;
var
  l: Integer;
begin
  (* Could be:
     *** /path/to/original   # orig file                     (Context Format)
     ***************         # start of chunk                (Context Format)
     *** 10,15 ****          # chunk: lines in orig file     (Context Format)
  *)
  l := length(fLine);
  if (not (fRange in [rsUnknown, rsCtxFirst..rsCtxLast])) or
     (l < 4) or (fLine[1] <> '*') or (fLine[2] <> '*')
  then begin
    UnknownProc;
    exit;
  end;

  if fLine[3] = '*' then begin
    // ***************
    fRange := rsCtxChunkHeader;
    FTokenID := tkChunkSeparator;
    Run := l;
    exit;
  end;

  if fLine[3] = ' ' then begin
    if fRange in [rsCtxChunkHeader..rsCtxChunkNew] then begin
      // *** 10,15 ****
      fRange := rsCtxChunkOrig;
      FTokenID := tkChunkOrigMark;
      Run := 3;
    end
    else begin
      // *** /path/to/original
      fRange := rsCtxFileOrig;
      FTokenID := tkFileOrigMark;
      Run := 3;
    end;
  end
  else
    UnknownProc;
end;

procedure TSynDiffSyn.MinusProc;
var
  l: Integer;
begin
  (* Could be:
     --- /path/to/new        # new file                      (Context) [usually after *** / path/to/orig]
     --- 10,15 ----          # chunk: lines in new file      (Context)
     - foo                   # Removed Line                  (Context)
     --- /path/to/original   # orig file                     (Unified) [usually before +++ / path/to/new]
     -foo                    # Removed Line                  (Unified)
  *)

  l := length(fLine);
  if (l > 4) and (fLine[1] = '-') and (fLine[2] = '-') and (fLine[3] = ' ') then begin
    // Todo: this check is unsufficent in Unified format; need to track amount of lines in Chunk
    if fRange in [rsUnknown, rsUniFirst..rsUniLast] then begin
      // --- /path/to/original   # orig file                   (Unified)
      fRange := rsUniFileOrig;
      FTokenID := tkFileOrigMark;
      Run := 3;
    end
    else if fRange in [rsCtxFileOrig] then begin
      // --- /path/to/new        # new file                    (Context)
      fRange := rsCtxFileNew;
      FTokenID := tkFileNewMark;
      Run := 3;
    end
    else begin
      // --- 10,15 ----          # new line                    (Context)
      fRange := rsCtxChunkNew;
      FTokenID := tkChunkNewMark;
      Run := 3;
    end;

    exit;
  end;

  if fRange = rsUnknown then begin
    FTokenID := tkUnknown;
    Run := l;
    exit;
  end;

  if (fRange in [rsCtxFirst..rsCtxLast]) then begin
    fRange := rsCtxLineRemoved;
    Run := Min(l, 2);
  end else begin
    fRange := rsUniLineRemoved;
    Run := 1;
  end;
  FTokenID := tkLineRemovedMark;
end;

procedure TSynDiffSyn.PlusProc;
var
  l: Integer;
begin
  (* Could be:
     + foo                   # Removed Line                  (Context)
     +++ /path/to/new        # orig file                     (Unified)
     +foo                    # Removed Line                  (Unified)
  *)

  l := length(fLine);
  if (l > 4) and (fLine[1] = '+') and (fLine[2] = '+') and (fLine[3] = ' ') then begin
    // Todo: this check is unsufficent in Unified format; need to track amount of lines in Chunk
    if fRange in [rsUnknown, rsUniFirst..rsUniLast] then begin
      // --- /path/to/new      # new file                    (Unified)
      fRange := rsUniFileNew;
      FTokenID := tkFileNewMark;
      Run := 3;
      exit;
    end;
  end;

  if fRange = rsUnknown then begin
    FTokenID := tkUnknown;
    Run := l;
    exit;
  end;

  if (fRange in [rsCtxFirst..rsCtxLast]) then begin
    fRange := rsCtxLineAdded;
    Run := Min(l, 2);
  end else begin
    fRange := rsUniLineAdded;
    Run := 1;
  end;
  FTokenID := tkLineAddedMark;
end;

procedure TSynDiffSyn.ExclamationProc;
var
  l: Integer;
begin
  (* Could be:
     ! foo                   # Changed Line                  (Context)
  *)

  l := length(fLine);
  if (fRange in [rsCtxFirst..rsCtxLast]) and (l >= 2) and (fLine[1] = ' ')
  then begin
    fRange := rsCtxLineChanged;
    FTokenID := tkLineChangedMark;
    Run := Min(l, 2);
    exit;
  end;

  UnknownProc;
end;

procedure TSynDiffSyn.AtProc;
var
  l: Integer;
begin
  (* Could be:
     @@ -1,3 +1,9 @@         # Start of Chunk                (Unified)
  *)

  l := length(fLine);
  if (fRange in [rsCtxFirst..rsCtxLast]) or
     (l < 3) or (fLine[1] <> '@') or (fLine[2] <> ' ')
  then begin
    UnknownProc;
    exit;
  end;

  fRange := rsUniChunkHeader;
  FTokenID := tkChunkMixedMark;
    Run := Min(l, 2);
end;

procedure TSynDiffSyn.SpaceProc;
begin
  (* Could be:
     " foo"                  # Context Line                  (Context, Unified)
  *)

  if fRange = rsUnknown then
    UnknownProc;

  if (fRange in [rsCtxFirst..rsCtxLast]) then begin
    fRange := rsCtxLineContext;
    Run := Min(length(fLine), 2);
  end else begin
    fRange := rsUniLineContext;
    Run := 1;
  end;

  FTokenID := tkChunkMixedMark;
end;

function TSynDiffSyn.GetSampleSource: string;
begin
  Result := ''#13#10 +
            '';
end;

constructor TSynDiffSyn.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FUnknownAttri := TSynHighlighterAttributes.Create(@SYNS_AttrUnknownWord, SYNS_XML_AttrUnknownWord);
  FUnknownAttri.Style := [fsItalic];
  AddAttribute(FUnknownAttri);

  FSpaceAttri := TSynHighlighterAttributes.Create(@SYNS_AttrSpace, SYNS_XML_AttrSpace);
  AddAttribute(FSpaceAttri);

  FOrigFileAttri := TSynHighlighterAttributes.Create(@SYNS_AttrOrigFile, SYNS_XML_AttrOrigFile);
  FOrigFileAttri.Style := [fsBold];
  FOrigFileAttri.Background := clRed;
  AddAttribute(FOrigFileAttri);

  FNewFileAttri := TSynHighlighterAttributes.Create(@SYNS_AttrNewFile, SYNS_XML_AttrNewFile);
  FNewFileAttri.Style := [fsBold];
  FNewFileAttri.Background := clGreen;
  AddAttribute(FNewFileAttri);

  FChunkMarkerAttri := TSynHighlighterAttributes.Create(@SYNS_AttrChunkMarker, SYNS_XML_AttrChunkMarker);
  FChunkMarkerAttri.Style := [fsBold];
  AddAttribute(FChunkMarkerAttri);

  FChunkNewAttri := TSynHighlighterAttributes.Create(@SYNS_AttrChunkNew, SYNS_XML_AttrChunkNew);
  FChunkNewAttri.Style := [fsBold];
  FChunkNewAttri.Foreground := clGreen;
  AddAttribute(FChunkNewAttri);

  FChunkOldAttri := TSynHighlighterAttributes.Create(@SYNS_AttrChunkOrig, SYNS_XML_AttrChunkOrig);
  FChunkOldAttri.Style := [fsBold];
  FChunkOldAttri.Foreground := clRed;
  AddAttribute(FChunkOldAttri);

  FChunkMixedAttri := TSynHighlighterAttributes.Create(@SYNS_AttrChunkMixed, SYNS_XML_AttrChunkMixed);
  FChunkMixedAttri.Style := [fsBold];
  FChunkMixedAttri.Foreground := clPurple;
  AddAttribute(FChunkMixedAttri);

  FLineAddedAttri := TSynHighlighterAttributes.Create(@SYNS_AttrLineAdded, SYNS_XML_AttrLineAdded);
  FLineAddedAttri.Foreground := clGreen;
  AddAttribute(FLineAddedAttri);

  FLineRemovedAttri := TSynHighlighterAttributes.Create(@SYNS_AttrLineRemoved, SYNS_XML_AttrLineRemoved);
  FLineRemovedAttri.Foreground := clRed;
  AddAttribute(FLineRemovedAttri);

  FLineChangedAttri := TSynHighlighterAttributes.Create(@SYNS_AttrLineChanged, SYNS_XML_AttrLineChanged);
  FLineChangedAttri.Foreground := clPurple;
  AddAttribute(FLineChangedAttri);

  FLineContextAttri := TSynHighlighterAttributes.Create(@SYNS_AttrLineContext, SYNS_XML_AttrLineContext);
  AddAttribute(FLineContextAttri);

  SetAttributesOnChange(@DefHighlightChange);
  MakeMethodTables;
  fDefaultFilter := '';
  fRange := rsUnknown;
end;

destructor TSynDiffSyn.Destroy;
begin
  inherited Destroy;
end;

function TSynDiffSyn.GetToken: String;
var
  Len: LongInt;
begin
  Result := '';
  Len := Run - fTokenPos;
  SetString(Result, (FLine + fTokenPos), Len);
end;

procedure TSynDiffSyn.GetTokenEx(out TokenStart: PChar;
  out TokenLength: integer);
begin
  TokenLength:=Run-fTokenPos;
  TokenStart:=FLine + fTokenPos;
end;

function TSynDiffSyn.GetTokenAttribute: TSynHighlighterAttributes;
begin
  case fTokenID of
    tkNull:            Result := FUnknownAttri;
    tkUnknown:         Result := FUnknownAttri;
    tkSpace:           Result := FSpaceAttri;
    tkFileOrigMark:    Result := FOrigFileAttri;
    tkFileNewMark:     Result := FNewFileAttri;
    tkFileOrig:        Result := FOrigFileAttri;
    tkFileNew:         Result := FNewFileAttri;
    tkChunkSeparator:  Result := FChunkMarkerAttri;
    tkChunkMixedMark:  Result := FChunkMixedAttri;
    tkChunkOrigMark:   Result := FChunkOldAttri;
    tkChunkNewMark:    Result := FChunkNewAttri;
    tkChunkMixed:      Result := FChunkMixedAttri;
    tkChunkOrig:       Result := FChunkOldAttri;
    tkChunkNew:        Result := FChunkNewAttri;
    tkLineRemovedMark: Result := FLineRemovedAttri;
    tkLineAddedMark:   Result := FLineAddedAttri;
    tkLineChangedMark: Result := FLineChangedAttri;
    tkLineContextMark: Result := FLineContextAttri;
    tkLineRemoved:     Result := FLineRemovedAttri;
    tkLineAdded:       Result := FLineAddedAttri;
    tkLineChanged:     Result := FLineChangedAttri;
    tkLineContext:     Result := FLineContextAttri;
    else Result := nil;
  end;
end;

function TSynDiffSyn.GetTokenKind: integer;
begin
  Result := Ord(fTokenID);
end;

function TSynDiffSyn.GetTokenPos: Integer;
begin
  Result := fTokenPos;
end;

function TSynDiffSyn.GetTokenID: TtkTokenKind;
begin
  Result := fTokenId;
end;

procedure TSynDiffSyn.Next;
var
  OldRange: TRangeState;
begin
  fTokenPos := Run;
  if Run > 0 then begin
    if fLine[Run] = #0 then NullProc
                       else LineProc;
  end else begin
    OldRange := fRange;
    fProcTable[fLine[Run]]();

    if ((fRange in [rsCtxFileOrig, rsCtxFileNew, rsUniFileOrig..rsUniFileNew]) and
        not (OldRange in [rsCtxFileOrig, rsCtxFileNew, rsUniFileOrig..rsUniFileNew]))
    then begin
      while (TopDiffCodeFoldBlockType in [cfbtDiffChunkSect, cfbtDiffChunk, cfbtDiffFile]) do
        EndDiffCodeFoldBlock;
      StartDiffCodeFoldBlock(cfbtDiffFile);
    end;

    if (fRange in [rsCtxChunkHeader, rsUniChunkHeader])
    then begin
      while (TopDiffCodeFoldBlockType in [cfbtDiffChunkSect, cfbtDiffChunk]) do
        EndDiffCodeFoldBlock;
      StartDiffCodeFoldBlock(cfbtDiffChunk);
    end;

    if (fRange in [rsCtxChunkOrig, rsCtxChunkNew])
    then begin
      if (TopDiffCodeFoldBlockType = cfbtDiffChunkSect) then
        EndDiffCodeFoldBlock;
      StartDiffCodeFoldBlock(cfbtDiffChunkSect);
    end;


  end;
end;

function TSynDiffSyn.GetEol: Boolean;
begin
  Result := fTokenId = tkNull;
end;

procedure TSynDiffSyn.SetLine(const NewValue: String;
  LineNumber: Integer);
begin
  inherited;
  fLine := PChar(NewValue);
  Run := 0;
  fLineNumber := LineNumber;
  Next;
end;

function TSynDiffSyn.GetRange: Pointer;
begin
  CodeFoldRange.RangeType:=Pointer(PtrUInt(Integer(fRange)));
  Result := inherited;
end;

procedure TSynDiffSyn.SetRange(Value: Pointer);
begin
  inherited;
  fRange := TRangeState(Integer(PtrUInt(CodeFoldRange.RangeType)));
end;

procedure TSynDiffSyn.ResetRange;
begin
  inherited;
  fRange := rsUnknown;
end;

function TSynDiffSyn.GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
begin
  Result := nil;
end;

class function TSynDiffSyn.GetLanguageName: string;
begin
  Result := SYNS_LangDiff;
end;

procedure TSynDiffSyn.CreateRootCodeFoldBlock;
begin
  inherited CreateRootCodeFoldBlock;
  RootCodeFoldBlock.InitRootBlockType(Pointer(PtrInt(cfbtDiffNone)));
end;

function TSynDiffSyn.StartDiffCodeFoldBlock(ABlockType: TDiffCodeFoldBlockType): TSynCustomCodeFoldBlock;
var
  FoldBlock: Boolean;
  p: PtrInt;
begin
  FoldBlock :=  FFoldConfig[ord(ABlockType)].Enabled;
  p := 0;
  if not FoldBlock then
    p := PtrInt(CountDiffCodeFoldBlockOffset);
  Result := StartCodeFoldBlock(p + Pointer(PtrInt(ABlockType)), FoldBlock);
end;

procedure TSynDiffSyn.EndDiffCodeFoldBlock;
var
  DecreaseLevel: Boolean;
begin
  DecreaseLevel := TopCodeFoldBlockType < CountDiffCodeFoldBlockOffset;
  EndCodeFoldBlock(DecreaseLevel);
end;

function TSynDiffSyn.TopDiffCodeFoldBlockType(DownIndex: Integer): TDiffCodeFoldBlockType;
var
  p: Pointer;
begin
  p := TopCodeFoldBlockType(DownIndex);
  if p >= CountDiffCodeFoldBlockOffset then
    p := p - PtrUInt(CountDiffCodeFoldBlockOffset);
  Result := TDiffCodeFoldBlockType(PtrUInt(p));
end;

function TSynDiffSyn.GetFoldConfigInstance(Index: Integer): TSynCustomFoldConfig;
begin
  Result := inherited GetFoldConfigInstance(Index);
  Result.Enabled := True;
end;

function TSynDiffSyn.GetFoldConfigCount: Integer;
begin
  // excluded cfbtDiffnone;
  Result := ord(high(TDiffCodeFoldBlockType)) - ord(low(TDiffCodeFoldBlockType));
end;

function TSynDiffSyn.GetFoldConfigInternalCount: Integer;
begin
  // include cfbtDiffnone;
  Result := ord(high(TDiffCodeFoldBlockType)) - ord(low(TDiffCodeFoldBlockType)) + 1;
end;

initialization
  RegisterPlaceableHighlighter(TSynDiffSyn);

end.