use of org.eclipse.jface.text.rules.IToken in project egit by eclipse.
the class HyperlinkTokenScanner method nextToken.
@Override
public IToken nextToken() {
tokenStart = currentOffset;
if (currentOffset >= endOfRange) {
hyperlinksOnLine.clear();
return Token.EOF;
}
if (hyperlinkDetectors != null && !hyperlinkDetectors.isEmpty()) {
try {
IRegion currentLine = document.getLineInformationOfOffset(currentOffset);
if (currentLine.getOffset() != lastLineStart) {
// Compute all hyperlinks in the line
hyperlinksOnLine.clear();
Iterator<IHyperlinkDetector> detectors = hyperlinkDetectors.iterator();
while (detectors.hasNext()) {
IHyperlinkDetector hyperlinkDetector = detectors.next();
// hyperlinks open also if no modifier key is active.
if (hyperlinkDetector instanceof HyperlinkSourceViewer.NoMaskHyperlinkDetector) {
continue;
}
IHyperlink[] newLinks = null;
try {
newLinks = hyperlinkDetector.detectHyperlinks(viewer, currentLine, false);
} catch (RuntimeException e) {
// Do *not* log: we have no way of identifying the
// broken hyperlink detector to ignore it in the
// future. Since we re-get the contributed detectors
// frequently, we'll get new instances of
// HyperlinkDetectorDelegate, and that doesn't give
// access to the extension id. So even if we remove
// the detector here, we may acquire a new broken
// instance again and then log over and over again,
// which is hyper-bothersome if the error log is
// open and set to activate on new errors. And
// anyway the problem is not in EGit.
detectors.remove();
}
if (newLinks != null && newLinks.length > 0) {
Collections.addAll(hyperlinksOnLine, newLinks);
}
}
// Sort them by offset, and with increasing length
Collections.sort(hyperlinksOnLine, new Comparator<IHyperlink>() {
@Override
public int compare(IHyperlink a, IHyperlink b) {
int diff = a.getHyperlinkRegion().getOffset() - b.getHyperlinkRegion().getOffset();
if (diff != 0) {
return diff;
}
return a.getHyperlinkRegion().getLength() - b.getHyperlinkRegion().getLength();
}
});
lastLineStart = currentLine.getOffset();
}
if (!hyperlinksOnLine.isEmpty()) {
// Find first hyperlink for the position. We may have to
// skip a few in case there are several hyperlinks at the
// same position and with the same length.
Iterator<IHyperlink> iterator = hyperlinksOnLine.iterator();
while (iterator.hasNext()) {
IHyperlink next = iterator.next();
IRegion linkRegion = next.getHyperlinkRegion();
int linkEnd = linkRegion.getOffset() + linkRegion.getLength();
if (currentOffset >= linkEnd) {
iterator.remove();
} else if (linkRegion.getOffset() <= currentOffset) {
// This is our link
iterator.remove();
int end = Math.min(endOfRange, linkEnd);
if (end > currentOffset) {
currentOffset = end;
return hyperlinkToken;
}
} else {
// Next hyperlink is beyond current position
break;
}
}
}
} catch (BadLocationException e) {
// Ignore and keep going
}
}
int actualOffset = currentOffset;
IToken token = scanToken();
if (token != null && actualOffset < currentOffset) {
return token;
}
currentOffset = actualOffset + 1;
return defaultToken;
}
use of org.eclipse.jface.text.rules.IToken in project egit by eclipse.
the class HyperlinkTokenScannerTest method assertTokens.
@SuppressWarnings("boxing")
private void assertTokens(String text, int offset, int length, IHyperlinkDetector[] hyperlinkDetectors, String expected) {
assertEquals("Test definition problem: 'expected' length mismatch", text.length(), expected.length());
IDocument testDocument = new Document(text);
when(viewer.getDocument()).thenReturn(testDocument);
when(configuration.getHyperlinkDetectors(viewer)).thenReturn(hyperlinkDetectors);
when(preferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_HYPERLINKS_ENABLED)).thenReturn(true);
when(preferenceStore.getBoolean("org.eclipse.ui.internal.editors.text.URLHyperlinkDetector")).thenReturn(hyperlinkDetectors.length == 0 || (hyperlinkDetectors[0] instanceof CrashingHyperlinkDetector));
HyperlinkTokenScanner scanner = new HyperlinkTokenScanner(configuration, viewer, preferenceStore, null);
scanner.setRangeAndColor(testDocument, offset, length, null);
IToken token = null;
char[] found = new char[text.length()];
Arrays.fill(found, ' ');
while (!(token = scanner.nextToken()).isEOF()) {
int tokenOffset = scanner.getTokenOffset();
int tokenLength = scanner.getTokenLength();
char ch = 'x';
Object data = token.getData();
if (data == null) {
ch = 'D';
} else if (data instanceof TextAttribute) {
int style = ((TextAttribute) data).getStyle();
if ((style & TextAttribute.UNDERLINE) != 0) {
ch = 'H';
}
}
Arrays.fill(found, tokenOffset, tokenOffset + tokenLength, ch);
}
assertEquals("Unexpected tokens", expected, new String(found));
}
use of org.eclipse.jface.text.rules.IToken in project mylyn.docs by eclipse.
the class MarkupTokenScannerTest method testTextileLinkWithStyle.
public void testTextileLinkWithStyle() {
IDocument document = new Document();
FastMarkupPartitioner partitioner = new FastMarkupPartitioner();
partitioner.setMarkupLanguage(new TextileLanguage());
String markup = "\"_text_\":http://example.com";
document.set(markup);
partitioner.connect(document);
document.setDocumentPartitioner(partitioner);
partitioner.computePartitioning(0, document.getLength(), false);
tokenScanner.setRange(document, 0, document.getLength());
List<MarkupTokenScanner.Token> tokens = new ArrayList<MarkupTokenScanner.Token>();
for (IToken token = tokenScanner.nextToken(); token != Token.EOF; token = tokenScanner.nextToken()) {
tokens.add((MarkupTokenScanner.Token) token);
}
// expecting:
// Token [offset=0, length=1]
// Token [offset=1, length=6]
// Token [offset=7, length=20]
assertEquals(3, tokens.size());
assertEquals(0, tokens.get(0).getOffset());
assertEquals(1, tokens.get(0).getLength());
assertFalse(tokens.get(0).getFontState().isItalic());
assertEquals(1, tokens.get(1).getOffset());
assertEquals(6, tokens.get(1).getLength());
assertTrue(tokens.get(1).getFontState().isItalic());
assertEquals(7, tokens.get(2).getOffset());
assertEquals(20, tokens.get(2).getLength());
assertFalse(tokens.get(2).getFontState().isItalic());
}
use of org.eclipse.jface.text.rules.IToken in project erlide_eclipse by erlang.
the class ErlangCharRuleTest method octal_3.
@Test
public void octal_3() {
final ErlangCharRule rule = new ErlangCharRule(token);
scanner = new StringCharacterScanner("$\\12");
final IToken tk = rule.evaluate(scanner);
assertEquals(token, tk);
assertEquals("", scanner.getCurrentContent());
assertEquals("$\\12", scanner.getScannedContent());
}
use of org.eclipse.jface.text.rules.IToken in project erlide_eclipse by erlang.
the class ErlangCharRuleTest method hex_3.
@Test
public void hex_3() {
final ErlangCharRule rule = new ErlangCharRule(token);
scanner = new StringCharacterScanner("$\\x{cFzE1}23z ");
final IToken tk = rule.evaluate(scanner);
assertEquals(token, tk);
assertEquals("{cFzE1}23z ", scanner.getCurrentContent());
assertEquals("$\\x", scanner.getScannedContent());
}
Aggregations