Eclipse 插件开发 5.3 编辑器 监听输入

Eclipse 插件开发 5.3 编辑器监 听输入

  • [1 插件配置](#1 插件配置)
  • [2 添加监听](#2 添加监听)
  • [3 查看效果](#3 查看效果)
java 复制代码
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Click1
Bundle-SymbolicName: com.xu.click1;singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: com.xu.click1.Activator
Bundle-Vendor: XU
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.ui.editors,
 org.eclipse.ui.workbench.texteditor,
 org.eclipse.core.resources,
 org.eclipse.text
Bundle-RequiredExecutionEnvironment: JavaSE-24
Automatic-Module-Name: com.xu.click1
Bundle-ActivationPolicy: lazy

org.eclipse.ui.IStartup 是 Eclipse 插件开发中用于在 工作台启动时自动执行代码 的接口。

org.eclipse.ui.IStartup 适用于以下场景:

1、启动时注册监听器(如监听编辑器、视图、资源等)

2、启动时预加载某些资源或服务

3、提前初始化你的插件逻辑

4、注册全局快捷键、UI 元素等

IStartup 注意事项

注意点 说明
UI 操作必须放在 UI 线程中 使用 Display.getDefault().asyncExec(...)
插件必须在 plugin.xml 中注册扩展点 否则 earlyStartup() 不会执行
不要阻塞 earlyStartup() 否则会影响 Eclipse 启动速度

IStartup 常见用途示例

用途 说明
自动附加文档监听器 监听所有打开的 ITextEditor 并获取实时输入
注册资源监听器 监听项目、文件变化
注册窗口、视图、编辑器监听器 管理 UI 生命周期
初始化日志系统、缓存等 提前准备插件运行环境

1 插件配置

java 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
    
    <extension point="org.eclipse.ui.startup">
        <startup class="com.xu.click1.StartupListener" />
    </extension>

</plugin>

2 添加监听

java 复制代码
package com.xu.click1;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IStartup;

import com.xu.edit.EditUtil;

public class StartupListener implements IStartup {

	@Override
	public void earlyStartup() {
		Display.getDefault().asyncExec(() -> {
			new EditUtil().addEditListener();
		});
	}

}
java 复制代码
package com.xu.edit;

import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

public class EditUtil {

	public void addEditListener() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		oldEditListener(workbench);
		newEditListener(workbench);
	}

	private void oldEditListener(IWorkbench workbench) {
		if (null == workbench) {
			return;
		}

		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		if (null == window) {
			return;
		}

		IWorkbenchPage page = window.getActivePage();
		if (null == page) {
			return;
		}

		for (IEditorReference ref : page.getEditorReferences()) {
			IEditorPart editor = ref.getEditor(true);
			if (null == editor) {
				continue;
			}
			addListener((ITextEditor) editor);
		}
	}

	private void addListener(ITextEditor editor) {
		if (null == editor) {
			return;
		}

		IDocumentProvider provider = editor.getDocumentProvider();
		IDocument document = provider.getDocument(editor.getEditorInput());
		document.addDocumentListener(new IDocumentListener() {

			@Override
			public void documentAboutToBeChanged(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void documentChanged(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(String.format("%s:%s", arg0.getOffset(), arg0.getText()));
			}

		});
	}

	private void newEditListener(IWorkbench workbench) {
		if (null == workbench) {
			return;
		}

		for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
			if (window == null) {
				continue;
			}

			IPartService service = window.getPartService();
			if (null == service) {
				continue;
			}

			service.addPartListener(new IPartListener2() {
				@Override
				public void partOpened(IWorkbenchPartReference partRef) {
					IWorkbenchPart part = partRef.getPart(false);
					if (part instanceof ITextEditor) {
						addListener((ITextEditor) part);
					}
				}
			});
		}
	}

}

3 查看效果

相关推荐
鲨鱼辣钊1 小时前
【FastAPI筑基-Day19】APScheduler定时任务全实战|自动执行、动态启停、后台常驻
java·spring·fastapi
学长毕业设计5 小时前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
东小西5 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西5 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午6 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
2601_962065256 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_6 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
予昊7 小时前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
2601_962203517 小时前
【SpringAI入门】初识SpringAI
java
weixin_461408587 小时前
Mybatis-flex小记
java·开发语言·mybatis