Stephane
Stephane

Reputation: 3402

Delphi: Wrong unit added unit to uses clause of source file when component is placed on a form

I have a weird situation with a component of mine: I cannot get the Delphi IDE to add the correct units in the source code when the component is placed on a form. Not only are the wrong units included but the right ones (which I attempted to add using a TSelectionEditor descendant) not included in some cases.

The components are supposed to be a replacement for the standard TOpenDialog/TSaveDialog component. They are actually proxy component that will instantiate the correct type (either the base dialog or a custom made one) based on some global configuration.

I have written several units implementing all the necessary classes and components, some of them should included everywhere the component is used:

The registration of the components is placed in a unit called 'git.dialogs.registration.pas'. It contains a TSelectionEditor descendant that overrides the RequiresUnits method as follow:

uses System.Classes, GIT.Dialogs, DesignEditors, DesignIntf;

type
  TGITDialogSelectionEditor = class(TSelectionEditor)
  public
    procedure RequiresUnits(Proc: TGetStrProc); override;
  end;

procedure register;
begin
  RegisterComponents('GIT Dialogs', [GIT.Dialogs.TGITFileOpenDialogCpn, GIT.Dialogs.TGITFileSaveDialogCpn]);
  RegisterSelectionEditor(GIT.Dialogs.TGITFileOpenDialogCpn, TGITDialogSelectionEditor);
  RegisterSelectionEditor(GIT.Dialogs.TGITFileSaveDialogCpn, TGITDialogSelectionEditor);
end;

{ TGITDialogSelectionEditor }

procedure TGITDialogSelectionEditor.RequiresUnits(Proc: TGetStrProc);
begin
  inherited;
  Proc('GIT.Dialogs');
  Proc('GIT.Dialogs.Interfaces');
end;

That class is then registered in the usual register procedure using RegisterSelectionEditor.

In theory, when I drop one of these components on a form, the IDE shoudl automatically add the GIT.Dialogs and GIT.Dialogs.Interfaces units to the interface uses clause of the source file. Yet, it does not:

How can I fix this situation? The adding of the GIT.Dialogs.FileDialog.Base unit isn't much of a problem since it's going to be linked anyway anytime the component is used but the fact that the GIT.Dialogs file isn't added require a manual fix of every form where the component is dropped or used.

Upvotes: 2

Views: 468

Answers (1)

NGLN
NGLN

Reputation: 43649

Specify the default VCL units fully, i.e. Dialogs should be specified as Vcl.Dialogs. Not only will this resolve this specific issue when dropping such a component on a form, but it will help eliminate other problems as well. Get accustomed to use fully qualified unit names.

For existing forms, there is no other way then to rewrite their unit clauses. A multi document search and replace tool could be handy then.

Upvotes: 1

Related Questions