Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » scoping function is called too late when code completion is expected(code completion problem)
scoping function is called too late when code completion is expected [message #708065] Tue, 02 August 2011 12:14 Go to next message
Michal S is currently offline Michal SFriend
Messages: 74
Registered: July 2011
Member
Hello,
I have following problem with scoping:

my grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	entities += Entity*;
	
Entity:
	EntityA|EntityB
;

EntityA:
	'A' name=ID '{'
		valuesA += Value*
	'}'
;

Value:
	name=ID '=' value=INT
;

EntityB:
	'B' name=EntityARef '{'
		valuesB += ValueRef*
	'}'
;

ValueRef:
	name = [Value]
;

EntityARef:
	name = [EntityA]
;



my model:
A aa{
	a = 1
	b = 2
}

A bb{
	x = 1
	y = 2
}

B bb {
	x
}


scoping function:
	IScope scope_ValueRef_name(ValueRef vr, EReference ref){
		System.out.println("I am here!");
		return Scopes.scopeFor(((EntityB) vr.eContainer()).getName().getName().getValuesA());
	}


and in workflow I enabled
fragment = exporting.SimpleNamesFragment {}


My problem is: when I am writing in model EntityB -> valuesB, I need the editor offers me all Value-s, but only those defined in specified EntityA.
If I write in this point value "b", it undrelines it - that is perfect (my scoping function is called). Unfortunatelly, I need it called when the completion options are shown, not only after writing.

If I change in my grammar ValueRef (e.g.):
ValueRef:
	'.' name = [Value]
;

then after writing "." editor calls my scoping function and it offers what I need, but I don't want the dot there. From context is clear, that the ValueRef name has to be written in that place - as you see from the grammar.


How can I make the code completion working?
Thank you.

Michal
Re: scoping function is called too late when code completion is expected [message #708069 is a reply to message #708065] Tue, 02 August 2011 12:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13882
Registered: July 2009
Senior Member
Hi,

the context might not be a ValueRef but a parent (containment) of it
=> offer additional scoping methods for parent contexts
e.g.

IScope scope_ValueRef_name(EntityB eb, EReference ref)

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 02 August 2011 12:32]

Report message to a moderator

Re: scoping function is called too late when code completion is expected [message #708118 is a reply to message #708069] Tue, 02 August 2011 13:35 Go to previous messageGo to next message
Michal S is currently offline Michal SFriend
Messages: 74
Registered: July 2011
Member
Thank you Christian, this works!

Michal
Re: scoping function is called too late when code completion is expected [message #713277 is a reply to message #708118] Mon, 08 August 2011 09:46 Go to previous messageGo to next message
Mark Christiaens is currently offline Mark ChristiaensFriend
Messages: 63
Registered: October 2010
Member
Is there a deeper reason why content assist can invoke a "higher" context than expected? How far up does one need to go?
Re: scoping function is called too late when code completion is expected [message #713614 is a reply to message #713277] Mon, 08 August 2011 16:52 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3109
Registered: July 2009
Senior Member
Hi Mark,

the context may be another instance since the parser may have choose
another path if the proposed element was in place.

Consider the following grammar:

Model: (foo += Foo | bar += Bar);
Foo: xref=[EObject | ID];
Bar: xref=[EObject | STRING];

The input file is this one where | indicates the cursor position:

|'other'

If you invoke content assist at offset 0, the most special model is of
type Bar since the parsed element is a string. When the cross reference
is resolved by the linker, it will pass the 'Bar' as the context to the
scope provider. Now consider content assist, which knows that a file may
start with an ID at that point (which in turn is the concrete syntax of
a cross reference that belongs to type 'Foo'). The bad news is, that no
'Foo' is present in the model thus it cannot be passed to the scope
provider. That's why its container is used. Unfortunately this can be an
object that was instantiated a couple of rules upwards in the parser's
call stack. The same logic is used if the lookahead is larger than 1.

Does that clarify the issue?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

On 08.08.11 11:46, Mark Christiaens wrote:
> Is there a deeper reason why content assist can invoke a "higher"
> context than expected? How far up does one need to go?
Re: scoping function is called too late when code completion is expected [message #713857 is a reply to message #713614] Tue, 09 August 2011 08:11 Go to previous messageGo to next message
Mark Christiaens is currently offline Mark ChristiaensFriend
Messages: 63
Registered: October 2010
Member
Thanks Sebastian, I think I understand.

Knowing this, it's unclear to me though how to "do the right thing" to fully support good content assist based on scoping. I mean, so far when I am writing a IScopeProvider, I have been reasoning about which cross-references could need to be resolved in which contexts after a parse of the model. But now, to also make content assist happy, I have to widen the scoping net substantially. It's a big mental burden to figure out what the possible contexts are to resolve cross references.

Would it not be more appropriate to do content assist a bit differently. In your example, would it not be possible to create all the different contexts prior to presenting them to scoping? In this case it would mean creating two variants of the model:

  1. Resource -> Model -> Foo (with empty cross reference)
  2. Resource -> Model -> Bar (with empty cross reference)

and then query scoping for valid candidates for Foo.xref in the context of the Foo EObject in model (1) followed by a query for valid candidates of Bar.xref in the context the Bar EObject in model (2)?

I guess that that would require locking the model, modifying it without triggering any listeners, reverting everything and then unlocking?

---
Mark Christiaens
Discover the Future of VHDL Design
Go to www.sigasi.com
Re: scoping function is called too late when code completion is expected [message #713867 is a reply to message #713857] Tue, 09 August 2011 08:47 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3109
Registered: July 2009
Senior Member
Hi Mark,

please follow the brief discussion here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=276619

It would be hardly possible to do this in a reasonable way from a
performance point of view and with regards to the complexity of the
implementation. For the rather complex grammar of Xbase and Xtend, we
customized the proposal provider to directly invoke methods on the scope
provider that take more context information into account. With a
reasonable amount of unit tests, it was possible to provide the
necessary information for helpful content assist.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

On 09.08.11 10:11, Mark Christiaens wrote:
> Thanks Sebastian, I think I understand.
> Knowing this, it's unclear to me though how to "do the right thing" to
> fully support good content assist based on scoping. I mean, so far when
> I am writing a IScopeProvider, I have been reasoning about which
> cross-references could need to be resolved in which contexts after a
> parse of the model. But now, to also make content assist happy, I have
> to widen the scoping net substantially. It's a big mental burden to
> figure out what the possible contexts are to resolve cross references.
>
> Would it not be more appropriate to do content assist a bit differently.
> In your example, would it not be possible to create all the different
> contexts prior to presenting them to scoping? In this case it would mean
> creating two variants of the model:
>
> Resource -> Model -> Foo (with empty cross reference)
> Resource -> Model -> Bar (with empty cross reference)
>
> and then query scoping for valid candidates for Foo.xref in the context
> of the Foo EObject in model (1) followed by a query for valid candidates
> of Bar.xref in the context the Bar EObject in model (2)?
>
> I guess that that would require locking the model, modifying it without
> triggering any listeners, reverting everything and then unlocking?
> ---
> Mark Christiaens
> Discover the Future of VHDL Design
> Go to http://www.sigasi.com
>
Re: scoping function is called too late when code completion is expected [message #713913 is a reply to message #713867] Tue, 09 August 2011 11:17 Go to previous messageGo to next message
Mark Christiaens is currently offline Mark ChristiaensFriend
Messages: 63
Registered: October 2010
Member
I can understand that it would be complex but performance wise, I don't quite understand it. Isn't maintaining/updating the model while the user is typing as expensive as modifying the model on-the-fly to generate content assistance?

---
Mark Christiaens
Discover the Future of VHDL Design
Go to www.sigasi.com
Re: scoping function is called too late when code completion is expected [message #713991 is a reply to message #713913] Tue, 09 August 2011 13:34 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3109
Registered: July 2009
Senior Member
Hi Mark,

you will already notice a delay when you invoke content assist directly
after a typing sequence - sometimes the parser is even too slow to
notify the infrastructure about ongoing work, but that is a nasty bug :-( .

The produced model may depended significantly on the inserted token,
e.g. an expression tree will look totally different depending on the
following input element. It is often not only a single other alternative
but actually a lot of them that have to be produced to pass them to the
proposal provider - and often they would require resolved cross
references to provide meaningful suggestions. Considering the impact on
the performance / usability and the required complexity in the
implementation it is simply not worth the effort (at least for now). It
is usually much easier to customize the proposal provider to achieve the
same results.

The model cannot be modified on the fly since there may be listeners
installed that have to be notified if something changes (e.g. the cache)
while others may be surprised if the model is changed during a read-only
operation such as computing the proposals. That's why we would have to
copy basically the complete resource set (the 'replaced' elements in the
model may be referenced by other resources which in turn are used to
compute the proposals ...).

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

On 09.08.11 13:17, Mark Christiaens wrote:
> I can understand that it would be complex but performance wise, I don't
> quite understand it. Isn't maintaining/updating the model while the user
> is typing as expensive as modifying the model on-the-fly to generate
> content assistance?
> ---
> Mark Christiaens
> Discover the Future of VHDL Design
> Go to http://www.sigasi.com
Previous Topic:Theoretical Question
Next Topic:xtext syntax question
Goto Forum:
  


Current Time: Wed Apr 14 15:23:20 GMT 2021

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

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

Back to the top