use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class DocumentUtil method findNextOffSetInPartition.
public int findNextOffSetInPartition(IDocument doc, int partitionOffSet, int minIndex) throws BadLocationException {
ITypedRegion partition = doc.getPartition(partitionOffSet);
ITypedRegion partition2 = doc.getPartition(minIndex);
if (partition.getType().equals(partition2.getType()) || partition2.getLength() == 0) {
return minIndex;
} else {
return findNextOffSetInPartition(doc, partitionOffSet, minIndex + partition2.getLength());
}
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class DocumentUtil method searchInSamePartition.
/**
* searches for the given string within the same partition type
*
* @return the region of the match or <code>null</code> if no match were found
* @since 2.4
*/
public IRegion searchInSamePartition(String toFind, String documentText, IDocument document, int startOffset) throws BadLocationException {
if (startOffset >= document.getLength()) {
return null;
}
String text = preProcessSearchString(documentText);
ITypedRegion partition = document.getPartition(startOffset);
int indexOf = text.indexOf(toFind, getOffset(toFind, startOffset));
while (indexOf >= 0 && indexOf < document.getLength()) {
ITypedRegion partition2 = document.getPartition(indexOf);
if (partition2.getType().equals(partition.getType())) {
return new Region(indexOf, toFind.length());
}
indexOf = text.indexOf(toFind, partition2.getOffset() + partition2.getLength());
}
String trimmed = toFind.trim();
if (trimmed.length() > 0 && trimmed.length() != toFind.length()) {
return searchInSamePartition(trimmed, documentText, document, startOffset);
}
return null;
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class DocumentPartitionerTest method testBug401433.
@Test
public void testBug401433() throws Exception {
XtextDocument document = getDocument(" /* */ ");
document.replace(10, 1, "");
ITypedRegion partition = document.getPartition(9);
assertEquals(5, partition.getOffset());
assertEquals(5, partition.getLength());
assertEquals(TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION, partition.getType());
document.replace(9, 1, "");
partition = document.getPartition(8);
assertEquals(5, partition.getOffset());
assertEquals(4, partition.getLength());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
document.replace(8, 1, "");
partition = document.getPartition(7);
assertEquals(5, partition.getOffset());
assertEquals(3, partition.getLength());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
document.replace(7, 1, "");
partition = document.getPartition(6);
assertEquals(5, partition.getOffset());
assertEquals(2, partition.getLength());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
document.replace(6, 1, "");
partition = document.getPartition(5);
assertEquals(5, partition.getOffset());
assertEquals(1, partition.getLength());
assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class DocumentPartitionerTest method testModification.
@Test
public void testModification() throws Exception {
XtextDocument document = getDocument("bar 345 grammar : so 'baz & so'");
document.replace(8, 7, "/*grammar*/");
ITypedRegion partition = document.getPartition(9);
assertEquals(8, partition.getOffset());
assertEquals(11, partition.getLength());
assertEquals(TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION, partition.getType());
}
use of org.eclipse.jface.text.ITypedRegion in project xtext-eclipse by eclipse.
the class ToggleSLCommentAction method isSelectionCommented.
/**
* Is the given selection single-line commented?
*
* @param selection Selection to check
* @return <code>true</code> iff all selected lines are commented
* @since 2.1
*/
protected boolean isSelectionCommented(ISelection selection) {
if (!(selection instanceof ITextSelection))
return false;
ITextSelection textSelection = (ITextSelection) selection;
if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
return false;
IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
try {
IRegion block = getTextBlockFromSelection(textSelection, document);
ITypedRegion[] regions = TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
// [startline, endline, startline, endline, ...]
int[] lines = new int[regions.length * 2];
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
// start line of region
lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
// end line of region
int length = regions[i].getLength();
int offset = regions[i].getOffset() + length;
if (length > 0)
offset--;
lines[j + 1] = (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
}
// Perform the check
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
String[] prefixes = fPrefixesMap.get(regions[i].getType());
if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
return false;
}
return true;
} catch (BadLocationException x) {
// should not happen
log.error(x.getMessage(), x);
}
return false;
}
Aggregations