what is the correct way to implement indent after "enter"

Answered

I'm trying to add an indent in my simple "if block" that looks exactly like in java. I tried with formatters, which doesn't seem to be the way to go. Now i'm trying with enter processors and handlers/delegates, but no luck still, they don't seem to trigger at all. Is there an example or documentation on how to do it?

3 comments
Comment actions Permalink

the last thing I tried was to extend "EnterBetweenBracesDelegate" and override "formatAtOffset" where I tried to append white spaces "    " and move caret. It did append the white spaces but didn't move the caret. 

editor.getDocument().insertString(editor.getCaretModel().getOffset() - 5, "    ");
LogicalPosition logicalPosition = editor.getCaretModel().getLogicalPosition();
LogicalPosition newLogicalPosition = new LogicalPosition(logicalPosition.line, logicalPosition.column + 4);

editor.getCaretModel().moveToLogicalPosition(newLogicalPosition);

Any way, what I'm trying doesn't sound right. But over the years, many people asked the same question as me, and I didn't see any useful answer. Seems like everyone is trying just about anything, eventually they get it working in probably sub-optimal way. Some documentation on this would be useful.

 

0
Comment actions Permalink

Same issue here, I have a formatter working fine, but I can't get the smart indent on enter working. I asked for an advice in Slack, see https://jetbrains-platform.slack.com/archives/C5U8BM1MK/p1610744329019200

0
Comment actions Permalink

Sorry for delay. Please try using extension point com.intellij.psi.codeStyle.lineIndent.LineIndentProvider which is Document-based (do not commit Document or use PSI), see https://plugins.jetbrains.com/intellij-platform-explorer/?extensions=com.intellij.lineIndentProvider for sample code.

0

Please sign in to leave a comment.