getChildAttributes caret position
Hello,
is it possible to somehow get caret position when Block is deciding Indentation within getChildAttributes method?
I know I can get caret via Editor, but problem is, what if there are multiple carets when pressing Enter - which one should be take into account for which childAttribute?
What I'm struggling with is Python like language (without curly brackets) so for example
func name():|
When I press Enter at this caret I can easily set Indent for a new line like
func name():
|
What I'd like to do is when there are already empty lines above caret like
func name():
var a = 1
|
And I press Enter at this location to no more setIndentation, so result would be:
func name():
var a = 1
|
But as blocks ignore whitespaces I don't know how to count empty lines above caret.
Getting lines after the last element of previous block is also troubling as there can be more empty lines below caret position which would be counted as well so in case of
func name():
var a = 1
|
func name2():
var a = 1
I'd get 3 lines while caret is at the 1st line so I'd like to keep the Indent at this moment.
Please sign in to leave a comment.
Hi,
There is no information about the caret position available. Formatting can be performed even for files that are not open in the editor.
I suggest trying to check the AST nodes structure (if you extend ASTBlock) to understand the current position and return the proper attributes.
You can check the Python block implementation for some inspiration:
https://github.com/JetBrains/intellij-community/blob/master/python/python-psi-impl/src/com/jetbrains/python/formatter/PyBlock.java
but as far as I can see, it doesn't work in the way that you would like to achieve.
Note there could also be multiple carets in one editor.
I was afraid it might not be possible, I also could not find anything this specific in PyBlock.
Thank you for response.
It's unclear to me if you tried what I meant. I meant getting an AST node of the current position, checking if it's indented or not, what are the prev/next nodes and returning the proper attributes. The code I pasted contains parts getting prev node, etc.
I'm extending AbstractBlock, which implemnents ASTBlock,
but the position does not take into account whitespaces so I don't know after how many empty line I'm at -> or I got something wrong.
f.e.
2nd, 4th and 5th lines in AST are just WHITE_SPACE tokens but formatter ignores them, so it can add/remove them as required, even looking into documentation it says:
"and the tree of blocks must be built so that the bottom-level blocks cover all non-whitespace characters in the file"
So my position is something like: "func" block, which has a child of "var" block and my position is 2 as there are no blocks covering those empty lines and no matter how many lines I add, I'll still be at position 2
Or did you mean different position, than index from
?
You are right, sorry.
By checking positions, I meant getting the current AST node, but as you wrote blocks cover non-whitespace code, so getting an AST node for any block will be not precise enough to determine the current position.