use of org.eclipse.jface.text.Position in project translationstudio8 by heartsome.
the class InnerTagUtil method getStyledTagRange.
// private static Pattern PATTERN = Pattern.compile("(" + INVISIBLE_CHAR + "\\d+)?" + INVISIBLE_CHAR
// + "(x|bx|ex|g|bpt|ept|mrk|sub|ph|it)" + INVISIBLE_CHAR + "(\\d+" + INVISIBLE_CHAR + ")?");
/**
* 得到一段带标记样式的文本中,指定位置上的标记的索引范围。
* @param tagStyledText
* 带标记样式的文本。
* @param offset
* @return 若指定位置上不存在标记,则返回 null;
*/
public static Position getStyledTagRange(String tagStyledText, int offset) {
Matcher m = PATTERN.matcher(tagStyledText);
while (m.find()) {
int start = m.start();
int end = m.end();
if (start < offset && offset < end) {
return new Position(start, end - start);
}
}
return null;
}
use of org.eclipse.jface.text.Position in project translationstudio8 by heartsome.
the class Comparator method Compare.
/**
* 用于比较的参照文本(不含内部标记)
* @param referenceText
* @param text
* ;
*/
public static void Compare(String referenceText, StyledText text) {
ColorConfigBean colorCfgBean = ColorConfigBean.getInstance();
final Color differencefg = colorCfgBean.getSrcDiffFgColor();
final Color differencebg = colorCfgBean.getSrcDiffBgColor();
Position[] tagRanges = InnerTagUtil.getStyledTagRanges(text.getText());
TokenComparator left = new TokenComparator(InnerTagUtil.getDisplayValueWithoutTags(text.getText()));
TokenComparator right = new TokenComparator(referenceText);
ArrayList<StyleRange> styleRanges = new ArrayList<StyleRange>();
RangeDifference[] e = RangeDifferencer.findRanges(left, right);
for (int i = 0; i < e.length; i++) {
RangeDifference es = e[i];
int leftStart = es.leftStart();
int leftEnd = es.leftEnd();
int lStart = left.getTokenStart(leftStart);
int lEnd = left.getTokenStart(leftEnd);
if (es.kind() == RangeDifference.CHANGE) {
int start = lStart;
int end = lEnd;
if (tagRanges.length > 0) {
for (Position tagRange : tagRanges) {
int tagStart = tagRange.getOffset();
int tagEnd = tagRange.getOffset() + tagRange.getLength();
if (tagStart <= start) {
start += tagRange.getLength();
end += tagRange.getLength();
} else if (start < tagStart && tagStart < end) {
StyleRange range = new StyleRange(start, tagStart - start, differencefg, differencebg);
styleRanges.add(range);
start = tagEnd;
end += tagRange.getLength();
} else {
break;
}
}
}
StyleRange range = new StyleRange(start, end - start, differencefg, differencebg);
styleRanges.add(range);
}
}
for (int i = 0; i < styleRanges.size(); i++) {
text.setStyleRange(styleRanges.get(i));
text.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
differencefg.dispose();
differencebg.dispose();
}
});
}
}
use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.
the class DocumentPartitioner method documentChanged2.
/**
* {@inheritDoc}
* <p>
* May be extended by subclasses.
* </p>
*
* @since 2.2
*/
@Override
public synchronized IRegion documentChanged2(DocumentEvent e) {
if (!fIsInitialized)
return null;
try {
Assert.isTrue(e.getDocument() == fDocument);
Position[] category = getPositions();
int reparseStart = e.getOffset();
String contentType = null;
int newLength = e.getText() == null ? 0 : e.getText().length();
int oldPosition = fDocument.computeIndexInCategory(fPositionCategory, reparseStart);
if (oldPosition > 0) {
Position oldPartition = category[oldPosition - 1];
if (oldPartition.offset + oldPartition.length > reparseStart)
reparseStart = category[oldPosition - 1].offset;
}
fScanner.setPartialRange(fDocument, reparseStart, fDocument.getLength() - reparseStart, /* ignore contentType */
null, reparseStart);
int behindLastScannedPosition = reparseStart;
IToken token = fScanner.nextToken();
int actualReparseStart = fScanner.getTokenOffset();
int partitionStart = -1;
int first = fDocument.computeIndexInCategory(fPositionCategory, actualReparseStart);
if (first > 0) {
TypedPosition partition = (TypedPosition) category[first - 1];
if (partition.includes(actualReparseStart)) {
partitionStart = partition.getOffset();
contentType = partition.getType();
if (e.getOffset() == partition.getOffset() + partition.getLength())
actualReparseStart = partitionStart;
--first;
} else if (actualReparseStart == e.getOffset() && actualReparseStart == partition.getOffset() + partition.getLength()) {
partitionStart = partition.getOffset();
contentType = partition.getType();
actualReparseStart = partitionStart;
--first;
} else {
partitionStart = partition.getOffset() + partition.getLength();
contentType = IDocument.DEFAULT_CONTENT_TYPE;
if (actualReparseStart != partitionStart) {
String message = String.format("Detected unexpected state in document partitioner. Please file a bug with the following information attached:%n" + "Document content after the event was applied:%n" + ">>>%s<<<%n" + "Document event: %s", fDocument.get(), String.valueOf(e));
log.error(message);
}
}
}
fScanner.setPartialRange(fDocument, actualReparseStart, fDocument.getLength() - actualReparseStart, contentType, actualReparseStart);
behindLastScannedPosition = actualReparseStart;
token = fScanner.nextToken();
fPositionUpdater.update(e);
for (int i = first; i < category.length; i++) {
Position p = category[i];
if (p.isDeleted) {
rememberDeletedOffset(e.getOffset());
break;
}
}
clearPositionCache();
category = getPositions();
while (!token.isEOF()) {
contentType = getTokenContentType(token);
if (!isSupportedContentType(contentType)) {
token = fScanner.nextToken();
continue;
}
int start = fScanner.getTokenOffset();
int length = fScanner.getTokenLength();
behindLastScannedPosition = start + length;
int lastScannedPosition = behindLastScannedPosition - 1;
// remove all affected positions
while (first < category.length) {
TypedPosition p = (TypedPosition) category[first];
if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!fDocument.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) {
rememberRegion(p.offset, p.length);
fDocument.removePosition(fPositionCategory, p);
++first;
} else
break;
}
// area covered by the event, we are done
if (fDocument.containsPosition(fPositionCategory, start, length)) {
if (lastScannedPosition >= e.getOffset() + newLength)
return createRegion();
++first;
} else {
// insert the new type position
try {
fDocument.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
rememberRegion(start, length);
} catch (BadPositionCategoryException x) {
} catch (BadLocationException x) {
}
}
token = fScanner.nextToken();
}
first = fDocument.computeIndexInCategory(fPositionCategory, behindLastScannedPosition);
clearPositionCache();
category = getPositions();
TypedPosition p;
while (first < category.length) {
p = (TypedPosition) category[first++];
fDocument.removePosition(fPositionCategory, p);
rememberRegion(p.offset, p.length);
}
} catch (BadPositionCategoryException x) {
// should never happen on connected documents
} catch (BadLocationException x) {
} finally {
clearPositionCache();
}
return createRegion();
}
use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.
the class DocumentPartitioner method getPartition.
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*
* @since 2.2
*/
@Override
public synchronized ITypedRegion getPartition(int offset) {
checkInitialization();
try {
Position[] category = getPositions();
if (category == null || category.length == 0)
return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
int index = fDocument.computeIndexInCategory(fPositionCategory, 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)
return new TypedRegion(0, next.offset, IDocument.DEFAULT_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();
return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
}
TypedPosition previous = (TypedPosition) category[category.length - 1];
if (previous.includes(offset)) {
return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
}
if (isOpenSingleLineCommentPartition(previous, offset)) {
return new TypedRegion(previous.getOffset(), previous.getLength() + 1, previous.getType());
}
int endOffset = previous.getOffset() + previous.getLength();
return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
} catch (BadPositionCategoryException x) {
} catch (BadLocationException x) {
}
return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
}
use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.
the class AbstractProblemHover method getAnnotations.
public List<Annotation> getAnnotations(final int lineNumber, final int offset) {
if (getAnnotationModel() == null) {
return Collections.emptyList();
}
final Iterator<?> iterator = getAnnotationModel().getAnnotationIterator();
List<Annotation> result = Lists.newArrayList();
while (iterator.hasNext()) {
final Annotation annotation = (Annotation) iterator.next();
if (isHandled(annotation)) {
Position position = getAnnotationModel().getPosition(annotation);
if (position != null) {
final int start = position.getOffset();
final int end = start + position.getLength();
if (offset > 0 && !(start <= offset && offset <= end)) {
continue;
}
try {
if (lineNumber != getDocument().getLineOfOffset(start)) {
continue;
}
} catch (final Exception x) {
continue;
}
if (!isLineDiffInfo(annotation)) {
result.add(annotation);
}
}
}
}
return result;
}
Aggregations