{******************************************************************************}
{                                                                              }
{                       Pascal Script Source File                              }
{                                                                              }
{             Run by RemObjects Pascal Script in CnPack IDE Wizards            }
{                                                                              }
{                                   Generated by CnPack IDE Wizards            }
{                                                                              }
{          本脚本能够根据 var 区声明的对象变量生成对应的创建释放代码块         }
{                                                                              }
{******************************************************************************}

program GenVarObjCodes;

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActnList, Buttons, ClipBrd, ComCtrls, ExtCtrls, ComObj, ExtDlgs, IniFiles,
  Menus, Printers, Registry, StdCtrls, TypInfo, ToolsAPI, CnDebug,
  RegExpr, ScriptEvent, CnCommon, CnWizClasses, CnWizUtils, CnWizIdeUtils,
  CnWizShortCut, CnWizOptions;

var
  I, J, Idx: Integer;
  S, AType: string;
  Lines, Names, VarList, TypeList: TStringList;
begin
  Lines := nil;
  VarList := nil;
  TypeList := nil;

  try
    Lines := TStringList.Create;
    if IdeGetEditorSelectedLines(Lines) then
    begin
      Names := TStringList.Create;
      VarList := TStringList.Create;
      TypeList := TStringList.Create;

      for I := 0 to Lines.Count - 1 do
      begin
        S := Trim(Lines[I])
        Idx := Pos(':', S);
        if Idx > 1 then
        begin
          AType := Trim(Copy(S, Idx + 1, MaxInt));
          S := Copy(S, 1, Idx - 1);

          if Length(AType) > 1 then
          begin
            if AType[Length(AType)] = ';' then
              Delete(AType, Length(AType), 1); // Get Type Name
          end
          else
            Continue;

          if (AType[1] <> 'T') and (AType[1] <> 't') then // 不是 T 开头的不要
            Continue;

          if LowerCase(AType) = 'tstrings' then // 换成可实例化的
            AType := 'TStringLis'
          else if LowerCase(AType) = 'tstream' then
            AType := 'TMemoryStream';

          // 以逗号劈开 S
          Names.Clear;
          ExtractStrings([','], [' '], S, Names);
          for J := 0 to Names.Count - 1 do
          begin
            if Trim(Names[J]) <> '' then
            begin
              VarList.Add(Trim(Names[J]));
              TypeList.Add(AType);
            end;
          end;  
        end;
      end;

      if VarList.Count <= 0 then
      begin
        ErrorDlg('NO Variable Declaration Found. Please Select a Variable Line in Editor');
        Exit;
      end;

      // 拿到了选中的变量名、类型名，组合成字符串
      Lines.Clear;
      for I := 0 to VarList.Count - 1 do
        Lines.Add('  ' + VarList[I] + ' := nil;');

      Lines.Add('');
      Lines.Add('  try');
      for I := 0 to VarList.Count - 1 do
        Lines.Add('    ' + VarList[I] + ' := ' + TypeList[I] + '.Create;');

      Lines.Add('');
      Lines.Add('  finally');
      for I := VarList.Count - 1 downto 0 do
        Lines.Add('    ' + VarList[I] + '.Free;');

      Lines.Add('  end;');

      Clipboard.AsText := Lines.Text;
      InfoDlg('Code Generated and Copied to Clipboard. Please Paste It in Editor.');
    end
    else
      ErrorDlg('NO Selection Found. Please Select a Variable Line in Editor');
  finally
    TypeList.Free;
    VarList.Free;
    Lines.Free;
    Names.Free;
  end;
end.
 
