這個(gè)實(shí)例將實(shí)現(xiàn)以下功能:
1、在tools菜單增加“AI助力手”,并設(shè)置相應(yīng)的圖標(biāo)
2、讀取代碼編輯器光標(biāo)行內(nèi)容,并傳給插件的Synedit1
3、實(shí)現(xiàn)停靠窗口
參考官方的文檔
Extending the IDE/zh CN - Free Pascal wiki
第一步:
1、軟件包-->新建軟件包
1、在tools菜單增加“AI助力手”,并設(shè)置相應(yīng)的圖標(biāo)
2、讀取代碼編輯器光標(biāo)行內(nèi)容,并傳給插件的Synedit1
3、實(shí)現(xiàn)停靠窗口
參考官方的文檔
Extending the IDE/zh CN - Free Pascal wiki
第一步:
1、軟件包-->新建軟件包

2、輸入插件的名稱

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

4、選擇“可停靠的IDE窗口”

5、輸入“Form.name”和“菜單項(xiàng)目標(biāo)題”名稱


注意事項(xiàng):
注冊(cè)停靠窗口時(shí)的唯一標(biāo)識(shí)不能與form同名
MyPkgAICreator:=IDEWindowCreators.Add( 'MyPkgAI1',//唯一標(biāo)識(shí) 注意:這個(gè)名稱不能與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; //取光標(biāo)當(dāng)前行和列 frm:=IDEWindowCreators.ShowForm(MyPkgAICreator.FormName,true); TMyPkgAI(frm).SynEdit1.Clear; //讀當(dāng)前行的信息并賦值給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); // 注冊(cè)快捷鍵 ToolsMyPkgAICommand:=RegisterIDECommand(CmdToolsMenu, 'ToolsMyPkgAI', MenuItemCaption, IDEShortCut(VK_F6, [ssCtrl]), // <- 設(shè)置快捷鍵 Ctrl+F6 CleanIDEShortCut, nil, @ShowMyPkgAI); //注冊(cè)在Tools菜單 MenuCommand:=RegisterIDEMenuCommand(itmSecondaryTools, 'ToolsMyPkgAI', MenuItemCaption, nil, nil, ToolsMyPkgAICommand); //IDEImages需在uses添加IDEImagesIntf單元 //將菜單的圖標(biāo)設(shè)置"menu_view_code_explorer" MenuCommand.ImageIndex:=IDEImages.LoadImage('menu_view_code_explorer'); //注冊(cè)停靠窗口 MyPkgAICreator:=IDEWindowCreators.Add( 'MyPkgAI1',//唯一標(biāo)識(shí) 注意:這個(gè)名稱不能與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.





浙公網(wǎng)安備 33010602011771號(hào)