Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext 2.4.2: how to have content assist together with validation(xtext, scope restriction, content assist, code completion, Ctrl + Space, validation, proposal provider, )
Xtext 2.4.2: how to have content assist together with validation [message #1066581] Wed, 03 July 2013 09:25 Go to next message
Kent To is currently offline Kent ToFriend
Messages: 4
Registered: July 2013
Junior Member
Hi all,

I've searched and read hexas of threads about content assist using xtext. But when I want to go deeper I got stuck in advices on how to do it using earlier versions of xtext.

For a grammar I am able to do validation on any element of the AST, now I would like to give the users of my text editor a new level of friendliness: content assist, or in other words scope restriction

I) What I have learned

From [1] I learn that to have scope restriction there are two ways


  1. Explicit restriction
  2. Implicit restriction


Explicit restriction is to use a filter to discard the elements falling out of scope. To be precise you would override methods of an AbstractDeclarativeScopeProvider

Implicit restriction is to specify, by using a filter, how the parser would accept a proposal before hand, and to validate that proposal afterwards. To be precise you would override methods of an Abstract[your grammar name]ProposalProvider and an Abstract[your grammar name]JavaValidator

[1] suggests people to use the Implicit restriction, because of the ability to provide useful error messages after the user has typed something.

II) What I want to do

In the example of [1], when we want to override a method

complete[ElementName]_[FeatureName]()

there is only a possibility to call lookupCrossReference(), but in my own grammar I only see the calls to completeRuleCall(), for example


public void complete[ElementName]_[FeatureName](EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
completeRuleCall(((RuleCall)assignment.getTerminal()), context, acceptor);
}


My question is
- What do the parameters {model, assignment, context, acceptor} mean ?
- If I have a filter on an element of the model tree (AST), for example

boolean filter(EObject anElement)

How can I insert that filter into the body of

complete[ElementName]_[FeatureName]()

?
Re: Xtext 2.4.2: how to have content assist together with validation [message #1066643 is a reply to message #1066581] Wed, 03 July 2013 14:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

you can wrap delegateGetScope(ctx, eref) into a FilteringScope


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext 2.4.2: how to have content assist together with validation [message #1066644 is a reply to message #1066643] Wed, 03 July 2013 14:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
e.g

IScope scope_A_b(A ctx, EReference ref) {
return new FilteringScope(delegateGetScope(ctx,ref), ....);
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext 2.4.2: how to have content assist together with validation [message #1066703 is a reply to message #1066644] Wed, 03 July 2013 21:12 Go to previous messageGo to next message
Kent To is currently offline Kent ToFriend
Messages: 4
Registered: July 2013
Junior Member
Hi Christian,

Thank you for your advice on using ScopeProvider in xtext 2.4.2, I'll try it.

Can you also provide an advice using implicit restriction, using a Proposal Provider ?

Here is my grammar

grammar org.eclipse.xtext.example.DomainModel with org.eclipse.xtext.common.Terminals

generate domainModel "http://www.eclipse.org/xtext/example/DomainModel"

Domainmodel:
  (elements += AbstractElement)*
;
 
Package:
  'package' name = QualifiedName '{'
    (elements += AbstractElement)*
  '}'
;
 
AbstractElement:
  Package | Type | Import
;
 
QualifiedName:
  ID ('.' ID)*
;
 
Import:
  'import' importedNamespace = QualifiedNameWithWildcard
;
  
QualifiedNameWithWildcard:
  QualifiedName '.*'?
;
  
Type:
  DataType | Entity
;
  
DataType:
  'datatype' name=ID
;
 
Entity:
  'entity' name = ID 
              ('extends' superType = [Entity | QualifiedName])?
  '{'
    (features += Feature)*
  '}'
;
 
Feature:
  (many ?= 'many')? name = ID ':' type = [Type | QualifiedName]
;



and in the model source code I would like to display only "Numeric" as type if the feature name has the word "Numeric"

/* this is a comment */
package java.lang {
  datatype String
  datatype Numeric
}
 
package a.new.package {
	import java.lang.*
	
	entity ThisIsADemo{
		aStringVar : String // Should only display String as type on Ctrl+Space
		aNumericVar: Numeric // Should only display Numeric as type on Ctrl+Space
	}	
}
Re: Xtext 2.4.2: how to have content assist together with validation [message #1066706 is a reply to message #1066703] Wed, 03 July 2013 21:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

?!? i thought so far you would get yourself.

is that your real world example? it is hard to say if this is the right way .....

class MyDslScopeProvider extends org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider {

	def IScope scope_Feature_type(Feature feature, EReference ref) {
		new FilteringScope(delegateGetScope(feature, ref), [
			ieod|feature.name.contains(ieod.name.toString)
		])
	}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext 2.4.2: how to have content assist together with validation [message #1066745 is a reply to message #1066706] Thu, 04 July 2013 08:21 Go to previous messageGo to next message
Kent To is currently offline Kent ToFriend
Messages: 4
Registered: July 2013
Junior Member
Hi Christian,

Many thanks for your time,

Your solution works for this toy example. I'm learning xtext and start with this toy example.

I'd like to extend the scope restriction to be more complicated, this new example does not work, and I would like to receive your advice. Also if you could provide a solution using ProposalProvider, that would be great.

class DomainModelProposalProvider extends AbstractDomainModelProposalProvider {
}




Many thanks.


Scope restriction rule:
"An entity A would only extend another entity B if the first letter of A's name == the first letter of B's name"


Model source (runtime) example:

package a.new.package {
        entity firstBaseEntity{}
        entity secondBaseEntity{}
        entity thirdBaseEntity{}

	entity secondDerivedEntity extends [Ctrl+Space goes here, only secondBaseEntity should appear] {
	}	
}


Here is how I try in xtend

public class DomainModelScopeProvider extends AbstractDeclarativeScopeProvider {
	def IScope scope_Entity_SuperType_name(Entity entity, EReference ref){
		
		new FilteringScope(
			delegateGetScope(entity, ref),
			[
				ieod | entity.name.startsWith(ieod.name.toString.substring(0, 1))
			]
		)
	}
}


Why does it not work ?
- I think that ieod refers to the object whose scope is being considered by that scope_Entity_SuperType_name() function. In this example is the entity name after the "extends" keyword.
- The "entity" parameter should refer to the entity before the "extends" keyword. Therefore the scope function should have the "SuperType" in its name.

But something is still missing ?

[Updated on: Thu, 04 July 2013 08:22]

Report message to a moderator

Re: Xtext 2.4.2: how to have content assist together with validation [message #1066748 is a reply to message #1066745] Thu, 04 July 2013 08:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi you can add println(...); to the closure

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext 2.4.2: how to have content assist together with validation [message #1070621 is a reply to message #1066748] Thu, 18 July 2013 10:22 Go to previous message
Kent To is currently offline Kent ToFriend
Messages: 4
Registered: July 2013
Junior Member
Thank you Christian
Previous Topic:proxy resolution
Next Topic:Does the "Xtext Simple Arithmetics Example" work anymore?
Goto Forum:
  


Current Time: Fri Mar 29 00:55:25 GMT 2024

Powered by FUDForum. Page generated in 0.04056 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top