Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Reorder a model via formatter2 or validation and quick fix(How to change position of elements in model to given order in Eclipse editor)
Reorder a model via formatter2 or validation and quick fix [message #1808544] Wed, 26 June 2019 11:15 Go to next message
Eclipse UserFriend
Hi for the first time, greetings.

I want to reorder elements of a group to default (defined) order. The group is defined as an unordered group:

VehicleSpec returns dpm::VehicleSpec:
    {dpm::VehicleSpec}
    '@Vehicle'
    ('('
        (
                ('name:' name=EString)? &
                (roads?='roads')? &
                (sky?='sky')? &
                (rivers?='rivers')? &
                (space?='space')? &
                (passengers+=Passenger )*
        )
    ')')?
;


So this is valid code:

@Vehicle(
  space sky roads rivers
  name: aMiracle
)


Which I want to reorder in the editor to:

@Vehicle(
  name: aMiracle
  roads sky rivers space 
)


I've tried to extend AbstractFormatter2 but it is not designed to make changes in the semantic model.

Another solution I've been working on was to use validator and quick fix mechanisms. I've managed to extend DefaultQuickfixProvider and I've written a simple working acceptor:


acceptor.accept(issue, 'reorder text' ,'apply default order', 'order.png',
	new ISemanticModification() {
		override apply(EObject element, IModificationContext context) throws Exception {
			val vehicle = ((element as MyObject).vehicle as VehicleSpec)
			val xtextDocument = context.xtextDocument`
			var content = '''
				@Vehicle(
					«IF vehicle.name !== null»name: «vehicle.name»«ENDIF»
					«IF vehicle.roads»roads«ENDIF»
					«IF vehicle.sky»sky«ENDIF»
					«IF vehicle.rivers»rivers«ENDIF»
					«IF vehicle.space»space«ENDIF»
					«IF vehicle.passengers.size>0»«vehicle.indices.generate»«ENDIF»
				)
			'''
			xtextDocument.replace(issue.offset, issue.length, content )									
		}
	}
)


But a fix is triggered by validation check which I have no idea how to write. I can only check e.g. can vehicle fly or swim but no iteration through the ordered list of elements or search through text representation. Can you give me some advice on where to start from or on a different approach?
Re: Reorder a model via formatter2 or validation and quick fix [message #1808562 is a reply to message #1808544] Wed, 26 June 2019 17:02 Go to previous messageGo to next message
Eclipse UserFriend
i am not aware of any std. feature for that
maybe you can use a custom command and call the serializer there.
(or do it in your quickfix)
or you implement https://github.com/eclipse/xtext-eclipse/issues/610
you might need to customize serialization too
(or throw away the nodemodel for the serializer)

import com.google.inject.Inject
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import org.eclipse.xtext.testing.util.ParseHelper
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.^extension.ExtendWith
import org.xtext.example.mydsl.myDsl.VehicleSpec
import org.eclipse.xtext.serializer.ISerializer
import org.eclipse.xtext.nodemodel.util.NodeModelUtils

@ExtendWith(InjectionExtension)
@InjectWith(MyDslInjectorProvider)
class MyDslParsingTest {
	@Inject
	ParseHelper<VehicleSpec> parseHelper
	
	@Inject
	ISerializer ser
	
	@Test
	def void loadModel() {
		val result = parseHelper.parse('''
			@Vehicle(
			  space sky roads rivers
			  name: aMiracle
			)
		''')
		Assertions.assertNotNull(result)
		val errors = result.eResource.errors
		Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
		println(ser.serialize(result))
		// remove node model
		result.eAdapters().remove(NodeModelUtils.getNode(result));
		println(ser.serialize(result))
	}
}

Re: Reorder a model via formatter2 or validation and quick fix [message #1808592 is a reply to message #1808562] Thu, 27 June 2019 08:34 Go to previous message
Eclipse UserFriend
You've given me a few ideas Christian. Thanks for the quick answer. I guess my problem is similar to:
https://www.eclipse.org/forums/index.php/t/1089958/
...but I will also take a look at content assists.
Previous Topic:Scoping with URI
Next Topic:boolean assignment operator not working as expected
Goto Forum:
  


Current Time: Mon Mar 17 21:10:48 EDT 2025

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

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

Back to the top