/components/jcf2/Process/Capitalisation/UnitNameCaps.pas

http://github.com/graemeg/lazarus · Pascal · 152 lines · 87 code · 25 blank · 40 comment · 12 complexity · 13107eca1d9bc463b09e2673f2fa20ba MD5 · raw file

  1. unit UnitNameCaps;
  2. {(*}
  3. (*------------------------------------------------------------------------------
  4. Delphi Code formatter source code
  5. The Original Code is UnitNameCaps, released June 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. { AFS 16 June 2003
  23. - fix capitalisation on unit names
  24. }
  25. {$I JcfGlobal.inc}
  26. interface
  27. uses SwitchableVisitor;
  28. type
  29. TUnitNameCaps = class(TSwitchableVisitor)
  30. private
  31. fiCount: integer;
  32. lsLastChange: string;
  33. protected
  34. function EnabledVisitSourceToken(const pcNode: TObject): Boolean; override;
  35. public
  36. constructor Create; override;
  37. function IsIncludedInSettings: boolean; override;
  38. { return true if you want the message logged}
  39. function FinalSummary(out psMessage: string): boolean; override;
  40. end;
  41. implementation
  42. uses
  43. { delphi }
  44. {$IFNDEF FPC}Windows,{$ENDIF} SysUtils,
  45. { local }
  46. SourceToken, Tokens, ParseTreeNodeType, JcfSettings, FormatFlags,
  47. TokenUtils;
  48. function IsUnitName(const pt: TSourceToken): boolean;
  49. var
  50. lcNext, lcPrev: TSourceToken;
  51. begin
  52. Result := False;
  53. if not IsIdentifier(pt, idStrict) then
  54. exit;
  55. { unit names can be found in these places:
  56. in unit names
  57. uses clause
  58. and in expressions as a prefix for vars, constants and functions }
  59. if pt.HasParentNode(nUnitName) then
  60. Result := True
  61. else if pt.HasParentNode(nUsesItem) then
  62. Result := True
  63. else if pt.HasParentNode(nDesignator) then
  64. begin
  65. // must be a dot to resolve unit name
  66. lcNext := pt.NextSolidToken;
  67. Result := (lcNext <> nil) and (lcNext.TokenType = ttDot);
  68. if Result then
  69. begin
  70. // unit name is always first part of designator. May not be preceeded by a dot
  71. lcPrev := pt.PriorSolidToken;
  72. Result := (lcPrev <> nil) and (lcPrev.TokenType <> ttDot);
  73. end;
  74. end;
  75. end;
  76. { TUnitNameCaps }
  77. constructor TUnitNameCaps.Create;
  78. begin
  79. inherited;
  80. fiCount := 0;
  81. lsLastChange := '';
  82. FormatFlags := FormatFlags + [eCapsSpecificWord];
  83. end;
  84. function TUnitNameCaps.FinalSummary(out psMessage: string): boolean;
  85. begin
  86. Result := (fiCount > 0);
  87. psMessage := '';
  88. if Result then
  89. begin
  90. psMessage := 'Unit name caps: ';
  91. if fiCount = 1 then
  92. psMessage := psMessage + 'One change was made: ' + lsLastChange
  93. else
  94. psMessage := psMessage + IntToStr(fiCount) + ' changes were made';
  95. end;
  96. end;
  97. function TUnitNameCaps.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
  98. var
  99. lcSourceToken: TSourceToken;
  100. lsChange: string;
  101. begin
  102. Result := False;
  103. lcSourceToken := TSourceToken(pcNode);
  104. if not IsUnitName(lcSourceToken) then
  105. exit;
  106. if FormatSettings.UnitNameCaps.HasWord(lcSourceToken.SourceCode) then
  107. begin
  108. // get the fixed version
  109. lsChange := FormatSettings.UnitNameCaps.CapitaliseWord(lcSourceToken.SourceCode);
  110. // case-sensitive test - see if anything to do.
  111. if AnsiCompareStr(lcSourceToken.SourceCode, lsChange) <> 0 then
  112. begin
  113. lsLastChange := lcSourceToken.SourceCode + ' to ' + lsChange;
  114. lcSourceToken.SourceCode := lsChange;
  115. Inc(fiCount);
  116. end;
  117. end;
  118. end;
  119. function TUnitNameCaps.IsIncludedInSettings: boolean;
  120. begin
  121. Result := FormatSettings.UnitNameCaps.Enabled;
  122. end;
  123. end.