Search code examples
xtextxtend

How to get the user entered value on the spot to assist proposal generation?


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.


Solution

  • 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