I'm working on a project that currently has a hundred source files and will have several hundred more. Each of these files (units and include files) has a specific structure (header, default modules, etc.), which I also intend to use for each new file of a given type. Currently, I have created code templates for these two types of files, which I have to manually insert into each new fileand this is a pain.
Lazarus is extensible, so it's better to just add two more items to the dialog, available in the
File/New... menu. And here comes the problem, because I can't understand how it's supposed to look like. I used this article —
Extending the IDE: Add a new file type — but it seems to be outdated because some things just don't exist anymore and the template it provides doesn't work very well.
I created a new package and added the following test code to it, to register two new file types (custom unit and custom include file):
unit CustomFileTypes;
{$MODE OBJFPC}{$H+}
interface
uses
ProjectIntf;
type
TFileDescriptorCustomUnit = class(TProjectFileDescriptor)
public
constructor Create(); override;
function GetLocalizedName(): String; override;
function GetLocalizedDescription(): String; override;
end;
type
TFileDescriptorCustomInclude = class(TProjectFileDescriptor)
public
constructor Create(); override;
function GetLocalizedName(): String; override;
function GetLocalizedDescription(): String; override;
end;
procedure Register();
implementation
procedure Register();
begin
RegisterProjectFileDescriptor(TFileDescriptorCustomUnit.Create(), FileDescGroupName);
RegisterProjectFileDescriptor(TFileDescriptorCustomInclude.Create(), FileDescGroupName);
end;
constructor TFileDescriptorCustomUnit.Create();
begin
inherited Create();
Name := 'CustomUnit';
DefaultFilename := 'CustomUnit.pp';
end;
function TFileDescriptorCustomUnit.GetLocalizedName(): String;
begin
Result := 'Custom unit';
end;
function TFileDescriptorCustomUnit.GetLocalizedDescription(): String;
begin
Result := 'Create a new custom unit.';
end;
constructor TFileDescriptorCustomInclude.Create();
begin
inherited Create();
Name := 'CustomInclude';
DefaultFilename := 'CustomInclude.inc';
end;
function TFileDescriptorCustomInclude.GetLocalizedName(): String;
begin
Result := 'Custom include file';
end;
function TFileDescriptorCustomInclude.GetLocalizedDescription(): String;
begin
Result := 'Create a new custom include file.';
end;
end.
After installing the package and rebuilding Lazarus, these two new items are visible in the dialog box, in the branch
Module (see screenshot attached). So far I've apparently done everything right.
When I select
CustomInclude from this dialog, Lazarus creates a new file, attaches it to the project and opens it in the code editor — the name of the tab is
CustomInclude.inc, which is what it should be to be. The second screenshot in the attachments shows what the code editor and the Project Inspector window look like (properly).
Unfortunately, strange things happen with
CustomUnit. First, the name of the module in the Project Inspector window is
.pp instead of
CustomUnit.pp, second, the name of the tab in the code editor is
unit1 instead of
CustomUnit, and thirdly, the code editor does not treat this module as a Pascal unit, but as a plain text file (hence the editor's white background instead of black).
I know I didn't do everything necessary, but when I started adding my descriptor's text property settings to the package code, Lazarus after rebuild either didn't start at all, or crashed, or freezed for a long time. I'd rather not waste any more time guessing what to do — it's better to ask and do whatever it takes to make it work properly.
Surely something needs to be done to get
CustomUnit to create properly, because for now something is wrong or missing and crazy things are happening. After fixing the problem with custom units, I would like new files (both units and include files) to be created with specific content, determined by me (with the same one I currently have in the code templates). And in addition, if possible, I would like my two new file types to be in a separate branch, preferably at the top of the tree, instead of in the
Module branch.
Could someone help me with this? I will be very grateful for tips.