Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Quickfix and format problem
icon8.gif  Quickfix and format problem [message #1698570] Tue, 16 June 2015 12:49 Go to next message
ayman salah is currently offline ayman salahFriend
Messages: 131
Registered: June 2015
Senior Member
I am reading from the book "Implementing DSL with xtext and xtend" it says that formatter is invoked when of course the Format from the context menu is used or the shortcut for it "Ctrl + Shift + F" is used. It also says that "If you provide a custom formatter this will be used not only when the format menu is invoked but also when xtext needs to update the editor contents after a manual modification of the AST model( for example a quickfix performing a semantic modification".

With that said I am having a problem that when my quickfix is invoked the formatter doesn't get invoked resulting in a terrible looking text.
Example:

----1-----
---Text before quickfix---

entity myentity {
	FooBar s;
} entity second{}


----2-----
---Quickfix add unimplemented entity---

entity myentity {
	FooBar s;
} entity FooBar {
} entity second{}


----3-----
---invoking the formatter MANUALLY(How it should look like)---

entity myentity {
	FooBar s;
}

entity FooBar {
}

entity second {
}


QuickFix implementation:
@Fix(Diagnostic::LINKING_DIAGNOSTIC)
	def CreateMissingEntity(Issue issue, IssueResolutionAcceptor acceptor)
	{
		acceptor.accept(issue,"Create missing entity.","Create missing entity.", "" ,
			[element, context | 
				val currentEntity = element.getContainerOfType(typeof(Entity))
				val model = currentEntity.eContainer as Model
				model.entities.add(model.entities.indexOf(currentEntity)+1, EntitiesFactory::eINSTANCE.createEntity() => [name = context.xtextDocument.get(issue.offset,issue.length)])
			]
		);
	}


Formatter implementation:
	@Inject extension EntitiesGrammarAccess g
	
	override protected void configureFormatting(FormattingConfig c) {
// It's usually a good idea to activate the following three statements.
// They will add and preserve newlines around comments
		//entitites
		val e = g.entityAccess
		// indentation between {}
		c.setIndentation(e.leftCurlyBracketKeyword_3,e.rightCurlyBracketKeyword_5)
		// newline after {
		c.setLinewrap.after(e.leftCurlyBracketKeyword_3)
		// newlines after }
		c.setLinewrap(2).after(e.rightCurlyBracketKeyword_5)
		//attributes
		val a = g.attributeAccess
		// newline after ;
		c.setLinewrap.after(a.semicolonKeyword_2)
		// remove spaces before ;
		c.setNoSpace.before(a.semicolonKeyword_2)
		
		c.setLinewrap(0, 1, 2).before(SL_COMMENTRule)
		c.setLinewrap(0, 1, 2).before(ML_COMMENTRule)
		c.setLinewrap(0, 1, 1).after(ML_COMMENTRule)
	}


I have been searching a lot if the formatter is actually invoked as the book says after a quickfix but found nothing. Is this real? and if not how can I invoke the formatter programmatically from the quickfix code.
Re: Quickfix and format problem [message #1698657 is a reply to message #1698570] Wed, 17 June 2015 04:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
hi you have to enable that explicitely

public class MyDslUiModule extends org.xtext.example.mydsl1.ui.AbstractMyDslUiModule {
    public MyDslUiModule(AbstractUIPlugin plugin) {
        super(plugin);
    }

    public Class<? extends ITextEditComposer> bindITextEditComposer() {
        return MyDslTextEditComposer.class;
    }
}

public class MyDslTextEditComposer extends DefaultTextEditComposer {

    @Override
    protected SaveOptions getSaveOptions() {
        return SaveOptions.newBuilder().format().getOptions();
    }

}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Quickfix and format problem [message #1698850 is a reply to message #1698657] Thu, 18 June 2015 12:06 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 131
Registered: June 2015
Senior Member
Thanks a lot,

That worked. I though want to ask you where should I go (some reference) when I have such problem. I don't have any sources for Xtext except that book and my poor internet search skills.

[Updated on: Thu, 18 June 2015 12:06]

Report message to a moderator

Re: Quickfix and format problem [message #1698870 is a reply to message #1698850] Thu, 18 June 2015 13:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
nope

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Quickfix and format problem [message #1699211 is a reply to message #1698570] Mon, 22 June 2015 15:41 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1813
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 16/06/2015 16:09, ayman salah wrote:
> I am reading from the book "Implementing DSL with xtext and xtend" it
> says that formatter is invoked when of course the Format from the
> context menu is used or the shortcut for it "Ctrl + Shift + F" is used.
> It also says that "If you provide a custom formatter this will be used
> not only when the format menu is invoked but also when xtext needs to
> update the editor contents after a manual modification of the AST model(
> for example a quickfix performing a semantic modification".
>
> With that said I am having a problem that when my quickfix is invoked
> the formatter doesn't get invoked resulting in a terrible looking text.
> Example:
>
> ----1-----
> ---Text before quickfix---
>
> entity myentity {
> FooBar s;
> } entity second{}
>
> ----2-----
> ---Quickfix add unimplemented entity---
>
> entity myentity {
> FooBar s;
> } entity FooBar {
> } entity second{}
>
> ----3-----
> ---invoking the formatter MANUALLY(How it should look like)---
> entity myentity {
> FooBar s;
> }
>
> entity FooBar {
> }
>
> entity second {
> }
>
> QuickFix implementation:
> @Fix(Diagnostic::LINKING_DIAGNOSTIC)
> def CreateMissingEntity(Issue issue, IssueResolutionAcceptor acceptor)
> {
> acceptor.accept(issue,"Create missing entity.","Create missing
> entity.", "" ,
> [element, context | val currentEntity =
> element.getContainerOfType(typeof(Entity))
> val model = currentEntity.eContainer as Model
>
> model.entities.add(model.entities.indexOf(currentEntity)+1,
> EntitiesFactory::eINSTANCE.createEntity() => [name =
> context.xtextDocument.get(issue.offset,issue.length)])
> ]
> );
> }

Hi there

If I remember correctly that behavior changed a few Xtext versions ago,
I'm afraid. At the time of writing the book it used to work like that :)

Please, also note that since Xtext 2.8 a new formatting mechanism has
been introduced that's completely different (and better :); that's
specified in the ERRATA of the book in the github repository.

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: Quickfix and format problem [message #1699434 is a reply to message #1699211] Wed, 24 June 2015 10:19 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 131
Registered: June 2015
Senior Member
The DSL I am implementing is huge. Looking at my formatter at the moment, it looks really difficult to change all of that to 2.8 formatter that I know nothing about. There isn't much resources online for 2.8 formatter to help me out. I have seen that github thing while searching. It though still doesn't help with 2.8 formatter.
Re: Quickfix and format problem [message #1700122 is a reply to message #1698657] Tue, 30 June 2015 09:57 Go to previous message
ayman salah is currently offline ayman salahFriend
Messages: 131
Registered: June 2015
Senior Member
How can I improve this making the formatting happen also on saving the document? on Ctrl+S?
Previous Topic:How to customize hint in language editor?
Next Topic:Switching to Formatting2
Goto Forum:
  


Current Time: Sat Sep 21 00:25:25 GMT 2024

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

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

Back to the top