-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCodeTemplatesImpl.pas
More file actions
131 lines (115 loc) · 3.32 KB
/
CodeTemplatesImpl.pas
File metadata and controls
131 lines (115 loc) · 3.32 KB
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
unit CodeTemplatesImpl;
interface
uses
ToolsAPI;
type
TCodeTemplate = record
Key, Description, IDString, Template: string;
end;
ICodeTemplates = interface
function GetCount: Integer;
function GetItem(const Index: Integer): TCodeTemplate;
function GetItemKey(const Key: string): TCodeTemplate;
function Exists(const Key: string; out Index: Integer): Boolean; overload;
function Exists(const Key: string): Boolean; overload;
property Item[const Index: Integer]: TCodeTemplate read GetItem; default;
property Count: Integer read GetCount;
end;
TCodeTemplates = class(TInterfacedObject, ICodeTemplates)
protected
FCodeTemplates: array of TCodeTemplate;
function GetItem(const Index: Integer): TCodeTemplate;
function GetCount: Integer;
function GetItemKey(const Key: string): TCodeTemplate;
function Exists(const Key: string): Boolean; overload;
function Exists(const Key: string; out Index: Integer): Boolean; overload;
public
constructor Create(const ACodeTemplatePath: string);
end;
implementation
uses
DeclParserIntf, Classes, SysUtils;
{ TCodeTemplates }
constructor TCodeTemplates.Create(const ACodeTemplatePath: string);
var
Parser: RegExp;
FS: TFileStream; SS: TStringStream;
Template, Data: string;
MC: IMatchCollection2;
Match, NextMatch: IMatch2;
Submatches: ISubMatches;
I, StartPos, Len: Integer;
begin
FS := TFileStream.Create(ACodeTemplatePath, fmOpenRead);
SS := TStringStream.Create('');
SS.CopyFrom(FS, 0);
Data := SS.DataString;
FS.Free;
SS.Free;
Parser := CoRegExp.Create;
Parser.Pattern := '('+
'^\[(.*)? \| (.*)? \| (.*)?\]'+
')+';
Parser.Global := True;
Parser.Multiline := True;
MC := Parser.Execute(Data) as IMatchCollection2;
SetLength(FCodeTemplates, MC.Count);
for I := 0 to MC.Count-1 do
begin
Match := MC.Item[I] as IMatch2;
StartPos := Match.FirstIndex+1+Length(Match.Value);
if I+1<=MC.Count-1 then
begin
NextMatch := MC.Item[I+1] as IMatch2;
Len := NextMatch.FirstIndex+1-StartPos;
end else
begin
Len := Length(Data);
end;
Template := Copy(Data, StartPos+2, Len-4);
Submatches := Match.SubMatches as ISubMatches;
FCodeTemplates[I].Key := Submatches[1];
FCodeTemplates[I].Description := Submatches[2];
FCodeTemplates[I].IDString := Submatches[3];
FCodeTemplates[I].Template := Template;
end;
end;
function TCodeTemplates.Exists(const Key: string): Boolean;
var
I: Integer;
begin
Result := Exists(Key, I);
end;
function TCodeTemplates.Exists(const Key: string; out Index: Integer): Boolean;
var
I: Integer;
begin
Result := False;
for I := Low(FCodeTemplates) to High(FCodeTemplates) do
if Key = FCodeTemplates[I].Key then
begin
Index := I;
Result := True;
Break;
end;
end;
function TCodeTemplates.GetCount: Integer;
begin
Result := Length(FCodeTemplates);
end;
function TCodeTemplates.GetItem(const Index: Integer): TCodeTemplate;
begin
Result := FCodeTemplates[Index];
end;
function TCodeTemplates.GetItemKey(const Key: string): TCodeTemplate;
var
I: Integer;
begin
for I := Low(FCodeTemplates) to High(FCodeTemplates) do
if Key = FCodeTemplates[I].Key then
begin
Result := FCodeTemplates[I];
Break;
end;
end;
end.