File: syntextmatesyn.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 (455 lines) | stat: -rw-r--r-- 14,125 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
unit SynTextMateSyn;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fgl,
  jsonparser, jsonscanner, fpjson,
  // LazUtils
  LazFileUtils,
  Laz2_XMLRead, Laz2_DOM, LazStringUtils,
  // LazEdit
  TextMateGrammar,
  // SynEdit
  SynEditHighlighter, SynEditHighlighterFoldBase, SynEditTypes, SynEditTextBase;

type

  TNameAttributesMap   = specialize TFPGMapObject<string, TSynHighlighterAttributes>;
  TGrammarLoadEvent = procedure(AGrammarFile, AGrammarPath: String; out AGrammarDef: String);

 TSynTextMateRangeInfo = record
    FoldLevel: Smallint;
  end;

  { TSynHighlighterTextMateRangeList }

  TSynHighlighterTextMateRangeList = class(TSynHighlighterRangeList)
  private
    FItemOffset: integer;
    function GetRangeInfo(Index: Integer): TSynTextMateRangeInfo;
    procedure SetRangeInfo(Index: Integer; AValue: TSynTextMateRangeInfo);
  public
    constructor Create;
    property RangeInfo[Index: Integer]: TSynTextMateRangeInfo read GetRangeInfo write SetRangeInfo;
  end;

  { TSynTextMateSyn }

  TSynTextMateSyn = class(TSynCustomFoldHighlighter)
  private
    FGrammarPath: String;
    FOnLoadGrammarFile: TGrammarLoadEvent;
    FTextMateGrammar: TTextMateGrammar;

  private
    FAttriMap: TNameAttributesMap;

    function  LoadFile(AGrammarFile: String): String;
    procedure SetGrammarPath(AValue: String);

    function GetOrCreateAttribIdxForName(AName: String): integer;
    procedure DoPopulateAttributeInfo(Sender: TTextMateGrammar; APattern: TTextMatePattern;
      AContextName: String; var AnAttribInfo: TSynAttributeInfo);
    procedure DoCheckAttributeInfo(Sender: TTextMatePattern;
      const AnAttribInfo: TSynAttributeInfo; out AnUseId, AnUseObject: Boolean);
  private
    FCurrentRange: Integer;
    FRangeInfo: TSynTextMateRangeInfo;
    FCurrentTokenPos, FCurrentTokenLen: Integer;
    FCurrentTokenKind: integer;
    FCurrentAttrib: TSynHighlighterAttributes;
    function GetParserError: String;

  protected
    function GetInstanceLanguageName: string; override;
    function CreateRangeList(ALines: TSynEditStringsBase): TSynHighlighterRangeList; override;
    function UpdateRangeInfoAtLine(Index: Integer): Boolean; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure LoadGrammar(AGrammarDef: String);
    procedure LoadGrammar(AGrammarFile, AGrammarPath: String);

    procedure SetLine(const NewValue: String; LineNumber: Integer); override;
    procedure Next; override;
    function GetEol: Boolean; override;
    function DebugAttrAtXPos(AXPos: Integer): String; // requires StartAtLineIndex before

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

    procedure SetRange(Value: Pointer); override;
    procedure ResetRange; override;
    function GetRange: Pointer; override;

    function FoldBlockEndLevel(ALineIndex: TLineIdx; const AFilter: TSynFoldBlockFilter): integer; override; overload;
    function FoldBlockMinLevel(ALineIndex: TLineIdx; const AFilter: TSynFoldBlockFilter): integer; override; overload;

    property OnLoadGrammarFile: TGrammarLoadEvent read FOnLoadGrammarFile write FOnLoadGrammarFile;
    property GrammarPath: String read FGrammarPath write SetGrammarPath;
    property ParserError: String read GetParserError;
    property TextMateGrammar: TTextMateGrammar read FTextMateGrammar;
  end;

implementation

{ TSynHighlighterTextMateRangeList }

function TSynHighlighterTextMateRangeList.GetRangeInfo(Index: Integer
  ): TSynTextMateRangeInfo;
begin
  if (Index < 0) or (Index >= Count) then
    Result := Default(TSynTextMateRangeInfo)
  else
    Result := TSynTextMateRangeInfo((ItemPointer[Index] + FItemOffset)^);
end;

procedure TSynHighlighterTextMateRangeList.SetRangeInfo(Index: Integer;
  AValue: TSynTextMateRangeInfo);
begin
  TSynTextMateRangeInfo((ItemPointer[Index] + FItemOffset)^) := AValue;
end;

constructor TSynHighlighterTextMateRangeList.Create;
begin
  inherited;
  FItemOffset := ItemSize;
  ItemSize := FItemOffset + SizeOf(TSynTextMateRangeInfo);
end;

{ TSynTextMateSyn }

function TSynTextMateSyn.LoadFile(AGrammarFile: String): String;
var
  s: TStringStream;
begin
  if Assigned(FOnLoadGrammarFile) then begin
    OnLoadGrammarFile(AGrammarFile, FGrammarPath, Result);
  end
  else begin
    s := TStringStream.Create('');
    try
      s.LoadFromFile(FGrammarPath + AGrammarFile);
      Result := s.DataString;
    finally
      s.Free;
    end;
  end;
end;

procedure TSynTextMateSyn.SetGrammarPath(AValue: String);
begin
  if FGrammarPath = AValue then Exit;
  if AValue <> '' then
    AppendPathDelim(AValue);
  FGrammarPath := AValue;
end;

function TSynTextMateSyn.GetOrCreateAttribIdxForName(AName: String): integer;
var
  attr: TSynHighlighterAttributes;
begin
  if AName = '' then
    exit(-1);
  Result := FAttriMap.IndexOf(AName);
  if Result >= 0 then
    exit;
  attr := TSynHighlighterAttributes.Create(AName, AName);
  AddAttribute(attr);
  Result := FAttriMap.Add(AName, attr);
end;

function TSynTextMateSyn.GetInstanceLanguageName: string;
begin
  Result := FTextMateGrammar.LanguageName;
end;

function TSynTextMateSyn.CreateRangeList(ALines: TSynEditStringsBase
  ): TSynHighlighterRangeList;
begin
  Result := TSynHighlighterTextMateRangeList.Create;
end;

function TSynTextMateSyn.UpdateRangeInfoAtLine(Index: Integer): Boolean;
var
  r: TSynTextMateRangeInfo;
  i: Integer;
begin
  GetRange;
  i := {%H-}Integer(TSynHighlighterTextMateRangeList(CurrentRanges).Range[Index]);
  if i <> FCurrentRange then
    FTextMateGrammar.MainPatternList[i].DecRefCount;

  Result := inherited;
  r := TSynHighlighterTextMateRangeList(CurrentRanges).RangeInfo[Index];
  Result := Result
        or (FRangeInfo.FoldLevel <> r.FoldLevel);
  TSynHighlighterTextMateRangeList(CurrentRanges).RangeInfo[Index] := FRangeInfo;

  if i <> FCurrentRange then
    FTextMateGrammar.MainPatternList[Integer(i)].IncRefCount;
end;

procedure TSynTextMateSyn.DoPopulateAttributeInfo(
  Sender: TTextMateGrammar; APattern: TTextMatePattern;
  AContextName: String; var AnAttribInfo: TSynAttributeInfo);
begin
  AnAttribInfo.TokId := GetOrCreateAttribIdxForName(AContextName);
  if AnAttribInfo.TokId < 0 then
    AnAttribInfo.TokObject := nil
  else
    AnAttribInfo.TokObject := FAttriMap.Data[AnAttribInfo.TokId];
end;

procedure TSynTextMateSyn.DoCheckAttributeInfo(Sender: TTextMatePattern;
  const AnAttribInfo: TSynAttributeInfo; out AnUseId, AnUseObject: Boolean);
begin
  AnUseId := AnAttribInfo.TokId >= 0;
  AnUseObject := (AnAttribInfo.TokObject <> nil) and
                 (TSynHighlighterAttributes(AnAttribInfo.TokObject).IsEnabled);
end;

function TSynTextMateSyn.GetParserError: String;
begin
  Result := FTextMateGrammar.ParserError;
end;

constructor TSynTextMateSyn.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FAttriMap := TNameAttributesMap.Create(False);
  FTextMateGrammar := TTextMateGrammar.Create;
  FTextMateGrammar.OnPopulateAttributeInfo := @DoPopulateAttributeInfo;
  FTextMateGrammar.OnCheckAttributeInfo := @DoCheckAttributeInfo;
end;

destructor TSynTextMateSyn.Destroy;
begin
  FTextMateGrammar.ClearGrammar;
  FTextMateGrammar.Free;
  FreeHighlighterAttributes;
  FAttriMap.Clear;
  FAttriMap.Free;
  inherited Destroy;
end;

procedure TSynTextMateSyn.LoadGrammar(AGrammarDef: String);
begin
  FTextMateGrammar.ParseGrammar(AGrammarDef);
end;

procedure TSynTextMateSyn.LoadGrammar(AGrammarFile, AGrammarPath: String);
begin
  GrammarPath := AGrammarPath;
  FTextMateGrammar.ParseGrammar(LoadFile(AGrammarFile));
  if FTextMateGrammar.LanguageName = '' then
    FTextMateGrammar.LanguageName := AGrammarFile;
end;

procedure TSynTextMateSyn.SetLine(const NewValue: String;
  LineNumber: Integer);
var
  nd: TSynFoldNodeInfo;
begin
  inherited SetLine(NewValue, LineNumber);

  if FCurrentRange = -2 then
    FCurrentRange := FTextMateGrammar.CurrentPatternIndex;
// TODO setline - keep range?

  FTextMateGrammar.SetLine(CurrentLineText, FCurrentRange);
  FCurrentRange := -2;

  if FTextMateGrammar.IsFoldBegin then begin
    if not FTextMateGrammar.IsFoldEnd then begin
      inc(FRangeInfo.FoldLevel);
      if IsCollectingNodeInfo then begin
        nd := Default(TSynFoldNodeInfo);
        nd.LineIndex := LineIndex;
        nd.FoldGroup := 1;
        nd.FoldLvlStart := FRangeInfo.FoldLevel - 1;
        nd.FoldLvlEnd := FRangeInfo.FoldLevel;
        nd.NestLvlStart := FRangeInfo.FoldLevel - 1;
        nd.NestLvlEnd := FRangeInfo.FoldLevel;
        nd.FoldAction := [sfaFold, sfaFoldFold, sfaOpen, sfaOpenFold];
        CollectingNodeInfoList.Add(nd);
      end;
    end;
  end
  else
  if FTextMateGrammar.IsFoldEnd and (FRangeInfo.FoldLevel > 0) then begin
    dec(FRangeInfo.FoldLevel);
    if IsCollectingNodeInfo then begin
      nd := Default(TSynFoldNodeInfo);
      nd.LineIndex := LineIndex;
      nd.FoldGroup := 1;
      nd.FoldLvlStart := FRangeInfo.FoldLevel + 1;
      nd.FoldLvlEnd := FRangeInfo.FoldLevel;
      nd.NestLvlStart := FRangeInfo.FoldLevel + 1;
      nd.NestLvlEnd := FRangeInfo.FoldLevel;
      nd.FoldAction := [sfaFold, sfaFoldFold, sfaClose, sfaCloseFold];
      CollectingNodeInfoList.Add(nd);
    end;
  end;

  if IsScanning then begin
    FTextMateGrammar.NextToEol;
  end
  else begin
    FTextMateGrammar.First;
    FCurrentTokenKind := FTextMateGrammar.CurrentTokenKind;
    FCurrentAttrib    := TSynHighlighterAttributes(FTextMateGrammar.CurrentAttrib);
    FCurrentTokenPos  := FTextMateGrammar.CurrentTokenPos;
    FCurrentTokenLen  := FTextMateGrammar.CurrentTokenLen;
  end;
  //FCurrentRange := FTextMateGrammar.CurrentPatternIndex;

end;

procedure TSynTextMateSyn.Next;
begin
  FTextMateGrammar.Next;
  FCurrentTokenKind := FTextMateGrammar.CurrentTokenKind;
  FCurrentAttrib    := TSynHighlighterAttributes(FTextMateGrammar.CurrentAttrib);
  FCurrentTokenPos  := FTextMateGrammar.CurrentTokenPos;
  FCurrentTokenLen  := FTextMateGrammar.CurrentTokenLen;
  //FCurrentRange     := FTextMateGrammar.CurrentPatternIndex;
end;

function TSynTextMateSyn.DebugAttrAtXPos(AXPos: Integer): String;
var
  st: TTextMatePatternState;
  i: Integer;
begin
  Result := '';
  st := FTextMateGrammar.CurrentState;
  SetLength(st.StateList, Length(st.StateList)) ;
  while (FCurrentTokenPos + FCurrentTokenLen < AXPos) do begin
    if GetEol then begin
      result := format('EOL At %d Len %d %s', [FCurrentTokenPos, FCurrentTokenLen, LineEnding]);
      exit;
    end;
    st := FTextMateGrammar.CurrentState;
    SetLength(st.StateList, Length(st.StateList)) ;
    Next;
  end;

  result := format('At %d Len %d %s', [FCurrentTokenPos, FCurrentTokenLen, LineEnding]);

  for i := st.StateIdx downto 0 do begin
    if st.StateList[i].Pattern <> nil then begin
      if (st.StateList[i].Pattern is TTextMatePatternBaseNested) and
         (st.StateList[i].Pattern.ForwardTarget <> nil)
      then
        Result := Result + Format('%2d: %4d: %s / %s >// %s %s%s', [
          i,
          st.StateList[i].Pattern.ForwardTarget.Index,
          st.StateList[i].Pattern.ForwardTarget.Name,
          st.StateList[i].Pattern.ForwardTarget.Comment,
          StripLN(st.StateList[i].Pattern.DebugDump(0,false,'')),
          st.StateList[i].Pattern.ForwardTarget.ClassName,
          LineEnding
        ])
      else
        Result := Result + Format('%2d: %4d: %s / %s  // %s%s%s', [
          i,
          st.StateList[i].Pattern.Index,
          st.StateList[i].Pattern.Name,
          st.StateList[i].Pattern.Comment,
          StripLN(st.StateList[i].Pattern.DebugDump(0,false,'')),
          st.StateList[i].Pattern.ClassName,
          LineEnding
        ]);
      end
      else
        Result := Result + '???';
  end;
end;

function TSynTextMateSyn.GetEol: Boolean;
begin
  Result := FTextMateGrammar.IsAtEol;
end;

function TSynTextMateSyn.GetToken: String;
begin
  Result := Copy(CurrentLineText, FCurrentTokenPos, FCurrentTokenLen);
end;

procedure TSynTextMateSyn.GetTokenEx(out TokenStart: PChar; out
  TokenLength: integer);
begin
  TokenStart := @CurrentLineText[FCurrentTokenPos];
  TokenLength := FCurrentTokenLen;
end;

function TSynTextMateSyn.GetTokenPos: Integer;
begin
  Result := FCurrentTokenPos - 1;
end;

function TSynTextMateSyn.GetTokenKind: integer;
begin
  Result := FCurrentTokenKind;
end;

function TSynTextMateSyn.GetTokenAttribute: TSynHighlighterAttributes;
begin
  Result := FCurrentAttrib;
end;

function TSynTextMateSyn.GetDefaultAttribute(Index: integer
  ): TSynHighlighterAttributes;
begin
  Result := FAttriMap.Data[FTextMateGrammar.RootPattern.AttribInfo.TokId];
end;

procedure TSynTextMateSyn.SetRange(Value: Pointer);
begin
  FCurrentRange := PtrUInt(Value);
  FRangeInfo := TSynHighlighterTextMateRangeList(CurrentRanges).RangeInfo[LineIndex-1];
end;

procedure TSynTextMateSyn.ResetRange;
begin
  FCurrentRange := -1;
  FRangeInfo := Default(TSynTextMateRangeInfo);
end;

function TSynTextMateSyn.GetRange: Pointer;
begin
  FCurrentRange     := FTextMateGrammar.CurrentPatternIndex;
  Result := Pointer(PtrUInt(FCurrentRange));
end;

function TSynTextMateSyn.FoldBlockEndLevel(ALineIndex: TLineIdx;
  const AFilter: TSynFoldBlockFilter): integer;
var
  RangeInfo: TSynTextMateRangeInfo;
begin
  RangeInfo := TSynHighlighterTextMateRangeList(CurrentRanges).RangeInfo[ALineIndex];
  Result := RangeInfo.FoldLevel;
end;

function TSynTextMateSyn.FoldBlockMinLevel(ALineIndex: TLineIdx;
  const AFilter: TSynFoldBlockFilter): integer;
var
  RangeInfo: TSynTextMateRangeInfo;
begin
  RangeInfo := TSynHighlighterTextMateRangeList(CurrentRanges).RangeInfo[ALineIndex-1];
  Result := RangeInfo.FoldLevel;
  RangeInfo := TSynHighlighterTextMateRangeList(CurrentRanges).RangeInfo[ALineIndex];
  if Result > RangeInfo.FoldLevel then
    Result := RangeInfo.FoldLevel;
end;

end.