Search in sources :

Example 11 with IHyperlinkDetector

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;
}
Also used : IHyperlinkDetector(org.eclipse.jface.text.hyperlink.IHyperlinkDetector) XSLHyperlinkDetector(org.eclipse.wst.xsl.ui.internal.editor.XSLHyperlinkDetector)

Example 12 with IHyperlinkDetector

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]);
}
Also used : IHyperlinkDetector(org.eclipse.jface.text.hyperlink.IHyperlinkDetector) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) IRegion(org.eclipse.jface.text.IRegion) SourceViewerConfiguration(org.eclipse.jface.text.source.SourceViewerConfiguration) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) StructuredTextViewer(org.eclipse.wst.sse.ui.internal.StructuredTextViewer)

Example 13 with IHyperlinkDetector

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()]);
}
Also used : IHyperlinkDetector(org.eclipse.jface.text.hyperlink.IHyperlinkDetector) LinkedList(java.util.LinkedList) IRegion(org.eclipse.jface.text.IRegion) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 14 with IHyperlinkDetector

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;
}
Also used : IHyperlinkDetector(org.eclipse.jface.text.hyperlink.IHyperlinkDetector) IRegion(org.eclipse.jface.text.IRegion) IToken(org.eclipse.jface.text.rules.IToken) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 15 with IHyperlinkDetector

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IHyperlinkDetector(org.eclipse.jface.text.hyperlink.IHyperlinkDetector) NonNull(org.eclipse.jgit.annotations.NonNull)

Aggregations

IHyperlinkDetector (org.eclipse.jface.text.hyperlink.IHyperlinkDetector)19 ArrayList (java.util.ArrayList)6 IRegion (org.eclipse.jface.text.IRegion)4 Point (org.eclipse.swt.graphics.Point)4 IAdaptable (org.eclipse.core.runtime.IAdaptable)3 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)3 HyperlinkDetectorRegistry (org.eclipse.ui.texteditor.HyperlinkDetectorRegistry)3 List (java.util.List)2 Map (java.util.Map)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 ITextViewerExtension6 (org.eclipse.jface.text.ITextViewerExtension6)2 IContentAssistantExtension2 (org.eclipse.jface.text.contentassist.IContentAssistantExtension2)2 IContentAssistantExtension4 (org.eclipse.jface.text.contentassist.IContentAssistantExtension4)2 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)2 SourceViewerConfiguration (org.eclipse.jface.text.source.SourceViewerConfiguration)2 SafeRunnable (org.eclipse.jface.util.SafeRunnable)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IColumnSupport (org.eclipse.ui.texteditor.rulers.IColumnSupport)2 RulerColumnDescriptor (org.eclipse.ui.texteditor.rulers.RulerColumnDescriptor)2