Java Code Examples for org.eclipse.jface.text.Document#addPosition()

The following examples show how to use org.eclipse.jface.text.Document#addPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartitionCodeReaderTest.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public void testPartitionCodeReaderKeepingPositions() throws Exception {
    PartitionCodeReader reader = new PartitionCodeReader(IDocument.DEFAULT_CONTENT_TYPE);
    Document document = new Document("abcde");
    String category = setupDocument(document);

    document.addPosition(category, new TypedPosition(1, 1, "cat1")); //skip b
    document.addPosition(category, new TypedPosition(3, 1, "cat1")); //skip d

    reader.configureForwardReader(document, 3, document.getLength(), true);
    FastStringBuffer buf = new FastStringBuffer(document.getLength());
    readAll(reader, buf);
    assertEquals("e", buf.toString());

    reader.configureForwardReaderKeepingPositions(2, document.getLength());
    buf.clear();
    readAll(reader, buf);
    assertEquals("ce", buf.toString());

    reader.configureForwardReaderKeepingPositions(0, document.getLength());
    buf.clear();
    readAll(reader, buf);
    assertEquals("ace", buf.toString());
}
 
Example 2
Source File: CommentFormatterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a document with the given content and the given positions
 * registered with the {@link DefaultPositionUpdater}.
 *
 * @param content the content
 * @param positions the positions
 * @return the document
 * @throws IllegalArgumentException
 */
private static Document createDocument(String content, Position[] positions) throws IllegalArgumentException {
	Document doc= new Document(content);
	try {
		if (positions != null) {
			final String POS_CATEGORY= "myCategory"; //$NON-NLS-1$

			doc.addPositionCategory(POS_CATEGORY);
			doc.addPositionUpdater(new DefaultPositionUpdater(POS_CATEGORY) {
				protected boolean notDeleted() {
					if (this.fOffset < this.fPosition.offset && (this.fPosition.offset + this.fPosition.length < this.fOffset + this.fLength)) {
						this.fPosition.offset= this.fOffset + this.fLength; // deleted positions: set to end of remove
						return false;
					}
					return true;
				}
			});
			for (int i= 0; i < positions.length; i++) {
				try {
					doc.addPosition(POS_CATEGORY, positions[i]);
				} catch (BadLocationException e) {
					throw new IllegalArgumentException("Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + content.length());   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
				}
			}
		}
	} catch (BadPositionCategoryException cannotHappen) {
		// can not happen: category is correctly set up
	}
	return doc;
}
 
Example 3
Source File: ASTRewriteFormatter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Document createDocument(String string, Position[] positions) throws IllegalArgumentException {
	Document doc= new Document(string);
	try {
		if (positions != null) {
			final String POS_CATEGORY= "myCategory"; //$NON-NLS-1$

			doc.addPositionCategory(POS_CATEGORY);
			doc.addPositionUpdater(new DefaultPositionUpdater(POS_CATEGORY) {
				protected boolean notDeleted() {
					int start= this.fOffset;
					int end= start + this.fLength;
					if (start < this.fPosition.offset && (this.fPosition.offset + this.fPosition.length < end)) {
						this.fPosition.offset= end; // deleted positions: set to end of remove
						return false;
					}
					return true;
				}
			});
			for (int i= 0; i < positions.length; i++) {
				try {
					doc.addPosition(POS_CATEGORY, positions[i]);
				} catch (BadLocationException e) {
					throw new IllegalArgumentException("Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + string.length());   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
				}
			}
		}
	} catch (BadPositionCategoryException cannotHappen) {
		// can not happen: category is correctly set up
	}
	return doc;
}
 
Example 4
Source File: PartitionCodeReaderTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testPartitionCodeReaderUnread2() throws Exception {
    PartitionCodeReader reader = new PartitionCodeReader(IDocument.DEFAULT_CONTENT_TYPE);
    Document document = new Document("abcde");
    String category = setupDocument(document);

    document.addPosition(category, new TypedPosition(1, 1, "cat1")); //skip b
    document.addPosition(category, new TypedPosition(3, 1, "cat1")); //skip d

    reader.configureForwardReader(document, 0, document.getLength());
    FastStringBuffer buf = new FastStringBuffer(document.getLength());
    readAll(reader, buf);
    reader.unread(); //EOF
    reader.unread(); //e
    reader.unread(); //c
    readAll(reader, buf);
    reader.unread(); //EOF
    reader.unread(); //e
    reader.unread(); //c
    reader.unread(); //a
    readAll(reader, buf);
    reader.unread(); //EOF
    assertEquals(-1, reader.read());
    reader.unread(); //EOF
    reader.unread(); //e
    readAll(reader, buf);
    assertEquals("aceceacee", buf.toString());
}
 
Example 5
Source File: PartitionCodeReaderTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testPartitionCodeReaderMark2() throws Exception {
    PartitionCodeReader reader = new PartitionCodeReader(IDocument.DEFAULT_CONTENT_TYPE);
    Document document = new Document("abcde");
    String category = setupDocument(document);

    document.addPosition(category, new TypedPosition(0, 1, "cat1")); //skip a
    document.addPosition(category, new TypedPosition(2, 1, "cat1")); //skip c
    document.addPosition(category, new TypedPosition(4, 1, "cat1")); //skip e

    reader.configureForwardReader(document, 0, document.getLength());
    int mark = reader.getMark();
    assertEquals(reader.read(), 'b');

    int mark2 = reader.getMark();
    assertEquals(reader.read(), 'd');

    int mark3 = reader.getMark();
    assertEquals(reader.read(), -1);

    reader.setMark(mark);
    assertEquals(reader.read(), 'b');
    assertEquals(reader.read(), 'd');
    assertEquals(reader.read(), -1);

    reader.setMark(mark2);
    assertEquals(reader.read(), 'd');
    assertEquals(reader.read(), -1);

    reader.setMark(mark3);
    assertEquals(reader.read(), -1);
}
 
Example 6
Source File: PartitionCodeReaderTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testPartitionCodeReaderMarkBackwards2() throws Exception {
    PartitionCodeReader reader = new PartitionCodeReader(IDocument.DEFAULT_CONTENT_TYPE);
    Document document = new Document("abcde");
    String category = setupDocument(document);

    document.addPosition(category, new TypedPosition(0, 1, "cat1")); //skip a
    document.addPosition(category, new TypedPosition(2, 1, "cat1")); //skip c
    document.addPosition(category, new TypedPosition(4, 1, "cat1")); //skip e

    reader.configureBackwardReader(document, document.getLength());
    int mark = reader.getMark();
    assertEquals(reader.read(), 'd');

    int mark2 = reader.getMark();
    assertEquals(reader.read(), 'b');

    int mark3 = reader.getMark();
    assertEquals(reader.read(), -1);

    reader.setMark(mark);
    assertEquals(reader.read(), 'd');
    assertEquals(reader.read(), 'b');
    assertEquals(reader.read(), -1);

    reader.setMark(mark2);
    assertEquals(reader.read(), 'b');
    assertEquals(reader.read(), -1);

    reader.setMark(mark3);
    assertEquals(reader.read(), -1);
}
 
Example 7
Source File: PartitionCodeReaderTest.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void testPartitionCodeReaderUnread() throws Exception {
    PartitionCodeReader reader = new PartitionCodeReader("cat1");
    Document document = new Document("abcde");
    String category = setupDocument(document);

    document.addPosition(category, new TypedPosition(1, 1, "cat1")); //skip b
    document.addPosition(category, new TypedPosition(3, 1, "cat1")); //skip d

    reader.configureForwardReader(document, 0, document.getLength(), true);

    assertEquals('b', reader.read());
    reader.unread();
    assertEquals('b', reader.read());
    reader.unread();

    assertEquals('b', reader.read());
    assertEquals('d', reader.read());
    assertEquals(-1, reader.read());
    reader.unread();
    assertEquals(-1, reader.read());
    reader.unread();
    assertEquals(-1, reader.read());
    reader.unread();
    reader.unread();
    assertEquals('d', reader.read());
    for (int i = 0; i < 5; i++) {
        reader.read(); //read past EOF (but should actually stay at EOF)
    }
    reader.unread();
    assertEquals(-1, reader.read());
    reader.unread();
    reader.unread();
    assertEquals('d', reader.read());
    for (int i = 0; i < 5; i++) {
        reader.unread(); //unread to the beggining
    }

    FastStringBuffer buf = new FastStringBuffer(document.getLength());
    readAll(reader, buf);
    reader.unread(); //EOF
    reader.unread(); //d
    reader.unread(); //b
    readAll(reader, buf);
    assertEquals("bdbd", buf.toString());

    reader.configureForwardReaderKeepingPositions(1, document.getLength());
    buf.clear();
    readAll(reader, buf);
    reader.unread(); //EOF
    reader.unread(); //d
    reader.unread(); //b
    readAll(reader, buf);
    assertEquals("bdbd", buf.toString());

    reader.configureForwardReaderKeepingPositions(2, document.getLength());
    buf.clear();
    readAll(reader, buf);
    reader.unread(); //EOF
    reader.unread(); //d
    reader.unread(); //b
    readAll(reader, buf);
    assertEquals("dd", buf.toString());
}
 
Example 8
Source File: PartitionCodeReaderTest.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void testPartitionCodeReaderMark() throws Exception {
    PartitionCodeReader reader = new PartitionCodeReader(IDocument.DEFAULT_CONTENT_TYPE);
    Document document = new Document("abcde");
    String category = setupDocument(document);

    document.addPosition(category, new TypedPosition(1, 1, "cat1")); //skip b
    document.addPosition(category, new TypedPosition(3, 1, "cat1")); //skip d

    reader.configureForwardReader(document, 0, document.getLength());
    int mark = reader.getMark();
    assertEquals(0, mark);
    assertEquals(reader.read(), 'a');

    int mark2 = reader.getMark();
    assertEquals(1, mark2);
    assertEquals(reader.read(), 'c');

    int mark3 = reader.getMark();
    assertEquals(3, mark3);
    assertEquals(reader.read(), 'e');

    int mark4 = reader.getMark();
    assertEquals(5, mark4);
    assertEquals(reader.read(), -1);

    reader.setMark(mark);
    assertEquals(reader.read(), 'a');
    assertEquals(reader.read(), 'c');
    assertEquals(reader.read(), 'e');
    assertEquals(reader.read(), -1);

    reader.setMark(mark2);
    assertEquals(reader.read(), 'c');
    assertEquals(reader.read(), 'e');
    assertEquals(reader.read(), -1);

    reader.setMark(mark3);
    assertEquals(reader.read(), 'e');
    assertEquals(reader.read(), -1);

    reader.setMark(mark4);
    assertEquals(reader.read(), -1);
}
 
Example 9
Source File: PartitionCodeReaderTest.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void testPartitionCodeReaderMarkBackwards() throws Exception {
    PartitionCodeReader reader = new PartitionCodeReader(IDocument.DEFAULT_CONTENT_TYPE);
    Document document = new Document("abcde");
    String category = setupDocument(document);

    document.addPosition(category, new TypedPosition(1, 1, "cat1")); //skip b
    document.addPosition(category, new TypedPosition(3, 1, "cat1")); //skip d

    reader.configureBackwardReader(document, document.getLength());
    int mark = reader.getMark();
    assertEquals(5, mark);
    assertEquals(reader.read(), 'e');

    int mark2 = reader.getMark();
    assertEquals(3, mark2);
    assertEquals(reader.read(), 'c');

    int mark3 = reader.getMark();
    assertEquals(1, mark3);
    assertEquals(reader.read(), 'a');

    int mark4 = reader.getMark();
    assertEquals(-1, mark4);
    assertEquals(reader.read(), -1);

    reader.setMark(mark);
    assertEquals(reader.read(), 'e');
    assertEquals(reader.read(), 'c');
    assertEquals(reader.read(), 'a');
    assertEquals(reader.read(), -1);

    reader.setMark(mark2);
    assertEquals(reader.read(), 'c');
    assertEquals(reader.read(), 'a');
    assertEquals(reader.read(), -1);

    reader.setMark(mark3);
    assertEquals(reader.read(), 'a');
    assertEquals(reader.read(), -1);

    reader.setMark(mark4);
    assertEquals(reader.read(), -1);
}