AsyncCompletionQuery.query中的null document和-1 caretOffset

时间:2013-12-08 00:26:36

标签: netbeans-platform

您好我使用JavaCC实现了一个新的语言词法分析器。这绝不是一个很好的实现,因为这是我第一次编写JavaCC语法文件。但我已经能够使语法突出显示工作。我没有必要实现解析器,所以我没有。但现在我想为关键字提供代码完成。请在下面找到CompletionProvider,CompletionItem和ExampleKeywords类的实现。出于某种原因,dcmnt方法中的caretOffsetquery分别设置为null-1

代码完成显示所有关键字,但由于Document和caretOffset已关闭,我无法在用户输入内容时过滤关键字。

我是否需要实现解析器才能使用代码完成?实际上,对解决这个问题的任何帮助表示赞赏。

ExampleKeywordCompletionProvider

@MimeRegistration(mimeType = "text/x-example", service = CompletionProvider.class)
public class ExampleKeywordCompletionProvider implements CompletionProvider {

    public ExampleKeywordCompletionProvider() {
    }

    @Override
    public CompletionTask createTask(int queryType, JTextComponent jtc) {
        /**
         * We need to test whether the user pressed the keys applicable to the COMPLETION_QUERY_TYPE.
         */
        if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE) return null;

        return new AsyncCompletionTask(new AsyncCompletionQuery(){


            @Override
            protected void query(CompletionResultSet crs, Document dcmnt, int caretOffset) {
                ExampleKeywords.find(crs, dcmnt, caretOffset).finish();
            }


        });
    }

    @Override
    public int getAutoQueryTypes(JTextComponent jtc, String string) {
        return 1;
    }

}

ExampleKeywordCompletionItem

public class ExampleKeywordCompletionItem implements CompletionItem {

    private String text;
    private static ImageIcon fieldIcon =
            new ImageIcon(ImageUtilities.loadImage("/org/netbeans/modules/plantumlnb/icon.png"));
    private static Color fieldColor = Color.decode("0x0000B2");
    private int caretOffset;
    private int dotOffset;

    ExampleKeywordCompletionItem(String keyword, int dotOffset, int caretOffset) {
        this.text = keyword;
        this.dotOffset = dotOffset;
        this.caretOffset = caretOffset;
    }

    @Override
    public void defaultAction(JTextComponent component) {
        try {
            StyledDocument doc = (StyledDocument) component.getDocument();
            //Here we remove the characters starting at the start offset
            //and ending at the point where the caret is currently found:
//            doc.remove(dotOffset, caretOffset - dotOffset);
            doc.insertString(dotOffset, text, null);
            //This statement will close the code completion box:
            Completion.get().hideAll();
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    @Override
    public void processKeyEvent(KeyEvent ke) {
    }

    @Override
    public int getPreferredWidth(Graphics graphics, Font font) {
        return CompletionUtilities.getPreferredWidth(text, null, graphics, font);
    }

    @Override
    public void render(Graphics g, Font defaultFont, Color color, Color color1, int width, int height, boolean selected) {        
        CompletionUtilities.renderHtml(fieldIcon, text, null, g, defaultFont, 
                (selected ? Color.white : fieldColor), width, height, selected);
    }

    @Override
    public CompletionTask createDocumentationTask() {
        return null;
    }

    @Override
    public CompletionTask createToolTipTask() {
        return null;
    }

    @Override
    public boolean instantSubstitution(JTextComponent jtc) {
        return false;
    }

    @Override
    public int getSortPriority() {
        return 0;
    }

    @Override
    public CharSequence getSortText() {
        return text;
    }

    @Override
    public CharSequence getInsertPrefix() {
        return text;
    }

}

ExampleKeywords

public class ExampleKeywords {

    public static final List<String> keywords = Arrays.asList(
            "as",
            "also",
            "autonumber",
            "title",
            "newpage",
            "box",
            "alt",
            "else",
            "opt",
            "loop",
            "par",
            "break",
            "critical",
            "note left",
            "note left of",
            "note left on link",
            "note right",
            "note right of",
            "note right on link",
            "note top",
            "note top of",
            "note top on link",
            "note bottom",
            "note bottom of",
            "note bottom on link",
            "note over",
            "note on",
            "end note",
            "ref over",
            "...",
            "group",
            "left",
            "right",
            "of",
            "on",
            "link",
            "over",
            "end",
            "activate",
            "deactivate",
            "destroy",
            "create",
            "footbox",
            "skinparam",
            "skin",
            "top",
            "bottom",
            "top to bottom direction",
            "package",
            "namespace",
            "page",
            "up",
            "down",
            "if",
            "endif",
            "partition",
            "footer",
            "header",
            "center",
            "rotate",
            "ref",
            "return",
            "is",
            "repeat",
            "start",
            "stop",
            "while",
            "endwhile",
            "fork",
            "again",
            /* TYPES */
            "actor",
            "participant",
            "usecase",
            "class",
            "interface",
            "abstract",
            "annotation",
            "enum",
            "component",
            "state",
            "object",
            /* PARTS */
            "artifact",
            "folder",
            "rect",
            "node",
            "frame",
            "cloud",
            "database",
            "storage",
            "agent",
            "boundary",
            "control",
            "entity",
            /* VISIBILITY */
            "show",
            "hide",
            "empty members"
    );


    public static List<String> find(String input) {
        List<String> allMatches = new ArrayList<>();

        for (String keyword : keywords) {
            if (keyword.contains(input)) {
                allMatches.add(keyword);
            }
        }

        return allMatches;
    }

    public static CompletionResultSet find(CompletionResultSet crs, Document document, int caretOffset) {

        for (String keyword : keywords) {
                crs.addItem(new ExampleKeywordCompletionItem(keyword, 0, 0));
        }

        return crs;
    }



}

1 个答案:

答案 0 :(得分:1)

完成情况:Netbeans Forums上有人找到了解决问题的方法。

您需要将组件传递给AsyncCompletionTask

@Override
public CompletionTask createTask(int queryType, JTextComponent jtc) {
    /**
     * We need to test whether the user pressed the keys applicable to the COMPLETION_QUERY_TYPE.
     */
    if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE) return null;

    return new AsyncCompletionTask(new AsyncCompletionQuery(){

        @Override
        protected void query(CompletionResultSet crs, Document dcmnt, int caretOffset) {
            ExampleKeywords.find(crs, dcmnt, caretOffset).finish();
        }
    }, jtc); // <--
}