/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

  1. unit RemoveBlankLinesAfterProcHeader;
  2. {(*}
  3. (*------------------------------------------------------------------------------
  4. Delphi Code formatter source code
  5. The Original Code is RemoveBlankLinesAfterProcHeader, released May 2003.
  6. The Initial Developer of the Original Code is Anthony Steele.
  7. Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
  8. All Rights Reserved.
  9. Contributor(s): Anthony Steele.
  10. The contents of this file are subject to the Mozilla Public License Version 1.1
  11. (the "License"). you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at http://www.mozilla.org/NPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied.
  15. See the License for the specific language governing rights and limitations
  16. under the License.
  17. Alternatively, the contents of this file may be used under the terms of
  18. the GNU General Public License Version 2 or later (the "GPL")
  19. See http://www.gnu.org/licenses/gpl.html
  20. ------------------------------------------------------------------------------*)
  21. {*)}
  22. {$I JcfGlobal.inc}
  23. interface
  24. uses SourceToken, SwitchableVisitor;
  25. type
  26. TRemoveBlankLinesAfterProcHeader = class(TSwitchableVisitor)
  27. protected
  28. function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
  29. public
  30. constructor Create; override;
  31. function IsIncludedInSettings: boolean; override;
  32. end;
  33. implementation
  34. uses
  35. JcfSettings, FormatFlags, Tokens, TokenUtils,
  36. ParseTreeNodeType;
  37. function IsPlaceForBlankLineRemoval(const ptToken, ptNextSolidToken:
  38. TSourceToken): boolean;
  39. begin
  40. Result := False;
  41. if (ptToken = nil) or (ptNextSolidToken = nil) then
  42. exit;
  43. { assume we're already under a procedure decl as tested below }
  44. { before the begin }
  45. if ptToken.HasParentNode([nCompoundStatement] + ProcedureNodes) and
  46. (ptNextSolidToken.TokenType = ttBegin) then
  47. begin
  48. Result := True;
  49. exit;
  50. end;
  51. { before the type, const, label, val }
  52. if ptToken.HasParentNode(nDeclSection) and
  53. (ptNextSolidToken.TokenType in [ttType, ttVar, ttConst, ttLabel]) then
  54. begin
  55. Result := True;
  56. exit;
  57. end;
  58. end;
  59. { TRemoveBlankLinesAfterProcHeader }
  60. constructor TRemoveBlankLinesAfterProcHeader.Create;
  61. begin
  62. inherited;
  63. FormatFlags := FormatFlags + [eRemoveReturn];
  64. end;
  65. function TRemoveBlankLinesAfterProcHeader.EnabledVisitSourceToken(
  66. const pcNode: TObject): Boolean;
  67. var
  68. lcSourceToken: TSourceToken;
  69. lcNext, lcTest: TSourceToken;
  70. liReturnCount: integer;
  71. liMaxReturns: integer;
  72. begin
  73. Result := False;
  74. lcSourceToken := TSourceToken(pcNode);
  75. { must be in procedure declarations or directives}
  76. if not lcSourceToken.HasParentNode(ProcedureNodes) then
  77. exit;
  78. if lcSourceToken.TokenType <> ttReturn then
  79. exit;
  80. lcNext := lcSourceToken.NextTokenWithExclusions([ttWhiteSpace, ttReturn]);
  81. { it must be a 'var', 'const', 'type' or 'begin'
  82. in the procedure defs/body section }
  83. { can be type, var etc. var}
  84. if not IsPlaceForBlankLineRemoval(lcSourceToken, lcNext) then
  85. exit;
  86. liReturnCount := 0;
  87. liMaxReturns := FormatSettings.Returns.MaxBlankLinesInSection + 1;
  88. lcTest := lcSourceToken;
  89. { remove all returns up to that point (except one) }
  90. while (lcTest <> lcNext) do
  91. begin
  92. if (lcTest.TokenType = ttReturn) then
  93. begin
  94. // allow two returns -> 1 blank line
  95. Inc(liReturnCount);
  96. if (liReturnCount > liMaxReturns) then
  97. BlankToken(lcTest);
  98. end;
  99. lcTest := lcTest.NextToken;
  100. end;
  101. end;
  102. function TRemoveBlankLinesAfterProcHeader.IsIncludedInSettings: boolean;
  103. begin
  104. Result := FormatSettings.Returns.RemoveProcedureDefReturns;
  105. end;
  106. end.