How to implement auto format code on type?

Answered

I like it when I wrote java code Intellij IDEA provide some auto reformat, for example after I type colon in a swich-case statement, idea will reformat the line with correct indent:

I want to implement a similar feature in my custom language plugin, which interface should I use? Any help/pointers are gratefully received :)

0
3 comments

com.intellij.psi.codeStyle.lineIndent.LineIndentProvider

0

Yann Cebron, Thanks! 

Further question:  I see that LineIndentProvider only adjusts the indent of the line, but not whitespaces. If I want to reformat the line with both indent and whitespaces, what interface should I use?

For example, I want to reformat this right after I type enter at the end of this line:

if        a: print(a)

what I expect this line to be reformatted to: (remove redundant whitespaces, add a line break with correct indent)

if a:
print(a)

Please take another look, thanks!

0

Xiarui, use the com.intellij.codeInsight.actions.ReformatCodeProcessor action.

0

Please sign in to leave a comment.