IDE: Simplify function ShowSaveProjectAsDialog. UseMainSourceFile parameter and sfSaveMainSourceAs flag are not needed.

This commit is contained in:
Juha 2022-11-19 23:04:26 +02:00
parent ecf49109b1
commit fa224fda62
2 changed files with 15 additions and 42 deletions

View File

@ -129,7 +129,6 @@ type
sfSaveNonProjectFiles,
sfDoNotSaveVirtualFiles,
sfCanAbort, // show 'Cancel all' button in error messages
sfSaveMainSourceAs, // on sfSaveAs use .lpr file instead of .lpi file
sfQuietUnitCheck // don't ask questions when adding unit dependency.
);
TSaveFlags = set of TSaveFlag;

View File

@ -298,7 +298,7 @@ function SaveEditorChangesToCodeCache(AEditor: TSourceEditorInterface): boolean;
function LoadIDECodeBuffer(var ACodeBuffer: TCodeBuffer;
const AFilename: string; Flags: TLoadBufferFlags; ShowAbort: boolean): TModalResult;
//save project
function ShowSaveProjectAsDialog(UseMainSourceFile: boolean): TModalResult;
function ShowSaveProjectAsDialog: TModalResult;
function SaveProjectInfo(var Flags: TSaveFlags): TModalResult;
procedure GetMainUnit(out MainUnitInfo: TUnitInfo; out MainUnitSrcEdit: TSourceEditor);
procedure SaveSrcEditorProjectSpecificSettings(AnEditorInfo: TUnitEditorInfo);
@ -2413,7 +2413,6 @@ var
OldUnitName, OldFilename: String;
NewUnitName, NewFilename: String;
WasVirtual, WasPascalSource, CanAbort, Confirm: Boolean;
SaveProjectFlags: TSaveFlags;
EMacro: TEditorMacro;
begin
Result:=mrCancel;
@ -2434,14 +2433,9 @@ begin
WasPascalSource:=FilenameIsPascalSource(AnUnitInfo.Filename);
// if this file is part of a virtual project then save the project first
if (not (sfProjectSaving in Flags)) and Project1.IsVirtual and AnUnitInfo.IsPartOfProject then
begin
SaveProjectFlags:=Flags*[sfSaveToTestDir];
if AnUnitInfo=Project1.MainUnitInfo then
Include(SaveProjectFlags,sfSaveMainSourceAs);
Result:=SaveProject(SaveProjectFlags);
exit;
end;
if (not (sfProjectSaving in Flags)) and Project1.IsVirtual and AnUnitInfo.IsPartOfProject
then
exit(SaveProject(Flags*[sfSaveToTestDir]));
// update codetools cache and collect Modified flags
if not (sfProjectSaving in Flags) then
@ -2478,10 +2472,8 @@ begin
// special cases (rare functions don't need front ends).
MainUnitInfo:=AnUnitInfo.Project.MainUnitInfo;
if (sfSaveAs in Flags) and (not (sfProjectSaving in Flags)) and (AnUnitInfo=MainUnitInfo)
then begin
Result:=SaveProject([sfSaveAs,sfSaveMainSourceAs]);
exit;
end;
then
exit(SaveProject([sfSaveAs]));
// if nothing modified then a simple Save can be skipped
//debugln(['SaveEditorFile A ',AnUnitInfo.Filename,' ',AnUnitInfo.NeedsSaveToDisk]);
@ -7468,12 +7460,12 @@ begin
Include(Flags,sfSaveAs);
if ([sfSaveAs,sfSaveToTestDir]*Flags=[sfSaveAs]) then begin
// let user choose a filename
Result := ShowSaveProjectAsDialog(sfSaveMainSourceAs in Flags);
Result := ShowSaveProjectAsDialog;
if Result<>mrOk then begin
debugln(['Info: (lazarus) [SaveProjectInfo] ShowSaveProjectAsDialog failed']);
exit;
end;
Flags:=Flags-[sfSaveAs,sfSaveMainSourceAs];
Exclude(Flags,sfSaveAs);
end;
// update HasResources information
@ -7586,7 +7578,7 @@ begin
end;
end;
function ShowSaveProjectAsDialog(UseMainSourceFile: boolean): TModalResult;
function ShowSaveProjectAsDialog: TModalResult;
var
MainUnitSrcEdit: TSourceEditor;
MainUnitInfo: TUnitInfo;
@ -7598,15 +7590,9 @@ var
AText, ACaption, Ext: string;
OldSourceCode, OldProjectDir, prDir: string;
begin
//DebugLn(['ShowSaveProjectAsDialog: UseMainSourceFile=', UseMainSourceFile]);
Project1.BeginUpdate(false);
try
OldProjectDir := Project1.Directory;
if Project1.IsVirtual then
UseMainSourceFile := False;
// ToDo: If this assertion never triggers, the code can be simplified a lot.
Assert(not UseMainSourceFile, 'ShowSaveProjectAsDialog: UseMainSourceFile is still on.');
// build a nice project info filename suggestion
if Assigned(Project1.MainUnitInfo) then
AFileName := Project1.MainUnitInfo.ReadUnitNameFromSource(false);
@ -7616,18 +7602,11 @@ begin
AFilename := Trim(Project1.GetTitle);
if AFilename = '' then
AFilename := 'Project1';
// Figure out a filename extension
Ext := ExtractFileExt(AFilename);
if UseMainSourceFile then
begin
if (Ext = '') or (not FilenameIsPascalSource(AFilename)) then
AFilename := ChangeFileExt(AFilename, '.pas');
end else begin
if (Ext = '') or FilenameIsPascalSource(AFilename) then
AFilename := ChangeFileExt(AFilename, '.lpi');
end;
Ext := ExtractFileExt(AFilename);
//DebugLn(['ShowSaveProjectAsDialog: 1. AFilename=',AFilename]);
// Filename extension
Assert((ExtractFileExt(AFilename)='') or FilenameIsPascalSource(AFilename),
'ShowSaveProjectAsDialog: '+AFilename+' is not Pascal source.');
Ext := '.lpi';
AFilename := ChangeFileExt(AFilename, Ext);
SaveDialog := IDESaveDialogClass.Create(nil);
try
@ -7652,7 +7631,6 @@ begin
if not SaveDialog.Execute then
exit; // user cancels
AFilename := ExpandFileNameUTF8(SaveDialog.FileName);
//DebugLn(['ShowSaveProjectAsDialog: 2. AFilename=',AFilename]);
// check program name
NewProgramName:=ExtractFileNameOnly(AFilename);
@ -7677,11 +7655,7 @@ begin
// check mainunit filename
Ext := ExtractFileExt(Project1.MainUnitInfo.Filename);
Assert(Ext<>'', 'ShowSaveProjectAsDialog: Ext is empty');
if UseMainSourceFile then
NewProgramFN := ExtractFileName(AFilename)
else
NewProgramFN := LCProgramName + Ext;
NewProgramFN := NewPath + NewProgramFN;
NewProgramFN := NewPath + LCProgramName + Ext;
if CompareFilenames(NewLPIFilename, NewProgramFN) = 0 then
begin
ACaption:=lisChooseADifferentName;