ide: save file as: check for pascal keyword in unit name and suggest retry or ampersand

This commit is contained in:
mattias 2025-02-10 16:04:00 +01:00
parent 838a85bc8c
commit 87d27af233
3 changed files with 27 additions and 2 deletions

View File

@ -559,7 +559,7 @@ type
function GatherOverloads(Code: TCodeBuffer; X,Y: integer;
out Graph: TDeclarationOverloadsGraph): boolean;
// find references, rename identifier, remove identifier
// find references, rename identifier, remove identifier. For unit/program name see FindSourceNameReferences
function FindReferences(IdentifierCode: TCodeBuffer;
X, Y: integer; SearchInCode: TCodeBuffer; SkipComments: boolean;
var ListOfPCodeXYPosition: TFPList;

View File

@ -769,6 +769,8 @@ resourcestring
lisisAnInvalidProjectNamePleaseChooseAnotherEGProject = '"%s" is an '
+'invalid project name.%sPlease choose another (e.g. project1.lpi)';
lisChooseADifferentName = 'Choose a different name';
lisUseInstead = 'Use "%s" instead';
lisUseAnyway = 'Use "%s" anyway';
lisTheProjectInfoFileIsEqualToTheProjectMainSource = 'The project info '
+'file "%s"%sis equal to the project main source file!';
lisUnitIdentifierExists = 'Unit identifier exists';
@ -1076,6 +1078,7 @@ resourcestring
lisForceRenaming = 'Force renaming';
lisCancelRenaming = 'Cancel renaming';
lisInvalidPascalIdentifierCap = 'Invalid Pascal Identifier';
lisTheNameContainsAPascalKeyword = 'The name "%s" contains a Pascal keyword.';
lisInvalidPascalIdentifierName = 'The name "%s" is not a valid Pascal identifier.'
+'%sUse it anyway?';

View File

@ -4905,7 +4905,8 @@ var
NewFilename, NewFileExt: string;
OldUnitName, NewUnitName: string;
ACaption, AText, APath: string;
Filter, AllEditorExt, AllFilter: string;
Filter, AllEditorExt, AllFilter, AmpUnitname: string;
r: integer;
begin
if (AnUnitInfo<>nil) and (AnUnitInfo.OpenEditorInfoCount>0) then
SrcEdit := TSourceEditor(AnUnitInfo.OpenEditorInfo[0].EditorComponent)
@ -5019,6 +5020,7 @@ begin
// Is it a valid name? Ask user.
if not IsValidUnitName(NewUnitName) then
begin
// it is not valid name -> Ask user
Result:=IDEQuestionDialogAb(lisInvalidPascalIdentifierCap,
Format(lisInvalidPascalIdentifierName,[NewUnitName,LineEnding]),
mtConfirmation, [mrIgnore, lisSave,
@ -5029,7 +5031,27 @@ begin
continue;
if Result in [mrCancel,mrAbort] then
exit;
end else if CodeToolBoss.IdentifierHasKeywords(NewUnitName,
ExtractFilePath(NewFilename),AmpUnitname)
then begin
// contains keywords -> Suggest to ampersand it
Result:=TaskDlg(lisInvalidPascalIdentifierCap,
Format(lisTheNameContainsAPascalKeyword, [NewUnitName]), '',
tdiWarning,[mbOk,mbCancel],mbOk,
[lisChooseADifferentName,
Format(lisUseInstead, [StringReplace(AmpUnitname,'&','&&',[rfReplaceAll])]),
Format(lisUseAnyway, [NewUnitName])], r);
if Result<>mrOk then
exit(mrCancel);
case r of
1: NewUnitName:=AmpUnitname;
2: ;
else
Result:=mrRetry;
continue; // retry
end;
end;
// Does the project already have such unit?
if Project1.IndexOfUnitWithName(NewUnitName,true,AnUnitInfo)>=0 then
begin