1 unit EditorConverter;
2 
3 {(*}
4 (*------------------------------------------------------------------------------
5  Delphi Code formatter source code
6 
7 The Original Code is EditorConverter.pas, released January 2001.
8 The Initial Developer of the Original Code is Anthony Steele.
9 Portions created by Anthony Steele are Copyright (C) 2001 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 { AFS 12 Jan 2K
29   Converter class for the IDE pluggin
30 }
31 
32 {$I JcfGlobal.inc}
33 
34 interface
35 
36 uses
37   Classes, SysUtils,
38   // IdeIntf
39   SrcEditorIntf,
40   { local }
41   Converter, ConvertTypes;
42 
43 type
44 
45   TEditorConverter = class(TObject)
46   private
47     { the string -> string converter }
48     fcConverter: TConverter;
49     { state }
50     fOnStatusMessage: TStatusMessageProc;
51     fsCurrentUnitName: string;
52     fiConvertCount: integer;
53 
54     procedure SendStatusMessage(const psUnit, psMessage: string;
55       const peMessageType: TStatusMessageType;
56       const piY, piX: integer);
57 
GetOnStatusMessagenull58     function GetOnStatusMessage: TStatusMessageProc;
59     procedure SetOnStatusMessage(const Value: TStatusMessageProc);
60 
ReadFromIDEnull61     function ReadFromIDE(const pcUnit: TSourceEditorInterface): string;
62     procedure WriteToIDE(const pcUnit: TSourceEditorInterface; const psText: string);
63 
64     procedure FinalSummary;
OriginalFileNamenull65     function OriginalFileName: string;
66 
67   protected
68 
69   public
70     constructor Create;
71     destructor Destroy; override;
72     procedure Convert(const pciUnit: TSourceEditorInterface);
73     procedure Clear;
ConvertErrornull74     function ConvertError: Boolean;
TokenCountnull75     function TokenCount: integer;
76 
77     procedure BeforeConvert;
78     procedure AfterConvert;
79 
80     property OnStatusMessage: TStatusMessageProc read GetOnStatusMessage write SetOnStatusMessage;
81   end;
82 
83 
84 implementation
85 
86 uses
87   { local }
88   JcfLog, JcfRegistrySettings, diffmerge;
89 
90 constructor TEditorConverter.Create;
91 begin
92   inherited;
93 
94   fcConverter := TConverter.Create;
95   fcConverter.OnStatusMessage := SendStatusMessage;
96 end;
97 
98 destructor TEditorConverter.Destroy;
99 begin
100   FreeAndNil(fcConverter);
101   inherited;
102 end;
103 
104 procedure TEditorConverter.Convert(const pciUnit: TSourceEditorInterface);
105 begin
106   Assert(pciUnit <> nil);
107 
108   if not GetRegSettings.HasRead then
109     GetRegSettings.ReadAll;
110 
111   { check for read-only  }
112   if pciUnit.ReadOnly then
113   begin
114     SendStatusMessage(pciUnit.FileName, 'Unit is read only. Cannot format ',
115       mtInputError, -1, -1);
116     exit;
117   end;
118 
119   fsCurrentUnitName := pciUnit.FileName;
120   fcConverter.InputCode := ReadFromIDE(pciUnit);
121 
122   // now convert
123   fcConverter.Convert;
124   fsCurrentUnitName := '';
125   if not ConvertError then
126   begin
127     WriteToIDE(pciUnit, fcConverter.OutputCode);
128     SendStatusMessage(pciUnit.FileName, 'Formatted unit', mtProgress, -1, -1);
129     Inc(fiConvertCount);
130   end;
131 end;
132 
ReadFromIDEnull133 function TEditorConverter.ReadFromIDE(const pcUnit: TSourceEditorInterface): string;
134 begin
135   Result := pcUnit.Lines.Text;
136 end;
137 
138 procedure TEditorConverter.WriteToIDE(const pcUnit: TSourceEditorInterface; const psText: string);
139 begin
140   if pcUnit = nil then
141     exit;
142   if psText <> fcConverter.InputCode then
143     DiffMergeEditor(pcUnit, psText);
144 end;
145 
146 procedure TEditorConverter.AfterConvert;
147 begin
148   FinalSummary;
149   Log.CloseLog;
150 
151   if GetRegSettings.ViewLogAfterRun then
152     GetRegSettings.ViewLog;
153 end;
154 
155 procedure TEditorConverter.Clear;
156 begin
157   fcConverter.Clear;
158 end;
159 
TEditorConverter.ConvertErrornull160 function TEditorConverter.ConvertError: Boolean;
161 begin
162   Result := fcConverter.ConvertError;
163 end;
164 
TEditorConverter.GetOnStatusMessagenull165 function TEditorConverter.GetOnStatusMessage: TStatusMessageProc;
166 begin
167   Result := fOnStatusMessage;
168 end;
169 
OriginalFileNamenull170 function TEditorConverter.OriginalFileName: string;
171 begin
172   if fsCurrentUnitName <> '' then
173     Result := fsCurrentUnitName
174   else
175     Result := 'IDE';
176 end;
177 
178 procedure TEditorConverter.SendStatusMessage(const psUnit, psMessage: string;
179   const peMessageType: TStatusMessageType;
180   const piY, piX: integer);
181 var
182   lsUnit: string;
183 begin
184   lsUnit := psUnit;
185   if lsUnit = '' then
186     lsUnit := OriginalFileName;
187 
188   if Assigned(fOnStatusMessage) then
189     fOnStatusMessage(lsUnit, psMessage, peMessageType, piY, piX);
190 end;
191 
192 procedure TEditorConverter.SetOnStatusMessage(const Value: TStatusMessageProc);
193 begin
194   fOnStatusMessage := Value;
195 end;
196 
TEditorConverter.TokenCountnull197 function TEditorConverter.TokenCount: integer;
198 begin
199   Result := fcConverter.TokenCount;
200 end;
201 
202 procedure TEditorConverter.FinalSummary;
203 var
204   lsMessage: string;
205 begin
206   if fiConvertCount = 0 then
207   begin
208     if ConvertError then
209       lsMessage := 'Aborted due to error'
210     else
211       lsMessage := 'Nothing done';
212   end
213   {
214   else if fbAbort then
215     lsMessage := 'Aborted after ' + DescribeFileCount(fiConvertCount)
216   }
217   else if fiConvertCount > 1 then
218     lsMessage := 'Finished processing ' + DescribeFileCount(fiConvertCount)
219   else
220     lsMessage := '';
221 
222   if lsMessage <> '' then
223     SendStatusMessage('', lsMessage, mtFinalSummary, -1, -1);
224 
225   Log.EmptyLine;
226   Log.Write(lsMessage);
227 end;
228 
229 procedure TEditorConverter.BeforeConvert;
230 begin
231   fiConvertCount := 0;
232 end;
233 
234 end.
235