Home » Modeling » TMF (Xtext) » Trigger JDT JavaDoc Formatter for Doc-Comments
| | | |
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717842 is a reply to message #717834] |
Mon, 22 August 2011 14:00 |
Daniel Missing name 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
[Updated on: Mon, 22 August 2011 15:45] Report message to a moderator
|
|
| | | |
Re: Trigger JDT JavaDoc Formatter for Doc-Comments [message #717911 is a reply to message #717877] |
Mon, 22 August 2011 16:53 |
Henrik Lindberg 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 |
Henrik Lindberg 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
|
|
| | |
Goto Forum:
Current Time: Fri Apr 26 21:12:28 GMT 2024
Powered by FUDForum. Page generated in 0.04872 seconds
|