PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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