Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to make formatter ignore part of the code?
How to make formatter ignore part of the code? [message #637030] Wed, 03 November 2010 20:55 Go to next message
Samantha Chan is currently offline Samantha ChanFriend
Messages: 60
Registered: July 2009
Member
Hi,

In my language the user can embed some snippet of scripts into the code. (similar how you can embed javascript in a JSP)

So, for example I can have something like this:

for (int var_<%scriptToGenerateName%>; var_<%scriptToGenerateName%> < 10)
{
// for loop stuff
}

I have my own formatter that subclassed from AbstractDeclarativeFormatter. By default, the formatter inserts a white space before every token. In my example, the code becomes invalid when a token is inserted before the snippet.

I am treating the snippet as comments, so it should be part of the hidden stream.

So, my question here is that, is there a way to control the formatter to preserve the formatting before the snippet?

Thanks...
Samantha
Re: How to make formatter ignore part of the code? [message #637053 is a reply to message #637030] Wed, 03 November 2010 23:00 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Samantha,

I could imagine that something like
setNoSpace().between().(var_keyword, myHiddenScriptRule) works.

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

Am 03.11.10 21:55, schrieb Samantha Chan:
> Hi,
>
> In my language the user can embed some snippet of scripts into the code.
> (similar how you can embed javascript in a JSP)
>
> So, for example I can have something like this:
>
> for (int var_<%scriptToGenerateName%>; var_<%scriptToGenerateName%> < 10)
> {
> // for loop stuff
> }
>
> I have my own formatter that subclassed from
> AbstractDeclarativeFormatter. By default, the formatter inserts a white
> space before every token. In my example, the code becomes invalid when a
> token is inserted before the snippet.
>
> I am treating the snippet as comments, so it should be part of the
> hidden stream.
> So, my question here is that, is there a way to control the formatter to
> preserve the formatting before the snippet?
>
> Thanks...
> Samantha
Re: How to make formatter ignore part of the code? [message #637488 is a reply to message #637053] Fri, 05 November 2010 19:08 Go to previous messageGo to next message
Samantha Chan is currently offline Samantha ChanFriend
Messages: 60
Registered: July 2009
Member
Hi Sebatian,

Thanks for the advise. But this did not work for me.

When I tried to implement this rule, none of the rules for the script terminal rule has any effect. The problem is that I have treated the script terminal rule as comment. But I did not customize DefaultHiddenTokenHelper to recognize that as comments. Therefore, the terminal rule was treated as a semantic node, and it would not take any effect.

I customized the DefaultHiddenTokenHelper to now recognize the script terminal rule as comment and the formatting rule has some effect.

However, your suggestion does not work for me because I did not want "no space" between the tokens, instead I want the spaces to be preserved.

For example:
for (int var_<%scriptToGenerateName%>; var_<%scriptToGenerateName%> < 10)
{
// for loop stuff
}

In this case here, I do not want any space between the keyword and the script terminal rule.

However in a case like this:

string <%script to generate variable name%> = "abc";

I actually want the space to be preserved between "string" and "<%script%>".

Adding the "no space" call will result in the space to be removed in this case. And causes compile errors for our compiler.

Do you have other ideas where I can preserve the space before and after my script terminal rule?

Thanks!
Samantha
Re: How to make formatter ignore part of the code? [message #637531 is a reply to message #637488] Sat, 06 November 2010 09:44 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Samantha,

sounds like you shouldn't use the hidden channel for your scriptlets.
Furthermore if ws has semantics (as in your case), you propably should
make that explicit in your grammar. Did you try to make your scriptlets
part of a valid identifier:

ValidID hidden():
(ID | Scriptlet)+;

This way you would have the chance to use the value converter to keep
any spaces you want.

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

Am 05.11.10 20:08, schrieb Samantha Chan:
> Hi Sebatian,
>
> Thanks for the advise. But this did not work for me.
>
> When I tried to implement this rule, none of the rules for the script
> terminal rule has any effect. The problem is that I have treated the
> script terminal rule as comment. But I did not customize
> DefaultHiddenTokenHelper to recognize that as comments. Therefore, the
> terminal rule was treated as a semantic node, and it would not take any
> effect.
>
> I customized the DefaultHiddenTokenHelper to now recognize the script
> terminal rule as comment and the formatting rule has some effect.
>
> However, your suggestion does not work for me because I did not want "no
> space" between the tokens, instead I want the spaces to be preserved.
>
> For example:
> for (int var_<%scriptToGenerateName%>; var_<%scriptToGenerateName%> < 10)
> {
> // for loop stuff
> }
>
> In this case here, I do not want any space between the keyword and the
> script terminal rule.
>
> However in a case like this:
>
> string <%script to generate variable name%> = "abc";
>
> I actually want the space to be preserved between "string" and
> "<%script%>".
>
> Adding the "no space" call will result in the space to be removed in
> this case. And causes compile errors for our compiler.
>
> Do you have other ideas where I can preserve the space before and after
> my script terminal rule?
>
> Thanks!
> Samantha
>
Previous Topic:Validation with StandaloneSetup
Next Topic:XText Tutorial Problem
Goto Forum:
  


Current Time: Fri Apr 19 11:19:46 GMT 2024

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

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

Back to the top