Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Solved] sorting AST through quickfix(a beginner's problem )
[Solved] sorting AST through quickfix [message #1173840] Wed, 06 November 2013 19:53 Go to next message
Eran B is currently offline Eran BFriend
Messages: 3
Registered: November 2013
Junior Member
Hi,
I've Been Trying to change the order of nodes through quickfix, but something is wrong.
Here's my code in xtend:

@Fix(org.xtext.custom.conventions.validation.ConventionsValidator::CONVENTION_NOT_ORDERED)
  def fixFeatureName(  Issue issue,   IssueResolutionAcceptor acceptor){   
  	acceptor.accept(issue, 'Sort', "Sort '" + issue.data.head + "'", null)[
      element, context |
        var gr=(element as Greeting)
        if (gr.name === null || gr.name.length === 0)
        	return;
        var econt=gr.eContainer.eContents
        var comparator = [ EObject obj1, EObject obj2 |
        	var o1 = (obj1 as Greeting)
        	var o2 = (obj2 as Greeting)
                return o1.name.compareTo(o2.name)
        	]
        ECollections::sort(econt, comparator)
	]
      }


No exception is being thrown to console, in debug I found an UnsupportedOperationException is thrown and handled by xtext.
I suspect that EList is immutable.
So how can I sort the AST?


(Here is the generated code: )
@Fix(ConventionsValidator.CONVENTION_NOT_ORDERED)
  public void fixFeatureName(final Issue issue, final IssueResolutionAcceptor acceptor) {
    String[] _data = issue.getData();
    String _head = IterableExtensions.<String>head(((Iterable<String>)Conversions.doWrapArray(_data)));
    String _plus = ("Sort \'" + _head);
    String _plus_1 = (_plus + "\'");
    final ISemanticModification _function = new ISemanticModification() {
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          Greeting gr = ((Greeting) element);
          boolean _or = false;
          String _name = gr.getName();
          boolean _tripleEquals = (_name == null);
          if (_tripleEquals) {
            _or = true;
          } else {
            String _name_1 = gr.getName();
            int _length = _name_1.length();
            boolean _tripleEquals_1 = (Integer.valueOf(_length) == Integer.valueOf(0));
            _or = (_tripleEquals || _tripleEquals_1);
          }
          if (_or) {
            return;
          }
          EObject _eContainer = gr.eContainer();
          EList<EObject> econt = _eContainer.eContents();
          final Function2<EObject,EObject,Integer> _function = new Function2<EObject,EObject,Integer>() {
              public Integer apply(final EObject obj1, final EObject obj2) {
                Greeting o1 = ((Greeting) obj1);
                Greeting o2 = ((Greeting) obj2);
                String _name = o1.getName();
                String _name_1 = o2.getName();
                return _name.compareTo(_name_1);
              }
            };
          Function2<EObject,EObject,Integer> comparator = _function;
          final Function2<EObject,EObject,Integer> _converted_comparator = (Function2<EObject,EObject,Integer>)comparator;
          ECollections.<EObject>sort(econt, new Comparator<EObject>() {
              public int compare(EObject o1,EObject o2) {
                return _converted_comparator.apply(o1,o2);
              }
          });
        }
      };
    acceptor.accept(issue, "Sort", _plus_1, null, _function);
  }



thanks!

[Updated on: Fri, 08 November 2013 01:23]

Report message to a moderator

Re: sorting AST through quickfix [message #1174435 is a reply to message #1173840] Thu, 07 November 2013 05:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13488
Registered: July 2009
Senior Member
Hi,

Adding Them to java until list. Sort them. Remove them from parents
get greetings and readd them sorted?

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: sorting AST through quickfix [message #1175828 is a reply to message #1174435] Fri, 08 November 2013 01:22 Go to previous messageGo to next message
Eran B is currently offline Eran BFriend
Messages: 3
Registered: November 2013
Junior Member
thanks for the answer, but it didn't help, even when I only did
econt.clear
I got the same exception.

so one solution was to force a cast of econtainer as it's runtime element (which is Model), and then getting a list with it's getGreetings getter, and with that element the sorting works, but I didn't want to involve non-generic code, for technical reasons.

So after a lot of experiments I finally found that element without involving any other elements or keywords from the grammar:
var econt = (gr.eContainer.eGet(gr.eContainingFeature) as EObjectContainmentEList<Greeting>)

and that is exactly what was looking for. Sorting is successful.

Here's the resulting Xtend code (got rid of casing in the comperator as well):
	@Fix(ConventionsValidator::CONVENTION_NOT_ORDERED)
	def fixFeatureName(Issue issue, IssueResolutionAcceptor acceptor) {
		acceptor.accept(issue, 'Sort', "Sort '" + issue.data.head + "'", null) [
		  element, context |
			var gr = (element as Greeting)
			if (gr.name === null || gr.name.length === 0)
				return;
			var econt = (gr.eContainer.eGet(gr.eContainingFeature) as EObjectContainmentEList<Greeting>)
			var comparator = [ Greeting o1, Greeting o2 |
				return o1.name.compareTo(o2.name)
			]
			ECollections::sort(econt, comparator)
		]
	}


and the generated java:
@Fix(ConventionsValidator.CONVENTION_NOT_ORDERED)
  public void fixFeatureName(final Issue issue, final IssueResolutionAcceptor acceptor) {
    String[] _data = issue.getData();
    String _head = IterableExtensions.<String>head(((Iterable<String>)Conversions.doWrapArray(_data)));
    String _plus = ("Sort \'" + _head);
    String _plus_1 = (_plus + "\'");
    final ISemanticModification _function = new ISemanticModification() {
        public void apply(final EObject element, final IModificationContext context) throws Exception {
          Greeting gr = ((Greeting) element);
          boolean _or = false;
          String _name = gr.getName();
          boolean _tripleEquals = (_name == null);
          if (_tripleEquals) {
            _or = true;
          } else {
            String _name_1 = gr.getName();
            int _length = _name_1.length();
            boolean _tripleEquals_1 = (Integer.valueOf(_length) == Integer.valueOf(0));
            _or = (_tripleEquals || _tripleEquals_1);
          }
          if (_or) {
            return;
          }
          EObject _eContainer = gr.eContainer();
          EStructuralFeature _eContainingFeature = gr.eContainingFeature();
          Object _eGet = _eContainer.eGet(_eContainingFeature);
          EObjectContainmentEList<Greeting> econt = ((EObjectContainmentEList<Greeting>) _eGet);
          final Function2<Greeting,Greeting,Integer> _function = new Function2<Greeting,Greeting,Integer>() {
              public Integer apply(final Greeting o1, final Greeting o2) {
                String _name = o1.getName();
                String _name_1 = o2.getName();
                return _name.compareTo(_name_1);
              }
            };
          Function2<Greeting,Greeting,Integer> comparator = _function;
          final Function2<Greeting,Greeting,Integer> _converted_comparator = (Function2<Greeting,Greeting,Integer>)comparator;
          ECollections.<Greeting>sort(econt, new Comparator<Greeting>() {
              public int compare(Greeting o1,Greeting o2) {
                return _converted_comparator.apply(o1,o2);
              }
          });
        }
      };
    acceptor.accept(issue, "Sort", _plus_1, null, _function);
  }
Re: sorting AST through quickfix [message #1176218 is a reply to message #1175828] Fri, 08 November 2013 07:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13488
Registered: July 2009
Senior Member
Hi,

with "parents get greetings" i actually implied this. if you do this generic (have no idea why) or via cast is a matter of taste


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: sorting AST through quickfix [message #1176344 is a reply to message #1176218] Fri, 08 November 2013 09:19 Go to previous message
Eran B is currently offline Eran BFriend
Messages: 3
Registered: November 2013
Junior Member
Oh, OK, I guess I didn't understand you fully.

The reason I wanted it done without casting is, in short, that this code is generated by a different program according to user input. so aside from picking the element he wants to sort, the user shouldn't supply any further information.

Thanks!
Previous Topic:Where to find org.eclipse.xtext.xtend_0.0.0
Next Topic:Abstract*RuntimeModule generation woes
Goto Forum:
  


Current Time: Mon Jul 06 06:13:58 GMT 2020

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

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

Back to the top