mirror of
				https://gitlab.com/freepascal.org/lazarus/lazarus.git
				synced 2025-10-31 16:21:45 +01:00 
			
		
		
		
	Converter: improve replacing used a unit with a comma separated list of units.
git-svn-id: trunk@41931 -
This commit is contained in:
		
							parent
							
								
									7309c93641
								
							
						
					
					
						commit
						af3f23c7b6
					
				| @ -120,6 +120,8 @@ type | |||||||
|     fImplUsedUnits: TUsedUnits; |     fImplUsedUnits: TUsedUnits; | ||||||
|     fOnCheckPackageDependency: TCheckUnitEvent; |     fOnCheckPackageDependency: TCheckUnitEvent; | ||||||
|     fOnCheckUnitForConversion: TCheckUnitEvent; |     fOnCheckUnitForConversion: TCheckUnitEvent; | ||||||
|  |     function HasUnit(aUnitName: string): Boolean; | ||||||
|  |     procedure MaybeOpenPackage(aUnitName: string); | ||||||
|     function GetMissingUnitCount: integer; |     function GetMissingUnitCount: integer; | ||||||
|   public |   public | ||||||
|     constructor Create(ACTLink: TCodeToolLink; AFilename: string); |     constructor Create(ACTLink: TCodeToolLink; AFilename: string); | ||||||
| @ -147,17 +149,16 @@ type | |||||||
| implementation | implementation | ||||||
| 
 | 
 | ||||||
| function Join(AList: TStringList): string; | function Join(AList: TStringList): string; | ||||||
| // Used in AddDelphiAndLCLSections. Could be moved to a more generic place. | // Make a comma separated list from a StringList. Could be moved to a more generic place. | ||||||
| var | var | ||||||
|   i: Integer; |   i: Integer; | ||||||
| begin | begin | ||||||
|   Result:=''; |   Result:=''; | ||||||
|   for i:=0 to AList.Count-1 do begin |   for i:=0 to AList.Count-1 do | ||||||
|     if i<AList.Count-1 then |     if i<AList.Count-1 then | ||||||
|       Result:=Result+AList[i]+', ' |       Result:=Result+AList[i]+', ' | ||||||
|     else |     else | ||||||
|       Result:=Result+AList[i]; |       Result:=Result+AList[i]; | ||||||
|   end; |  | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| { TUsedUnits } | { TUsedUnits } | ||||||
| @ -177,6 +178,7 @@ begin | |||||||
|   fUnitsToRenameKeys.CaseSensitive:=false; |   fUnitsToRenameKeys.CaseSensitive:=false; | ||||||
|   fUnitsToRenameVals:=TStringList.Create; |   fUnitsToRenameVals:=TStringList.Create; | ||||||
|   fUnitsToRenameVals.CaseSensitive:=false; |   fUnitsToRenameVals.CaseSensitive:=false; | ||||||
|  |   fUnitsToRenameVals.Sorted:=True; | ||||||
|   fUnitsToFixCase:=TStringToStringTree.Create(true); |   fUnitsToFixCase:=TStringToStringTree.Create(true); | ||||||
|   fUnitsToComment:=TStringList.Create; |   fUnitsToComment:=TStringList.Create; | ||||||
|   fMissingUnits:=TStringList.Create; |   fMissingUnits:=TStringList.Create; | ||||||
| @ -280,24 +282,38 @@ end; | |||||||
| procedure TUsedUnits.ToBeRenamedOrRemoved(AOldName, ANewName: string); | procedure TUsedUnits.ToBeRenamedOrRemoved(AOldName, ANewName: string); | ||||||
| // Replace a unit name with a new name or remove it if there is no new name. | // Replace a unit name with a new name or remove it if there is no new name. | ||||||
| var | var | ||||||
|   UnitInFileName: string; |   sl: TStringList; | ||||||
|  |   WillRemove: Boolean; | ||||||
|   i: Integer; |   i: Integer; | ||||||
| begin | begin | ||||||
|   if ANewName<>'' then begin |   WillRemove:=ANewName=''; | ||||||
|     fUnitsToRename[AOldName]:=ANewName; |   if not WillRemove then begin | ||||||
|     fUnitsToRenameKeys.Add(AOldName); |     // ANewName can have comma separated list of units. Use only units that don't yet exist. | ||||||
|     fUnitsToRenameVals.Add(ANewName); |     sl:=TStringList.Create; | ||||||
|     fCTLink.Settings.AddLogLine(Format(lisConvDelphiReplacedUnitInUsesSection, |     try | ||||||
|                                        [AOldName, ANewName])); |       sl.Delimiter:=','; | ||||||
|     // If the unit is not found, open the package containing it. |       sl.DelimitedText:=ANewName; | ||||||
|     UnitInFileName:=''; |       for i:=sl.Count-1 downto 0 do begin | ||||||
|     if fCTLink.CodeTool.DirectoryCache.FindUnitSourceInCompletePath( |         if fOwnerTool.HasUnit(sl[i]) then | ||||||
|                                     ANewName,UnitInFileName,True,False) = '' then |           sl.Delete(i) | ||||||
|       if Assigned(fOwnerTool.OnCheckPackageDependency) then |         else | ||||||
|         if not fOwnerTool.OnCheckPackageDependency(ANewName) then |           fOwnerTool.MaybeOpenPackage(sl[i]); | ||||||
|           ; |       end; | ||||||
|   end |       WillRemove:=sl.Count=0; | ||||||
|   else begin |       if not WillRemove then begin | ||||||
|  |         // At least some new units will be used | ||||||
|  |         ANewName:=Join(sl); | ||||||
|  |         fUnitsToRename[AOldName]:=ANewName; | ||||||
|  |         fUnitsToRenameKeys.Add(AOldName); | ||||||
|  |         fUnitsToRenameVals.AddStrings(sl); | ||||||
|  |         fCTLink.Settings.AddLogLine(Format(lisConvDelphiReplacedUnitInUsesSection, | ||||||
|  |                                            [AOldName, ANewName])); | ||||||
|  |       end; | ||||||
|  |     finally | ||||||
|  |       sl.Free; | ||||||
|  |     end; | ||||||
|  |   end; | ||||||
|  |   if WillRemove then begin | ||||||
|     i:=Pos(' in ',AOldName); |     i:=Pos(' in ',AOldName); | ||||||
|     if i>1 then |     if i>1 then | ||||||
|       AOldName:=Copy(AOldName, 1, i-1);  // Strip the file name part. |       AOldName:=Copy(AOldName, 1, i-1);  // Strip the file name part. | ||||||
| @ -605,6 +621,30 @@ begin | |||||||
|   end; |   end; | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
|  | function TUsedUnitsTool.HasUnit(aUnitName: string): Boolean; | ||||||
|  | // Return True if a given unit already is used or will be used later. | ||||||
|  | var | ||||||
|  |   x: Integer; | ||||||
|  | begin | ||||||
|  |   Result := fMainUsedUnits.fExistingUnits.Find(aUnitName, x) | ||||||
|  |          or fImplUsedUnits.fExistingUnits.Find(aUnitName, x) | ||||||
|  |          or(fMainUsedUnits.fUnitsToAdd.IndexOf(aUnitName) > -1) | ||||||
|  |          or fMainUsedUnits.fUnitsToRenameVals.Find(aUnitName, x) | ||||||
|  |          or fImplUsedUnits.fUnitsToRenameVals.Find(aUnitName, x); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
|  | procedure TUsedUnitsTool.MaybeOpenPackage(aUnitName: string); | ||||||
|  | // Open a package containing a unit. Called when the unit is not found. | ||||||
|  | var | ||||||
|  |   s: String; | ||||||
|  | begin | ||||||
|  |   s:=''; | ||||||
|  |   if fCTLink.CodeTool.DirectoryCache.FindUnitSourceInCompletePath(aUnitName,s,True) = '' then | ||||||
|  |     if Assigned(fOnCheckPackageDependency) then | ||||||
|  |       if not fOnCheckPackageDependency(aUnitName) then | ||||||
|  |         ; | ||||||
|  | end; | ||||||
|  | 
 | ||||||
| function TUsedUnitsTool.ConvertUsed: TModalResult; | function TUsedUnitsTool.ConvertUsed: TModalResult; | ||||||
| // Add, remove, rename and comment out unit names that were marked earlier. | // Add, remove, rename and comment out unit names that were marked earlier. | ||||||
| var | var | ||||||
| @ -721,38 +761,11 @@ begin | |||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure TUsedUnitsTool.AddUnitIfNeeded(aUnitName: string); | procedure TUsedUnitsTool.AddUnitIfNeeded(aUnitName: string); | ||||||
| 
 |  | ||||||
|   // Return True if the rename (target) value contains aUnitName. |  | ||||||
|   // The rename value can have many comma separated unit names. |  | ||||||
|   function RenameValHasUnit(aUsedUnits: TUsedUnits): Boolean; |  | ||||||
|   var |  | ||||||
|     i: Integer; |  | ||||||
|   begin |  | ||||||
|     Result := False; |  | ||||||
|     for i := 0 to aUsedUnits.fUnitsToRenameVals.Count-1 do |  | ||||||
|       if Pos(aUnitName, aUsedUnits.fUnitsToRenameVals[i]) > 0 then |  | ||||||
|         Exit(True); |  | ||||||
|   end; |  | ||||||
| 
 |  | ||||||
| var |  | ||||||
|   UnitInFileName: String; |  | ||||||
|   x: Integer; |  | ||||||
| begin | begin | ||||||
|   if not ( fMainUsedUnits.fExistingUnits.Find(aUnitName, x) |   if not HasUnit(aUnitName) then begin | ||||||
|         or fImplUsedUnits.fExistingUnits.Find(aUnitName, x) |  | ||||||
|         or (fMainUsedUnits.fUnitsToAdd.IndexOf(aUnitName) > -1) |  | ||||||
|         or RenameValHasUnit(fMainUsedUnits) |  | ||||||
|         or RenameValHasUnit(fImplUsedUnits) ) then |  | ||||||
|   begin |  | ||||||
|     fMainUsedUnits.fUnitsToAdd.Add(aUnitName); |     fMainUsedUnits.fUnitsToAdd.Add(aUnitName); | ||||||
|     fCTLink.Settings.AddLogLine('Added unit '+aUnitName+ ' to uses section'); |     fCTLink.Settings.AddLogLine('Added unit '+aUnitName+ ' to uses section'); | ||||||
|     // If the unit is not found, open the package containing it. |     MaybeOpenPackage(aUnitName); | ||||||
|     UnitInFileName:=''; |  | ||||||
|     if fCTLink.CodeTool.DirectoryCache.FindUnitSourceInCompletePath( |  | ||||||
|                                    aUnitName,UnitInFileName,True,False) = '' then |  | ||||||
|       if Assigned(fOnCheckPackageDependency) then |  | ||||||
|         if not fOnCheckPackageDependency(aUnitName) then |  | ||||||
|           ; |  | ||||||
|   end; |   end; | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 juha
						juha