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;
	}

}
相关推荐
Tony_yitao1 小时前
9.华为OD机试真题 - 最长的顺子 - 2024E卷 Java
java·华为od·algorithm
毕设源码-赖学姐2 小时前
【开题答辩全过程】以 非凡物流公司电商物流管理系统的设计与实现为例,包含答辩的问题和答案
java·eclipse
菠菠萝宝3 小时前
【Java手搓RAGFlow】-3- 用户认证与权限管理
java·开发语言·人工智能·llm·openai·qwen·rag
print(未来)5 小时前
C++ 与 C# 的性能比较:选择合适的语言进行高效开发
java·开发语言
王者之座7 小时前
java+maven配置yguard的一次实验
java·spring boot·maven
q***61508 小时前
eclipse配置Spring
java·spring·eclipse
Miraitowa_cheems8 小时前
LeetCode算法日记 - Day 106: 两个字符串的最小ASCII删除和
java·数据结构·算法·leetcode·深度优先
q***58198 小时前
Spring全家桶简介
java·后端·spring
武昌库里写JAVA9 小时前
微擎服务器配置要求,微擎云主机多少钱一年?
java·vue.js·spring boot·后端·sql