1 unit RemoveReturnsBeforeEnd;
2 
3 {(*}
4 (*------------------------------------------------------------------------------
5  Delphi Code formatter source code
6 
7 The Original Code is RemoveReturnsBeforeEnd, released May 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 uses SourceToken, SwitchableVisitor;
33 
34 type
35   TRemoveReturnsBeforeEnd = class(TSwitchableVisitor)
36   protected
EnabledVisitSourceTokennull37     function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
38 
39   public
40     constructor Create; override;
41 
IsIncludedInSettingsnull42     function IsIncludedInSettings: boolean; override;
43   end;
44 
45 implementation
46 
47 uses JcfSettings, Tokens, TokenUtils;
48 
49 { TRemoveReturnsBeforeEnd }
50 
51 constructor TRemoveReturnsBeforeEnd.Create;
52 begin
53   inherited;
54 end;
55 
EnabledVisitSourceTokennull56 function TRemoveReturnsBeforeEnd.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
57 var
58   lcSourceToken: TSourceToken;
59   lcNext: TSourceToken;
60   lcTest: TSourceToken;
61   liReturnCount: integer;
62   liMaxReturns: integer;
63 begin
64   Result := False;
65   lcSourceToken := TSourceToken(pcNode);
66 
67   if lcSourceToken.TokenType <> ttReturn then
68     exit;
69 
70   if not InStatements(lcSourceToken) then
71     exit;
72 
73   lcNext := lcSourceToken.NextTokenWithExclusions([ttWhiteSpace, ttReturn]);
74 
75   if (lcNext = nil) or (lcNext.TokenType <> ttEnd) then
76     exit;
77 
78   liReturnCount := 0;
79   liMaxReturns := Succ(FormattingSettings.Returns.MaxBlankLinesInSection);
80   lcTest := lcSourceToken;
81 
82   { remove all returns up to that point (except one) }
83   while (lcTest <> lcNext) do
84   begin
85     if (lcTest.TokenType = ttReturn) then
86     begin
87       // allow two returns -> 1 blank line
88       Inc(liReturnCount);
89       if (liReturnCount > liMaxReturns) then
90       begin
91         BlankToken(lcTest);
92       end;
93     end;
94     lcTest := lcTest.NextToken;
95   end;
96 end;
97 
IsIncludedInSettingsnull98 function TRemoveReturnsBeforeEnd.IsIncludedInSettings: boolean;
99 begin
100   Result := FormattingSettings.Returns.RemoveBlockBlankLines;
101 end;
102 
103 end.
104