Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatter2 autowrap
Formatter2 autowrap [message #1840454] Thu, 15 April 2021 16:31 Go to next message
Dimo Petroff is currently offline Dimo PetroffFriend
Messages: 15
Registered: November 2018
Junior Member
Is there some trick to enable autowrap? It doesn't seem to work even with a simple DSL:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Model:
	definitions+=VariableDefinition*
	eval=Expression;

VariableDefinition:
	name=ID ('(' parameter=ID ')')? '=' value=Expression;

Expression:
	FeatureCall | Literal;

Literal:
	literal=Boolean;

FeatureCall:
	target=[ecore::EObject|ID] ('(' argument=ID ')')?;

Boolean returns ecore::EString:
	'true' | 'false'


And its formatter:
class MyDslFormatter extends AbstractFormatter2 {
	
	@Inject extension MyDslGrammarAccess

	def dispatch void format(Model model, extension IFormattableDocument document) {
		model.definitions.forEach[format]
	}
	
	def dispatch void format(VariableDefinition it, extension IFormattableDocument document){
		val extension access = variableDefinitionAccess
		regionFor.keyword(equalsSignKeyword_2).prepend[oneSpace].append[oneSpace autowrap]
	}
}


Given this, the following test fails:
@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class MyDslFormattingTest {
	@Inject
	extension FormatterTestHelper

	@Test
	def void loadModel() {
		assertFormatted[
			request.preferences = new MapBasedPreferenceValues => [
				put(FormatterPreferenceKeys.maxLineWidth, 31)
			]
			toBeFormatted = '''
				myThing = longggggggggggggggggggggggggg
				myThing
			'''
			expectation = '''
				myThing =
				longggggggggggggggggggggggggg
				myThing
			'''
		]
	}
}


Maybe I'm missing something but the only documentation I could find was these slides and it looks like this should work.
Re: Formatter2 autowrap [message #1840466 is a reply to message #1840454] Thu, 15 April 2021 20:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13930
Registered: July 2009
Senior Member
you need some formatting after the stuff that should be autowrapped.
(am not sure if this is a bug or feature. so you may try

def dispatch void format(VariableDefinition it, extension IFormattableDocument document){
val kw = regionFor.keyword("=")
kw.prepend[oneSpace]
kw.append[autowrap; oneSpace]
append[newLine]
}


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
Re: Formatter2 autowrap [message #1840490 is a reply to message #1840466] Fri, 16 April 2021 11:53 Go to previous messageGo to next message
Dimo Petroff is currently offline Dimo PetroffFriend
Messages: 15
Registered: November 2018
Junior Member
This definitely looks like a bug to me. What is considered "after the stuff that should be autowrapped" though? Is it at the end of the model element specifically, or on the next hidden region? In my real DSL, I have plenty of formatting "after the stuff that should be autowrapped", it just happens in the next semantic element, for example if we change it to:
Expression:
	Addition;

Addition:
	Primary ({Addition.left=current} '+' right=Primary)*;

Primary:
	FeatureCall | Literal;


def dispatch void format(VariableDefinition it, extension IFormattableDocument document){
	val extension access = variableDefinitionAccess
	regionFor.keyword(equalsSignKeyword_2).prepend[oneSpace].append[oneSpace autowrap]
	append[newLine]
}
	
def dispatch void format(Addition it, extension IFormattableDocument document) {
	val extension access = additionAccess
	regionFor.keyword(plusSignKeyword_1_1).prepend[oneSpace autowrap].append[oneSpace]
	append[newLine]
}


assertFormatted[
	request.preferences = new MapBasedPreferenceValues => [
		put(FormatterPreferenceKeys.maxLineWidth, 31)
	]
	toBeFormatted = '''
		myThing = thing + longggggggggggggggggggggggggg + another
		myThing
	'''
	expectation = '''
		myThing =
		thing
		+ longggggggggggggggggggggggggg
		+ another
		myThing
	'''
]


This works for the autowrap at the equals sign, but there's no further autowrap at the + sign. I would have actually expected a forced line break before the second + in that test since I put append[newLine] on the Addition.
Re: Formatter2 autowrap [message #1840491 is a reply to message #1840490] Fri, 16 April 2021 12:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13930
Registered: July 2009
Senior Member
see

org.eclipse.xtext.formatting2.internal.FormattableDocument.createReplacements(ITextReplacerContext)

please debug there for differences

the autowrap happens only if there is something to format after the autowrap position
i dont know why. moritz is no longer active with Xtext


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: Fri, 16 April 2021 12:13]

Report message to a moderator

Re: Formatter2 autowrap [message #1840577 is a reply to message #1840491] Mon, 19 April 2021 16:37 Go to previous message
Dimo Petroff is currently offline Dimo PetroffFriend
Messages: 15
Registered: November 2018
Junior Member
The clue was in the missing forced line break before the second +, since I had forgotten to actually format the child expressions.

It looks like the "formatting after the stuff that should be autowrapped" is only needed if there is no other semantic element after "the stuff" instead.

The workaround I've settled on is putting append[lowPriority noSpace] on my top level element:
def dispatch void format(Model model, extension IFormattableDocument document) {
	model.definitions.forEach[format]
	append[lowPriority noSpace] // this is the workaround
}

This seems to enable the autowrap in all contained elements as suggested, and is unlikely to conflict with some trailing formatting that might be present on the last expression in the file.
Previous Topic:Bind STRINGValueConverter
Next Topic:Xtext LSP, Client with separate server process
Goto Forum:
  


Current Time: Sat May 08 23:38:28 GMT 2021

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

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

Back to the top