use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.
the class RuleBasedPartitioner method documentChanged2.
@Override
public IRegion documentChanged2(DocumentEvent e) {
try {
IDocument d = e.getDocument();
Position[] category = d.getPositions(fPositionCategory);
int first = 0;
int reparseStart = 0;
int originalSize = category.length;
if (originalSize > 0) {
/*
* determine character position at which the scanner starts:
* first position behind the last non-default partition the actual position is not involved with
*/
first = d.computeIndexInCategory(fPositionCategory, e.getOffset());
Position p = null;
do {
--first;
if (first < 0)
break;
p = category[first];
} while (p.overlapsWith(e.getOffset(), e.getLength()) || (e.getOffset() == fPreviousDocumentLength && (p.getOffset() + p.getLength() == fPreviousDocumentLength)));
fPositionUpdater.update(e);
for (Position element : category) {
p = element;
if (p.isDeleted) {
rememberDeletedOffset(e.getOffset());
break;
}
}
category = d.getPositions(fPositionCategory);
if (first >= 0) {
p = category[first];
reparseStart = p.getOffset() + p.getLength();
}
++first;
}
fScanner.setRange(d, reparseStart, d.getLength() - reparseStart);
int lastScannedPosition = reparseStart;
IToken token = fScanner.nextToken();
while (!token.isEOF()) {
String contentType = getTokenContentType(token);
if (!isSupportedContentType(contentType)) {
token = fScanner.nextToken();
continue;
}
int start = fScanner.getTokenOffset();
int length = fScanner.getTokenLength();
lastScannedPosition = start + length - 1;
// remove all affected positions
while (first < category.length) {
TypedPosition p = (TypedPosition) category[first];
if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!d.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) {
rememberRegion(p.offset, p.length);
d.removePosition(fPositionCategory, p);
++first;
} else
break;
}
// if position already exists we are done
if (d.containsPosition(fPositionCategory, start, length))
return createRegion();
// insert the new type position
try {
d.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
rememberRegion(start, length);
} catch (BadPositionCategoryException x) {
} catch (BadLocationException x) {
}
token = fScanner.nextToken();
}
// remove all positions behind lastScannedPosition since there aren't any further types
if (lastScannedPosition != reparseStart) {
// if this condition is not met, nothing has been scanned because of a delete
++lastScannedPosition;
}
first = d.computeIndexInCategory(fPositionCategory, lastScannedPosition);
TypedPosition p;
while (first < category.length) {
p = (TypedPosition) category[first++];
d.removePosition(fPositionCategory, p);
rememberRegion(p.offset, p.length);
}
} catch (BadPositionCategoryException x) {
// should never happen on connected documents
} catch (BadLocationException x) {
}
return createRegion();
}
use of org.eclipse.jface.text.BadPositionCategoryException in project linuxtools by eclipse.
the class SpecfileChangelogFormatter method mergeChangelog.
@Override
public String mergeChangelog(String dateLine, String functionGuess, String defaultContent, IEditorPart changelog, String changeLogLocation, String fileLocation) {
if (changelog instanceof SpecfileEditor) {
SpecfileEditor specEditor = (SpecfileEditor) changelog;
IDocument doc = specEditor.getDocumentProvider().getDocument(specEditor.getEditorInput());
String[] positionCategories = doc.getPositionCategories();
String contentTypesPositionCategory = null;
// we need to find the one we want
for (String positionCategory : positionCategories) {
if (positionCategory.startsWith("__content_types_category")) {
// $NON-NLS-1$
contentTypesPositionCategory = positionCategory;
}
}
if (contentTypesPositionCategory != null) {
try {
Position[] sectionPositions = doc.getPositions(contentTypesPositionCategory);
ITypedRegion changelogPartition = null;
for (Position position : sectionPositions) {
int offset = position.getOffset();
ITypedRegion partition = doc.getPartition(offset);
if (partition.getType().equals(SpecfilePartitionScanner.SPEC_CHANGELOG)) {
changelogPartition = partition;
}
}
// Temporary buffer for changelog text
StringBuilder buf = new StringBuilder();
String changelogText = EMPTY_STRING;
String[] changelogLines = new String[] {};
int offset = doc.getLength();
int length = 0;
// there was no changelog partition add it.
if (changelogPartition == null) {
// make sure there are at least 2 newlines before
// the changelog section
String endString = doc.get(doc.getLength() - 2, 2);
if (endString.charAt(0) != '\n') {
buf.append('\n');
}
if (endString.charAt(1) != '\n') {
buf.append('\n');
}
// $NON-NLS-1$
buf.append("%changelog\n");
// or get the old text and add the header
} else {
offset = changelogPartition.getOffset();
length = changelogPartition.getLength();
changelogText = doc.get(offset, length);
// get old changelog text
// $NON-NLS-1$
changelogLines = changelogText.split("\n");
// add the %changelog header
buf.append(changelogLines[0]).append('\n');
}
// now add the entry stub
buf.append(dateLine);
buf.append('\n');
// $NON-NLS-1$
buf.append("- \n");
// set the cursor at the end of the entry,
// count back 2 '\n's
int newCursorOffset = offset + buf.length() - 1;
for (int i = 1; i < changelogLines.length; i++) {
buf.append('\n').append(changelogLines[i]);
}
// always terminate the file with a new line
if (changelogLines.length > 0) {
buf.append('\n');
}
doc.replace(offset, length, buf.toString());
specEditor.selectAndReveal(newCursorOffset, 0);
specEditor.setFocus();
} catch (BadPositionCategoryException | BadLocationException e) {
SpecfileLog.logError(e);
}
}
}
return EMPTY_STRING;
}
use of org.eclipse.jface.text.BadPositionCategoryException in project tmdm-studio-se by Talend.
the class XMLDocumentPartitioner method findPreviousNonWhiteSpacePosition.
public TypedPosition findPreviousNonWhiteSpacePosition(int offset) {
try {
int index = document.computeIndexInCategory(positionCategory, offset);
Position[] positions = document.getPositions(positionCategory);
if (positions.length == 0) {
return null;
}
if (index == positions.length) {
index--;
}
for (; index >= 0; index--) {
TypedPosition position = (TypedPosition) positions[index];
if (position instanceof XMLNode) {
XMLNode node = (XMLNode) position;
if (!node.isEmpty())
return node;
}
}
} catch (BadLocationException e) {
e.printStackTrace();
} catch (BadPositionCategoryException e) {
e.printStackTrace();
}
return null;
}
use of org.eclipse.jface.text.BadPositionCategoryException in project tmdm-studio-se by Talend.
the class XMLDocumentPartitioner method getPartition.
public ITypedRegion getPartition(int offset) {
try {
Position[] category = document.getPositions(positionCategory);
if (category == null || category.length == 0) {
// $NON-NLS-1$
return new TypedRegion(0, document.getLength(), "__dftl_partition_content_type");
}
int index = document.computeIndexInCategory(positionCategory, offset);
if (index < category.length) {
TypedPosition next = (TypedPosition) category[index];
if (offset == next.offset) {
return new TypedRegion(next.getOffset(), next.getLength(), next.getType());
}
if (index == 0) {
// $NON-NLS-1$
return new TypedRegion(0, next.offset, "__dftl_partition_content_type");
}
TypedPosition previous = (TypedPosition) category[index - 1];
if (previous.includes(offset)) {
return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
}
int endOffset = previous.getOffset() + previous.getLength();
// $NON-NLS-1$
return new TypedRegion(endOffset, next.getOffset() - endOffset, "__dftl_partition_content_type");
}
TypedPosition previous = (TypedPosition) category[category.length - 1];
if (previous.includes(offset)) {
return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
}
int endOffset = previous.getOffset() + previous.getLength();
// $NON-NLS-1$
return new TypedRegion(endOffset, document.getLength() - endOffset, "__dftl_partition_content_type");
} catch (BadPositionCategoryException _ex) {
} catch (BadLocationException _ex) {
}
// $NON-NLS-1$
return new TypedRegion(0, document.getLength(), "__dftl_partition_content_type");
}
use of org.eclipse.jface.text.BadPositionCategoryException in project tmdm-studio-se by Talend.
the class XMLDocumentPartitioner method initialize.
protected void initialize() {
partitionScanner.setRange(document, 0, document.getLength());
try {
for (IToken token = partitionScanner.nextToken(); !token.isEOF(); token = partitionScanner.nextToken()) {
String contentType = getTokenContentType(token);
if (isSupportedContentType(contentType)) {
TypedPosition p = createPosition(partitionScanner.getTokenOffset(), partitionScanner.getTokenLength(), contentType);
addPosition(document, positionCategory, p);
}
}
} catch (BadLocationException _ex) {
} catch (BadPositionCategoryException _ex) {
}
}
Aggregations