Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to get xtext comment information?
How to get xtext comment information? [message #1839353] Fri, 19 March 2021 09:24 Go to next message
Eclipse UserFriend
I want to preserve the comments in my dsl and change the comments like python's grammar.
such as:
In my dsl:
// comments
/* comments */ A = B /* comments */; 

generated python code:
// comments
''' comments ''' A = B /* comments */

If I define a new comment rule like
Comment: /* (name+=Any_Other)* */
, I cannot figure out a way to put the rule in my grammar. ( I have to restrict the place where comment can exist! )
Is there a simple way to achieve this?
Re: How to get xtext comment information? [message #1839354 is a reply to message #1839353] Fri, 19 March 2021 09:26 Go to previous messageGo to next message
Eclipse UserFriend
you need to access the node model and traverce it. NodeModelUtils might be a starting point.
Re: How to get xtext comment information? [message #1839366 is a reply to message #1839354] Fri, 19 March 2021 10:19 Go to previous messageGo to next message
Eclipse UserFriend
I have looked through some information in the node by debug mode, but I don't know how to get the comment in one node. The function getTokenText will still remove hidden tokens such as whitespace/comments. Is there any way to change the default hidden action for comments?
Re: How to get xtext comment information? [message #1839368 is a reply to message #1839366] Fri, 19 March 2021 10:25 Go to previous messageGo to next message
Eclipse UserFriend
you can call INode.getText and INode.getPrevious/getNextSibling
you may let you inspire from MultiLineCommentDocumentationProvider
Re: How to get xtext comment information? [message #1839379 is a reply to message #1839368] Fri, 19 March 2021 11:52 Go to previous messageGo to next message
Eclipse UserFriend
Thank you ! Now I can get text of the nodes. But when it comes to preserve the comments, I wonder where should I use these nodes and comments.
For example, in my xtend file I define compile functions to translate my rules to python. In these functions I can get nodes and then get the comments. But I still need to know the location of comments. Otherwise it cannot handle code such as
/**/ A /**/ = B;

My compile function is like this:
private def compile(AbstractElement e) {
    	var ret = new String;
    	/*Where should I get and add the comments to the return string?*/
    	if(...) {
    		ret += '''«e.a.compile»'''
    	}
    	else
    		ret += '''«e.b.compile»'''
        return ret;
   }
Re: How to get xtext comment information? [message #1839385 is a reply to message #1839379] Fri, 19 March 2021 12:28 Go to previous messageGo to next message
Eclipse UserFriend
use the node of your objects or features of object as starting point (see nodeModelUtils)
Re: How to get xtext comment information? [message #1839392 is a reply to message #1839385] Fri, 19 March 2021 14:39 Go to previous messageGo to next message
Eclipse UserFriend
Hi

You might find that https://git.eclipse.org/r/plugins/gitiles/ocl/org.eclipse.ocl/+/refs/heads/master/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/IdiomsCommentSegmentSupport.java gives you some ideas.

Regards

Ed Willink
Re: How to get xtext comment information? [message #1839405 is a reply to message #1839385] Sat, 20 March 2021 02:08 Go to previous messageGo to next message
Eclipse UserFriend
Do you mean I have to generate comments like this:
private def compile(AbstractElement e) {
    	var ret = new String;
    	if(findcomment()) { ret +=  commentStirng; }
    	ret += '''«e.a.compile»'''
        if(findcomment()) { ret +=  commentStirng; }
    	ret += '''«e.b.compile»'''
        if(findcomment()) { ret +=  commentStirng; }
        return ret;
   }
Re: How to get xtext comment information? [message #1839406 is a reply to message #1839405] Sat, 20 March 2021 02:20 Go to previous messageGo to next message
Eclipse UserFriend
Besides the very inconvenient use of
Xtend Rich strings more or less yes
Re: How to get xtext comment information? [message #1839414 is a reply to message #1839406] Sat, 20 March 2021 09:20 Go to previous message
Eclipse UserFriend
Hi

Note that the Xtext philosophy is that comments are transient, i.e. they are converted from source text to the node model but are not incorporated in the final AS model, so if you save the AS model and reload and serialize back to text the comments are lost.

If you care enough about comments to extract them from the node model, you should perhaps make them first class rather than hidden components of your grammar. This will avoid all need to recover comments from the node model but will incur a bot of a fight to support comments everywhere, which isn't always that sensible, so supporting only sensible comments may be adequate.

In Java, who really wants to be able to type

import/*this is an import*/ java// This is the system package
./*separated from*/lang./*and now the real class*/String;

(Both Xtext and Java get this perversion right!).

Regards

Ed Willink
Previous Topic:Xtext 2.14 - programmatic EMF model manipulation with minimal re-indexing
Next Topic:Not valid for cross reference
Goto Forum:
  


Current Time: Thu Jul 10 00:55:43 EDT 2025

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

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

Back to the top