Home » Modeling » TMF (Xtext) » Trigger JDT JavaDoc Formatter for Doc-Comments
Trigger JDT JavaDoc Formatter for Doc-Comments [message #716851] |
Thu, 18 August 2011 11:21  |
Eclipse User |
|
|
|
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 #717842 is a reply to message #717834] |
Mon, 22 August 2011 10:00   |
Eclipse User |
|
|
|
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
[Updated on: Mon, 22 August 2011 11:45] by Moderator
|
|
| | | |
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717911 is a reply to message #717877] |
Mon, 22 August 2011 12:53   |
Eclipse User |
|
|
|
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 12:53   |
Eclipse User |
|
|
|
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 #760568 is a reply to message #760547] |
Fri, 02 December 2011 18:38  |
Eclipse User |
|
|
|
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.
>
|
|
|
Goto Forum:
Current Time: Tue Aug 12 03:50:03 EDT 2025
Powered by FUDForum. Page generated in 0.15016 seconds
|