如何在SWT组合中嵌入Eclipse CDT CSourceViewer

时间:2016-02-20 19:57:58

标签: java eclipse swt eclipse-cdt

我在Eclipse中设置了一个多页面编辑器插件,并希望将Eclipse CDT中的CSourceViewer嵌入到一个页面中,该页面还有一些其他元素,如按钮等。最简单的方法是什么?我尝试了类似下面的内容,但是当我尝试在源查看器中输入内容时,这会导致空指针异常。

    CTextTools textTools= CUIPlugin.getDefault().getTextTools();
    IPreferenceStore store = CUIPlugin.getDefault().getCombinedPreferenceStore();
    CSourceViewer viewer = new CSourceViewer(composite, null, null, false, SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL| SWT.FULL_SELECTION, CUIPlugin.getDefault().getPreferenceStore());
    viewer.configure(new CSourceViewerConfiguration(textTools.getColorManager(), store, null, textTools.getDocumentPartitioning()));
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.heightHint = 200;
    gd.widthHint = 300;
    viewer.getControl().setLayoutData(gd);

它适用于普通的源查看器。但是CDT CSourceViewer非常适合语法高亮等等。

堆栈跟踪:

java.lang.NullPointerException
at org.eclipse.jface.text.source.projection.ProjectionViewer.handleVerifyEvent(ProjectionViewer.java:1264)
at org.eclipse.jface.text.TextViewer$TextVerifyListener.verifyText(TextViewer.java:437)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:273)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1137)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1122)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:788)
at org.eclipse.swt.custom.StyledText.modifyContent(StyledText.java:7273)
at org.eclipse.swt.custom.StyledText.sendKeyEvent(StyledText.java:8156)
at org.eclipse.swt.custom.StyledText.doContent(StyledText.java:2502)
at org.eclipse.swt.custom.StyledText.handleKey(StyledText.java:5964)
at org.eclipse.swt.custom.StyledText.handleKeyDown(StyledText.java:5995)
at org.eclipse.swt.custom.StyledText$7.handleEvent(StyledText.java:5682)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1137)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1122)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1164)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1160)
at org.eclipse.swt.widgets.Widget.wmChar(Widget.java:1581)
at org.eclipse.swt.widgets.Control.WM_CHAR(Control.java:4795)
at org.eclipse.swt.widgets.Canvas.WM_CHAR(Canvas.java:343)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4676)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:339)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:5050)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2549)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3767)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:654)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:598)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:139)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
at org.eclipse.equinox.launcher.Main.main(Main.java:1488)

1 个答案:

答案 0 :(得分:1)

问题是未设置查看器的Document,因此为null。添加以下内容解决了它:

    Document document = new Document("class Test { };");
    textTools.setupCDocument(document);
    viewer.setDocument(document);