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 天前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
jmxwzy1 天前
Spring全家桶
java·spring·rpc
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法
Fleshy数模1 天前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
像少年啦飞驰点、1 天前
零基础入门 Spring Boot:从“Hello World”到可上线的 Web 应用全闭环指南
java·spring boot·web开发·编程入门·后端开发
苍煜1 天前
万字详解Maven打包策略:从基础插件到多模块实战
java·maven
有来技术1 天前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5161 天前
xxx医患档案管理系统
java·spring boot·vue·毕业设计·智慧城市
一个响当当的名号1 天前
lectrue9 索引并发控制
java·开发语言·数据库
进阶小白猿1 天前
Java技术八股学习Day30
java·开发语言·学习