PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/components/jcf2/Process/Returns/RemoveBlankLinesAfterProcHeader.pas

http://github.com/graemeg/lazarus
Pascal | 138 lines | 75 code | 28 blank | 35 comment | 9 complexity | 35b53af2e221fb40269c29447063a899 MD5 | raw file
  1unit RemoveBlankLinesAfterProcHeader;
  2
  3{(*}
  4(*------------------------------------------------------------------------------
  5 Delphi Code formatter source code 
  6
  7The Original Code is RemoveBlankLinesAfterProcHeader, 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  TRemoveBlankLinesAfterProcHeader = 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
 48  JcfSettings, FormatFlags, Tokens, TokenUtils,
 49  ParseTreeNodeType;
 50
 51function IsPlaceForBlankLineRemoval(const ptToken, ptNextSolidToken:
 52  TSourceToken): boolean;
 53begin
 54  Result := False;
 55
 56  if (ptToken = nil) or (ptNextSolidToken = nil) then
 57    exit;
 58
 59  { assume we're already under a procedure decl as tested below }
 60
 61  { before the begin }
 62  if ptToken.HasParentNode([nCompoundStatement] + ProcedureNodes) and
 63    (ptNextSolidToken.TokenType = ttBegin) then
 64  begin
 65    Result := True;
 66    exit;
 67  end;
 68
 69  { before the type, const, label, val }
 70  if ptToken.HasParentNode(nDeclSection) and
 71    (ptNextSolidToken.TokenType in [ttType, ttVar, ttConst, ttLabel]) then
 72  begin
 73    Result := True;
 74    exit;
 75  end;
 76end;
 77
 78{ TRemoveBlankLinesAfterProcHeader }
 79
 80constructor TRemoveBlankLinesAfterProcHeader.Create;
 81begin
 82  inherited;
 83  FormatFlags := FormatFlags + [eRemoveReturn];
 84end;
 85
 86function TRemoveBlankLinesAfterProcHeader.EnabledVisitSourceToken(
 87  const pcNode: TObject): Boolean;
 88var
 89  lcSourceToken:  TSourceToken;
 90  lcNext, lcTest: TSourceToken;
 91  liReturnCount:  integer;
 92  liMaxReturns:   integer;
 93begin
 94  Result := False;
 95  lcSourceToken := TSourceToken(pcNode);
 96
 97  { must be in procedure declarations or directives}
 98  if not lcSourceToken.HasParentNode(ProcedureNodes) then
 99    exit;
100
101  if lcSourceToken.TokenType <> ttReturn then
102    exit;
103
104  lcNext := lcSourceToken.NextTokenWithExclusions([ttWhiteSpace, ttReturn]);
105
106  { it must be a 'var', 'const', 'type' or 'begin'
107   in the procedure defs/body section }
108
109
110  { can be type, var etc. var}
111  if not IsPlaceForBlankLineRemoval(lcSourceToken, lcNext) then
112    exit;
113
114  liReturnCount := 0;
115  liMaxReturns := FormatSettings.Returns.MaxBlankLinesInSection + 1;
116  lcTest := lcSourceToken;
117
118  { remove all returns up to that point (except one) }
119  while (lcTest <> lcNext) do
120  begin
121    if (lcTest.TokenType = ttReturn) then
122    begin
123      // allow two returns -> 1 blank line
124      Inc(liReturnCount);
125      if (liReturnCount > liMaxReturns) then
126        BlankToken(lcTest);
127    end;
128    lcTest := lcTest.NextToken;
129  end;
130
131end;
132
133function TRemoveBlankLinesAfterProcHeader.IsIncludedInSettings: boolean;
134begin
135  Result := FormatSettings.Returns.RemoveProcedureDefReturns;
136end;
137
138end.