How can I Access Different Java Partitions in my IDocument

95 Views Asked by At

I am currently developing a plugin in which I need a function to only execute when my cursor is within a javadoc comment partition.

I have tried to execute IDocument.computePartitioning() as well as IDocument.getContentType(). So far the only partition type that is returning is the default content type. I can see that, although the IDocument instance is correct, the getDocumentPartitioner() method returns null.

My question is, my IDocument clearly contains IJavaPartitions as it's a .java file. How can I access this partitioner in order to determine what partition my cursor is located within?

2

There are 2 best solutions below

1
On BEST ANSWER

You can use the computePartitioning method of org.eclipse.jface.text.TextUtilities to get the region(s) for a range:

IDocument document = ... get document

String partitioning = IJavaPartitions.JAVA_PARTITIONING;

int start = ... start offset

int length = ... length of area

ITypedRegion[] regions = TextUtilities.computePartitioning(document, partitioning, start, length, false);

This will deal with any IDocumentExtension3

You can also get the partitioning name from the TextViewer / SourceViewer by calling the getDocumentPartitioning method.

0
On

You need to look at, and use the methods on, IDocumentExtension3. A document can be partitioned in many different ways, by different partitioners. Each of these is considered a partitioning, with the computePartitioning being a case of a bad name choice sticking around for the sake of binary compatibility. In the case of Java documents, they exclusively use a partitioning value of their own. I think it is held as a constant in IJavaPartitions.JAVA_PARTITIONING.