/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

  1. unit RemoveReturnsBeforeEnd;
  2. {(*}
  3. (*------------------------------------------------------------------------------
  4. Delphi Code formatter source code
  5. The Original Code is RemoveReturnsBeforeEnd, 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. TRemoveReturnsBeforeEnd = 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 JcfSettings, Tokens, TokenUtils;
  35. { TRemoveReturnsBeforeEnd }
  36. constructor TRemoveReturnsBeforeEnd.Create;
  37. begin
  38. inherited;
  39. end;
  40. function TRemoveReturnsBeforeEnd.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
  41. var
  42. lcSourceToken: TSourceToken;
  43. lcNext: TSourceToken;
  44. lcTest: TSourceToken;
  45. liReturnCount: integer;
  46. liMaxReturns: integer;
  47. begin
  48. Result := False;
  49. lcSourceToken := TSourceToken(pcNode);
  50. if lcSourceToken.TokenType <> ttReturn then
  51. exit;
  52. if not InStatements(lcSourceToken) then
  53. exit;
  54. lcNext := lcSourceToken.NextTokenWithExclusions([ttWhiteSpace, ttReturn]);
  55. if (lcNext = nil) or (lcNext.TokenType <> ttEnd) then
  56. exit;
  57. liReturnCount := 0;
  58. liMaxReturns := 2;
  59. lcTest := lcSourceToken;
  60. { remove all returns up to that point (except one) }
  61. while (lcTest <> lcNext) do
  62. begin
  63. if (lcTest.TokenType = ttReturn) then
  64. begin
  65. // allow two returns -> 1 blank line
  66. Inc(liReturnCount);
  67. if (liReturnCount > liMaxReturns) then
  68. begin
  69. BlankToken(lcTest);
  70. end;
  71. end;
  72. lcTest := lcTest.NextToken;
  73. end;
  74. end;
  75. function TRemoveReturnsBeforeEnd.IsIncludedInSettings: boolean;
  76. begin
  77. Result := FormatSettings.Returns.RemoveBlockBlankLines;
  78. end;
  79. end.