use of org.eclipse.jface.text.hyperlink.IHyperlinkDetector in project webtools.sourceediting by eclipse.
the class StructuredTextViewerConfigurationXSL method getHyperlinkDetectors.
/**
* (non-Javadoc)
*
* @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getHyperlinkDetectors(org.eclipse.jface.text.source.ISourceViewer)
*/
@Override
public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
if (sourceViewer == null || !fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED))
return null;
IHyperlinkDetector[] superDetectors = super.getHyperlinkDetectors(sourceViewer);
IHyperlinkDetector[] allDetectors = new IHyperlinkDetector[superDetectors.length + 1];
allDetectors[0] = new XSLHyperlinkDetector();
System.arraycopy(superDetectors, 0, allDetectors, 1, superDetectors.length);
return allDetectors;
}
use of org.eclipse.jface.text.hyperlink.IHyperlinkDetector in project liferay-ide by liferay.
the class XmlSearchTestsUtils method getHyperLinks.
private static IHyperlink[] getHyperLinks(IFile file, int nodeType, String... nodeNames) throws Exception {
List<IHyperlink> retval = new ArrayList<IHyperlink>();
IDOMModel domModel = null;
Node targetNode = null;
final StructuredTextViewer viewer = getEditor(file).getTextViewer();
final SourceViewerConfiguration conf = getSourceViewerConfiguraionFromExtensionPoint(file);
if (nodeType == Node.ELEMENT_NODE) {
String elementName = nodeNames[0];
domModel = getDOMModel(file, false);
// the actual node is text node of this.element
targetNode = domModel.getDocument().getElementsByTagName(elementName).item(0).getFirstChild();
} else if (nodeType == Node.ATTRIBUTE_NODE) {
String elementName = nodeNames[0];
String attrName = nodeNames[1];
domModel = getDOMModel(file, false);
NodeList elements = domModel.getDocument().getElementsByTagName(elementName);
targetNode = getElementByAttr(elements, attrName).getAttributes().getNamedItem(attrName);
elements = domModel.getDocument().getElementsByTagName(elementName);
} else {
return new IHyperlink[0];
}
assertNotNull(targetNode);
viewer.refresh();
final IHyperlinkDetector[] hyperlinkDetectors = conf.getHyperlinkDetectors(viewer);
final IRegion region = getRegion(targetNode);
for (IHyperlinkDetector detector : hyperlinkDetectors) {
IHyperlink[] tempHyperlinks = detector.detectHyperlinks(viewer, region, true);
if (tempHyperlinks != null && tempHyperlinks.length > 0) {
retval.addAll(Arrays.asList(tempHyperlinks));
}
}
domModel.releaseFromRead();
return retval.toArray(new IHyperlink[0]);
}
use of org.eclipse.jface.text.hyperlink.IHyperlinkDetector in project xtext-eclipse by eclipse.
the class XtextSourceViewerConfiguration method getHyperlinkDetectors.
@Override
public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
List<IHyperlinkDetector> detectors = new LinkedList<IHyperlinkDetector>();
IHyperlinkDetector[] inheritedDetectors = super.getHyperlinkDetectors(sourceViewer);
if (inheritedDetectors != null) {
for (final IHyperlinkDetector detector : inheritedDetectors) {
detectors.add(new IHyperlinkDetector() {
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
try {
IHyperlink[] result = detector.detectHyperlinks(textViewer, region, canShowMultipleHyperlinks);
// but null (see org.eclipse.jface.text.hyperlink.HyperlinkManager.findHyperlinks(IRegion))
if (result != null && result.length == 0) {
return null;
}
return result;
} catch (Throwable e) {
// fail safe hyperlink detector - prevent others
// from failing
}
return null;
}
});
}
}
detectors.add(detector);
return detectors.toArray(new IHyperlinkDetector[detectors.size()]);
}
use of org.eclipse.jface.text.hyperlink.IHyperlinkDetector 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.hyperlink.IHyperlinkDetector in project egit by eclipse.
the class HyperlinkTokenScanner method getHyperlinkDetectors.
@NonNull
private Set<IHyperlinkDetector> getHyperlinkDetectors() {
Set<IHyperlinkDetector> allDetectors = new LinkedHashSet<>();
IHyperlinkDetector[] configuredDetectors = configuration.getHyperlinkDetectors(viewer);
if (configuredDetectors != null && configuredDetectors.length > 0) {
for (IHyperlinkDetector detector : configuredDetectors) {
allDetectors.add(detector);
}
if (preferenceStore.getBoolean(URL_HYPERLINK_DETECTOR_KEY) || !preferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_HYPERLINKS_ENABLED)) {
return allDetectors;
}
// URLHyperlinkDetector can only detect hyperlinks at the start of
// the range. We need one that can detect all hyperlinks in a given
// region.
allDetectors.add(new MultiURLHyperlinkDetector());
}
return allDetectors;
}
Aggregations