Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Trigger JDT JavaDoc Formatter for Doc-Comments
Trigger JDT JavaDoc Formatter for Doc-Comments [message #716851] Thu, 18 August 2011 15:21 Go to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Hi everybody.

In my DSL I provide a JavaDoc like syntax for documenting elements (including annotations). The highlighting is done using an ISemanticHighlightingCalculator. The problem: Since the whole doc-comment is a terminal the formatting does not work well.

Since my doc comments are syntactically the same as JavaDoc I thought it would be great to use the JDT JavaDoc formatter since it is quite impressive.

Is there a possiblity to trigger the JDT formatter on subsections of Xtext documents like my comment terminal?

Also a great extension would be if I could use the JDT Syntax Highlighter to highlight my comment section. The SemanticHighlighter is not as fast as the JavaDoc highlighting and the JavaDoc highlighting even supports HTML highlighting.


Cheers
Daniel

Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717734 is a reply to message #716851] Mon, 22 August 2011 08:20 Go to previous messageGo to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
I found out how to trigger the code formatter for JavaDoc. Quite simple:
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
    TextEdit formattedJavaDoc = formatter.format(CodeFormatter.K_JAVA_DOC, source, indentionLevel, source.length(), 0, "\n");


But the problem is how to integrate this piece of code into the FormattingConfigBasedStream used by the Xtext formatting system. Any ideas?

Greetings
Daniel
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717739 is a reply to message #717734] Mon, 22 August 2011 08:34 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

you will not be able to solve that via configuration only. My *guess* is that you have to override createFormatterStream in your Formatter, returning an adapted version of FormattingConfigBasedStream, where you create the tokens for the JavaDoc sections yourself.

Alex
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717834 is a reply to message #717739] Mon, 22 August 2011 13:34 Go to previous messageGo to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Hi.

Your hint gave me another idea. As it would be quite hard to split up my JavaDoc in custom tokens, I'd like to keep the comment as one token but replace it by a formatted token during formatting. Think of this scenario:

The FormattingConfigBasedStream is formatting semantic elements till a JavaDoc element comes along. From this element I create an IDocument and format it using the previously mentioned code. The old comment token is thrown away and a new token containing the formatted comment string is added to the stream.

I'll give it a try and post the results here.
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717842 is a reply to message #717834] Mon, 22 August 2011 14:00 Go to previous messageGo to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Simply awesome, works like in JDT! And it can even be used for multiline comment and singleline comment formatting.

Here's my source:

1. Add the required dependencies to your project project (not the ui plugin):
org.eclipse.jdt
org.eclipse.jdt.core
org.eclipse.text

2. Add this piece of code to your MyDslFormatter to use your own token stream
  @Inject
  private IGrammarAccess grammarAccess;
  
  @Override
  public ITokenStream createFormatterStream(String indent, ITokenStream out, boolean preserveWhitespaces) {
    return new DocFormattingConfigBasedStream(out, indent, getConfig(), createMatcher(), getHiddenTokenHelper(),
        preserveWhitespaces, (MyDslGrammarAccess) grammarAccess);
  }


3. Create the class DocFormattingConfigBasedStream which triggers the JDT
package at.mydsl.formatting;

import java.io.IOException;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.formatting.IElementMatcherProvider.IElementMatcher;
import org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementPattern;
import org.eclipse.xtext.formatting.impl.FormattingConfig;
import org.eclipse.xtext.formatting.impl.FormattingConfigBasedStream;
import org.eclipse.xtext.parsetree.reconstr.IHiddenTokenHelper;
import org.eclipse.xtext.parsetree.reconstr.ITokenStream;

/**
 * This stream uses the JDT code formatter for formatting 
 * single-line, multi-line and documentation comments
 * @author Daniel Kuschny
 */
public class DocFormattingConfigBasedStream extends FormattingConfigBasedStream {
  
  private AsgardGrammarAccess grammarAccess;

  // create a formatter for reusage
  private static CodeFormatter formatter = ToolFactory
                                             .createCodeFormatter(null);

  public DocFormattingConfigBasedStream(ITokenStream out, String indentation,
      FormattingConfig cfg, IElementMatcher<ElementPattern> matcher,
      IHiddenTokenHelper hiddenTokenHelper, boolean preserveSpaces, 
      MyDslGrammarAccess grammarAccess) {
    super(out, indentation, cfg, matcher, hiddenTokenHelper, preserveSpaces);
    this.grammarAccess = grammarAccess;
  }

  @Override
  protected void addLineEntry(EObject grammarElement, String value,
      boolean isHidden) throws IOException {
    
    // check for terminal rules
    if(grammarElement instanceof TerminalRule) {
      
      
      TerminalRule rule = (TerminalRule)grammarElement;
      
      // check for our lovely comment rules
      // and trigger the formatter to replace the token value
      
      if(grammarAccess.getDOC_COMMENTRule().equals(rule)) {
        value = formatComment(CodeFormatter.K_JAVA_DOC, value);
      }
      else if(grammarAccess.getML_COMMENTRule().equals(rule)) {
        value = formatComment(CodeFormatter.K_MULTI_LINE_COMMENT, value);
      }
      else if(grammarAccess.getSL_COMMENTRule().equals(rule)) {
        value = formatComment(CodeFormatter.K_SINGLE_LINE_COMMENT, value);
      }
    }

    super.addLineEntry(grammarElement, value, isHidden);
  }

  /**
   * Formats the given code using the JDT code formatter. 
   * @param kind Use to specify the kind of the code snippet to format. It can
   * be any of these:
   * <ul>
   *  <li>{@link CodeFormatter#K_EXPRESSION}</li>
   *  <li>{@link CodeFormatter#K_STATEMENTS}</li>
   *  <li>{@link CodeFormatter#K_CLASS_BODY_DECLARATIONS}</li>
   *  <li>{@link CodeFormatter#K_COMPILATION_UNIT}</li>
   *  <li>{@link CodeFormatter#K_UNKNOWN}</li>
   *  <li>{@link CodeFormatter#K_SINGLE_LINE_COMMENT}</li>
   *  <li>{@link CodeFormatter#K_MULTI_LINE_COMMENT}</li>
   *  <li>{@link CodeFormatter#K_JAVA_DOC}</li>
   * </ul>
   * @param source the source to format
   * @see CodeFormatter#format(int, String, int, int, int, String)
   * @return the formatted code 
   */
  private static String formatComment(int mode, String source) {
    try {
      // get the text diff
      TextEdit formatted = formatter.format(mode, source, 0,
          source.length(), 0, "\n");
      
      // apply the diff to the source  
      IDocument document = new Document(source);
      formatted.apply(document);
      return document.get();
    }
    catch (Exception e) {
      // on failure return the original code
      return source;
    }
  }
}


Feel free to use it anywhere you like Very Happy

[Updated on: Mon, 22 August 2011 15:45]

Report message to a moderator

Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717846 is a reply to message #717842] Mon, 22 August 2011 14:06 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

in order to make the code a bit more robust, you should use your specific grammar access in order to compare the terminal rule directly instead of comparing the name of the rule.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717858 is a reply to message #717834] Mon, 22 August 2011 13:52 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
I do something similar, I parse comments, and attach the styling
information in an adapter. I then pick this up in semantic highlighting.

- henrik
On 8/22/11 3:35 PM, Daniel wrote:
> Hi.
> Your hint gave me another idea. As it would be quite hard to split up my
> JavaDoc in custom tokens, I'd like to keep the comment as one token but
> replace it by a formatted token during formatting. Think of this scenario:
>
> The FormattingConfigBasedStream is formatting semantic elements till a
> JavaDoc element comes along. From this element I create an IDocument and
> format it using the previously mentioned code. The old comment token is
> thrown away and a new token containing the formatted comment string is
> added to the stream.
> I'll give it a try and post the results here.
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717877 is a reply to message #717846] Mon, 22 August 2011 15:49 Go to previous messageGo to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Alexander Nittka wrote on Mon, 22 August 2011 16:06
...
in order to make the code a bit more robust, you should use your specific grammar access in order to compare the terminal rule directly instead of comparing the name of the rule.
...


Thanks for the hint. I changed the code to use IGrammarAccess.

Henrik Lindberg wrote on Mon, 22 August 2011 15:52
I do something similar, I parse comments, and attach the styling
information in an adapter. I then pick this up in semantic highlighting.


Sounds interesting. I'd like to see how you pick up the highlighting of the original Java highlighter and use it in the semantic highlighter of Xtext.

> Daniel
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717911 is a reply to message #717877] Mon, 22 August 2011 16:53 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 8/22/11 5:49 PM, Daniel wrote:
> Henrik Lindberg wrote on Mon, 22 August 2011 15:52
>> I do something similar, I parse comments, and attach the styling
>> information in an adapter. I then pick this up in semantic highlighting.
>
>
> Sounds interesting. I'd like to see how you pick up the highlighting of
> the original Java highlighter and use it in the semantic highlighter of
> Xtext.

I am not using the JavaDoc highlighter, I parse todo/fixme's and Rdoc
style comments. Code is at github in clodusmith/geppetto.

As an example, I store Task instances in an adapter, the Task has
message, offset, length and priority. Semantic highlighting just paints
"task style" for the given offsets and lengths.

- henrik
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717913 is a reply to message #717877] Mon, 22 August 2011 16:53 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 8/22/11 5:49 PM, Daniel wrote:
> Henrik Lindberg wrote on Mon, 22 August 2011 15:52
>> I do something similar, I parse comments, and attach the styling
>> information in an adapter. I then pick this up in semantic highlighting.
>
>
> Sounds interesting. I'd like to see how you pick up the highlighting of
> the original Java highlighter and use it in the semantic highlighter of
> Xtext.

I am not using the JavaDoc highlighter, I parse todo/fixme's and Rdoc
style comments. Code is at github in clodusmith/geppetto.

As an example, I store Task instances in an adapter, the Task has
message, offset, length and priority. Semantic highlighting just paints
"task style" for the given offsets and lengths.

- henrik
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #760547 is a reply to message #717911] Fri, 02 December 2011 19:26 Go to previous messageGo to next message
Federico Sellanes is currently offline Federico SellanesFriend
Messages: 71
Registered: November 2011
Member
Can somebody give me an example of how i have to write the doc_comment terminal, i am a beginner in xtext and i need a little help, and how is associated this doc_comment terminal with a function rule of my dsl. Those are my questions.

Sincerely i read a lot of times this post and try a few things and I can not get my work.

Thanks, in advance.
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #760568 is a reply to message #760547] Fri, 02 December 2011 23:38 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
A javadoc terminal is quite trivial (unless you have other terminals
that overlaps with this):

terminal JAVADOC : '/**' -> '*/' ;

The easiest is to have the javadoc elements as optional elements in the
grammar. (I implemented it that way in Eclipse/b3). i.e. something like:

classdeclaration : doc = JAVADOC? 'class' name = ID ... ;

Hope that helps.

- henrik

On 2011-02-12 20:26, Federico Sellanes wrote:
> Can somebody give me an example of how i have to write the doc_comment
> terminal, i am a beginner in xtext and i need a little help, and how is
> associated this doc_comment terminal with a function rule of my dsl.
> Those are my questions.
>
> Sincerely i read a lot of times this post and try a few things and I can
> not get my work.
> Thanks, in advance.
>
Previous Topic:Generate Model Artifacts Programatically
Next Topic:link reference issue
Goto Forum:
  


Current Time: Fri Apr 26 21:12:28 GMT 2024

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

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

Back to the top