File: codetemplatestool.pas

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (313 lines) | stat: -rw-r--r-- 8,688 bytes parent folder | download | duplicates (5)
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
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner

  Abstract:
    TCodeTemplatesTool enhances TStandardCodeTool with the ability to insert
    code templates.
}
unit CodeTemplatesTool;

{$mode objfpc}{$H+}

interface

{$I codetools.inc}

uses
  {$IFDEF MEM_CHECK}
  MemCheck,
  {$ENDIF}
  Classes, SysUtils,
  // Codetools
  FileProcs, CodeTree, CodeCache, KeywordFuncLists, LinkScanner, BasicCodeTools,
  SourceChanger, PascalParserTool, StdCodeTools;

type
  TCodeTemplateSyntax = (
    ctsxDefault
    );
  { ctsxDefault:
  
    The backslash '\' makes special chars to normal chars.
    |  for cursor
    $(MacroName) for macro variables
    $MacroName(Param) for macro functions


    If the variable contains several lines, the lines are indented like the
    first line.
    Example:
      Inserting the text
      
        "begin"
        "  i:=1;"
        "end;"
      Into the text
      
        "begin"
        "  |"
        "end;"
        
      at the | results in
      
        "begin"
        "  begin"
        "    i:=1;"
        "  end;"
        "end;"


    Defined variables:
    
    $(Selection) - replaced by the current selection
    
    $(Indent) - replaced by nothing. Set Indent for multiline macros
                Example:
                  Text: " Text: $(Indent)$(Selection)"
                  Selection: "First"
                             "Second"
                  Result: " Text: First"
                          "       Second"
  }

  TCodeToolTemplate = class
  private
    FIndent: integer;
    FNewLineAtEnd: boolean;
    FNewLineAtStart: boolean;
    FSyntax: TCodeTemplateSyntax;
    FTemplate: string;
    procedure SetIndent(const AValue: integer);
    procedure SetNewLineAtEnd(const AValue: boolean);
    procedure SetNewLineAtStart(const AValue: boolean);
    procedure SetSyntax(const AValue: TCodeTemplateSyntax);
    procedure SetTemplate(const AValue: string);
  public
    property Template: string read FTemplate write SetTemplate;
    property NewLineAtStart: boolean read FNewLineAtStart write SetNewLineAtStart;
    property NewLineAtEnd: boolean read FNewLineAtEnd write SetNewLineAtEnd;
    property Syntax: TCodeTemplateSyntax read FSyntax write SetSyntax;
    property Indent: integer read FIndent write SetIndent;
  end;

  { TCodeTemplatesTool }

  TCodeTemplatesTool = class(TStandardCodeTool)
  public
    function InsertCodeTemplate(CursorPos,EndPos: TCodeXYPosition;
      TopLine: integer; CodeTemplate: TCodeToolTemplate;
      out NewPos: TCodeXYPosition; out NewTopLine: integer;
      SourceChangeCache: TSourceChangeCache): boolean;
    function ExtractProcedureHeader(CursorPos: TCodeXYPosition;
      Attributes: TProcHeadAttributes; var ProcHead: string): boolean;

    procedure CalcMemSize(Stats: TCTMemStats); override;
  end;

implementation

{ TCodeTemplatesTool }

function TCodeTemplatesTool.InsertCodeTemplate(CursorPos,
  EndPos: TCodeXYPosition; TopLine: integer; CodeTemplate: TCodeToolTemplate;
  out NewPos: TCodeXYPosition; out NewTopLine: integer;
  SourceChangeCache: TSourceChangeCache): boolean;
var
  NewText: TMemoryStream;
  TemplatePos: Integer;
  LastWrittenTemplatePos: Integer;
  TemplateStr: String;
  TemplateStrLen: Integer;
  
  procedure FlushTemplate;
  var
    FromPos: Integer;
    ToPos: Integer;
  begin
    FromPos:=LastWrittenTemplatePos+1;
    ToPos:=TemplatePos-1;
    if ToPos>TemplateStrLen then
      ToPos:=TemplateStrLen;
    if FromPos<=ToPos then
      NewText.Write(TemplateStr[FromPos],ToPos-FromPos+1);
    LastWrittenTemplatePos:=ToPos;
  end;
  
  procedure CalculateNewCursorPos;
  begin

  end;
  
  procedure ParseMacro;
  var
    MacroStart: Integer;
    MacroFuncNameStart: Integer;
    MacroFuncNameEnd: Integer;
    BracketLvl: Integer;
    MacroParamStart: Integer;
    MacroParamEnd: Integer;
  begin
    MacroStart:=TemplatePos;
    inc(MacroStart);
    MacroFuncNameStart:=TemplatePos;
    while (TemplatePos<=TemplateStrLen)
    and IsIdentChar[TemplateStr[TemplatePos]] do
      inc(TemplatePos);
    MacroFuncNameEnd:=TemplatePos;
    if (TemplatePos<=TemplateStrLen) and (TemplateStr[TemplatePos]='(') then
    begin
      inc(TemplatePos);
      MacroParamStart:=TemplatePos;
      MacroParamEnd:=MacroParamStart;
      BracketLvl:=1;
      while (TemplatePos<=TemplateStrLen) do begin
        case TemplateStr[TemplatePos] of
        '\':
          begin
            inc(TemplatePos);
            if (TemplatePos>TemplateStrLen) then break;
          end;
        '(': inc(BracketLvl);
        ')':
          begin
            dec(BracketLvl);
            if BracketLvl=0 then begin
              MacroParamEnd:=TemplatePos;
              break;
            end;
          end;
        end;
        inc(TemplatePos);
      end;
      if MacroFuncNameStart<MacroFuncNameEnd then begin
        // a macro function
        // ToDo
      end else if MacroParamStart<MacroParamEnd then begin
        // a macro variable
        // ToDo
      end;
    end;
    LastWrittenTemplatePos:=TemplatePos;
  end;

begin
  Result:=false;
  NewPos:=CursorPos;
  NewText:=TMemoryStream.Create;
  try
    // parse the template
    TemplatePos:=1;
    LastWrittenTemplatePos:=TemplatePos-1;
    TemplateStr:=CodeTemplate.Template;
    TemplateStrLen:=length(TemplateStr);
    while TemplatePos<=TemplateStrLen do begin
      case TemplateStr[TemplatePos] of
      '\':
        begin
          FlushTemplate;
          LastWrittenTemplatePos:=TemplatePos;
          inc(TemplatePos,2);
        end;
        
      '|':
        begin
          FlushTemplate;
          CalculateNewCursorPos;
          LastWrittenTemplatePos:=TemplatePos;
          inc(TemplatePos);
        end;

      '$':
        begin
          FlushTemplate;
          ParseMacro;
        end;

      else
        inc(TemplatePos);
      end;
    end;
    FlushTemplate;
    
    
  finally
    NewText.Free;
  end;
end;

function TCodeTemplatesTool.ExtractProcedureHeader(CursorPos: TCodeXYPosition;
  Attributes: TProcHeadAttributes; var ProcHead: string): boolean;
var
  CleanCursorPos: integer;
  ANode: TCodeTreeNode;
begin
  Result:=false;
  ProcHead:='';
  BuildTreeAndGetCleanPos(trTillCursor,lsrEnd,CursorPos,CleanCursorPos,
    [btSetIgnoreErrorPos,btCursorPosOutAllowed]);
  ANode:=FindDeepestNodeAtPos(CleanCursorPos,True);
  while (ANode<>nil) and (ANode.Desc<>ctnProcedure) do
    ANode:=ANode.Parent;
  if ANode=nil then exit;
  ProcHead:=ExtractProcHead(ANode,Attributes);
  Result:=true;
end;

procedure TCodeTemplatesTool.CalcMemSize(Stats: TCTMemStats);
begin
  inherited CalcMemSize(Stats);
end;

{ TCodeToolTemplate }

procedure TCodeToolTemplate.SetIndent(const AValue: integer);
begin
  if FIndent=AValue then exit;
  FIndent:=AValue;
end;

procedure TCodeToolTemplate.SetNewLineAtEnd(const AValue: boolean);
begin
  if FNewLineAtEnd=AValue then exit;
  FNewLineAtEnd:=AValue;
end;

procedure TCodeToolTemplate.SetNewLineAtStart(const AValue: boolean);
begin
  if FNewLineAtStart=AValue then exit;
  FNewLineAtStart:=AValue;
end;

procedure TCodeToolTemplate.SetSyntax(const AValue: TCodeTemplateSyntax);
begin
  if FSyntax=AValue then exit;
  FSyntax:=AValue;
end;

procedure TCodeToolTemplate.SetTemplate(const AValue: string);
begin
  if FTemplate=AValue then exit;
  FTemplate:=AValue;
end;

end.