Proper way to handle indentation
Given nested structure e.g.
item1 {
item2 {
}
}
I would like the IDE to place caret at one additional level of indentation when I hit Enter after `item2 {`.
By default indentation would be the same as of parent element (item2), but it should be nested one level more.
I guess this happens because I skip PsiWhitespace when building CodeBlock tree as tutorial suggests and thus I don't have control over empty lines (which is the initial state of the new line after I hit Enter).
What would be the proper way of handling it? I tried `EditorModificationUtilEx.insertStringAtCaret(editor, " ")` inside `EnterHandlerDelegate` but then IDE sees these WSs as normal whitespaces not as indentation (when I hit Backspace it first deletes these custom WSs and only after that upon additional Backspace hit it deletes whole indentation and places caret at previous line).
Should I consider including WS symbols into CodeBlock tree? Or is there a simpler way?
请先登录再写评论。
Hi,
Have you tried to implement
getChildAttributes
? See https://plugins.jetbrains.com/docs/intellij/code-formatting.html#building-the-block-tree.Hi,
The new child line is initially empty, thus not a part of block tree (since I drop WSs) and consequently no child attributes are applied.
Should I include WSs into block tree and handle all of them explicitly? (just, will it play well with spacing builder etc?)
Sorry, but I don't understand what the problem is. Sticking to recommendations from the documentation should allow implementing it.
Your language looks similar to JSON. Maybe its implementation can help?
https://github.com/JetBrains/intellij-community/blob/master/json/src/com/intellij/json/formatter/JsonBlock.java
The issue is that I get cursor like this after hitting enter
but I'd like to get it like this
Thanks for your recommendation, I tried to look into JSON implementation. It filters out WSs as well (
if (isWhitespaceOrEmpty(child)) continue;
), so I just don't get how it manages to apply additional level of indentation on empty line, maybe I'll have to “debug” it