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

Eclipse TextMergeViewer不显示内容?如何解决空白问题

问题描述

以下代码中的TextMergeViewer始终显示空白,但将tmv.setInput()替换为CompareUI.openCompareDialog()后,就能正常显示内容。推测CompareEditorInput部分逻辑正常,疑问是否需要执行额外操作强制TextMergeViewer识别输入?

public class SSCCE extends Dialog {

protected SSCCE(Shell parentShell) { super(parentShell); }

protected Control createDialogArea(Composite parent) {
    Composite area = (Composite)super.createDialogArea(parent);
    CompareConfiguration cc = new CompareConfiguration();
    TextMergeViewer tmv = new TextMergeViewer(area, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL, cc);
    tmv.setInput(new CompareEditorInput(cc) {
        protected Object prepareInput(IProgressMonitor arg0) throws InvocationTargetException, InterruptedException {
            return new DiffNode(new CompareEntry("lName", "lContent"), new CompareEntry("lName", "lContent"));
        }});
    GridDataFactory.fillDefaults().grab(true, true).applyTo(tmv.getControl());
    return area;
}

protected boolean isResizable() { return true; }    

private class CompareEntry implements IStreamContentAccessor, ITypedElement {

    String contents, name;
    
    public CompareEntry(String _contents,String _name) {                
        contents = _contents; name = _name;
    }
    
    public InputStream getContents() throws CoreException {             
        return new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));             
    }
    
    public String getType() { return ITypedElement.TEXT_TYPE; }
    
    public Image getImage() { return null; }

    public String getName() { return name; }

}
}
解决方法

问题出在TextMergeViewer不会自动处理CompareEditorInput的异步准备逻辑,而CompareUI.openCompareDialog()内部会完成输入准备、视图刷新等完整流程。需要手动完成以下步骤:

  • 先实例化CompareEditorInput,手动调用其prepareInput方法获取实际的比较节点(注意处理异常和进度监控)
  • 将获取到的DiffNode直接传给TextMergeViewer.setInput()
  • 调用TextMergeViewer.refresh()触发视图刷新

修改后的关键代码片段:

CompareConfiguration cc = new CompareConfiguration();
TextMergeViewer tmv = new TextMergeViewer(area, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL, cc);

// 实例化CompareEditorInput
CompareEditorInput editorInput = new CompareEditorInput(cc) {
    protected Object prepareInput(IProgressMonitor arg0) throws InvocationTargetException, InterruptedException {
        return new DiffNode(new CompareEntry("lName", "lContent"), new CompareEntry("lName", "lContent"));
    }
};

// 手动执行输入准备
try {
    Object input = editorInput.prepareInput(new NullProgressMonitor());
    tmv.setInput(input);
    // 强制刷新视图
    tmv.refresh();
} catch (InvocationTargetException | InterruptedException e) {
    e.printStackTrace();
}

GridDataFactory.fillDefaults().grab(true, true).applyTo(tmv.getControl());

内容的提问来源于stack exchange,提问作者Dave Carpeneto

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 14:55:01