File: synedittexttabexpander.pas

package info (click to toggle)
lazarus 1.2.4%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 170,220 kB
  • ctags: 115,165
  • sloc: pascal: 1,386,898; xml: 257,878; sh: 2,935; java: 603; makefile: 549; perl: 297; sql: 174; ansic: 137
file content (392 lines) | stat: -rw-r--r-- 11,644 bytes parent folder | download | duplicates (2)
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
{-------------------------------------------------------------------------------
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
  LCLProc, Classes, SysUtils, LazSynEditText, SynEditTextBase;

type

  // lines longer than 16383 chars, will be stored as unknown
  TLineLen = Word;
  PLineLen = ^TLineLen;

  { TSynEditStringTabData }

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

{ TSynEditStringTabExpander }

  TSynEditStringTabExpander = class(TSynEditStringsLinked)
  private
    FTabWidth: integer;
    FIndexOfLongestLine: Integer;
    FTabData: TSynEditStringTabData;
    FLastLineHasTab: Boolean; // Last line, parsed by GetPhysicalCharWidths
    FLastLinePhysLen: Integer;
    FViewChangeStamp: int64;
    procedure TextBufferChanged(Sender: TObject);
    procedure LineCountChanged(Sender: TSynEditStrings; AIndex, ACount : Integer);
    function ExpandedString(Index: integer): string;
    function ExpandedStringLength(Index: integer): Integer;
  protected
    function GetViewChangeStamp: int64; override;
    function  GetTabWidth : integer;
    procedure SetTabWidth(const AValue : integer);
    function  GetExpandedString(Index: integer): string; override;
    function  GetLengthOfLongestLine: integer; override;
    procedure DoGetPhysicalCharWidths(Line: PChar; LineLen, Index: Integer; PWidths: PPhysicalCharWidth); override;
  public
    constructor Create(ASynStringSource: TSynEditStrings);
    destructor Destroy; override;

    property LengthOfLongestLine: integer read GetLengthOfLongestLine;
  public
    property TabWidth: integer read GetTabWidth write SetTabWidth;
  end;


implementation

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 = high(TLineLen) div 2;
  LINE_LEN_UNKNOWN = high(TLineLen);
  MAX_LINE_LEN_STORED = NO_TAB_IN_LINE_OFFSET - 1;

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;

{ TSynEditStringTabData }

function TSynEditStringTabData.GetLineLen(Index: Integer): TLineLen;
begin
  Result := PLineLen(ItemPointer[Index])^;
end;

procedure TSynEditStringTabData.SetLineLen(Index: Integer; const AValue: TLineLen);
begin
  PLineLen(ItemPointer[Index])^ := AValue;
end;

constructor TSynEditStringTabData.Create;
begin
  inherited;
  ItemSize := SizeOf(TLineLen);
  FRefCount := 1;
end;

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

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

{ TSynEditStringTabExpander }

constructor TSynEditStringTabExpander.Create(ASynStringSource: TSynEditStrings);
begin
  FIndexOfLongestLine := -1;
  inherited Create(ASynStringSource);
  TextBufferChanged(nil);
  TabWidth := 8;
  fSynStrings.AddChangeHandler(senrLineCount, @LineCountChanged);
  fSynStrings.AddChangeHandler(senrLineChange, @LineCountChanged);
  fSynStrings.AddNotifyHandler(senrTextBufferChanged, @TextBufferChanged);
end;

destructor TSynEditStringTabExpander.Destroy;
var
  Data: TSynEditStringTabData;
begin
  Data := TSynEditStringTabData(fSynStrings.Ranges[Self]);
  if Assigned(Data) then begin
    Data.DecRefCount;
    if Data.RefCount = 0 then begin
      fSynStrings.Ranges[Self] := nil;
      Data.Free;
    end;
  end;
  fSynStrings.RemoveChangeHandler(senrLineChange, @LineCountChanged);
  fSynStrings.RemoveChangeHandler(senrLineCount, @LineCountChanged);
  fSynStrings.RemoveNotifyHandler(senrTextBufferChanged, @TextBufferChanged);
  inherited Destroy;
end;

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

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

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

  FTabWidth := AValue;
  FIndexOfLongestLine := -1;
  for i := 0 to Count - 1 do
    if not(FTabData[i] >= NO_TAB_IN_LINE_OFFSET) then
      FTabData[i] := LINE_LEN_UNKNOWN;
end;

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

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

  if Sender <> nil then begin
    Data := TSynEditStringTabData(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;
  LineCountChanged(TSynEditStrings(Sender), 0, Count);
end;

procedure TSynEditStringTabExpander.LineCountChanged(Sender: TSynEditStrings; AIndex, ACount: Integer);
var
  i: integer;
begin
  FIndexOfLongestLine := -1;
  for i := AIndex to AIndex + ACount - 1 do
    FTabData[i] := LINE_LEN_UNKNOWN;
end;

function TSynEditStringTabExpander.ExpandedString(Index: integer): string;
var
  Line: String;
  CharWidths: TPhysicalCharWidths;
  i, j, l: Integer;
begin
// this is only used by trimmer.lengthOfLongestLine / which is not called, if a tab module is present
  Line := fSynStrings[Index];
  if (Line = '') or (not GetHasTabs(PChar(Line))) then begin
    Result := Line;
    // xxx wrong double width // none latin ...
    //FTabData[Index] := length(Result) + NO_TAB_IN_LINE_OFFSET;
  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);
    SetLength(Result, l);

    l := 1;
    for i := 1 to length(CharWidths) do begin
      if Line[i] <> #9 then begin
        Result[l] := Line[i];
        inc(l);
      end else begin
        for j := 1 to (CharWidths[i-1] and PCWMask) do begin
          Result[l] := ' ';
          inc(l);
        end;
      end;
    end;
    FTabData[Index] := length(Result);
  end;
end;

function TSynEditStringTabExpander.ExpandedStringLength(Index: integer): Integer;
var
  Line: String;
  CharWidths: TPhysicalCharWidths;
  i: Integer;
begin
  Line := fSynStrings[Index];
  if (Line = '') then begin
    Result := 0;
    FTabData[Index] := Result + NO_TAB_IN_LINE_OFFSET;
  end else begin
    i := length(Line);
    SetLength(CharWidths, i);
    DoGetPhysicalCharWidths(Pchar(Line), i, Index, @CharWidths[0]);
    Result := 0;
    for i := 0 to length(CharWidths)-1 do
      Result := Result + (CharWidths[i] and PCWMask);

    if FLastLineHasTab then // FLastLineHasTab is set by GetPhysicalCharWidths
      FTabData[Index] := Result
    else
      FTabData[Index] := Result + NO_TAB_IN_LINE_OFFSET;
  end;
end;

function TSynEditStringTabExpander.GetExpandedString(Index: integer): string;
begin
  if (Index >= 0) and (Index < Count) then begin
    if FTabData[Index] >= NO_TAB_IN_LINE_OFFSET then
      Result := fSynStrings[Index]
    else
      Result := ExpandedString(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.GetLengthOfLongestLine: integer;
var
  Line: PChar;
  LineLen: Integer;
  CharWidths: PPhysicalCharWidth;
  i, j, m: Integer;
  Line1, Line2: Integer;
begin
  Line1 := 0;
  Line2 := Count - 1;
  if (fIndexOfLongestLine >= 0) and (fIndexOfLongestLine < Count) then begin
    Result := FTabData[fIndexOfLongestLine];
    if Result <> LINE_LEN_UNKNOWN then begin
      if Result >= NO_TAB_IN_LINE_OFFSET then Result := Result -  NO_TAB_IN_LINE_OFFSET;
      exit;
    end;
    Line1 := fIndexOfLongestLine;
    Line2 := fIndexOfLongestLine;
  end;

  try
    Result := 0;
    m := 0;
    CharWidths := nil;
    for i := Line1 to Line2 do begin
      j := FTabData[i];
      if j = LINE_LEN_UNKNOWN then begin
        // embedd a copy of ExpandedStringLength
        // allows one to re-use CharWidths
        Line := NextLines.GetPChar(i,LineLen); // fSynStrings[i];
        j := 0;
        if (LineLen = 0) then begin
          FTabData[i] := j + NO_TAB_IN_LINE_OFFSET;
        end else begin
          if LineLen > m then begin
            ReAllocMem(CharWidths, LineLen * SizeOf(TPhysicalCharWidth));
            m := LineLen;
          end;
          DoGetPhysicalCharWidths(Line, LineLen, i, CharWidths);
          j := FLastLinePhysLen;

          if j > MAX_LINE_LEN_STORED then
            FTabData[i] := LINE_LEN_UNKNOWN
          else if FLastLineHasTab then // FLastLineHasTab is set by GetPhysicalCharWidths
            FTabData[i] := j
          else
            FTabData[i] := j + NO_TAB_IN_LINE_OFFSET;
        end;
      end
      else
      if j >= NO_TAB_IN_LINE_OFFSET then
        j := j -  NO_TAB_IN_LINE_OFFSET;
      if j > Result then begin
        Result := j;
        fIndexOfLongestLine := i;
      end;
    end;
  finally
    ReAllocMem(CharWidths, 0);
  end;
end;

end.