IDE: new form: avoid unit names of projects and packages

git-svn-id: trunk@35023 -
This commit is contained in:
mattias 2012-01-29 18:38:25 +00:00
parent 44897bf607
commit 3543c6f1d0
3 changed files with 72 additions and 42 deletions

View File

@ -32,7 +32,9 @@ ToDos:
- fpc messages:
- wiki:
- fpc compiler options:
- IDE dialogs / wiki:
download content, without wiki links like upload, history, ...
- how to build lcl.chm?

View File

@ -714,6 +714,7 @@ type
function CreateNewForm(NewUnitInfo: TUnitInfo;
AncestorType: TPersistentClass; ResourceCode: TCodeBuffer;
UseCreateFormStatements, DisableAutoSize: Boolean): TModalResult;
function NewUniqueComponentName(Prefix: string): string;
// methods for 'save unit'
function DoShowSaveFileAsDialog(var AFilename: string; AnUnitInfo: TUnitInfo;
@ -5267,6 +5268,72 @@ begin
Result:=mrOk;
end;
function TMainIDE.NewUniqueComponentName(Prefix: string): string;
function SearchProject(AProject: TProject; const Identifier: string): boolean;
var
i: Integer;
AnUnitInfo: TUnitInfo;
begin
if AProject=nil then exit(false);
Result:=true;
for i:=0 to AProject.UnitCount-1 do
begin
AnUnitInfo:=AProject.Units[i];
if (AnUnitInfo.Component<>nil) then begin
if CompareText(AnUnitInfo.Component.Name,Identifier)=0 then exit;
if CompareText(AnUnitInfo.Component.ClassName,Identifier)=0 then exit;
end else if (AnUnitInfo.ComponentName<>'')
and ((AnUnitInfo.IsPartOfProject) or AnUnitInfo.Loaded) then begin
if SysUtils.CompareText(AnUnitInfo.Unit_Name,Identifier)=0 then exit;
if SysUtils.CompareText(AnUnitInfo.ComponentName,Identifier)=0 then exit;
end;
end;
Result:=true;
end;
function SearchPackage(APackage: TLazPackage; const Identifier: string): boolean;
var
i: Integer;
PkgFile: TPkgFile;
begin
if APackage=nil then exit(false);
Result:=true;
if SysUtils.CompareText(APackage.Name,Identifier)=0 then exit;
for i:=0 to APackage.FileCount-1 do
begin
PkgFile:=APackage.Files[i];
if SysUtils.CompareText(PkgFile.Unit_Name,Identifier)=0 then exit;
end;
Result:=false;
end;
function IdentifierExists(Identifier: string): boolean;
var
i: Integer;
begin
Result:=true;
if GetClass(Identifier)<>nil then exit;
if SearchProject(Project1,Identifier) then exit;
for i:=0 to PackageGraph.Count-1 do
if SearchPackage(PackageGraph[i],Identifier) then exit;
Result:=false;
end;
var
i: Integer;
begin
while (Prefix<>'') and (Prefix[length(Prefix)] in ['0'..'9']) do
System.Delete(Prefix,length(Prefix),1);
if (Prefix='') or (not IsValidIdent(Prefix)) then
Prefix:='Resource';
i:=0;
repeat
inc(i);
Result:=Prefix+IntToStr(i);
until (not IdentifierExists(Result)) and (not IdentifierExists('T'+Result));
end;
function TMainIDE.DoLoadResourceFile(AnUnitInfo: TUnitInfo;
var LFMCode, ResourceCode: TCodeBuffer;
IgnoreSourceErrors, AutoCreateResourceCode, ShowAbort: boolean): TModalResult;
@ -8625,8 +8692,8 @@ begin
//debugln('TMainIDE.DoNewEditorFile A nfCreateDefaultSrc=',nfCreateDefaultSrc in NewFlags,' ResourceClass=',dbgs(NewFileDescriptor.ResourceClass));
if nfCreateDefaultSrc in NewFlags then begin
if (NewFileDescriptor.ResourceClass<>nil) then begin
NewUnitInfo.ComponentName:=
AProject.NewUniqueComponentName(NewFileDescriptor.DefaultResourceName);
NewUnitInfo.ComponentName:=NewUniqueComponentName(
NewFileDescriptor.DefaultResourceName);
NewUnitInfo.ComponentResourceName:='';
end;
NewUnitInfo.CreateStartCode(NewFileDescriptor,NewUnitName);

View File

@ -909,7 +909,6 @@ type
function UnitCount:integer;
function GetFileCount: integer; override;
function NewUniqueUnitName(const AnUnitName: string): string;
function NewUniqueComponentName(const AComponentPrefix: string): string;
function NewUniqueFilename(const Filename: string): string;
procedure AddFile(ProjectFile: TLazProjectFile;
AddToProjectUsesClause: boolean); override;
@ -3972,44 +3971,6 @@ begin
until (not UnitNameExists(Result));
end;
function TProject.NewUniqueComponentName(const AComponentPrefix: string): string;
function FormComponentExists(const AComponentName: string): boolean;
var i: integer;
ComponentClassName: string;
begin
Result:=true;
if GetClass(AComponentName)<>nil then exit;
ComponentClassName:=ClassNameToComponentName(AComponentName);
for i:=0 to UnitCount-1 do begin
if (Units[i].Component<>nil) then begin
if CompareText(Units[i].Component.Name,AComponentName)=0 then exit;
if CompareText(Units[i].Component.ClassName,ComponentClassName)=0
then exit;
end else if (Units[i].ComponentName<>'')
and ((Units[i].IsPartOfProject) or (Units[i].Loaded)) then begin
if SysUtils.CompareText(Units[i].ComponentName,AComponentName)=0 then exit;
end;
end;
Result:=false;
end;
var
u: integer;
Prefix: string;
begin
Prefix:=AComponentPrefix;
while (Prefix<>'') and (Prefix[length(Prefix)] in ['0'..'9']) do
Prefix:=copy(Prefix,1,length(Prefix)-1);
if (Prefix='') or (not IsValidIdent(Prefix)) then
Prefix:='Resource';
u:=0;
repeat
inc(u);
Result:=Prefix+IntToStr(u);
until (not FormComponentExists(Result));
end;
function TProject.NewUniqueFilename(const Filename: string): string;
var
FileNameOnly: String;