下面列出了怎么用org.eclipse.jface.text.TypedRegion的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*
* @since 2.2
*/
@Override
public synchronized ITypedRegion getPartition(int offset, boolean preferOpenPartitions) {
ITypedRegion region = getPartition(offset);
if (preferOpenPartitions) {
if (region.getOffset() == offset && !region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) {
if (offset > 0) {
region = getPartition(offset - 1);
if (region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE))
return region;
}
return new TypedRegion(offset, 0, IDocument.DEFAULT_CONTENT_TYPE);
}
}
return region;
}
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*/
@Override
public ITypedRegion getPartition(int offset, boolean preferOpenPartitions) {
ITypedRegion region = getPartition(offset);
if (preferOpenPartitions) {
if (region.getOffset() == offset && !region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) {
if (offset > 0) {
region = getPartition(offset - 1);
if (region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) {
return region;
}
}
return new TypedRegion(offset, 0, IDocument.DEFAULT_CONTENT_TYPE);
}
}
return region;
}
@Override
public ITypedRegion getPartition(int offset, boolean preferOpenPartitions) {
if (preferOpenPartitions) {
if (offset <= 0) {
return new TypedRegion(offset, 0, IDocument.DEFAULT_CONTENT_TYPE);
}
if (fDocument != null && offset == fDocument.getLength()) {
// Fix issue where a wrong partition is being gotten when a comment is being typed
// as the last thing in the document.
// Fixes #PyDev-762: Code completion is active in comments.
try {
int lineOffset = fDocument.getLineOffset(fDocument.getLineOfOffset(offset));
if (lineOffset != offset) { // A comment must start with a #, so, the char 0 of a line can't be a comment itself.
ITypedRegion region = getPartition(offset - 1);
if (IPythonPartitions.PY_COMMENT.equals(region.getType())
|| IDocument.DEFAULT_CONTENT_TYPE.equals(region.getType())) {
return region;
}
}
} catch (BadLocationException e) {
// ignore
}
}
}
return super.getPartition(offset, preferOpenPartitions);
}
@Override
public void addPosition(int offset, int length, String... id) {
Assert.assertEquals(1, id.length);
TypedRegion region = new TypedRegion(offset, length, id[0]);
Assert.assertFalse(region.toString(), expectedRegions.isEmpty());
Assert.assertTrue("expected: " + expectedRegions.toString() + " but was: " + region, expectedRegions.remove(region));
}
@Override
public void addPosition(int offset, int length, String... id) {
assertEquals(1, id.length);
TypedRegion region = new TypedRegion(offset, length, id[0]);
assertFalse(region.toString(), expectedRegions.isEmpty());
assertTrue("expected: " + expectedRegions.toString() + " but was: " + region, expectedRegions.remove(region));
}
@Override
public ITypedRegion getPartition(int arg0) {
/*
* Never gets called because this class implements IDocumentPartitioner
* rather than IDocumentPartitionerExtension2 which is required for
* IDocumentExtension3
* Confused ?? Me too...
*/
return new TypedRegion(0, fDocument.getLength(), IMPEX_HEADER);
}
public ITypedRegion[] merge(ITypedRegion[] original) {
if (original == null || original.length == 0)
return original;
ITypedRegion[] result = new ITypedRegion[original.length];
String contentType = original[0].getType();
result[0] = original[0];
for(int i = 1; i < original.length; i++) {
ITypedRegion copyMe = original[i];
result[i] = new TypedRegion(copyMe.getOffset(), copyMe.getLength(), contentType);
}
return result;
}
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*/
public ITypedRegion getPartition(int offset, boolean preferOpenPartitions) {
ITypedRegion region= getPartition(offset);
if (preferOpenPartitions) {
if (region.getOffset() == offset && !region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) {
if (offset > 0) {
region= getPartition(offset - 1);
if (region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE))
return region;
}
return new TypedRegion(offset, 0, IDocument.DEFAULT_CONTENT_TYPE);
}
}
return region;
}
/**
* @return
*/
public ITypedRegion getUserRegion()
{
if (hasUserPartitions())
{
ITypedRegion region = PartitionToolkit.mergePartitions((ITypedRegion[]) userOutput
.toArray(new TypedRegion[userOutput.size()]));
// re-initialize the user partitions
resetUserPartitions();
return region;
} else
{
return null;
}
}
protected ITypedRegion[] computePartitioning(int offset, int length)
{
ITypedRegion[] regions = null;
try
{
regions = TextUtilities
.computePartitioning(getDocument(), getDocumentPartitioning(), offset, length, false);
}
catch (BadLocationException x)
{
regions = new TypedRegion[0];
}
return regions;
}
/**
* Computes and returns the partitioning for the given region of the input document
* of the reconciler's connected text viewer.
*
* @param offset the region offset
* @param length the region length
* @return the computed partitioning
* @since 3.0
*/
private ITypedRegion[] computePartitioning(int offset, int length)
{
ITypedRegion[] regions = null;
try
{
regions = TextUtilities.computePartitioning(getDocument(), getDocumentPartitioning(), offset, length, false);
} catch (BadLocationException x)
{
regions = new TypedRegion[0];
}
return regions;
}
protected boolean expect(int offset, int length, String type) {
return expectedRegions.add(new TypedRegion(offset, length, type));
}
@Override
public void documentChanged(DocumentEvent event) {
lastRegion = damager.getDamageRegion(new TypedRegion(0,event.getDocument().getLength(), IDocument.DEFAULT_CONTENT_TYPE), event, false);
}
protected void expect(int offset, int length, String type) {
expectedRegions.add(new TypedRegion(offset, length, type));
}
@Override
public void documentChanged(DocumentEvent event) {
lastRegion = damager.getDamageRegion(new TypedRegion(0,event.getDocument().getLength(), IDocument.DEFAULT_CONTENT_TYPE), event, false);
}
@Override
public void documentChanged(DocumentEvent event) {
lastRegion = damager.getDamageRegion(new TypedRegion(0,event.getDocument().getLength(), IDocument.DEFAULT_CONTENT_TYPE), event, false);
}
@Override
public ITypedRegion[] computePartitioning(int offset, int length) {
/*
* Never gets called because this class implements IDocumentPartitioner
* rather than IDocumentPartitionerExtension2 which is required for
* IDocumentExtension3
* Confused ?? Me too...
*/
List<TypedRegion> list = new ArrayList<TypedRegion>();
try {
int start;
int nextOffset;
boolean isHeader = true;
int docLength = fDocument.getLength();
if (offset == 0) {
nextOffset = getLineEndOffset(1, fDocument);
list.add(new TypedRegion(0, nextOffset + 1, IMPEX_HEADER));
int i = 1;
while (nextOffset + 1 < docLength) {
start = nextOffset+ 1;
if (Character.isDigit(fDocument.getChar(start))) {
isHeader = true;
}
else {
isHeader = false;
}
nextOffset = getLineEndOffset(i + 1, fDocument);
if (isHeader) {
list.add(new TypedRegion(start, nextOffset - start + 1, IMPEX_INSTRUCTION));
}
else {
list.add(new TypedRegion(start, nextOffset - start + 1, IMPEX_DATA));
}
i = i + 1;
}
}
else {
if (Character.isDigit(fDocument.getChar(offset))) {
isHeader = true;
}
else {
isHeader = false;
}
if (isHeader) {
list.add(new TypedRegion(offset, length, IMPEX_HEADER));
}
else {
list.add(new TypedRegion(offset, length, IMPEX_DATA));
}
}
}
catch (BadLocationException ble) {
Activator.logError("BadLocationException", ble);
}
if (list.isEmpty()) {
list.add(new TypedRegion(offset, length, null));
}
TypedRegion[] result = new TypedRegion[list.size()];
list.toArray(result);
return result;
}
/**
* Test method for {@link org.lamport.tla.toolbox.tool.tlc.output.ParsingTLCOutputSink#mergePartitions(org.eclipse.jface.text.ITypedRegion[])}.
*/
public void testMergePartitions()
{
assertEquals(new TypedRegion(0, 100, "type"), PartitionToolkit.mergePartitions(new ITypedRegion[] {
new TypedRegion(0, 10, "type"), new TypedRegion(10, 80, "type"), new TypedRegion(90, 10, "type") }));
}
public void testMergePartitions2()
{
assertEquals(new TypedRegion(0, 100, "type"), PartitionToolkit
.mergePartitions(new ITypedRegion[] { new TypedRegion(0, 100, "type") }));
}
@Override
public ITypedRegion[] computePartitioning(int offset, int length) {
return new TypedRegion[] { new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE) };
}
@Override
public ITypedRegion getPartition(int offset) {
return new TypedRegion(offset, 1, IDocument.DEFAULT_CONTENT_TYPE);
}