File: SortUsesData.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (442 lines) | stat: -rw-r--r-- 10,197 bytes parent folder | download | duplicates (8)
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
unit SortUsesData;
{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code

The Original Code is SortUsesData.pas, released September 2004.
The Initial Developer of the Original Code is Anthony Steele.
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved. 
Contributor(s): Anthony Steele.

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/NPL/

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") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

{$I JcfGlobal.inc}

interface

uses
  { delphi }
  Contnrs, Classes,
  { local }
  SourceToken, ParseTreeNode;

{ some classes used in sorting a uses clause }

type
  { stores a uses unit name and some trailing tokens }
  TUsesItem = class(TObject)
  private
    fcItems: TObjectList;
    
    function GetItem(const piIndex: integer): TSourceToken;

  public
    constructor Create;
    destructor Destroy; override;

    function Count: integer;
    function SortKey: string;
    function Text: string;
    function IndentifierIndex: integer;

    procedure Add(const pcItem: TSourceToken);
    procedure AttachTo(const pcNode: TParseTreeNode; const pbAllowCommaFirst: boolean);

    property Items[const piIndex: integer]: TSourceToken read GetItem;
  end;

  { a section is one of two things
    - It is a list of uses items, to be sorted
    - breaking tokens between these lists. Not sorted  }
  TUsesSection = class(TObject)
  private
    fbSorted: boolean;
    fcItems: TObjectList;

    function GetItem(const piIndex: integer): TUsesItem;
  public
    constructor Create;
    destructor Destroy; override;

    function Count: integer;
    function Text: string;

    procedure Sort(Compare: TListSortCompare);

    procedure AddUsesItem;
    procedure AddToken(const pcItem: TSourceToken);
    procedure AttachTo(const pcNode: TParseTreeNode; pbAllowCommaFirst: boolean);

    property Sorted: boolean read fbSorted write fbSorted;

    property Items[const piIndex: integer]: TUsesItem read GetItem;
  end;

function AlphaNameSort(Item1, Item2: Pointer): integer;
function ReverseAlphaNameSort(Item1, Item2: Pointer): integer;
function LengthNameSort(Item1, Item2: Pointer): integer;
function ReverseLengthNameSort(Item1, Item2: Pointer): integer;


implementation

uses
  { delphi }
  {$IFNDEF FPC}Windows,{$ENDIF} Math, SysUtils,
  {local }
  ParseTreeNodeType, Tokens, TokenUtils;

function AlphaNameSort(Item1, Item2: Pointer): integer;
var
  lcItem1, lcItem2: TUsesItem;
begin
  lcItem1 := TUsesItem(Item1);
  lcItem2 := TUsesItem(Item2);

  if lcItem1 = nil then
    Result := -1
  else if lcItem2 = nil then
    Result := 1
  else
  begin
    Result := AnsiCompareText(lcItem1.SortKey, lcItem2.SortKey);
  end;
end;

function ReverseAlphaNameSort(Item1, Item2: Pointer): integer;
begin
  Result := AlphaNameSort(Item1, Item2) * -1;
end;

function LengthNameSort(Item1, Item2: Pointer): integer;
var
  lcItem1, lcItem2: TUsesItem;
begin
  lcItem1 := TUsesItem(Item1);
  lcItem2 := TUsesItem(Item2);

  if lcItem1 = nil then
    Result := -1
  else if lcItem2 = nil then
    Result := 1
  else
  begin
    Result := Sign(Length(lcItem1.SortKey) - Length(lcItem2.SortKey));

    { Length has more sort collisions that alphabetic
      same length -> Alphabetic sort }
    if Result = 0 then
      Result := AnsiCompareText(lcItem1.SortKey, lcItem2.SortKey);
  end;
end;

function ReverseLengthNameSort(Item1, Item2: Pointer): integer;
var
  lcItem1, lcItem2: TUsesItem;
begin
  lcItem1 := TUsesItem(Item1);
  lcItem2 := TUsesItem(Item2);

  if lcItem1 = nil then
    Result := -1
  else if lcItem2 = nil then
    Result := 1
  else
  begin
    Result := Sign(Length(lcItem2.SortKey) - Length(lcItem1.SortKey));

    { Soting the other way by length, but still forwards alphbetically }
    if Result = 0 then
      Result := AnsiCompareText(lcItem1.SortKey, lcItem2.SortKey);
  end;
end;


{-------------------------------------------------------------------------------
  TUsesItem }


constructor TUsesItem.Create;
begin
  inherited;
  fcItems := TObjectList.Create;
  // tokens are just referred here, owned by the parse treee
  fcItems.OwnsObjects := False;
end;

destructor TUsesItem.Destroy;
begin
  FreeAndNil(fcItems);
  inherited;
end;
procedure TUsesItem.Add(const pcItem: TSourceToken);
begin
  fcItems.Add(pcItem);
end;

function TUsesItem.Count: integer;
begin
  Result := fcItems.Count;
end;

function TUsesItem.GetItem(const piIndex: integer): TSourceToken;
begin
  Result := TSourceToken(fcItems[piIndex]);
end;

function TUsesItem.SortKey: string;
var
  liLoop: integer;
  lcTest: TSourceToken;
begin
  Result := '';

  // find the identifiers
  for liLoop := 0 to Count - 1 do
  begin
    lcTest := Items[liLoop];
    if lcTest.TokenType in [ttIdentifier, ttDot] then
    begin
      Result := Result + lcTest.SourceCode;
    end;
  end;
end;

function TUsesItem.Text: string;
var
  liLoop: integer;
  lcTest: TSourceToken;
begin
  Result := '';

  for liLoop := 0 to Count - 1 do
  begin
    lcTest := Items[liLoop];
    Result := Result + lcTest.SourceCode;
  end;
end;


function TUsesItem.IndentifierIndex: integer;
var
  liLoop: integer;
  lcTest: TSourceToken;
begin
  Result := -1;

  // find the first identifier
  for liLoop := 0 to Count - 1 do
  begin
    lcTest := Items[liLoop];
    if lcTest.TokenType = ttIdentifier then
    begin
      Result := liLoop;
      break;
    end;
  end;
end;

procedure TUsesItem.AttachTo(const pcNode: TParseTreeNode; const pbAllowCommaFirst: boolean);
var
  lbHasSolid: boolean;

  procedure AddToken(const pcToken: TSourceToken);
  begin
    // skip the comma if it's the first token and asked to do so
    if (not lbHasSolid) and (not pbAllowCommaFirst) and
    (pcToken.TokenType = ttComma) then
      exit; 

    if (not lbHasSolid) and pcToken.IsSolid then
      lbHasSolid := True;

    pcNode.AddChild(pcToken);
  end;

var
  liIndentifierIndex: integer;
  lcUsesItem: TParseTreeNode;
  lcIdent: TParseTreeNode;
  lcToken: TSourceToken;
  liloop: integer;
  lbHasComma: Boolean;
  lcComma: TSourceToken;
  lsFileName: string;

begin
  if Count < 1 then
    exit;

  Assert(pcNode <> nil);
  liIndentifierIndex := IndentifierIndex;
  lbHasSolid := False;
  lsFileName := '';

  if liIndentifierIndex < 0 then
  begin
    for liLoop := 0 to Count - 1 do
    begin
      lcToken := Items[liLoop];
      lsFileName := lcToken.FileName;
      AddToken(lcToken);
    end;
  end
  else
  begin
    for liLoop := 0 to liIndentifierIndex - 1 do
    begin
      lcToken := Items[liLoop];
      lsFileName := lcToken.FileName;
      AddToken(lcToken);
    end;

    // attach the data back onto the parent node, a uses clause
    lbHasSolid := True;
    lcUsesItem := TParseTreeNode.Create;
    lcUsesItem.NodeType := nUsesItem;

    pcNode.AddChild(lcUsesItem);

    lcIdent := TParseTreeNode.Create;
    lcIdent.NodeType := nUsesItem;

    lcUsesItem.AddChild(lcIdent);

    // add the identifier and trailing tokens
    lcIdent.AddChild(Items[liIndentifierIndex]);

    // add trailing tokens
    lbHasComma := False;
    for liLoop := liIndentifierIndex + 1 to Count - 1 do
    begin
      lcToken := Items[liLoop];
      if lcToken.TokenType = ttComma then
        lbHasComma := True;
      AddToken(lcToken);
    end;

    { needs to end with a comma }
    if not lbHasComma then
    begin
      lcComma := TSourceToken.Create;
      lcComma.FileName := lsFileName;

      lcComma.SourceCode := ',';
      lcComma.TokenType := ttComma;

      AddToken(lcComma);

      AddToken(NewSpace(1));
    end;
  end;
end;


{------------------------------------------------------------------------------
   TUsesSection }

constructor TUsesSection.Create;
begin
  inherited;
  fcItems := TObjectList.Create;
  // tokens are just referred here, owned by the parse treee
  fcItems.OwnsObjects := False;
end;

destructor TUsesSection.Destroy;
begin
  FreeAndNil(fcItems);
  inherited;
end;

procedure TUsesSection.AddUsesItem;
var
  lcNewItem: TUsesItem;
begin
  lcNewItem := TUsesItem.Create;
  fcItems.Add(lcNewItem);
end;

function TUsesSection.Count: integer;
begin
  Result := fcItems.Count;
end;

function TUsesSection.GetItem(const piIndex: integer): TUsesItem;
begin
  Result := TUsesItem(fcItems.Items[piIndex]);
end;

procedure TUsesSection.AddToken(const pcItem: TSourceToken);
begin
  if Count = 0 then
    AddUsesItem;

  // add to the last uses item
  Items[Count - 1].Add(pcItem);
end;


procedure TUsesSection.Sort(Compare: TListSortCompare);
begin
  if Sorted then
    fcItems.Sort(Compare);
end;

function TUsesSection.Text: string;
var
  liItemLoop: integer;
  lcItem: TusesItem;
begin
  Result := '';

  for liItemLoop := 0 to Count - 1 do
  begin
    lcItem := Items[liItemLoop];
    Result := Result + lcItem.Text;
  end;

end;

procedure TUsesSection.AttachTo(const pcNode: TParseTreeNode; pbAllowCommaFirst: boolean);
var
  liItemLoop: integer;
  lcItem: TusesItem;
  lcLastToken, lcNextToken: TSourceToken;
begin
  for liItemLoop := 0 to Count - 1 do
  begin
    lcItem := Items[liItemLoop];
    lcItem.AttachTo(pcNode, pbAllowCommaFirst);
    pbAllowCommaFirst := True; // set true fater first one

    // space it?
    if Sorted and (liItemLoop < (Count - 1)) and (lcItem.Count > 0) then
    begin
      lcLastToken := lcItem.Items[lcItem.Count - 1];
      if lcLastToken.TokenType = ttComma then
      begin
        lcNextToken := Items[liItemLoop + 1].Items[0];

        if lcNextToken.IsSolid then
          pcNode.AddChild(NewSpace(1));
      end;
    end;
  end
end;

end.