This application launches a project to sort Delphi XE3 source code into tidy sorted (in ascending sequence) code. Program snippets and component parts are easier to locate in source code if the source code is stored in ascending alphabetical sequence.
This project is a non-trivial task therefore I have subrogated the tasks into processing code snippets. Once all the categories of code snippets are being handled properly then the task of placing the snippets in the appropriate order will be addressed.
The source code for this project can be found here. The component documentation can be found here.
{*----------------------------------------------------------------------------- Procedure: FormatUses1Click Date: 27-May-2013 @Param Sender: TObject @Return None -----------------------------------------------------------------------------} procedure TBCSSourceHelperC.FormatUses1Click(Sender: TObject); var buf: String; obuf: String; i: Integer; j: Integer; k: Integer; begin BCSSourceHelperPageControl1.ActivePageIndex := 0; k := 1; Repeat buf := memUnsorted.Lines[k]; buf := Trim(buf); if Length(buf) > 0 then begin i := 1; j := 1; repeat if ((buf[i] = ',') or (buf[i] = ';')) then begin obuf := Trim(obuf); memSorted.Items.Add(obuf); j := 0; obuf := ''; end else begin obuf := obuf + buf[i]; end; Inc(i); Inc(j); until i > Length(buf); end; Inc(k); Until k > memUnsorted.Lines.Count - 1; obuf := ''; i := 0; j := 1; obuf := ''; memSortFormated.Lines.Clear; memSortFormated.Lines.Add('uses'); Repeat if (Length(obuf) + Length(memSorted.Items[i]) + 2 < 80) then begin obuf := obuf + memSorted.Items[i] + ', '; end else begin memSortFormated.Lines.Add(' ' + obuf); obuf := memSorted.Items[i] + ', '; end; Inc(i); Until i > memSorted.Items.Count - 1; Delete(obuf, Length(obuf) - 1, 2); obuf := obuf + ';'; memSortFormated.Lines.Add(' ' + obuf); end;
This code disassembles the uses statement by storing each statement member in a sorted list box. The items are then retrieved (in order) from the list box and a new uses statement is generated.
uses System.Classes, System.SysUtils, System.Variants, Vcl.ComCtrls, Vcl.Controls, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Forms, Vcl.Graphics, Vcl.TabNotBk, Winapi.Messages, Winapi.Windows, Vcl.Menus, BCSXE3Utilsdp;
The resulting output looks like this.
uses BCSXE3Utilsdp, System.Classes, System.SysUtils, System.Variants, Vcl.ComCtrls, Vcl.Controls, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Forms, Vcl.Graphics, Vcl.Menus, Vcl.TabNotBk, Winapi.Messages, Winapi.Windows;
The main class for any dialog is comprised of many parts. The next section of code deals with class variables.
{*----------------------------------------------------------------------------- Procedure: ClassVariablesClick Date: 28-May-2013 @Param Sender: TObject @Return None -----------------------------------------------------------------------------} procedure TBCSSourceHelperC.ClassVariablesClick(Sender: TObject); var vars: Array [1 .. 255] of rec_area; i: Integer; j: Integer; buf: String; varn: String; f3: String; begin BCSSourceHelperPageControl1.ActivePageIndex := 1; ClassPageControl.ActivePageIndex := 0; i := 0; j := 1; Repeat buf := memRawClassVariables.Lines[i]; f3 := Trim(buf); Delete(f3, 4, Length(f3)); if f3 = '///' then begin vars[j].com := buf; end else begin vars[j].vari := buf; buf := Trim(buf); varn := buf; Delete(varn, Pos(':', varn), Length(varn)); lbxSortedVariables.Items.Add(varn + '@' + IntToStr(j)); Inc(j); vars[j].com := ''; end; Inc(i); Until i > memRawClassVariables.Lines.Count - 1; i := 0; repeat buf := lbxSortedVariables.Items[i]; Delete(buf, 1, Pos('@', buf)); if vars[StrToInt(buf)].com > '' then begin memRestructuredClassVariables.Lines.Add(vars[StrToInt(buf)].com); end; memRestructuredClassVariables.Lines.Add(vars[StrToInt(buf)].vari); Inc(i); until i > lbxSortedVariables.Items.Count - 1; end;
For the class variables the variable and comment are stored in a record. The variable name and a pointer to the array of records is then passed to a sorted list box.
/// Timer for Dialog BCSSourceHelperTimer1: TTimer; /// Status Panel For Dialog BCSSourceHelperStatusPanel1: TStatusBar; /// Main Page Control BCSSourceHelperPageControl1: TPageControl; /// Tab sheet 1 for page control TabSheet1: TTabSheet; /// Tab sheet 2 for page control TabSheet2: TTabSheet; /// BCS XE3 Utilities Component BCSXE3UtilsCmp1: TBCSXE3UtilsCmp; /// BCSSourceHelper Main Menu BCSSourceHelperMainMenu1: TMainMenu; /// Help Menu Item Help1: TMenuItem; /// Raw Uses List memUnsorted: TMemo; /// Menu Item To Invoke Reformatting FormatUses1: TMenuItem; /// Sorted Uses Members Names memSorted: TListBox; /// Sorted And Formatted Uses Clause memSortFormated: TMemo; ClassPageControl: TPageControl; TabSheet3: TTabSheet; TabSheet4: TTabSheet; TabSheet5: TTabSheet; TabSheet6: TTabSheet; Memo1: TMemo; ReverseEngineerProgram1: TMenuItem; Class1: TMenuItem; C1: TMenuItem;
The variables are retrieved in order and the newly created variable list is generated. The result are listed below.
/// BCSSourceHelper Main Menu BCSSourceHelperMainMenu1: TMainMenu; /// Main Page Control BCSSourceHelperPageControl1: TPageControl; /// Status Panel For Dialog BCSSourceHelperStatusPanel1: TStatusBar; /// Timer for Dialog BCSSourceHelperTimer1: TTimer; /// BCS XE3 Utilities Component BCSXE3UtilsCmp1: TBCSXE3UtilsCmp; C1: TMenuItem; Class1: TMenuItem; ClassPageControl: TPageControl; /// Menu Item To Invoke Reformatting FormatUses1: TMenuItem; /// Help Menu Item Help1: TMenuItem; Memo1: TMemo; /// Sorted Uses Members Names memSorted: TListBox; /// Sorted And Formatted Uses Clause memSortFormated: TMemo; /// Raw Uses List memUnsorted: TMemo; PrivateDeclarations1: TMenuItem; Procedures1: TMenuItem; PublicDeclarations1: TMenuItem; ReverseEngineerProgram1: TMenuItem; /// Tab sheet 1 for page control TabSheet1: TTabSheet; /// Tab sheet 2 for page control TabSheet2: TTabSheet; TabSheet3: TTabSheet; TabSheet4: TTabSheet; TabSheet5: TTabSheet; TabSheet6: TTabSheet;
The process will continue with the remaining sections.
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems authored this article.