File: synedittexttabexpander.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (690 lines) | stat: -rw-r--r-- 20,974 bytes parent folder | download
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
{-------------------------------------------------------------------------------
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.

-------------------------------------------------------------------------------}
unit SynEditTextTabExpander;

{$I synedit.inc}

interface

uses
  Classes, SysUtils, Math, LazSynEditText, SynEditTextBase, SynEditTypes;

type

  // lines longer than "High(TLineLen) div 2" chars, will be stored as unknown
  TStoredLineLen = DWord;
  PStoredLineLen = ^TStoredLineLen;

  { TSynEditStringLineLenData }

  TSynEditStringLineLenData = class(TSynManagedStorageMem)
  private
    FRefCount: Integer;
    procedure SetLineLen(Index: Integer; const AValue: TStoredLineLen);
  protected
    function GetLineLen(Index: Integer): TStoredLineLen; virtual;
  public
    constructor Create;
    procedure IncRefCount;
    procedure DecRefCount;
    property RefCount: Integer read FRefCount;
    property LineLen[Index: Integer]: TStoredLineLen read GetLineLen write SetLineLen; default;
  end;

  { TSynEditStringTabData }

  TSynEditStringTabData = class(TSynEditStringLineLenData)
  private const
    // Offset to add to LengthOfLine, if Line has no tabs.
    // (Length will still be valid if tab-width changes)
    NO_TAB_IN_LINE_OFFSET = TStoredLineLen(
                                ( ( (high(TStoredLineLen) >> 1) and high(Integer) ) << 1) and
                              not ( (high(TStoredLineLen) >> 1) and high(Integer) )
                            );
    MAX_LINE_LEN_STORED = NO_TAB_IN_LINE_OFFSET - 2;
    LINE_INFO_UNKNOWN = 0; // invalid entry
  public const
    LINE_LEN_UNKNOWN  = TStoredLineLen(NO_TAB_IN_LINE_OFFSET- 1);
    (* Special values
       - LINE_INFO_UNKNOWN                        : Nothing know, not even if tab exists
       - LINE_LEN_UNKNOWN                         : Tab exists, but nothing else known (an empty line can't have tabs, so 0 can be used as marker)
       - LINE_LEN_UNKNOWN + NO_TAB_IN_LINE_OFFSET : NO Tab exists, but nothing else known
    *)
  private
    function GetHasTab(Index: Integer): Boolean; inline;
    function GetRawData(Index: Integer): TStoredLineLen;
  protected
    function GetLineLen(Index: Integer): TStoredLineLen; override;
  public
    procedure SetLineInfo(AnIndex: Integer; ALineLen: Integer; AnHasTab: Boolean); inline;
    procedure SetLineInfoUnknown(AnIndex: Integer); inline;
    procedure SetLineInfoUnknownEx(AnIndex: Integer; AnHasTab: Boolean); inline;
    function  GetLineInfo(AnIndex: Integer; out ALineLen: Integer; out AnHasTab: Boolean): boolean; inline;

    property HasTab[Index: Integer]: Boolean read GetHasTab;
    property LineLen[Index: Integer]: TStoredLineLen read GetLineLen; default;
    property RawData[Index: Integer]: TStoredLineLen read GetRawData;
  end;

const
  LINE_LEN_UNKNOWN = TSynEditStringTabData.LINE_LEN_UNKNOWN;

type

  { TSynEditStringTabExpanderBase }

  TSynEditStringTabExpanderBase = class(TSynEditStringsLinked)
  protected
    function  GetTabWidth : integer; virtual; abstract;
    procedure SetTabWidth(const AValue : integer); virtual; abstract;
  public
    constructor Create; virtual;
    property TabWidth: integer read GetTabWidth write SetTabWidth;
  end;

  { TSynEditStringTabExpanderCommon }

  TSynEditStringTabExpanderCommon = class(TSynEditStringTabExpanderBase)
  private
    FTabData: TSynEditStringTabData;

    procedure ReleaseTabData;
  protected
    procedure TextBufferChanged(Sender: TObject); virtual;
    procedure LineCountChanged(Sender: TSynEditStrings; AIndex, ACount : Integer); virtual;
    procedure LineTextChanged(Sender: TSynEditStrings; aIndex, aCount: Integer); virtual;
    procedure SetManager(AManager: TSynTextViewsManager); override;
    procedure SetSynStrings(AValue: TSynEditStrings); override;

    property TabData: TSynEditStringTabData read FTabData;
  public
    destructor Destroy; override;
  end;

  { TSynEditStringTabExpanderWithLongest }

  TSynEditStringTabExpanderWithLongest = class(TSynEditStringTabExpanderCommon)
  private
    FIndexOfLongestLine: Integer;
    FFirstUnknownLongestLine, FLastUnknownLongestLine: Integer;

  protected
    procedure InvalidateLongestLineInfo;
    procedure LineCountChanged(Sender: TSynEditStrings; AIndex, ACount : Integer); override;
    procedure LineTextChanged(Sender: TSynEditStrings; aIndex, aCount: Integer); override;

    function  GetIndexOfLongestLine(AStartIndex, AnEndIndex: IntIdx; out ALen: integer): integer; virtual;
    function  GetKnownLengthOfLine(AnIndex: IntIdx): integer; virtual;
    procedure SetLongestLineInfo(AnIndex: IntIdx; ALen: Integer); virtual;
    function  GetLengthOfLongestLine: integer; override;
  public
    constructor Create; override;
  end;


  TSynEditStringTabExpander = class(TSynEditStringTabExpanderWithLongest)
  private
    FTabWidth: integer;
    FLastLineHasTab: Boolean; // Last line, parsed by GetPhysicalCharWidths
    FLastLinePhysLen: Integer;
    FViewChangeStamp: int64;
    function ExpandedString(Index: integer): string;
    function ExpandedStringLength(Index: integer): Integer;
  protected
    procedure TextBufferChanged(Sender: TObject); override;
    function GetViewChangeStamp: int64; override;
    function  GetTabWidth : integer; override;
    procedure SetTabWidth(const AValue : integer); override;
    function  GetExpandedString(Index: integer): string; override;
    function GetKnownLengthOfLine(AnIndex: IntIdx): integer; override;
    function GetIndexOfLongestLine(AStartIndex, AnEndIndex: IntIdx; out ALen: integer): integer; override;
    procedure DoGetPhysicalCharWidths(Line: PChar; LineLen, Index: Integer; PWidths: PPhysicalCharWidth); override;
  public
    constructor Create; override;
  end;

  TSynEditStringTabExpanderClass = class of TSynEditStringTabExpanderBase;


implementation


function GetHasTabs(pLine: PChar): boolean;
begin
  if Assigned(pLine) then begin
    while (pLine^ <> #0) do begin
      if (pLine^ = #9) then break;
      Inc(pLine);
    end;
    Result := (pLine^ = #9);
  end else
    Result := FALSE;
end;

{ TSynEditStringLineLenData }

function TSynEditStringLineLenData.GetLineLen(Index: Integer): TStoredLineLen;
begin
  Result := PStoredLineLen(ItemPointer[Index])^;
end;

procedure TSynEditStringLineLenData.SetLineLen(Index: Integer; const AValue: TStoredLineLen);
begin
  PStoredLineLen(ItemPointer[Index])^ := AValue;
end;

constructor TSynEditStringLineLenData.Create;
begin
  inherited;
  ItemSize := SizeOf(TStoredLineLen);
  FRefCount := 1;
end;

procedure TSynEditStringLineLenData.IncRefCount;
begin
  inc(FRefCount);
end;

procedure TSynEditStringLineLenData.DecRefCount;
begin
  dec(FRefCount);
end;

{ TSynEditStringTabData }

function TSynEditStringTabData.GetHasTab(Index: Integer): Boolean;
var
  l: TStoredLineLen;
begin
  l := inherited GetLineLen(Index);
  Result := (l < NO_TAB_IN_LINE_OFFSET) and (l <> LINE_INFO_UNKNOWN);
end;

function TSynEditStringTabData.GetRawData(Index: Integer): TStoredLineLen;
begin
  Result := inherited GetLineLen(Index);
end;

function TSynEditStringTabData.GetLineLen(Index: Integer): TStoredLineLen;
begin
  Result := inherited GetLineLen(Index);
  if Result = LINE_INFO_UNKNOWN then
    Result := LINE_LEN_UNKNOWN // unkonwn len, but tabs exist
  else
  if (Result >= NO_TAB_IN_LINE_OFFSET) then
    Result := Result - NO_TAB_IN_LINE_OFFSET;
end;

procedure TSynEditStringTabData.SetLineInfo(AnIndex: Integer;
  ALineLen: Integer; AnHasTab: Boolean);
begin
  assert((not AnHasTab) or (ALineLen>0), 'TSynEditStringTabData.SetLineInfo: (not AnHasTab) or (ALineLen>0)');
  if ALineLen > MAX_LINE_LEN_STORED then
    ALineLen := LINE_LEN_UNKNOWN;
  if AnHasTab then
    inherited SetLineLen(AnIndex, ALineLen)
  else
    inherited SetLineLen(AnIndex, ALineLen + NO_TAB_IN_LINE_OFFSET);
end;

procedure TSynEditStringTabData.SetLineInfoUnknown(AnIndex: Integer);
begin
  inherited SetLineLen(AnIndex, LINE_INFO_UNKNOWN);
end;

procedure TSynEditStringTabData.SetLineInfoUnknownEx(AnIndex: Integer;
  AnHasTab: Boolean);
begin
  if AnHasTab then
    inherited SetLineLen(AnIndex, LINE_LEN_UNKNOWN)
  else
    inherited SetLineLen(AnIndex, LINE_LEN_UNKNOWN + NO_TAB_IN_LINE_OFFSET);
end;

function TSynEditStringTabData.GetLineInfo(AnIndex: Integer; out
  ALineLen: Integer; out AnHasTab: Boolean): boolean;
var
  d: TStoredLineLen;
begin
  d := inherited GetLineLen(AnIndex);
  Result := d <> LINE_INFO_UNKNOWN;
  AnHasTab := Result and (d < NO_TAB_IN_LINE_OFFSET);

  if not Result then
    ALineLen := LINE_LEN_UNKNOWN
  else
  if (d >= NO_TAB_IN_LINE_OFFSET) then
    ALineLen := d - NO_TAB_IN_LINE_OFFSET
  else
    ALineLen := d;
end;

{ TSynEditStringTabExpanderBase }

constructor TSynEditStringTabExpanderBase.Create;
begin
  inherited Create;
end;

{ TSynEditStringTabExpanderCommon }

procedure TSynEditStringTabExpanderCommon.ReleaseTabData;
var
  Data: TSynEditStringLineLenData;
begin
  Data := TSynEditStringTabData(NextLines.Ranges[Self]);
  if Assigned(Data) then begin
    Data.DecRefCount;
    if Data.RefCount = 0 then begin
      NextLines.Ranges[Self] := nil;
      Data.Free;
    end;
  end;
  FTabData := nil;
end;

procedure TSynEditStringTabExpanderCommon.TextBufferChanged(Sender: TObject);
var
  Data: TSynEditStringLineLenData;
begin
  // Using self, instead as class, to register tab-width-data
  // other shared edits can have different tab-width
  if (Sender <> nil) and
     (FTabData = TSynEditStringLineLenData(NextLines.Ranges[Self]))
  then
    exit;

  if Sender <> nil then begin
    Data := TSynEditStringLineLenData(TSynEditStrings(Sender).Ranges[Self]);
    if Assigned(Data) then begin
      Data.DecRefCount;
      if Data.RefCount = 0 then begin
        TSynEditStrings(Sender).Ranges[Self] := nil;
        Data.Free;
      end;
    end;
  end;

  FTabData := TSynEditStringTabData(NextLines.Ranges[Self]);
  if FTabData = nil then begin
    FTabData := TSynEditStringTabData.Create;
    NextLines.Ranges[Self] := FTabData;
  end
  else
    FTabData.IncRefCount;
end;

procedure TSynEditStringTabExpanderCommon.LineCountChanged(
  Sender: TSynEditStrings; AIndex, ACount: Integer);
begin
  //
end;

procedure TSynEditStringTabExpanderCommon.LineTextChanged(
  Sender: TSynEditStrings; aIndex, aCount: Integer);
var
  i: Integer;
begin
  for i := AIndex to AIndex + ACount - 1 do
    TabData.SetLineInfoUnknown(i);
end;

procedure TSynEditStringTabExpanderCommon.SetManager(
  AManager: TSynTextViewsManager);
begin
  if Manager <> nil then begin
    RemoveNotifyHandler(senrTextBufferChanged, @TextBufferChanged);
    RemoveChangeHandler(senrLineCount, @LineCountChanged);
    RemoveChangeHandler(senrLineChange, @LineTextChanged);
  end;
  if AManager = nil then
    ReleaseTabData;
  inherited SetManager(AManager);
  if Manager <> nil then begin
    AddChangeHandler(senrLineChange, @LineTextChanged);
    AddChangeHandler(senrLineCount, @LineCountChanged);
    AddNotifyHandler(senrTextBufferChanged, @TextBufferChanged);
  end;
end;

procedure TSynEditStringTabExpanderCommon.SetSynStrings(AValue: TSynEditStrings
  );
begin
  inherited SetSynStrings(AValue);
  if NextLines <> nil then begin
    if TabData = nil then
      TextBufferChanged(nil);
  end;
end;

destructor TSynEditStringTabExpanderCommon.Destroy;
begin
  ReleaseTabData;
  inherited Destroy;
  NextLines := nil;
end;

{ TSynEditStringTabExpanderWithLongest }

procedure TSynEditStringTabExpanderWithLongest.InvalidateLongestLineInfo;
begin
  FIndexOfLongestLine := -1;
  FFirstUnknownLongestLine := -1;
  FLastUnknownLongestLine := -1;
end;

procedure TSynEditStringTabExpanderWithLongest.LineCountChanged(
  Sender: TSynEditStrings; AIndex, ACount: Integer);
begin
  inherited LineCountChanged(Sender, AIndex, ACount);

  if (FIndexOfLongestLine >= AIndex) then
    FIndexOfLongestLine := FIndexOfLongestLine + ACount;

  if ACount < 0 then begin
    if (FIndexOfLongestLine >= AIndex+ACount) and (FIndexOfLongestLine < AIndex) then
      FIndexOfLongestLine := -1;
    if (FFirstUnknownLongestLine >= 0) then begin
      if (AIndex < FFirstUnknownLongestLine) then
        FFirstUnknownLongestLine := Max(AIndex, FFirstUnknownLongestLine + ACount);
      if (AIndex < FLastUnknownLongestLine) then
        FLastUnknownLongestLine := Max(AIndex, FLastUnknownLongestLine + ACount);
    end;

    exit;
  end;

  if (FFirstUnknownLongestLine < 0) or (AIndex < FFirstUnknownLongestLine) then
    FFirstUnknownLongestLine := AIndex;
  if (AIndex < FLastUnknownLongestLine) or (FLastUnknownLongestLine < 0) then
    FLastUnknownLongestLine := Max(AIndex, FLastUnknownLongestLine) +ACount;
end;

procedure TSynEditStringTabExpanderWithLongest.LineTextChanged(
  Sender: TSynEditStrings; aIndex, aCount: Integer);
begin
  inherited LineTextChanged(Sender, aIndex, aCount);

  if (FIndexOfLongestLine >= AIndex) and (FIndexOfLongestLine < AIndex+ACount) then
    FIndexOfLongestLine := -1;
  if (FFirstUnknownLongestLine < 0) or (AIndex < FFirstUnknownLongestLine) then
    FFirstUnknownLongestLine := AIndex;
  if AIndex+ACount-1 > FLastUnknownLongestLine then
    FLastUnknownLongestLine := AIndex+ACount-1;
end;

function TSynEditStringTabExpanderWithLongest.GetIndexOfLongestLine(
  AStartIndex, AnEndIndex: IntIdx; out ALen: integer): integer;
begin
  ALen := High(Result) - 5;
  Result := -1;
end;

function TSynEditStringTabExpanderWithLongest.GetKnownLengthOfLine(
  AnIndex: IntIdx): integer;
begin
  Result := High(Result) - 5;
end;

procedure TSynEditStringTabExpanderWithLongest.SetLongestLineInfo(
  AnIndex: IntIdx; ALen: Integer);
begin
  fIndexOfLongestLine := AnIndex;
end;

function TSynEditStringTabExpanderWithLongest.GetLengthOfLongestLine: integer;
var
  NewIdx, NewLen: Integer;
  Line1, Line2: Integer;
begin
  Result := 0;
  Line1 := 0;
  Line2 := Count - 1;

  if (fIndexOfLongestLine >= 0) and (fIndexOfLongestLine < Count) then begin
    Result := GetKnownLengthOfLine(FIndexOfLongestLine);
    if Result <> LINE_LEN_UNKNOWN then begin
      if (FFirstUnknownLongestLine < 0) then
        exit;
      // Result has the value from index
      Line1 := FFirstUnknownLongestLine;
      if (FLastUnknownLongestLine < Line2) then
        Line2 := FLastUnknownLongestLine;
    end
    else begin
      Result := 0;
      if (FFirstUnknownLongestLine < 0) then begin
        Line1 := fIndexOfLongestLine;
        Line2 := fIndexOfLongestLine;
      end
      else begin // TODO: Calculate for fIndexOfLongestLine, instead of extending the range
        Line1 := Min(fIndexOfLongestLine, FFirstUnknownLongestLine);
        if (FLastUnknownLongestLine < Line2) then
          Line2 := Max(fIndexOfLongestLine, FLastUnknownLongestLine);
      end;
    end;
  end;

  NewIdx := GetIndexOfLongestLine(Line1, Line2, NewLen);
  if NewLen > Result then begin
    SetLongestLineInfo(NewIdx, NewLen);
    Result := NewLen;
  end;

  FFirstUnknownLongestLine := -1;
  FLastUnknownLongestLine := -1;
end;

constructor TSynEditStringTabExpanderWithLongest.Create;
begin
  InvalidateLongestLineInfo;
  inherited Create;
end;

{ TSynEditStringTabExpander }

constructor TSynEditStringTabExpander.Create;
begin
  inherited Create;
  TabWidth := 8;
end;

function TSynEditStringTabExpander.GetTabWidth: integer;
begin
  Result := FTabWidth;
end;

procedure TSynEditStringTabExpander.SetTabWidth(const AValue: integer);
begin
  if FTabWidth = AValue then exit;

  {$PUSH}{$Q-}{$R-}
  FViewChangeStamp := FViewChangeStamp + 1;
  {$POP}

  FTabWidth := AValue;
  if NextLines <> nil then
    LineTextChanged(nil, 0, Count);
  InvalidateLongestLineInfo;
end;

function TSynEditStringTabExpander.GetViewChangeStamp: int64;
begin
  Result := inherited GetViewChangeStamp;
  {$PUSH}{$Q-}{$R-}
  Result := Result + FViewChangeStamp;
  {$POP}
end;

procedure TSynEditStringTabExpander.TextBufferChanged(Sender: TObject);
begin
  inherited TextBufferChanged(Sender);
  LineTextChanged(TSynEditStrings(Sender), 0, Count);
end;

function TSynEditStringTabExpander.ExpandedString(Index: integer): string;
var
  Line: String;
  CharWidths: TPhysicalCharWidths;
  i, j, l: Integer;
  HasTab: Boolean;
begin
// this is only used by trimmer.lengthOfLongestLine / which is not called, if a tab module is present
  Line := NextLines[Index];
  if (Line = '') or (not GetHasTabs(PChar(Line))) then begin
    Result := Line;
    // xxx wrong double width // none latin ...
    //TabData.SetLineInfo(Index, Length(Result, False));
  end else begin
    CharWidths := GetPhysicalCharWidths(Pchar(Line), length(Line), Index);
    l := 0;
    for i := 0 to length(CharWidths)-1 do
      l := l + (CharWidths[i] and PCWMask);
    Result := '';
    SetLength(Result, l);

    l := 1;
    HasTab := False;
    for i := 1 to length(CharWidths) do begin
      if Line[i] <> #9 then begin
        Result[l] := Line[i];
        inc(l);
      end else begin
        HasTab := True;
        for j := 1 to (CharWidths[i-1] and PCWMask) do begin
          Result[l] := ' ';
          inc(l);
        end;
      end;
    end;
    TabData.SetLineInfo(Index, Length(Result), HasTab);
  end;
end;

function TSynEditStringTabExpander.ExpandedStringLength(Index: integer): Integer;
var
  Line: String;
  CharWidths: TPhysicalCharWidths;
  i: Integer;
begin
  Line := NextLines[Index];
  if (Line = '') then begin
    Result := 0;
    TabData.SetLineInfo(Index, 0, False);
  end else begin
    i := length(Line);
    SetLength(CharWidths{%H-}, i);
    DoGetPhysicalCharWidths(Pchar(Line), i, Index, @CharWidths[0]);
    Result := 0;
    for i := 0 to length(CharWidths)-1 do
      Result := Result + (CharWidths[i] and PCWMask);

    TabData.SetLineInfo(Index, Result, FLastLineHasTab);
  end;
end;

function TSynEditStringTabExpander.GetExpandedString(Index: integer): string;
begin
  if (Index >= 0) and (Index < Count) then begin
    if TabData.HasTab[Index] then
      Result := ExpandedString(Index)
    else
      Result := NextLines[Index];
  end else
    Result := '';
end;

procedure TSynEditStringTabExpander.DoGetPhysicalCharWidths(Line: PChar;
  LineLen, Index: Integer; PWidths: PPhysicalCharWidth);
var
  HasTab: Boolean;
  i, j: Integer;
begin
  inherited DoGetPhysicalCharWidths(Line, LineLen, Index, PWidths);
  HasTab := False;
  j := 0;
  for i := 0 to LineLen - 1 do begin
    if (PWidths^ and PCWMask) <> 0 then begin
      if Line^ = #9 then begin
        PWidths^ := (FTabWidth - (j mod FTabWidth) and PCWMask) or (PWidths^  and (not PCWMask));
        HasTab := True;
      end;
      j := j + (PWidths^ and PCWMask);
    end;
    inc(Line);
    inc(PWidths);
  end;
  FLastLineHasTab := HasTab;
  FLastLinePhysLen := j;
end;

function TSynEditStringTabExpander.GetKnownLengthOfLine(AnIndex: IntIdx
  ): integer;
begin
  Result := TabData[fIndexOfLongestLine];
end;

function TSynEditStringTabExpander.GetIndexOfLongestLine(AStartIndex,
  AnEndIndex: IntIdx; out ALen: integer): integer;
var
  Line: PChar;
  LineLen: Integer;
  CharWidths: PPhysicalCharWidth;
  i, j, m: Integer;
begin
  Result := 0;
  ALen := 0;
  try
    m := 0;
    CharWidths := nil;
    for i := AStartIndex to AnEndIndex do begin
      j := TabData[i];
      if j = LINE_LEN_UNKNOWN then begin
        // embedd a copy of ExpandedStringLength
        // allows one to re-use CharWidths
        Line := NextLines.GetPChar(i,LineLen); // NextLines[i];
        j := 0;
        if (LineLen = 0) then begin
          TabData.SetLineInfo(i, LineLen, False);
        end else begin
          if LineLen > m then begin
            ReAllocMem(CharWidths, LineLen * SizeOf(TPhysicalCharWidth));
            m := LineLen;
          end;
          DoGetPhysicalCharWidths(Line, LineLen, i, CharWidths);
          j := FLastLinePhysLen;
          TabData.SetLineInfo(i, j, FLastLineHasTab);
        end;
      end;

      if j > ALen then begin
        ALen := j;
        Result := i;
      end;
    end;
  finally
    ReAllocMem(CharWidths, 0);
  end;
end;

end.