Merge branch 'Cody/Dictionary/Refac' into 'main'

Cody/Dictionary: Refactoring

See merge request freepascal.org/lazarus/lazarus!497
This commit is contained in:
Mattias Gaertner 2025-06-21 07:40:12 +00:00
commit f99b581e24
2 changed files with 75 additions and 82 deletions

View File

@ -796,8 +796,8 @@ end;
constructor TCodyUnitDictionary.Create; constructor TCodyUnitDictionary.Create;
begin begin
inherited Create; inherited Create;
FSaveIntervalInS:=60*3; // every 3 minutes FSaveIntervalInS:=cDefSaveIntervalInS;
FLoadAfterStartInS:=3; FLoadAfterStartInS:=cDefLoadDelayInS;
InitCriticalSection(fCritSec); InitCriticalSection(fCritSec);
fQueuedTools:=TAVLTree.Create; fQueuedTools:=TAVLTree.Create;
CodeToolBoss.AddHandlerToolTreeChanging(@ToolTreeChanged); CodeToolBoss.AddHandlerToolTreeChanging(@ToolTreeChanged);
@ -879,49 +879,49 @@ end;
procedure TCodyIdentifiersDlg.DeletePackageClick(Sender: TObject); procedure TCodyIdentifiersDlg.DeletePackageClick(Sender: TObject);
var var
Identifier: string; lId: TCodyIdentifier;
UnitFilename: string; lMsg: string;
GroupName: string; lGroup: TUDUnitGroup;
GroupFilename: string;
Group: TUDUnitGroup;
s: String;
begin begin
if not FindSelectedItem(Identifier, UnitFilename, GroupName, GroupFilename) lId := FindSelectedIdentifier;
then exit; if lId = nil then exit;
if GroupFilename='' then exit; if lId.GroupFile = '' then exit;
s:=Format(crsReallyDeleteThePackageFromTheDatabaseNoteThisDoe, [#13, #13,
#13, GroupFilename]); // confirm
if IDEMessageDialog(crsDeletePackage, s, mtConfirmation, [mbYes, mbNo], '')<> lMsg := Format(crsReallyDeleteThePackageFromTheDatabaseNoteThisDoe,
mrYes [LineEnding, LineEnding, LineEnding, lId.GroupFile]);
then exit; if IDEMessageDialog(crsDeletePackage, lMsg, mtConfirmation, mbYesNo) <> mrYes then exit;
Group:=CodyUnitDictionary.FindGroupWithFilename(GroupFilename);
if Group=nil then exit; // delete
CodyUnitDictionary.DeleteGroup(Group,true); lGroup := CodyUnitDictionary.FindGroupWithFilename(lId.GroupFile);
if lGroup = nil then exit;
CodyUnitDictionary.DeleteGroup(lGroup, true);
UpdateGeneralInfo; UpdateGeneralInfo;
UpdateItemsList; UpdateItemsList;
end; end;
procedure TCodyIdentifiersDlg.DeleteUnitClick(Sender: TObject); procedure TCodyIdentifiersDlg.DeleteUnitClick(Sender: TObject);
var var
Identifier: string; lId: TCodyIdentifier;
UnitFilename: string; lMsg: string;
GroupName: string; lUnit: TUDUnit;
GroupFilename: string;
CurUnit: TUDUnit;
s: String;
begin begin
if not FindSelectedItem(Identifier, UnitFilename, GroupName, GroupFilename) lId := FindSelectedIdentifier;
then exit; if lId = nil then exit;
s:=Format(crsReallyDeleteTheUnitFromTheDatabaseNoteThisDoesNo, [#13, #13,
#13, UnitFilename]); // confirm
if GroupFilename<>'' then lMsg := Format(crsReallyDeleteTheUnitFromTheDatabaseNoteThisDoesNo,
s+=#13+Format(crsIn, [GroupFilename]); [LineEnding, LineEnding, LineEnding, lId.UnitFile]);
if IDEMessageDialog(crsDeleteUnit, s, mtConfirmation, [mbYes, mbNo], '')<> if lId.GroupFile <> '' then
mrYes lMsg := lMsg + LineEnding + Format(crsIn, [lId.GroupFile]);
then exit; if IDEMessageDialog(crsDeleteUnit, lMsg, mtConfirmation, mbYesNo) <> mrYes then exit;
CurUnit:=CodyUnitDictionary.FindUnitWithFilename(UnitFilename);
if CurUnit=nil then exit; // delete
CodyUnitDictionary.DeleteUnit(CurUnit,true); lUnit := CodyUnitDictionary.FindUnitWithFilename(lId.UnitFile);
if lUnit = nil then exit;
CodyUnitDictionary.DeleteUnit(lUnit, true);
UpdateGeneralInfo; UpdateGeneralInfo;
UpdateItemsList; UpdateItemsList;
end; end;
@ -983,7 +983,7 @@ begin
ButtonPanel1.HelpButton.OnClick:=@ButtonPanel1HelpButtonClick; ButtonPanel1.HelpButton.OnClick:=@ButtonPanel1HelpButtonClick;
ButtonPanel1.OKButton.Caption:=crsUseIdentifier; ButtonPanel1.OKButton.Caption:=crsUseIdentifier;
ButtonPanel1.OKButton.OnClick:=@UseIdentifierClick; ButtonPanel1.OKButton.OnClick:=@UseIdentifierClick;
FMaxItems:=40; FMaxItems:=cDefMaxListItems;
FilterEdit.TextHint:=crsFilter; FilterEdit.TextHint:=crsFilter;
FItems:=TObjectList.Create; FItems:=TObjectList.Create;
HideOtherProjectsCheckBox.Checked:=true; HideOtherProjectsCheckBox.Checked:=true;
@ -1352,14 +1352,11 @@ begin
end; end;
function TCodyIdentifiersDlg.FindSelectedIdentifier: TCodyIdentifier; function TCodyIdentifiersDlg.FindSelectedIdentifier: TCodyIdentifier;
var
i: Integer;
begin begin
Result:=nil; if assigned(FItems) and InRange(ItemsListBox.ItemIndex, 0, FItems.Count - 1) then
if FItems=nil then exit; result := TCodyIdentifier(FItems[ItemsListBox.ItemIndex])
i:=ItemsListBox.ItemIndex; else
if (i<0) or (i>=FItems.Count) then exit; result := nil;
Result:=TCodyIdentifier(FItems[i]);
end; end;
function TCodyIdentifiersDlg.FindSelectedItem(out Identifier, UnitFilename, function TCodyIdentifiersDlg.FindSelectedItem(out Identifier, UnitFilename,
@ -1367,19 +1364,21 @@ function TCodyIdentifiersDlg.FindSelectedItem(out Identifier, UnitFilename,
var var
Item: TCodyIdentifier; Item: TCodyIdentifier;
begin begin
Result:=false; Item := FindSelectedIdentifier;
Identifier:=''; result := assigned(Item);
UnitFilename:=''; if result then
GroupName:=''; begin
GroupFilename:=''; Identifier := Item.Identifier;
Item:=FindSelectedIdentifier; UnitFilename := Item.UnitFile;
if Item=nil then exit; GroupName := Item.GroupName;
Identifier:=Item.Identifier; GroupFilename := Item.GroupFile;
UnitFilename:=Item.UnitFile; end else begin
GroupName:=Item.GroupName; Identifier := '';
GroupFilename:=Item.GroupFile; UnitFilename := '';
GroupName := '';
GroupFilename := '';
end;
//debugln(['TCodyIdentifiersDlg.FindSelectedItem ',Identifier,' Unit=',UnitFilename,' Pkg=',GroupFilename]); //debugln(['TCodyIdentifiersDlg.FindSelectedItem ',Identifier,' Unit=',UnitFilename,' Pkg=',GroupFilename]);
Result:=true;
end; end;
function TCodyIdentifiersDlg.Init: boolean; function TCodyIdentifiersDlg.Init: boolean;

View File

@ -15,12 +15,16 @@ uses
const const
CodyConfigVersion = 1; CodyConfigVersion = 1;
const
cDefLoadDelayInS = 10;
cDefSaveIntervalInS = 600;
cDefMaxListItems = 50;
var var
CodyMiscOptionID: integer = 1000; CodyMiscOptionID: integer = 1000;
type type
{ TCodyMiscOptions }
TCodyMiscOptions = class(TPersistent) TCodyMiscOptions = class(TPersistent)
private private
FChangeStep: integer; FChangeStep: integer;
@ -61,8 +65,6 @@ var
implementation implementation
{ TCodyMiscOptions }
procedure TCodyMiscOptions.SetModified(AValue: boolean); procedure TCodyMiscOptions.SetModified(AValue: boolean);
begin begin
if AValue then if AValue then
@ -104,29 +106,21 @@ begin
end; end;
procedure TCodyMiscOptions.Assign(Source: TPersistent); procedure TCodyMiscOptions.Assign(Source: TPersistent);
var
aSource: TCodyMiscOptions;
begin begin
if Source is TCodyMiscOptions then if Source is TCodyMiscOptions then
begin begin
aSource:=TCodyMiscOptions(Source); UDSaveIntervalInS := TCodyMiscOptions(Source).UDSaveIntervalInS;
UDSaveIntervalInS:=aSource.UDSaveIntervalInS; UDLoadDelayInS := TCodyMiscOptions(Source).UDLoadDelayInS;
UDLoadDelayInS:=aSource.UDLoadDelayInS;
end else end else
inherited Assign(Source); inherited Assign(Source);
end; end;
function TCodyMiscOptions.Equals(Obj: TObject): boolean; function TCodyMiscOptions.Equals(Obj: TObject): boolean;
var
Src: TCodyMiscOptions;
begin begin
Result:=false; Result :=
if not (Obj is TCodyMiscOptions) then exit; (Obj is TCodyMiscOptions) and // "is" also checks for nil
Src:=TCodyMiscOptions(Obj); (UDLoadDelayInS = TCodyMiscOptions(Obj).UDLoadDelayInS ) and
if (UDLoadDelayInS<>Src.UDLoadDelayInS) (UDSaveIntervalInS = TCodyMiscOptions(Obj).UDSaveIntervalInS);
or (UDSaveIntervalInS<>Src.UDSaveIntervalInS)
then exit;
Result:=true;
end; end;
procedure TCodyMiscOptions.SaveSafe; procedure TCodyMiscOptions.SaveSafe;
@ -157,8 +151,8 @@ var
begin begin
Cfg:=GetIDEConfigStorage(Filename,false); Cfg:=GetIDEConfigStorage(Filename,false);
try try
Cfg.SetDeleteValue('UnitDictionary/LoadDelay',UDLoadDelayInS,10); Cfg.SetDeleteValue('UnitDictionary/LoadDelay', UDLoadDelayInS, cDefLoadDelayInS);
Cfg.SetDeleteValue('UnitDictionary/SaveInterval',UDSaveIntervalInS,600); Cfg.SetDeleteValue('UnitDictionary/SaveInterval',UDSaveIntervalInS,cDefSaveIntervalInS);
Cfg.SetDeleteValue('Uses/PreferImplementationSection',PreferImplementationUsesSection,false); Cfg.SetDeleteValue('Uses/PreferImplementationSection',PreferImplementationUsesSection,false);
finally finally
Cfg.Free; Cfg.Free;
@ -172,8 +166,8 @@ begin
Clear; Clear;
Cfg:=GetIDEConfigStorage(Filename,true); Cfg:=GetIDEConfigStorage(Filename,true);
try try
UDLoadDelayInS:=Cfg.GetValue('UnitDictionary/LoadDelay',10); UDLoadDelayInS :=Cfg.GetValue('UnitDictionary/LoadDelay', cDefLoadDelayInS);
UDSaveIntervalInS:=Cfg.GetValue('UnitDictionary/SaveInterval',600); UDSaveIntervalInS:=Cfg.GetValue('UnitDictionary/SaveInterval',cDefSaveIntervalInS);
PreferImplementationUsesSection:=Cfg.GetValue('Uses/PreferImplementationSection',false); PreferImplementationUsesSection:=Cfg.GetValue('Uses/PreferImplementationSection',false);
//debugln(['TCodyMiscOptions.LoadFromFile UDSaveIntervalInS=',UDSaveIntervalInS,' LoadDelay=',UDLoadDelayInS]); //debugln(['TCodyMiscOptions.LoadFromFile UDSaveIntervalInS=',UDSaveIntervalInS,' LoadDelay=',UDLoadDelayInS]);
finally finally
@ -183,8 +177,8 @@ end;
procedure TCodyMiscOptions.Clear; procedure TCodyMiscOptions.Clear;
begin begin
UDLoadDelayInS:=10; UDLoadDelayInS := cDefLoadDelayInS;
UDSaveIntervalInS:=600; UDSaveIntervalInS := cDefSaveIntervalInS;
end; end;
procedure TCodyMiscOptions.Apply; procedure TCodyMiscOptions.Apply;