Configuring an Eclipse SourceViewer for Java

94 Views Asked by At

I am trying to create a viewer that embeds a Java Editor/Viewer. However, I cannot get the Source Viewer to do the proper Java scanning & coloring. This is the code I have so far.

ScopedPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE,
        ID);
JavaTextTools tools = new JavaTextTools(store);

Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));

viewer = new SourceViewer(composite, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
viewer.getTextWidget().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
viewer.setEditable(true);
viewer.getControl().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
Document document = new Document();
tools.setupJavaDocumentPartitioner(document);
viewer.setDocument(document);
viewer.configure(new JavaSourceViewerConfiguration(tools.getColorManager(), store, null,
        IJavaPartitions.JAVA_PARTITIONING));

Just amazed how hard it seem to just embed a simple Java editor ... Whatever I do, the viewer works but remains without any coloring.

1

There are 1 best solutions below

0
user22925716 On

SourceViewer is not a java editor, instead it's a class used to create source editors for custom languages as described in the eclipse docs.

If you need just syntax coloring, then it might be easier to use SourceViewer and implement syntax coloring by yourself as described here.

Another option is use the actual java editor from eclipse as greg-449 mentioned.
But expect A LOT of code there.