的Xtext。无法添加HyperlinkHelper

时间:2016-09-27 14:22:25

标签: xtext

我尝试自定义HyperlinkHelper。所以我重写了HypertextDetector

package org.xtext.example.mydsl.ui;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.xtext.ui.editor.hyperlinking.DefaultHyperlinkDetector;
import org.eclipse.xtext.ui.editor.hyperlinking.IHyperlinkHelper;

public class MyHyperlinkDetector extends DefaultHyperlinkDetector {
    private static final String PREFERENCES = ".hyper";

    @Override
    public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
        IDocument document = textViewer.getDocument();
        int offset = region.getOffset();

        // extract relevant characters
        IRegion lineRegion;
        String candidate;
        try {
            lineRegion = document.getLineInformationOfOffset(offset);
            candidate = document.get(lineRegion.getOffset(), lineRegion.getLength());
        } catch (BadLocationException ex) {
            return null;
        }

        // look for keyword
        int index = candidate.indexOf(PREFERENCES);
        if (index != -1) {

            // detect region containing keyword
            IRegion targetRegion = new Region(lineRegion.getOffset() + index, PREFERENCES.length());
            if ((targetRegion.getOffset() <= offset)
                    && ((targetRegion.getOffset() + targetRegion.getLength()) > offset))
                // create link
                return new IHyperlink[] { new PreferencesHyperlink(targetRegion, candidate) };
        }

        return null;
    }

    @Override
    public IHyperlinkHelper getHelper() {
        // TODO Auto-generated method stub
        return new MyHyperlinkHelper();
    }
}

超链接检测器正常工作,但永远不会创建MyHyperlinkHelper。即使我评论方法detectHyperlinks。

我的目标是在我编辑的dsl语法中打开我点击的文件名。这就是我需要HyperlinkHelper的原因。即我需要检查我的子字符串是否是正确的文件名。

如何解决?

此致 弗拉基米尔。

1 个答案:

答案 0 :(得分:1)

不要覆盖该方法。只需使用guice并从impl

中的超类调用该方法
public Class<? extends IHyperlinkHelper> bindIHyperlinkHelper() {
    return DomainmodelHyperlinkHelper.class;
}

或在Xtend中

def Class<? extends IHyperlinkHelper> bindIHyperlinkHelper() {
    return DomainmodelHyperlinkHelper;
}