type
TCodeBeautifierWizard =
class(TNotifierObject, IOTAWizard, IOTAMenuWizard)
// IOTAWizard
function GetIDString:
string;
function GetName:
string;
function GetState: TWizardState;
procedure Execute;
virtual;
// IOTAMenuWizard
function GetMenuText:
string;
end;
// ...
function GetReaderTextSize(fReader: IOTAEditReader): LongInt;
var
iHigh, iLow, iMiddle: LongInt;
c: Char;
begin
Result := 0;
if (fReader <>
nil)
then begin
iHigh := high(LongInt);
iLow := 0;
while iLow <= iHigh
do begin
iMiddle := (iHigh + iLow)
div 2;
if fReader.GetText(iMiddle, @c, 1) = 1
then begin
iLow := iMiddle + 1;
end else begin
iHigh := iMiddle - 1;
end;
end;
Result := iLow;
end;
end;
// ...
procedure TCodeBeautifierWizard.Execute;
var
ModuleServices: IOTAModuleServices;
Module: IOTAModule;
Intf: IOTAEditor;
Editor: IOTASourceEditor;
View: IOTAEditView;
i: Integer;
pf: TPascalFile;
Reader: IOTAEditReader;
Writer: IOTAEditWriter;
s: AnsiString;
c: cardinal;
begin
ModuleServices := BorlandIDEServices
as IOTAModuleServices;
// Get the module interface for the current file.
Module := ModuleServices.CurrentModule;
// If no file is open, Module is nil.
if Module =
nil then begin
Messagedlg('
No source file selected, Code Beautifier works on the current file only!', mtError, [mbOK], 0);
Exit;
end;
// Get the interface to the source editor.
for i := 0
to Module.GetModuleFileCount - 1
do begin
Intf := Module.GetModuleFileEditor(i);
if Intf.QueryInterface(IOTASourceEditor, Editor) = S_OK
then Break;
end;
// If the file is not a source file, Editor is nil.
if Editor =
nil then begin
Messagedlg('
No source shown, Code Beautifier works in source view only!', mtError, [mbOK], 0);
Exit;
end;
// The expert cannot tell which view is active, so force
// the user to have only one view at a time.
if Editor.EditViewCount > 1
then raise Exception.Create(sCloseViews);
View := Editor.EditViews[0];
pf := TPascalFile.Create;
Reader := Editor.CreateReader;
try
pf.Filename := Editor.Filename;
c := GetReaderTextSize(Reader);
SetLength(s, c);
Reader.GetText(0, PAnsiChar(s), c);
pf.Text := s;
if ShowCodeBeautifierDialog(pf)
then begin
Writer := Editor.CreateUndoableWriter;
try
Writer.DeleteTo( high(LongInt));
Writer.Insert(PAnsiChar(AnsiString(pf.Text)));
finally
Writer :=
nil;
end;
end;
Editor.Show;
finally
pf.Free;
Reader :=
nil;
end;
end;