1

I am new to xtext/xtend, and would appreciate your help here. After reading a lot of online articles/documents/tutorials, I could not find a way on how to get the user entered value.

For example, let's say I have a simple grammar:

 Path: 'path' name=STRING

In the editor, the user entered:

 path c:/x

And I have a customized proposal provider with signature as:

 class DomainmodelProposalProvider extends AbstractDomainmodelProposalProvider {

   def override completePath_Name(EObject model,
                                         Assignment assignment,
                                         ContentAssistContext context,
                                         ICompletionProposalAcceptor acceptor) {
        ...
      }
  }

which will try to propose the list of valid paths based on the user's current input. For example, with path c:/x it would propose c:\xyz and c:\x-ray back when ctrl-space is pressed. To do that, I need get the name value to do the checking, but I don't know which APIs to call.

EDIT: I was able to get the last suggestion from Christian working, i.e. by downcasting the Emodel object. Here is the snippet of the code:

 val pObj = model as Path
 val allowedList = DomainmodelStandaloneSetup.readAllowedPaths()

 var String tmp
 if (pObj.name == null) tmp = "" else tmp = pObj.name

 val target = tmp
 val proposedList = allowedList.filter[startsWith(target)] 

 for (item : proposedList) {
    val p = createCompletionProposal(item.toString(), context)
    acceptor.accept(p)
}

You can see I am struggling a little with the val/var constraints of Xtend. Had to use a val for the lamda, had to use a var to handle the case when pObj.name is null.

gwang
  • 153
  • 1
  • 7

1 Answers1

1

There is no generaral Answer to this question. but here are some hints that ususally work

  • you can inspect ContentAssistContext for prefix
  • you can inspect ContentAssistContext for the current ast
  • you can inspect ContentAssistContext for the current and last complete node model

You could change your grammar to

Path: {Path}'path' name=STRING

and then downcast the EObject model parameter to Path and ask it for its name

Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
  • Thanks, Christian. I am a little confused with the answer. Do you mean downcast `EObject model` is the 4th option, or it must be done for the prior three to work? Thanks. – gwang Jan 22 '16 at 05:50
  • I was able to get the last suggestion working in my code with xtend. See the edit above. Thanks. – gwang Jan 22 '16 at 21:34
  • 1
    Xtend is much more powerful than you think: ```val target = if (pObj.name == null) "" else pObj.name``` or even ```val target = pObj.name ?: ""``` – Christian Dietrich Jan 23 '16 at 07:02