如何为Xtext Web编辑器正确使用IdeContentProposalProvider

时间:2018-10-31 14:25:02

标签: dsl xtext xtend content-assist

我无法让提案提供商正常工作。我的目标是提供用户可以创建的所有可能导入的列表。我可以验证该类是否已正确绑定,并且正在根据编辑器的协助请求被调用。我什至可以看到正在创建提案并将其传递给接受者,但是编辑器中什么都没有显示。

所以我的问题可能是两部分:

  1. 您能在下面的代码中看到我可能做错了什么,以解释未在编辑器中显示的内容辅助吗
  2. 您能解释一下Assignment,RuleCall和Keyword分配方法之间的区别吗?我发现所有三种方法都针对相同的内容辅助请求,因此我到底要编写什么不同的代码,它们又做不同的事情?

如果有任何效果,我也在使用CodeMirror。

    class mydslContentAssist extends IdeContentProposalProvider{
    @Inject extension mydslGrammarAccess stAccess
    @Inject mydslGlobalScopeProvider sp
    @Inject extension IQualifiedNameProvider

    override dispatch createProposals(Assignment assignment, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
        switch(assignment) {
            case stAccess.libraryModelAccess.importsAssignment_0: {
                var libs = sp.descriptionData.getExportedObjectsByType(ModelPackage.eINSTANCE.libraryModel)
                for (lib : libs.filter[(EObjectOrProxy as LibraryModel).name.startsWith(context.prefix)]) {
                    var proposal = proposalCreator.createProposal('import ' + (lib.EObjectOrProxy as LibraryModel).name + ".*;", context) [
                        source = lib
                        description = "import entire library contents"
                    ]
                    acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
                }
            }
            default : {
                super._createProposals(assignment, context, acceptor)
            }
        }
    }
    override dispatch createProposals(RuleCall rulecall, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
        switch(rulecall.rule) {
            case importRule: {
                var libs = sp.descriptionData.getExportedObjectsByType(ModelPackage.eINSTANCE.libraryModel)
                for (lib : libs) {
                    var proposal = proposalCreator.createProposal('import ' + (lib.EObjectOrProxy as LibraryModel).name + ".*;", context) [
                        source = lib
                        description = "import entire library contents"
                    ]
                    acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
                }
            }
            default : {
                super._createProposals(rulecall, context, acceptor)
            }
        }
    }

    override dispatch createProposals(Keyword keyword, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
        switch (keyword) {
            case stAccess.importAccess.importKeyword_0: {
                var libs = sp.descriptionData.getExportedObjectsByType(ModelPackage.eINSTANCE.libraryModel)
                for (lib : libs) {
                    var proposal = proposalCreator.createProposal('import ' + (lib.EObjectOrProxy as LibraryModel).name + ".*;", context) [
                        source = lib
                        description = "import entire library contents"
                    ]
                    acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
                }
            }
            default: {
                super._createProposals(keyword, context, acceptor)
            }

        }
    }
    }

编辑:仅当键入'import'关键字时调用内容帮助时,我的代码才会被击中。但是键入importedNamespace字符串时不可以

1 个答案:

答案 0 :(得分:0)

我尝试了一下,无法复制

语法:

Model:
    imports+=Import*;

Import: 'import' importedNamespace=QualifiedNameWithWildcard ';'
    ;
    QualifiedNameWithWildcard:
        ID ("." ID)* (".*")?
;

和展示

    package org.xtext.example.mydsl.web

import com.google.inject.Inject
import org.eclipse.xtext.Assignment
import org.eclipse.xtext.Keyword
import org.eclipse.xtext.RuleCall
import org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext
import org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor
import org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalProvider
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.xtext.example.mydsl.services.MyDslGrammarAccess

class MydslContentAssist extends IdeContentProposalProvider {
        @Inject extension MyDslGrammarAccess stAccess
        static val LIBS = #[
            "XX1", "XX2", "YY1", "YY2"
        ]

        override dispatch createProposals(Assignment assignment, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
            switch(assignment) {
                case stAccess.importAccess.importedNamespaceAssignment_1: {
                    for (lib : LIBS.filter[it.startsWith(context.prefix)]) {
                        var proposal = proposalCreator.createProposal(lib + ".*;", context) [
                            source = lib
                            description = "import entire library contents"
                        ]
                        acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
                    }
                }
                default : {
                    super._createProposals(assignment, context, acceptor)
                }
            }
        }
        override dispatch createProposals(RuleCall rulecall, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
            switch(rulecall.rule) {
                case importRule: {
                    for (lib : LIBS) {
                        var proposal = proposalCreator.createProposal('import ' + lib + ".*;", context) [
                            source = lib
                            description = "import entire library contents"
                        ]
                        acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
                    }
                }
                default : {
                    super._createProposals(rulecall, context, acceptor)
                }
            }
        }

        override dispatch createProposals(Keyword keyword, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
            switch (keyword) {
                case stAccess.importAccess.importKeyword_0: {
                    for (lib : LIBS) {
                        var proposal = proposalCreator.createProposal('import ' + lib + ".*;", context) [
                            source = lib
                            description = "import entire library contents"
                        ]
                        acceptor.accept(proposal, proposalPriorities.getDefaultPriority(proposal))
                    }
                }
                default: {
                    super._createProposals(keyword, context, acceptor)
                }

            }
        }
    }