mirror of
				https://gitlab.com/freepascal.org/lazarus/lazarus.git
				synced 2025-11-04 15:09:36 +01:00 
			
		
		
		
	IDE: Refactor RemoveEmptyMethods->RemoveEmptyMethodsInUnit. Remove repeat loop for parent types, props are already iterated. Prepare for issue #37163.
git-svn-id: trunk@63780 -
This commit is contained in:
		
							parent
							
								
									125e794a87
								
							
						
					
					
						commit
						5b1d9b4f7c
					
				@ -109,7 +109,7 @@ type
 | 
			
		||||
    FLookupRoot: TComponent;
 | 
			
		||||
    FNode: TTreeNode;
 | 
			
		||||
    procedure AddCollection(AColl: TCollection; AParentNode: TTreeNode);
 | 
			
		||||
    procedure AddOwnedPersistent(APers: TPersistent; APropName: String;
 | 
			
		||||
    procedure AddOwnedPersistent(APers: TPersistent; const APropName: String;
 | 
			
		||||
      AParentNode: TTreeNode);
 | 
			
		||||
    procedure GetOwnedPersistents(APers: TPersistent; AParentNode: TTreeNode);
 | 
			
		||||
    function PersistentFoundInNode(APers: TPersistent): Boolean;
 | 
			
		||||
@ -173,7 +173,7 @@ begin
 | 
			
		||||
end;
 | 
			
		||||
 | 
			
		||||
procedure TComponentWalker.AddOwnedPersistent(APers: TPersistent;
 | 
			
		||||
  APropName: String; AParentNode: TTreeNode);
 | 
			
		||||
  const APropName: String; AParentNode: TTreeNode);
 | 
			
		||||
var
 | 
			
		||||
  TVNode: TTreeNode;
 | 
			
		||||
  TheRoot: TPersistent;
 | 
			
		||||
 | 
			
		||||
@ -81,9 +81,8 @@ type
 | 
			
		||||
 | 
			
		||||
function ShowEmptyMethodsDialog: TModalResult;
 | 
			
		||||
 | 
			
		||||
function RemoveEmptyMethods(Code: TCodeBuffer; AClassName: string;
 | 
			
		||||
  X, Y: integer; CommitSrcEditor: boolean; Sections: TPascalClassSections
 | 
			
		||||
  ): TModalResult;
 | 
			
		||||
function RemoveEmptyMethodsInUnit(Code: TCodeBuffer; AClassName: string;
 | 
			
		||||
  X, Y: integer; Sections: TPascalClassSections): TModalResult;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
implementation
 | 
			
		||||
@ -153,39 +152,11 @@ begin
 | 
			
		||||
  end;
 | 
			
		||||
end;
 | 
			
		||||
 | 
			
		||||
function RemoveEmptyMethods(Code: TCodeBuffer; AClassName: string;
 | 
			
		||||
  X, Y: integer; CommitSrcEditor: boolean; Sections: TPascalClassSections
 | 
			
		||||
  ): TModalResult;
 | 
			
		||||
function GetInheritedMethod(AComponent: TComponent; PropInfo: PPropInfo): TMethod;
 | 
			
		||||
var
 | 
			
		||||
  RemovedProcHeads: TStrings;
 | 
			
		||||
  PropChanged: boolean;
 | 
			
		||||
 | 
			
		||||
  function ExtractClassName: string;
 | 
			
		||||
  var
 | 
			
		||||
    ProcName: string;
 | 
			
		||||
    p: LongInt;
 | 
			
		||||
    i: Integer;
 | 
			
		||||
  begin
 | 
			
		||||
    Result:='';
 | 
			
		||||
    if (RemovedProcHeads=nil) or (RemovedProcHeads.Count=0) then exit;
 | 
			
		||||
    for i:=RemovedProcHeads.Count-1 downto 0 do begin
 | 
			
		||||
      ProcName:=RemovedProcHeads[i];
 | 
			
		||||
      p:=System.Pos('.',ProcName);
 | 
			
		||||
      if p<1 then
 | 
			
		||||
        RemovedProcHeads.Delete(i)
 | 
			
		||||
      else begin
 | 
			
		||||
        Result:=copy(ProcName,1,p-1);
 | 
			
		||||
        RemovedProcHeads[i]:=copy(ProcName,p+1,length(ProcName));
 | 
			
		||||
        //DebugLn(['ExtractClassName RemovedProcHeads[i]=',RemovedProcHeads[i]]);
 | 
			
		||||
      end;
 | 
			
		||||
    end;
 | 
			
		||||
  end;
 | 
			
		||||
 | 
			
		||||
  function GetInheritedMethod(AComponent: TComponent; PropInfo: PPropInfo): TMethod;
 | 
			
		||||
  var
 | 
			
		||||
  AncestorRoot, AncestorComponent: TComponent;
 | 
			
		||||
  AncestorMethod: TMethod;
 | 
			
		||||
  begin
 | 
			
		||||
begin
 | 
			
		||||
  FillByte(Result{%H-}, SizeOf(Result), 0);
 | 
			
		||||
  if csAncestor in AComponent.ComponentState then
 | 
			
		||||
  begin
 | 
			
		||||
@ -199,10 +170,7 @@ var
 | 
			
		||||
        AncestorComponent := nil;
 | 
			
		||||
    end
 | 
			
		||||
    else
 | 
			
		||||
      begin
 | 
			
		||||
        AncestorRoot := BaseFormEditor1.GetAncestorInstance(AComponent);
 | 
			
		||||
        AncestorComponent := AncestorRoot;
 | 
			
		||||
      end;
 | 
			
		||||
      AncestorComponent := BaseFormEditor1.GetAncestorInstance(AComponent);
 | 
			
		||||
 | 
			
		||||
    if Assigned(AncestorComponent) then
 | 
			
		||||
    begin
 | 
			
		||||
@ -211,12 +179,39 @@ var
 | 
			
		||||
        Result := AncestorMethod
 | 
			
		||||
    end;
 | 
			
		||||
  end;
 | 
			
		||||
end;
 | 
			
		||||
 | 
			
		||||
function RemoveEmptyMethodsInUnit(Code: TCodeBuffer; AClassName: string;
 | 
			
		||||
  X, Y: integer; Sections: TPascalClassSections): TModalResult;
 | 
			
		||||
var
 | 
			
		||||
  RemovedProcHeads: TStrings;
 | 
			
		||||
  PropChanged: boolean;
 | 
			
		||||
 | 
			
		||||
  function ExtractClassName: string;
 | 
			
		||||
  var
 | 
			
		||||
    ProcName: string;
 | 
			
		||||
    p: LongInt;
 | 
			
		||||
    i: Integer;
 | 
			
		||||
  begin
 | 
			
		||||
    Result:='';
 | 
			
		||||
    for i:=RemovedProcHeads.Count-1 downto 0 do
 | 
			
		||||
    begin
 | 
			
		||||
      ProcName:=RemovedProcHeads[i];
 | 
			
		||||
      p:=System.Pos('.',ProcName);
 | 
			
		||||
      if p<1 then
 | 
			
		||||
        RemovedProcHeads.Delete(i)
 | 
			
		||||
      else begin
 | 
			
		||||
        Result:=copy(ProcName,1,p-1);
 | 
			
		||||
        RemovedProcHeads[i]:=copy(ProcName,p+1,length(ProcName));
 | 
			
		||||
        DebugLn(['ExtractClassName ClassName=',Result, ', RemovedProcHeads[i]=',RemovedProcHeads[i]]);
 | 
			
		||||
      end;
 | 
			
		||||
    end;
 | 
			
		||||
  end;
 | 
			
		||||
 | 
			
		||||
  procedure CheckEvents(AComponent: TComponent);
 | 
			
		||||
  var
 | 
			
		||||
    TypeInfo: PTypeInfo;
 | 
			
		||||
    TypeData: PTypeData;
 | 
			
		||||
    //TypeData: PTypeData;
 | 
			
		||||
    PropInfo: PPropInfo;
 | 
			
		||||
    PropList: PPropList;
 | 
			
		||||
    CurCount,ic: integer;
 | 
			
		||||
@ -224,29 +219,33 @@ var
 | 
			
		||||
    AMethodName: String;
 | 
			
		||||
    i: Integer;
 | 
			
		||||
  begin
 | 
			
		||||
    //DebugLn('');
 | 
			
		||||
    // read all properties and remove doubles
 | 
			
		||||
    TypeInfo:=PTypeInfo(AComponent.ClassInfo);
 | 
			
		||||
    repeat
 | 
			
		||||
    //DebugLn(['CheckEvents: AComponent=',AComponent, ', TypeInfo^.Name=',TypeInfo^.Name]);
 | 
			
		||||
    // read all property infos of current class
 | 
			
		||||
      TypeData:=GetTypeData(TypeInfo);
 | 
			
		||||
    //TypeData:=GetTypeData(TypeInfo);
 | 
			
		||||
    // read property count
 | 
			
		||||
      CurCount:=GetPropList(TypeInfo,PropList);;
 | 
			
		||||
    CurCount:=GetPropList(TypeInfo,PropList);
 | 
			
		||||
    //DebugLn(['CheckEvents: CurCount=', CurCount, ', ClassType=', TypeData^.ClassType]);
 | 
			
		||||
    try
 | 
			
		||||
      // read properties
 | 
			
		||||
        for ic:=0 to CurCount-1 do begin
 | 
			
		||||
      for ic:=0 to CurCount-1 do
 | 
			
		||||
      begin
 | 
			
		||||
        PropInfo:=PropList^[ic];
 | 
			
		||||
          if (PropInfo^.PropType^.Kind=tkMethod) then begin
 | 
			
		||||
        if PropInfo^.PropType^.Kind=tkMethod then
 | 
			
		||||
        begin
 | 
			
		||||
          // event
 | 
			
		||||
          AMethod:=GetMethodProp(AComponent,PropInfo);
 | 
			
		||||
          AMethodName:=GlobalDesignHook.GetMethodName(AMethod,nil);
 | 
			
		||||
            //DebugLn(['CheckEvents ',PropInfo^.Name,' AMethodName=',AMethodName]);
 | 
			
		||||
            if AMethodName<>'' then begin
 | 
			
		||||
          if AMethodName<>'' then
 | 
			
		||||
          begin
 | 
			
		||||
            i:=RemovedProcHeads.Count-1;
 | 
			
		||||
              while (i>=0)
 | 
			
		||||
              and (SysUtils.CompareText(RemovedProcHeads[i],AMethodName)<>0) do
 | 
			
		||||
            while (i>=0) and (CompareText(RemovedProcHeads[i],AMethodName)<>0) do
 | 
			
		||||
              dec(i);
 | 
			
		||||
              if i>=0 then begin
 | 
			
		||||
                DebugLn(['RemoveEmptyMethods Clearing Property=',PropInfo^.Name,' AMethodName=',AMethodName]);
 | 
			
		||||
            if i>=0 then
 | 
			
		||||
            begin
 | 
			
		||||
              //DebugLn(['  CheckEvents Clearing Property=',PropInfo^.Name,' AMethodName=',AMethodName]);
 | 
			
		||||
              AMethod := GetInheritedMethod(AComponent, PropInfo);
 | 
			
		||||
              SetMethodProp(AComponent, PropInfo, AMethod);
 | 
			
		||||
              PropChanged:=true;
 | 
			
		||||
@ -257,8 +256,6 @@ var
 | 
			
		||||
    finally
 | 
			
		||||
      FreeMem(PropList);
 | 
			
		||||
    end;
 | 
			
		||||
      TypeInfo:=TypeData^.ParentInfo;
 | 
			
		||||
    until TypeInfo=nil;
 | 
			
		||||
  end;
 | 
			
		||||
 | 
			
		||||
var
 | 
			
		||||
@ -269,32 +266,28 @@ var
 | 
			
		||||
  CurClassName: String;
 | 
			
		||||
begin
 | 
			
		||||
  Result:=mrCancel;
 | 
			
		||||
  if CommitSrcEditor and (not LazarusIDE.BeginCodeTools) then exit;
 | 
			
		||||
 | 
			
		||||
  //DebugLn(['TEmptyMethodsDialog.OKButtonClick ']);
 | 
			
		||||
  RemovedProcHeads:=nil;
 | 
			
		||||
  try
 | 
			
		||||
    if (not CodeToolBoss.RemoveEmptyMethods(Code,AClassName,X,Y,
 | 
			
		||||
      Sections,AllEmpty,
 | 
			
		||||
    if not CodeToolBoss.RemoveEmptyMethods(Code,AClassName,X,Y,Sections,AllEmpty,
 | 
			
		||||
      [phpAddClassName,phpDoNotAddSemicolon,phpWithoutParamList,
 | 
			
		||||
       phpWithoutBrackets,phpWithoutClassKeyword,phpWithoutSemicolon],
 | 
			
		||||
      RemovedProcHeads))
 | 
			
		||||
      RemovedProcHeads)
 | 
			
		||||
    then begin
 | 
			
		||||
      DebugLn(['RemoveEmptyMethods failed']);
 | 
			
		||||
      exit;
 | 
			
		||||
    end;
 | 
			
		||||
    if (RemovedProcHeads<>nil) and (RemovedProcHeads.Count>0) then begin
 | 
			
		||||
      // RemovedProcHeads contains a list of classname.procname
 | 
			
		||||
      // remove the classname from the list
 | 
			
		||||
      // RemovedProcHeads contains a list of classname.procname, remove classname from the list
 | 
			
		||||
      CurClassName:=ExtractClassName;
 | 
			
		||||
      if CurClassName<>'' then begin
 | 
			
		||||
        if (Project1<>nil) then begin
 | 
			
		||||
      if (CurClassName<>'') and (Project1<>nil) then
 | 
			
		||||
      begin
 | 
			
		||||
        AnUnitInfo:=Project1.UnitInfoWithFilename(Code.Filename);
 | 
			
		||||
          if AnUnitInfo<>nil then begin
 | 
			
		||||
        if AnUnitInfo<>nil then
 | 
			
		||||
        begin
 | 
			
		||||
          // fix events of designer components
 | 
			
		||||
          LookupRoot:=AnUnitInfo.Component;
 | 
			
		||||
            if (LookupRoot<>nil)
 | 
			
		||||
            and (SysUtils.CompareText(LookupRoot.ClassName,CurClassName)=0) then
 | 
			
		||||
          //DebugLn(['RemoveEmptyMethods: LookupRoot=', LookupRoot]);
 | 
			
		||||
          if (LookupRoot<>nil) and (CompareText(LookupRoot.ClassName,CurClassName)=0) then
 | 
			
		||||
          begin
 | 
			
		||||
            PropChanged:=false;
 | 
			
		||||
            CheckEvents(LookupRoot);
 | 
			
		||||
@ -307,7 +300,6 @@ begin
 | 
			
		||||
        end;
 | 
			
		||||
      end;
 | 
			
		||||
    end;
 | 
			
		||||
    end;
 | 
			
		||||
  finally
 | 
			
		||||
    RemovedProcHeads.Free;
 | 
			
		||||
  end;
 | 
			
		||||
@ -337,7 +329,8 @@ end;
 | 
			
		||||
 | 
			
		||||
procedure TEmptyMethodsDialog.OKButtonClick(Sender: TObject);
 | 
			
		||||
begin
 | 
			
		||||
  if RemoveEmptyMethods(Code,'',Caret.X,Caret.Y,true,Sections)<>mrOk then exit;
 | 
			
		||||
  if LazarusIDE.BeginCodeTools
 | 
			
		||||
  and (RemoveEmptyMethodsInUnit(Code,'',Caret.X,Caret.Y,Sections)=mrOk) then
 | 
			
		||||
    ModalResult:=mrOk;
 | 
			
		||||
end;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2498,8 +2498,8 @@ begin
 | 
			
		||||
    // Note: When removing published methods, the source, the lfm, the lrs
 | 
			
		||||
    //       and the form must be changed. At the moment editing the lfm without
 | 
			
		||||
    //       the component is not yet implemented.
 | 
			
		||||
    Result:=RemoveEmptyMethods(AnUnitInfo.Source,
 | 
			
		||||
                   AnUnitInfo.Component.ClassName,0,0,false,[pcsPublished]);
 | 
			
		||||
    Result:=RemoveEmptyMethodsInUnit(AnUnitInfo.Source, AnUnitInfo.Component.ClassName,
 | 
			
		||||
                                     0,0,[pcsPublished]);
 | 
			
		||||
    if Result=mrAbort then exit;
 | 
			
		||||
  end;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user