PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/components/jcf2/Process/Returns/RemoveReturnsBeforeEnd.pas

http://github.com/graemeg/lazarus
Pascal | 103 lines | 55 code | 20 blank | 28 comment | 6 complexity | 0d6683517569a3c02514e56538a0b993 MD5 | raw file
  1unit RemoveReturnsBeforeEnd;
  2
  3{(*}
  4(*------------------------------------------------------------------------------
  5 Delphi Code formatter source code 
  6
  7The Original Code is RemoveReturnsBeforeEnd, released May 2003.
  8The Initial Developer of the Original Code is Anthony Steele. 
  9Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
 10All Rights Reserved. 
 11Contributor(s): Anthony Steele. 
 12
 13The 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.
 15You may obtain a copy of the License at http://www.mozilla.org/NPL/
 16
 17Software distributed under the License is distributed on an "AS IS" basis,
 18WITHOUT WARRANTY OF ANY KIND, either express or implied.
 19See the License for the specific language governing rights and limitations 
 20under the License.
 21
 22Alternatively, the contents of this file may be used under the terms of
 23the GNU General Public License Version 2 or later (the "GPL") 
 24See http://www.gnu.org/licenses/gpl.html
 25------------------------------------------------------------------------------*)
 26{*)}
 27
 28{$I JcfGlobal.inc}
 29
 30interface
 31
 32uses SourceToken, SwitchableVisitor;
 33
 34type
 35  TRemoveReturnsBeforeEnd = class(TSwitchableVisitor)
 36  protected
 37    function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
 38
 39  public
 40    constructor Create; override;
 41
 42    function IsIncludedInSettings: boolean; override;
 43  end;
 44
 45implementation
 46
 47uses JcfSettings, Tokens, TokenUtils;
 48
 49{ TRemoveReturnsBeforeEnd }
 50
 51constructor TRemoveReturnsBeforeEnd.Create;
 52begin
 53  inherited;
 54end;
 55
 56function TRemoveReturnsBeforeEnd.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
 57var
 58  lcSourceToken: TSourceToken;
 59  lcNext: TSourceToken;
 60  lcTest: TSourceToken;
 61  liReturnCount: integer;
 62  liMaxReturns: integer;
 63begin
 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 := 2;
 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;
 96end;
 97
 98function TRemoveReturnsBeforeEnd.IsIncludedInSettings: boolean;
 99begin
100  Result := FormatSettings.Returns.RemoveBlockBlankLines;
101end;
102
103end.