秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
这个实例将实现以下功能:
1、在tools菜单增加“AI助力手”,并设置相应的图标
2、读取代码编辑器光标行内容,并传给插件的Synedit1
3、实现停靠窗口

参考官方的文档
Extending the IDE/zh CN - Free Pascal wiki
第一步:
1、软件包-->新建软件包

 2、输入插件的名称


3、添加-->新建文件:

 

 

4、选择“可停靠的IDE窗口”

5、输入“Form.name”和“菜单项目标题”名称

 

注意事项:
注册停靠窗口时的唯一标识不能与form同名

  MyPkgAICreator:=IDEWindowCreators.Add(
    'MyPkgAI1',//唯一标识 注意:这个名称不能与form同名
    @CreateMyPkgAI, nil,
    '100', '100', '300', '300'  // default place at left=100, top=100, right=300, bottom=300
      // you can also define percentage values of screen or relative positions, see wiki
    );

下面红色代码是新增加的:

unit Unit3;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LazLoggerBase, FileUtil, Forms, Controls, Graphics, 
  Dialogs,
  SrcEditorIntf,IDEImagesIntf,
  LCLType, IDECommands, IDEWindowIntf, LazIDEIntf, MenuIntf, SynEdit;

type

  { TMyPkgAI }

  TMyPkgAI = class(TForm)
    SynEdit1: TSynEdit;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  MyPkgAI: TMyPkgAI;
  MyPkgAICreator: TIDEWindowCreator; // set by Register procedure

procedure ShowMyPkgAI(Sender: TObject);
procedure Register; // Check the "Register Unit" of this unit in the package editor.implementation

implementation

{$R *.lfm}

procedure ShowMyPkgAI(Sender: TObject);
var
  Editor:TSourceEditorInterface;
  TextPos:TPoint;
  frm:TCustomForm;
begin
  //激活源代码编辑器
  //SourceEditorManagerIntf需在uses添加SrcEditorIntf单元
  Editor:=SourceEditorManagerIntf.ActiveEditor;
  if Editor=nil then exit;
  TextPos:=Editor.CursorTextXY; //取光标当前行和列

  frm:=IDEWindowCreators.ShowForm(MyPkgAICreator.FormName,true);
  TMyPkgAI(frm).SynEdit1.Clear;
  //读当前行的信息并赋值给SynEdit1
  TMyPkgAI(frm).SynEdit1.Lines.Add(Editor.Lines[TextPos.Y-1]);
end;

procedure CreateMyPkgAI(Sender: TObject; aFormName: string;
  var AForm: TCustomForm; DoDisableAutoSizing: boolean);
begin
  // sanity check to avoid clashing with another package that has registered a window with the same name
  if CompareText(aFormName, 'MyPkgAI1')<>0 then begin
    DebugLn(['ERROR: CreateMyPkgAI: there is already a form with this name']);
    exit;
  end;
  IDEWindowCreators.CreateForm(AForm, TMyPkgAI, DoDisableAutoSizing,
    LazarusIDE.OwningComponent);
  AForm.Name:=aFormName;
  MyPkgAI:=AForm as TMyPkgAI;
end;

procedure Register;
var
  CmdToolsMenu: TIDECommandCategory;
  ToolsMyPkgAICommand: TIDECommand;
  MenuItemCaption: String;
  MenuCommand: TIDEMenuCommand;
begin
  //菜单名称
  MenuItemCaption:='我的AI助手';
  // search shortcut category
  CmdToolsMenu:=IDECommandList.FindCategoryByName(CommandCategoryToolMenuName);
  // 注册快捷键
  ToolsMyPkgAICommand:=RegisterIDECommand(CmdToolsMenu,
    'ToolsMyPkgAI',
    MenuItemCaption,
    IDEShortCut(VK_F6, [ssCtrl]), // <- 设置快捷键 Ctrl+F6
    CleanIDEShortCut, nil, @ShowMyPkgAI);

  //注册在Tools菜单
  MenuCommand:=RegisterIDEMenuCommand(itmSecondaryTools,
    'ToolsMyPkgAI',
    MenuItemCaption, nil, nil, ToolsMyPkgAICommand);

  //IDEImages需在uses添加IDEImagesIntf单元
  //将菜单的图标设置"menu_view_code_explorer"
  MenuCommand.ImageIndex:=IDEImages.LoadImage('menu_view_code_explorer');
  //注册停靠窗口
  MyPkgAICreator:=IDEWindowCreators.Add(
    'MyPkgAI1',//唯一标识 注意:这个名称不能与form同名
    @CreateMyPkgAI, nil,
    '100', '100', '300', '300'  // default place at left=100, top=100, right=300, bottom=300
      // you can also define percentage values of screen or relative positions, see wiki
    );
end;

end.








posted on 2025-06-20 09:23  秋·风  阅读(223)  评论(0)    收藏  举报