Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Narrowing content assist choices(XTetxt : Filter proposed choices)
icon3.gif  Narrowing content assist choices [message #558169] Fri, 10 September 2010 12:00 Go to next message
Jerome is currently offline JeromeFriend
Messages: 5
Registered: September 2010
Junior Member
Hello,

I am new to XText and I'd like to do a sort of Ctrl+Space choice filtering with that (very nice Razz ) plugin. Until now, I haven't found a way to make it.
Let's keep it simple and take an example. I want to use Xtext in order to provide content assist with this kind of text:

Classroom B442 has students:
Annie present
Bob missing
Zoe present

{Bob, Job, Annie} are possible choices and {absent, present} are their possible state.
Until now it's okay, we found our way and managed to write the xtext grammar, scope provider...etc.
But I want to narrow the choices proposed by Ctrl+Space when encountering a student.
In the classroom example, we could decide to fill the presence sheet alphabetically and have all the students listed. When doing Ctrl-space for the Nth student choice, I want content assist to propose only the Nth. That way, after Annie, the only viable (and possible) choice is Bob.

So my grammar looks like that:
InputClassroom:
	'Classroom ' classroomName=[ClassroomDefinition] ' has students : '
	(students+=InputStudent)*
InputStudent:
	chosenStudent=[StudentDefinition] state

enum state:
	' present ' | ' missing '
	
ClassroomDefinition:
	'Classroom ' classRoomName=ID
	(classroomStudents+= StudentDefinition)*

StudentDefinition:
	'Student ' studentName=ID

And the database looks like:

Classroom B442
	Annie
	Bob
	Zoe

Student Annie
	....
Student Bob
	...
Student Zoe
	...


In order to narrow the choices, I modified the scope provider that way:

public IScope scope_ InputStudent _ chosenStudent (InputStudent inputStudent, EReference ref) {
	StudentDefinition nextStudent = ((InputClassroom)inputStudent.eContainer()).getClassroomDefinition(). GetClassroomStudent ().size()-1);
	@SuppressWarnings("unchecked")
EList<EObject> choices = (EList<EObject>) Proxy.newProxyInstance(Student.class.getClassLoader(),
				new Class [] {EList.class}, new InvocationHandler() {
					private List< StudentDefinition> list = new ArrayList< StudentDefinition> ();
					
					@Override
					public Object invoke(Object obj, Method meth, Object[] args)
							throws Throwable {
						return meth.invoke(list, args);
					}
				});
		choices.add(nextStudent);
		return Scopes.scopeFor(choices);
	}


Okay it's a bit dirty and it's doing half what I expected... It's not compliant with the validation process of Xtext (which seems to call that scope method). When we have more than one student in the classroom, little red cross will appear in the editor for the first one. That makes sense because I check the number of already provided students to propose only one possible choice... but then, how can I modify my grammar / scope provider or else in Xtext in order to have this behaviour ?

Thanks a lot, Jérôme

[Updated on: Fri, 10 September 2010 12:05]

Report message to a moderator

Re: Narrowing content assist choices [message #558182 is a reply to message #558169] Fri, 10 September 2010 12:45 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

just as a very brief first thought: I would not approach the desired effect by adapting scoping. (Think of, commenting out one student, breaking the references to all the others, thereby disableing navigation and the like.) All students should be visible from all positions (scoping), but you might want to have specific validation rules making sure they are in the right order. Also you'd rather adapt the content assist directly in order to propose only the valid choices.
Template proposals with all students at once might also be an option.

Alex
Re: Narrowing content assist choices [message #558192 is a reply to message #558182] Fri, 10 September 2010 13:37 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
The content assist is a nicety of the editor and should be compliant with the semantic rules of the language. So, by limiting the valid choices in the proposal provider only, the language as such does not behave any differently. The name scoping is a bit misleading, as it's a wider concept in Xtext than it usually is (in the context of visibility). For Xtext, it's best practice to implement the narrowing of choice in cross-referencable stuff in scoping, rather than elsewhere.


Re: Narrowing content assist choices [message #558290 is a reply to message #558192] Fri, 10 September 2010 20:06 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi Meinte,

I did not say that modifying content assist changes the language. I argued that in order to get the wanted content assist you should not necessarily restrict the semantics of the language.

As I understand the example, it really does not matter in which order the students are listed (i.e. all are visible from everywhere within the classroom), but the user might always want to place them in alphabetical order, so that a corresponding adaption to the content assist would make sense.
It would not be sensible to change the scoping for that. As hinted, links will be broken, once the list of student changes etc. Taking a far fetched java analogon: how would you feel about not being able to navigate to an interface your class is supposed to implement, just because some of the required methods are missing? The interface is still visible, only some constraints are not yet met.
I propose the same thing: let all the students be visible, but make content assist smarter.

By the way, scoping and content assist are usually not smart enough by default not to allow for multiple references to the same object. Although this may be a semantic requirement of your language, you would not use scoping to take care of that but rather have a validation rule.

Alex
Re: Narrowing content assist choices [message #558339 is a reply to message #558290] Sat, 11 September 2010 10:53 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
@Alexander: you're right. I read the posts hastily, I'm afraid.

So, you would put this kind of "editorial guidance" into the proposal provider and check in a validation that the order's alphabetical.


Re: Narrowing content assist choices [message #558344 is a reply to message #558339] Sat, 11 September 2010 11:56 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi Meinte,

indeed. If you make the scoping too "sophisticated" you may end up with linking errors the user will hardly understand. So even if the language specification requires a specific order, I would not necessarily implement that order via scoping (even if this would give you content assist and validation for free), but rather have a generous scoping and customise content assist and validation.

This will allow for more helpful feedback to the user. Rather than getting a "could not resolve reference to John Smith" (who actually exists, the user wondering what is wrong), you can give the custom error message "Sorry, but you chose the wrong position for John Smith".

As often, it is a matter of taste how to achieve the desired behaviour. So I am not saying the scoping-way is wrong. I just say it has severe drawbacks that need to be considered.

Alex
Re: Narrowing content assist choices [message #558608 is a reply to message #558169] Mon, 13 September 2010 16:36 Go to previous messageGo to next message
Jerome is currently offline JeromeFriend
Messages: 5
Registered: September 2010
Junior Member
Thanks for these replies. You are right, customizing the "scoping" that way produces an incoherent set of data in Xtext workflow and could trigger many problems :s

From my understanding, scoping is providing a set of possible values and content assist is relying on these values to display it on the GUI. Is that correct ? Therefore, where could I (find) override or extend the content assist and define my narrowing rule ?
I find Xtext's documentation a bit difficult (at least for an Xtext beginner), so my question may look stupid Sad

So, If I understood, my scope provider will allow all the valid values (all students) and my content assist will propose the user a reduced (but still valid) choice (only the next student in the alphabetical order).
Then, the validation process will have to take into account that new rule in order to make sure that the user chose the student by alphabetical order. Then, where do I customize that validation ?

Thanks, Jerome
Re: Narrowing content assist choices [message #558653 is a reply to message #558608] Mon, 13 September 2010 19:47 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Validation customization happens in the <MyDsl>JavaValidator class and is pretty easy to do, generally. I think you'd need the 3-args form of the error method here, so you can validate at the level of the innermost container of the items which have to be in alphabetical order, i.e. ClassRoom, I guess.

Note that the content assist by default mirrors the scoping but does not take validation into account -that could either give quite a performance hit or be impossible to do since a validation may take a greater part of the AST into account, so you'd have to create "candidate-ASTs" for everything in scope...


Re: Narrowing content assist choices [message #558654 is a reply to message #558608] Mon, 13 September 2010 19:55 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

the default content assist implementation for cross references proposes all elements provided by the scope.
The UI plugin contains a contentassist-package. There you find the entry point for adapting code completion. The method you have to override will be called something like completeInputStudent_Chosenstudent(). If you look at the original implemetation a lookupcrossreference-method is called. If you navigate to the implementation, you'll see that there are various such methods. There is also one which takes a filter as parameter. I guess, this is where you should hook into: directly calling this method providing a filter that accepts only the student allowed at that position.

Via the context parameter you have access to the model (in order to check at which position code completion was invoked.

The validation hook is located in the grammar plugin in the validation package. It is covered in the documentation. For examples see any Xtext project (e.g. those shipped with Xtext).

Validation and content assist are not related (nothing prevents you from proposing illegal strings).

Alex
Re: Narrowing content assist choices [message #558745 is a reply to message #558169] Tue, 14 September 2010 09:41 Go to previous message
Jerome is currently offline JeromeFriend
Messages: 5
Registered: September 2010
Junior Member
Very nice ! As you said, I got the expected behaviour by overriding the ProposalProvider in the UI project (by providing a custom "Predicate") and the JavaValidator.

Thanks a lot, both of you. That was very helpful !
Previous Topic:Implementation of AbstractScope.getAllContent() results in performance problem
Next Topic:Hyperlinks in Strings/Comments
Goto Forum:
  


Current Time: Fri Apr 26 05:35:29 GMT 2024

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

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

Back to the top