1 unit SpaceToTab;
2 
3 {(*}
4 (*------------------------------------------------------------------------------
5  Delphi Code formatter source code
6 
7 The Original Code is SpaceToTab, released May 2003.
8 The Initial Developer of the Original Code is Anthony Steele.
9 Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
10 All Rights Reserved.
11 Contributor(s): Anthony Steele.
12 
13 The 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.
15 You may obtain a copy of the License at http://www.mozilla.org/NPL/
16 
17 Software distributed under the License is distributed on an "AS IS" basis,
18 WITHOUT WARRANTY OF ANY KIND, either express or implied.
19 See the License for the specific language governing rights and limitations
20 under the License.
21 
22 Alternatively, the contents of this file may be used under the terms of
23 the GNU General Public License Version 2 or later (the "GPL")
24 See http://www.gnu.org/licenses/gpl.html
25 ------------------------------------------------------------------------------*)
26 {*)}
27 
28 {$I JcfGlobal.inc}
29 
30 interface
31 
32 { AFS 4 Jan 2002
33   convert spaces tabs }
34 
35 uses SwitchableVisitor;
36 
37 type
38   TSpaceToTab = class(TSwitchableVisitor)
39   private
40     fsSpaces: string;
41 
42   protected
EnabledVisitSourceTokennull43     function EnabledVisitSourceToken(const pcNode: TObject): boolean; override;
44   public
45     constructor Create; override;
46 
IsIncludedInSettingsnull47     function IsIncludedInSettings: boolean; override;
48   end;
49 
50 implementation
51 
52 uses
53   SysUtils,
54   { local }
55   JcfStringUtils,
56   JcfSettings, SourceToken, Tokens, FormatFlags;
57 
58 constructor TSpaceToTab.Create;
59 begin
60   inherited;
61   fsSpaces    := string(StrRepeat(NativeSpace, FormattingSettings.Spaces.SpacesForTab));
62   FormatFlags := FormatFlags + [eAddSpace, eRemoveSpace];
63 end;
64 
EnabledVisitSourceTokennull65 function TSpaceToTab.EnabledVisitSourceToken(const pcNode: TObject): Boolean;
66 var
67   lcSourceToken, lcNextToken: TSourceToken;
68   ls, lsTab: string;
69 begin
70   Result := False;
71   lcSourceToken := TSourceToken(pcNode);
72 
73   { work only on whitespace tokens.
74     Indent spaces also occur in multiline comments, but leave them alone }
75   if (lcSourceToken.TokenType <> ttWhiteSpace) then
76     exit;
77 
78   { Merge following space tokens
79     can't pass property as var parameter so ls local var is used }
80 
81 	ls := lcSourceToken.SourceCode;
82 	lcNextToken := lcSourceToken.NextToken;
83 	while (lcNextToken <> nil) and (lcNextToken.TokenType = ttWhiteSpace) do
84 	begin
85 		ls := ls + lcNextToken.SourceCode;
86 		lcNextToken.SourceCode := '';
87 		lcNextToken := lcNextToken.NextToken;
88 	end;
89 
90   lsTab := NativeTab;
91   StrReplace(ls, fsSpaces, lsTab, [rfReplaceAll]);
92   lcSourceToken.SourceCode := ls;
93 end;
94 
IsIncludedInSettingsnull95 function TSpaceToTab.IsIncludedInSettings: boolean;
96 begin
97   Result := FormattingSettings.Spaces.SpacesToTabs;
98 end;
99 
100 end.
101