1 unit ReturnsAfterFinalEnd;
2 
3 {(*}
4 (*------------------------------------------------------------------------------
5  Delphi Code formatter source code
6 
7 The Original Code is ReturnsAfterFinalEnd.pas, released September 2003.
8 The Initial Developer of the Original Code is Anthony Steele.
9 Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
10 All Rights Reserved.
11 Contributor(s): Anthony Steele.
12 
13 The contents of this file are subject to the Mozilla Public License Version 1.1
14 (the "License"). you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at http://www.mozilla.org/NPL/
16 
17 Software distributed under the License is distributed on an "AS IS" basis,
18 WITHOUT WARRANTY OF ANY KIND, either express or implied.
19 See the License for the specific language governing rights and limitations
20 under the License.
21 
22 Alternatively, the contents of this file may be used under the terms of
23 the GNU General Public License Version 2 or later (the "GPL")
24 See http://www.gnu.org/licenses/gpl.html
25 ------------------------------------------------------------------------------*)
26 {*)}
27 
28 {$I JcfGlobal.inc}
29 
30 interface
31 
32 { AFS 27 Sept 2003
33   process to standardise the number of returns
34   after the final "end." of the unit }
35 
36 uses SwitchableVisitor;
37 
38 type
39   TReturnsAfterFinalEnd = class(TSwitchableVisitor)
40   private
41   protected
EnabledVisitSourceTokennull42     function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
43   public
44     constructor Create; override;
45 
46   end;
47 
48 
49 implementation
50 
51 uses
52   JcfSettings,
53   SourceToken, FormatFlags, Tokens, ParseTreeNodeType, TokenUtils;
54 
55 { TReturnsAfterFinalEnd }
56 
57 constructor TReturnsAfterFinalEnd.Create;
58 begin
59   inherited;
60   FormatFlags := FormatFlags + [eAddReturn, eRemoveReturn];
61 end;
62 
EnabledVisitSourceTokennull63 function TReturnsAfterFinalEnd.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
64 var
65   lcSourceToken:     TSourceToken;
66   lcCurrent, lcPrev: TSourceToken;
67   liReturnsWanted, liReturnsFound: integer;
68 begin
69   Result := False;
70   lcSourceToken := TSourceToken(pcNode);
71 
72   if (lcSourceToken.TokenType = ttDot) and
73     (lcSourceToken.HasParentNode(TopOfFileSection, 1)) then
74   begin
75     // count the returns
76     lcCurrent      := lcSourceToken;
77     liReturnsWanted := FormattingSettings.Returns.NumReturnsAfterFinalEnd;
78     liReturnsFound := 0;
79 
80     while lcCurrent <> nil do
81     begin
82       if lcCurrent.TokenType = ttReturn then
83       begin
84         Inc(liReturnsFound);
85 
86         lcPrev := lcCurrent.PriorToken;
87 
88         if (liReturnsFound > liReturnsWanted) and ( not lcPrev.IsSolid) then
89         begin
90           { this returns is surplus - remove it }
91           BlankToken(lcCurrent);
92         end;
93       end;
94 
95       lcCurrent := lcCurrent.NextToken;
96     end;
97 
98     { need to insert some returns? }
99     while liReturnsFound < liReturnsWanted do
100     begin
101       InsertReturnAfter(lcSourceToken);
102       Inc(liReturnsFound);
103     end;
104   end;
105 end;
106 
107 end.
108