How to add javadoc style documentation comments to a language plugin?
When adding a custom language plugin for Courier, I added support for javadoc style documentation comments, e.g.:
/**
* A comment.
*/
record Example {
}
I've mimicked the behavior of the Intellij Java plugin as closely as I could to get this to work properly and it ALMOST works!
The only remaining issue is that when placing the cursor at the end of the first line of the comment, e.g.:
/**<cursor>
* A comment.
*/
record Example...
And then hitting enter to add a newline, the 2nd line of the comment is incorrectly indented like:
/**
* <--- incorrectly indented.
* A comment.
*/
record Example...
I'd like, if possible, to figure out how to get that line indented correctly.
Some notes on how I implemented the javadoc doc comment support:
- Declare a single token in my custom language's grammar for doc comments
- Customize the element type for doc comments so that they are parsed by a separate grammar, similar to the Java plugin's JavaDocElementType.DOC_COMMENT.
- Add an ASTBlock for the doc comment token similar to the Java plugin's DocComment Block
- Write a grammar for doc comments similar to the java plugin's _JavaDocLexer.flex and a custom lexer similar to JavaDocLexer.java.
- Write a CodeDocumentationAwareCommenterEx
This gets very close to the correct behavior. But something is missing causing the formatter to incorrectly indent for the case above. Anyone know what else must be done?
请先登录再写评论。
Hi, Joe!
Reason why * is indented properly is because formatting model is implemented for java language.
If you invoke reformat code action (Ctrl-Alt-L by default) in java on:
Yaroslav,
Thanks! I added a new FormattingModelBuilder for the doc comment sub-language and included a rule like:
Works perfectly!
-Joe