You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Eclipse活动编辑器及CDT中打开文件的列表与IFile[]对象

嘿,我来帮你搞定这两个Eclipse插件开发里的常见需求,直接上干货~

获取Eclipse活动编辑器窗口中已打开的文件列表

要获取当前活动窗口里所有打开的文件,咱们可以借助Eclipse Platform UI的API来实现,核心是通过IWorkbenchPage获取编辑器引用,再从编辑器输入中提取对应的文件对象:

import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.core.resources.IFile;
import java.util.ArrayList;
import java.util.List;

public List<IFile> getOpenFilesInActiveWindow() {
    List<IFile> openFiles = new ArrayList<>();
    IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    
    if (activeWindow != null) {
        IWorkbenchPage activePage = activeWindow.getActivePage();
        if (activePage != null) {
            IEditorReference[] editorRefs = activePage.getEditorReferences();
            
            for (IEditorReference ref : editorRefs) {
                try {
                    // 从编辑器输入中获取文件对象
                    FileEditorInput input = (FileEditorInput) ref.getEditorInput();
                    IFile file = input.getFile();
                    if (file != null && file.exists()) {
                        openFiles.add(file);
                    }
                } catch (Exception e) {
                    // 跳过非文件类型的编辑器(比如临时文件编辑器、视图类编辑器)
                    continue;
                }
            }
        }
    }
    return openFiles;
}

这里要注意:有些编辑器的输入可能不是FileEditorInput,所以加了异常捕获来跳过这些情况,避免程序报错。

获取Eclipse CDT编辑器中已打开的.c文件对应的IFile[]

针对CDT的C编辑器,咱们需要额外过滤编辑器类型和文件扩展名,确保只拿到打开的.c文件对应的IFile对象:

import org.eclipse.cdt.ui.editor.CEditor;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.core.resources.IFile;
import java.util.ArrayList;
import java.util.List;

public IFile[] getOpenCFilesInCDTEditors() {
    List<IFile> cFiles = new ArrayList<>();
    IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    
    if (activeWindow != null) {
        IWorkbenchPage activePage = activeWindow.getActivePage();
        if (activePage != null) {
            IEditorReference[] editorRefs = activePage.getEditorReferences();
            
            for (IEditorReference ref : editorRefs) {
                try {
                    // 判断是否是CDT的C编辑器
                    if (CEditor.ID.equals(ref.getId())) {
                        FileEditorInput input = (FileEditorInput) ref.getEditorInput();
                        IFile file = input.getFile();
                        
                        // 过滤扩展名是.c的文件(忽略大小写)
                        if (file != null && file.exists() && "c".equalsIgnoreCase(file.getFileExtension())) {
                            cFiles.add(file);
                        }
                    }
                } catch (Exception e) {
                    continue;
                }
            }
        }
    }
    // 转成IFile数组返回
    return cFiles.toArray(new IFile[cFiles.size()]);
}

这里用到了CDT内置的CEditor.ID常量(值为org.eclipse.cdt.ui.editor.CEditor)来精准匹配C编辑器,同时通过getFileExtension()过滤出.c后缀的文件,这样就能得到你需要的IFile[]数组了。

内容的提问来源于stack exchange,提问作者Maruthamalai A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:58:57