0%

代码编辑器开发

代码编辑器开发

vscode

中文文档 VS Code 插件开发中文文档

官方文档 vscode

环境配置

  1. node.js
  2. git
  3. Yeoman VS Code Extension Generator 项目生成
1
npm install -g yo generator-code
  1. vsce 打包插件
1
npm install -g vsce

流程

创建项目

1
2
yo code
# 根据交互式提示,创建项目

填充代码

默认主文件 extension.ts -- 在 package.json 中配置

添加命令

命令是 vscode 插件执行的入口

1
2
3
4
5
6
7
8
9
10
11
// package.json
...
"contributes" :{
"commands" : [
// 命令声明
{
"command": "runlua-unity.luacommand", // 命令名称
"title": "在unity中运行所有Lua代码" // 命令显示名称
},
]
}

添加命令显示方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"contributes": {
"commands": [
// ...
],
"menus": {
"editor/context": [ // 显示位置
{
"command": "runlua-unity.luacommand", // 命令名称
"when": "canShowRunCommand", // 显示时机 可以由内置时机 和用户自定义时机决定
"group": "*****" // 命令分组
}
]
}
},

快捷键F5调试项目

第一次调试项目会报错要求设置调试配置 根据自动生成配置文件即可

添加 changelog

修改 CHANGELOG.md 文件

打包插件

1
vsce package

可能需要的 API

1
2
3
4
5
6
7
vscode.window.showInformationMessage        // 右下角显示信息
vscode.window.activeTextEditor // 获取当前激活编辑器
vscode.window.onDidChangeActiveTextEditor // 监听激活编辑器
vscode.commands.executeCommand('setContext'
, 'canShowRunCommand', canShowRunCommand); // 修改自定义时机
vscode.commands.registerCommand // 注册命令
vscode.window.createOutputChannel // 创建输出窗口

IntelliJ Platform

环境配置

  1. 指定版本 JDK 版本见 build-number-ranges
  2. IDEA
  3. IDEA Plugin Devkit 插件(2023.2 后需要自行安装)

流程

创建项目

安装 Plugin Devkit 后,在 File 菜单中选择 New Project 通过 IDE 插件模板创建项目

Gradle 配置

等待 Gradle 初始化结束

配置项目

通过 plugin.xml 配置项目

新建 Action

在项目浏览器中,选中需要新建 Action 的目录,在右键菜单中选择 New -> Plugin DevKit 新建 Action

补全 Action ID (xxx.xxx.xxx) 类名 (xxxAction) 描述 与 名称(显示在UI中)

选择添加的组与键盘快捷键 组即代表 Action 会根据什么操作来显示在 UI 中

调试项目

Gradle 选项 Tasks/intellij/runide 执行调试 ,将会打开一个新的安装有指定插件的 idea 沙盒

添加 changelog

修改 plugin.xml 文件 中 标签

打包插件

Gradle 选项 Tasks/intellij/buildPlugin 执行打包 生成结果出现在 项目目录下 build/distributions

可能需要的 API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

@Override
public void actionPerformed(AnActionEvent e) {
// Action执行时调用
}

@Override
public void update(AnActionEvent e){
// Action更新时调用 显隐逻辑声明在这里
e.getPresentation().setEnabledAndVisible(); // 更新Action的显示逻辑
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
// 在这里指定更新逻辑应该运行在哪个线程 与 update 相关
// 返回 ActionUpdateThread.BGT 让更新逻辑在背景线程执行
// 返回 ActionUpdateThread.EDT 让更新逻辑在事件分发线程(EDT)执行
return ActionUpdateThread.BGT;
}

//在plugin.xml` 中配置 NotificationGroup
//<extensions defaultExtensionNs="com.intellij">
// <notificationGroup id="Mugen Notification Group" displayType="BALLOON" toolWindowId="Some ToolWindow ID" />
//</extensions>
{
// 获取通知组
NotificationGroupManager.getInstance().getNotificationGroup("Notification Group");
// 创建通知实例
Notification notification = notificationGroup.createNotification("Message from Unity", message, NotificationType.INFORMATION);
// 显示右下角通知
notification.notify(project);
}

值得注意的点

  1. 如果出现编辑器调试乱码 可以通过修改 gradle 配置 build.gradle.kts 将 gradle 编码设置为 UTF-8

    1
    2
    3
    4
    5
    6
    7
    tasks {
    ...
    withType<JavaCompile> {
    ...
    options.encoding = "UTF-8"
    }
    }
    同时每个java文件的编码也需要设置为 UTF-8 ,注意在右下角的文件编码选项中移除 BOM

  2. 修改插件的名称可以通过修改 gradle 配置 build.gradle.kts

1
2
3
4
intellij {
pluginName = "****"
...
}