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:

  1. Declare a single token in my custom language's grammar for doc comments
  2. 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.
  3. Add an ASTBlock for the doc comment token similar to the Java plugin's DocComment Block
  4. Write a grammar for doc comments similar to the java plugin's _JavaDocLexer.flex and a custom lexer similar to JavaDocLexer.java.
  5. 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?

2 comments

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:

/**
*
*/
asterisks will be adjusted properly.

The same engine works when you hitting enter:
- you hit enter
- line feed is inserted
- if we are inside comment * is inserted
- after that "codeStyleManager.adjustLineIndent(myFile, myOffset);"
(currently EnterHandler:424) is called and formatter engine calculates proper indent
- proper indent is applied.

You need to implement FormattingModelBuilder(which will handle all the formatting)
for your language.
0

Yaroslav,

Thanks! I added a new FormattingModelBuilder for the doc comment sub-language and included a rule like:

.before(SchemadocTypes.DOC_COMMENT_LEADING_ASTRISK).spaces(1)

Works perfectly!

-Joe

0

Please sign in to leave a comment.