File: synhighlighterposition.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 (439 lines) | stat: -rw-r--r-- 11,656 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
{-------------------------------------------------------------------------------
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.


$Id: synhighlighterposition.pas 41721 2013-06-14 21:41:35Z mattias $

This is a basic synedit highlighter that does not parse text to create the
attributes, but stores a list of ranges.

-------------------------------------------------------------------------------}
unit SynHighlighterPosition;

{$I synedit.inc}

interface

uses
  Classes, SysUtils, Graphics, SynEditStrConst, SynEditTypes,
  SynEditHighlighter;

const
  tkNone   = 0;
  tkText   = 1;
  tkCustom = 2;
  
  MaxColumns = 100000;

type
  TtkTokenKind = integer;

  TPositionToken = record
    Kind: TtkTokenKind;
    Column: integer;
  end;
  PPositionToken = ^TPositionToken;

  TPositionTokens = record
    Count: integer;
    Tokens: array[0..MaxColumns] of TPositionToken;
  end;
  PPositionTokens = ^TPositionTokens;

  { TSynPositionHighlighter }

  TSynPositionHighlighter = class(TSynCustomHighlighter)
  private
    fCopiedAttributes: TList;
    fLine: string;
    fLineLen: integer;
    fLineNumber: Integer;
    fTokenEnd: LongInt; // end of current token
    fTextAttri: TSynHighlighterAttributes;
    fTokenPos: Integer;
    fTokenArrayPos: integer;
    FTokenKind: TtkTokenKind;
    fTokens: TList; // list of PPositionTokens
    function GetTokens(TheLineNumber: integer): PPositionTokens;
  protected
    function GetIdentChars: TSynIdentChars; override;
    function IsFilterStored: boolean; override;                                 //mh 2000-10-08
    function GetPositionTokensSize(ItemCount: integer): integer;
    function GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
      override;
  public
    class function GetLanguageName: string; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    function GetEol: Boolean; override;
    function GetRange: Pointer; 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;
    procedure Next; override;
    procedure ResetRange; override;
    procedure SetLine(const NewValue: string;
      LineNumber:Integer); override;
    procedure SetRange(Value: Pointer); override;
    function UseUserSettings(settingIndex: integer): boolean; override;
    procedure EnumUserSettings(settings: TStrings); override;
    property IdentChars;
  public
    procedure AddToken(Line, // 0 based
                       Col: integer; // 1 based
                       TokenKind: TtkTokenKind);
    procedure ClearTokens(Line: integer); // 0 based
    procedure ClearAllCopiedAttributes;
    procedure ClearAllTokens;
    procedure InsertTokens(Lines: TStringList;
      Highlighter: TSynCustomHighlighter;
      Line, //0 based
      Col: integer // 1 based
      );
    function CreateTokenID(const aName: string; Foreground, BackGround: TColor;
                           Style: TFontStyles): TtkTokenKind;
    function GetCopiedTokenID(Attr: TSynHighlighterAttributes): TtkTokenKind;
    function GetCopiedAttribute(TokenID: TtkTokenKind): TSynHighlighterAttributes;
    property Tokens[TheLineNumber: integer]: PPositionTokens read GetTokens;
  published
    property TextAttri: TSynHighlighterAttributes read fTextAttri write fTextAttri;
  end;

implementation

{ TSynPositionHighlighter }

function TSynPositionHighlighter.GetTokens(TheLineNumber: integer
  ): PPositionTokens;
begin
  if (TheLineNumber>=0) and (TheLineNumber<fTokens.Count) then
    Result:=PPositionTokens(fTokens[TheLineNumber])
  else
    Result:=nil;
end;

function TSynPositionHighlighter.GetIdentChars: TSynIdentChars;
begin
  Result := ['_', '0'..'9', 'a'..'z', 'A'..'Z'];
end;

function TSynPositionHighlighter.IsFilterStored: boolean;
begin
  Result:=true;
end;

function TSynPositionHighlighter.GetPositionTokensSize(ItemCount: integer
  ): integer;
begin
  Result:=SizeOf(integer)+SizeOf(TPositionToken)*ItemCount;
end;

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

class function TSynPositionHighlighter.GetLanguageName: string;
begin
  Result:='Position based highlighter';
end;

constructor TSynPositionHighlighter.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  fTokens:=TList.Create;
  fCopiedAttributes:=TList.Create;
  fTextAttri := TSynHighlighterAttributes.Create(@SYNS_AttrText, SYNS_XML_AttrText);
  AddAttribute(fTextAttri);
  SetAttributesOnChange(@DefHighlightChange);

  fDefaultFilter := '';
end;

destructor TSynPositionHighlighter.Destroy;
begin
  ClearAllTokens;
  fTokens.Free;
  ClearAllCopiedAttributes;
  fCopiedAttributes.Free;
  inherited Destroy;
end;

function TSynPositionHighlighter.GetEol: Boolean;
begin
  Result := fTokenKind = tkNone;
end;

function TSynPositionHighlighter.GetRange: Pointer;
begin
  Result := Pointer(PtrInt(fLineNumber));
end;

function TSynPositionHighlighter.GetToken: string;
var
  Len: LongInt;
begin
  Len := fTokenEnd - fTokenPos;
  SetLength(Result,Len);
  System.Move(fLine[fTokenPos],Result[1],Len);
end;

procedure TSynPositionHighlighter.GetTokenEx(out TokenStart: PChar;
  out TokenLength: integer);
begin
  TokenLength:=fTokenEnd-fTokenPos;
  if TokenLength>0 then begin
    TokenStart:=@fLine[fTokenPos];
  end else begin
    TokenStart:=nil;
  end;
end;

function TSynPositionHighlighter.GetTokenAttribute: TSynHighlighterAttributes;
var
  t: TtkTokenKind;
begin
  t:=GetTokenKind;
  if t=tkText then
    Result:=fTextAttri
  else if (t<0) then
    // this is a copied attribute
    Result:=GetCopiedAttribute(t)
  else
    Result:=nil;
end;

function TSynPositionHighlighter.GetTokenKind: integer;
begin
  Result:=fTokenKind;
end;

function TSynPositionHighlighter.GetTokenPos: Integer;
begin
  Result:=fTokenPos-1;
end;

procedure TSynPositionHighlighter.Next;
var
  p: PPositionTokens;
begin
  fTokenPos := fTokenEnd;
  if fTokenEnd>fLineLen then begin
    fTokenKind := tkNone;
    exit;
  end;
  inc(fTokenArrayPos);
  p:=Tokens[fLineNumber];
  if (p<>nil) and (p^.Count>fTokenArrayPos) then begin
    fTokenKind := p^.Tokens[fTokenArrayPos].Kind;
    fTokenEnd := p^.Tokens[fTokenArrayPos].Column+1;
    if fTokenEnd>fLineLen+1 then
      fTokenEnd := fLineLen+1;
    if fTokenEnd=fTokenPos then
      Next;
  end else begin
    fTokenEnd := fLineLen+1;
    fTokenKind := tkText;
  end;
end;

procedure TSynPositionHighlighter.ResetRange;
begin
  inherited ResetRange;
end;

procedure TSynPositionHighlighter.SetLine(const NewValue: string;
  LineNumber: Integer);
var
  p: PPositionTokens;
begin
  inherited;
  fLine := NewValue;
  fLineLen := length(fLine);
  fLineNumber := LineNumber;
  fTokenArrayPos := 0;
  fTokenPos := 1;
  p:=Tokens[fLineNumber];
  if p<>nil then begin
    fTokenEnd := p^.Tokens[0].Column+1;
    if fTokenEnd>fLineLen+1 then
      fTokenEnd:=fLineLen+1;
    FTokenKind := p^.Tokens[0].Kind;
    if fTokenEnd=fTokenPos then Next;
  end else begin
    fTokenEnd := fLineLen+1;
    FTokenKind := tkText;
  end;
end;

procedure TSynPositionHighlighter.SetRange(Value: Pointer);
begin
  inherited SetRange(Value);
end;

function TSynPositionHighlighter.UseUserSettings(settingIndex: integer
  ): boolean;
begin
  Result:=inherited UseUserSettings(settingIndex);
end;

procedure TSynPositionHighlighter.EnumUserSettings(settings: TStrings);
begin
  inherited EnumUserSettings(settings);
end;

procedure TSynPositionHighlighter.AddToken(Line, Col: integer;
  TokenKind: TtkTokenKind);
var
  p: PPositionTokens;
  TokenIndex, TokenCount: integer;
begin
  if Col<1 then exit;
  // fill up lines
  while (Line>=fTokens.Count) do fTokens.Add(nil);
  // resize Token array
  p:=Tokens[Line];
  TokenIndex:=0;
  if p<>nil then
    TokenCount:=p^.Count+1
  else
    TokenCount:=1;
  ReAllocMem(p,GetPositionTokensSize(TokenCount));
  fTokens[Line]:=p;
  p^.Count:=TokenCount;
  // insert Token
  TokenIndex:=TokenCount-1;
  while (TokenIndex>0)
  and (p^.Tokens[TokenIndex-1].Column>Col) do
    dec(TokenIndex);
  if TokenIndex<TokenCount-1 then begin
    System.Move(p^.Tokens[TokenIndex],p^.Tokens[TokenIndex+1],
      SizeOf(TPositionToken)*(TokenCount-TokenIndex-1));
  end;
  with p^.Tokens[TokenIndex] do begin
    Kind:=TokenKind;
    Column:=Col;
  end;
  DefHighlightChange(Self);
end;

procedure TSynPositionHighlighter.ClearTokens(Line: integer);
var
  p: pointer;
begin
  if (Line>=0) and (Line<fTokens.Count) then begin
    p:=fTokens[Line];
    if p<>nil then begin
      FreeMem(p);
      fTokens[Line]:=nil;
    end;
  end;
end;

procedure TSynPositionHighlighter.ClearAllCopiedAttributes;
var
  i: Integer;
begin
  for i:=0 to fCopiedAttributes.Count-1 do
    TObject(fCopiedAttributes[i]).Free;
  fCopiedAttributes.Clear;
end;

procedure TSynPositionHighlighter.ClearAllTokens;
var
  i: Integer;
begin
  for i:=0 to fTokens.Count-1 do
    ClearTokens(i);
  fTokens.Clear;
end;

procedure TSynPositionHighlighter.InsertTokens(Lines: TStringList;
  Highlighter: TSynCustomHighlighter; Line, Col: integer);
var
  RelLine: integer;
  nTokenLen: integer;
  sToken: PChar;
  Attr: TSynHighlighterAttributes;
  TokenID: integer;
begin
  if (Lines=nil) or (Lines.Count=0) or (Highlighter=nil) then exit;
  RelLine:=0;
  while RelLine<Lines.Count do begin
    HighLighter.SetLine(Lines[RelLine], RelLine);
    while not Highlighter.GetEol do begin
      Attr:=Highlighter.GetTokenAttribute;
      TokenID:=GetCopiedTokenID(Attr);
      Highlighter.GetTokenEx(sToken,nTokenLen);
      inc(Col,nTokenLen);
      AddToken(Line,Col-1,TokenID);
      // next Token
      Highlighter.Next;
    end;
    // next line
    inc(Line);
    inc(RelLine);
    Col:=1;
  end;
end;

function TSynPositionHighlighter.CreateTokenID(const aName: string;
  Foreground, BackGround: TColor;
  Style: TFontStyles): TtkTokenKind;
var
  Attr: TSynHighlighterAttributes;
begin
  Attr:=TSynHighlighterAttributes.Create(aName);
  Attr.Foreground:=Foreground;
  Attr.Background:=BackGround;
  Attr.Style:=Style;
  Result:=GetCopiedTokenID(Attr);
  Attr.Free;
end;

function TSynPositionHighlighter.GetCopiedTokenID(
  Attr: TSynHighlighterAttributes): TtkTokenKind;
var
  i: Integer;
  CurAttr: TSynHighlighterAttributes;
begin
  i:=fCopiedAttributes.Count-1;
  while i>=0 do begin
    CurAttr:=TSynHighlighterAttributes(fCopiedAttributes[i]);
    if (Attr.ForeGround=CurAttr.ForeGround)
    and (Attr.BackGround=CurAttr.BackGround)
    and (Attr.Style=CurAttr.Style) then begin
      // attribute already exists
      Result:=(-i-1);
      exit;
    end;
    dec(i);
  end;
  // create new attribute
  CurAttr:=TSynHighlighterAttributes.Create(nil);
  CurAttr.Assign(Attr);
  fCopiedAttributes.Add(CurAttr);
  Result:= -fCopiedAttributes.Count;
end;

function TSynPositionHighlighter.GetCopiedAttribute(TokenID: TtkTokenKind
  ): TSynHighlighterAttributes;
begin
  if (TokenID<0) and (fCopiedAttributes.Count>=-TokenID) then
    Result:=TSynHighlighterAttributes(fCopiedAttributes[-TokenID-1])
  else
    Result:=nil;
end;

end.