Forum > Packages and Libraries

Help - Developing an IDE Extension - Access Package from Menu

(1/1)

gcscaglia:
I'm trying to develop an IDE Extension for Lazarus (following https://wiki.lazarus.freepascal.org/Extending_the_IDE).

While there are examples of how to get the currently open project, all examples I've found for opening packages assume the developer knows the package name.

I'm trying to develop an extension that will be accessed via a menu item on the Package Editor form. I've succeeded in creating the menu item, as follows:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit UGenerateGitIgnore; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, MenuIntf; procedure WriteGitIgnoreForPackage(Sender: TObject); procedure Register; implementation procedure WriteGitIgnoreForPackage(Sender: TObject);beginend; procedure Register;begin  RegisterIDEMenuCommand(      PkgEditMenuSectionMisc,      'gitignore',      'Generate .gitignore',      nil,      @WriteGitIgnoreForPackage  );end; end. 
My current problem is, how do I know, inside the WriteGitIgnoreForPackage procedure, which package did the user clicked the menu item from?

For this particular use case what I really must know is where the .lpk file resides, but ideally I would be able to get an TIDEPackage reference.

I've already tried using
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---PackageEditingInterface.GetPackageOfEditorItem(Sender); (found this being done on ToDoDlg.pas, on lazarus/components/todolist) but this always returns nil.

gcscaglia:
To future readers:

Shame on me, reading lazarus/packager/pkgmanager.pas solved my problem:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TPkgManager.GetPackageOfEditorItem(Sender: TObject): TIDEPackage;begin  Result:=nil;  while (Sender is TMenuItem) and (TMenuItem(Sender).Parent<>nil) do    Sender:=TMenuItem(Sender).Parent;  if (Sender is TMenuItem) and (TMenuItem(Sender).Menu<>nil)  then    Sender:=TMenuItem(Sender).Menu;  if (Sender is TComponent) and (TComponent(Sender).Owner is TCustomForm) then    Sender:=TCustomForm(TComponent(Sender).Owner);  if Sender is TPackageEditorForm then    Result:=TPackageEditorForm(Sender).LazPackage;end; 
In other words: I need to pass a TComponent into PackageEditingInterface.GetPackageOfEditorItem. On my particular case, Sender was an TIDEMenuCommand instance. Modifying WriteGitIgnoreForPackage as follows worked for me:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure WriteGitIgnoreForPackage(Sender: TObject);var  lCmd: TComponent;  lPkg: TIDEPackage;begin  if (Sender is TIDEMenuCommand) then    lCmd := TIDEMenuCommand(Sender).MenuItem  else if (Sender is TComponent) then    lCmd := TComponent(Sender)  else    ShowMessage('Unsupported Sender!');   lPkg := PackageEditingInterface.GetPackageOfEditorItem(lCmd);  ShowMessage('Pkg: ' + lPkg.Name);end; 
I'm probably going to submit a patch for GetPackageOfEditorItem to check for TIDEMenuCommand as well.

Navigation

[0] Message Index

Go to full version