Eclipse 插件开发 5.2 编辑器 获取当前编辑器

Eclipse 插件开发 5.2 编辑器 获取当前编辑器

  • [1 获取活跃编辑器](#1 获取活跃编辑器)
  • [2 获取全部编辑器](#2 获取全部编辑器)
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

1 获取活跃编辑器

在 Eclipse RCP 插件开发中,获取编辑区(Editor)及其内容、判断当前活跃的编辑器,需要使用 Eclipse Platform 提供的 API,特别是 IWorkbench, IWorkbenchWindow, IWorkbenchPage, IEditorPart 等接口。

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

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

public class SampleHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IEditorPart part = page.getActiveEditor();
		if (part instanceof ITextEditor) {
			ITextEditor editor = (ITextEditor) part;
			IDocumentProvider provider = editor.getDocumentProvider();
			IDocument document = provider.getDocument(editor.getEditorInput());
			System.out.println(String.format("%s:%s", editor.getTitle(), document.get()));
		}
		return null;
	}

}

2 获取全部编辑器

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

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

public class SampleHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

		for (IEditorReference ref : page.getEditorReferences()) {
			IEditorPart editor = ref.getEditor(false);
			if (null == editor) {
				System.out.println(String.format("%s未被编辑", ref.getTitle()));
				continue;
			}
			System.out.println(String.format("%s正在编辑:%s", ref.getTitle(), getEditContent(editor)));
		}

		return null;
	}

	private String getEditContent(IEditorPart part) {
		if (part instanceof ITextEditor) {
			ITextEditor editor = (ITextEditor) part;
			IDocumentProvider provider = editor.getDocumentProvider();
			IDocument document = provider.getDocument(editor.getEditorInput());
			return document.get();
		}
		return null;
	}

}
相关推荐
云烟成雨TD1 天前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
于慨1 天前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
swg3213211 天前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
gelald1 天前
SpringBoot - 自动配置原理
java·spring boot·后端
殷紫川1 天前
深入理解 AQS:从架构到实现,解锁 Java 并发编程的核心密钥
java
一轮弯弯的明月1 天前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得
chenjingming6661 天前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
qq_339554821 天前
英飞凌ModusToolbox环境搭建
c语言·eclipse
殷紫川1 天前
深入拆解 Java volatile:从内存屏障到无锁编程的实战指南
java
eddieHoo1 天前
查看 Tomcat 的堆内存参数
java·tomcat