Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serialization: Indent missing at start of semantic modification
Serialization: Indent missing at start of semantic modification [message #1746528] Mon, 31 October 2016 17:17 Go to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1479
Registered: July 2009
Senior Member
This looks like room for improvement in the serializer, but let me check with you before filing a bug:

I have a model of this structure
module E {
    entity a {}
}

I have a quick fix that should add more entities, which is implemented as a semantic modification. The result of applying the quick fix is:
module E {
    entity a {}
entity b {}
    entity c {}
}


Problem: entity b is not properly indented. Formatter seems to be OK as Ctrl-Shift-F corrects the problem.

I could narrow it down to problematic interaction between HiddenTokenSequencer and FormattingConfigBasedStream:
When processing the '}' right before the insertion point for the new entities, we create a "preservedWS" containing just "\n".
Later during FormattingConfigBasedStream.Line.getSpacesStr(LineEntry, boolean), this preservedWS prevents computation of correct WS including indentation.

I could experimentally fix this inside HiddenTokenSequencer.getHiddenNodesBetween(): at the end of the (to == null) branch, I simply remove any trailing whitespace from out (still ensuring that trailing comments are correctly preserved incl. previous whitespace).
			for (int i = out.size() - 1; i >= 0; i--) {
				if (!tokenUtil.isWhitespaceNode(out.get(i)))
					break;
				out.remove(i);
			}

This seems to ensure that at the transition from old to new elements (the latter not having any nodes, yet) we don't spill preservedWS into the next (yet unformatted) element.

Does this make sense wrt the design of serialization? If so, are bugs in bugzilla still accepted (I don't have a github account)?

best,
Stephan



Re: Serialization: Indent missing at start of semantic modification [message #1746562 is a reply to message #1746528] Mon, 31 October 2016 21:58 Go to previous message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 10799
Registered: July 2009
Senior Member
i can not reproduce this with a simple unit test (with 2.11).

Module:
	"module" name=ID "{"
		entities+=Entity*	
	"}";
	
Entity:
	'entity' name=ID '{''}';


import com.google.inject.Inject
import org.eclipse.xtext.serializer.ISerializer
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.eclipse.xtext.testing.util.ParseHelper
import org.junit.Test
import org.junit.runner.RunWith
import org.xtext.example.mydsl1.myDsl.Module
import org.xtext.example.mydsl1.myDsl.MyDslFactory

@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class DummyTest {
	
	@Inject
	extension ParseHelper<Module> 
	
	@Inject 
	extension ISerializer
	
	@Test
	def void testIt() {
		val m =
		'''
		module E {
		    entity a {}
		}
		'''.parse
		m.entities += MyDslFactory.eINSTANCE.createEntity=>[name="b"] 
		m.entities += MyDslFactory.eINSTANCE.createEntity=>[name="c"] 
		
		println(m.serialize(SaveOptions.newBuilder.format.options))
	}
	
}


class MyDslFormatter extends AbstractFormatter2 {
	
	@Inject extension MyDslGrammarAccess

	def dispatch void format(Module module, extension IFormattableDocument document) {
		module.regionFor.keyword("{").append[newLine]
		module.regionFor.keyword("}").prepend[newLine]
		interior(module.regionFor.keyword("{"),module.regionFor.keyword("}"))[indent]
		for (Entity entity : module.getEntities()) {
			entity.format;
		}
	}
	
	def dispatch void format(Entity e, extension IFormattableDocument document) {
		e.regionFor.keyword("entity").prepend[newLine]
	}
}


but i can reproduce it from a quickfix

can you please file a ticket? (bugzilla is ok although github issue is prefered)


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com

[Updated on: Mon, 31 October 2016 22:20]

Report message to a moderator

Previous Topic:dynamic template proposals in xtext revisited
Next Topic:Debugging non-deterministic serialization failure
Goto Forum:
  


Current Time: Thu Jun 29 07:36:47 GMT 2017

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

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