Handle editor newline creation/deletion
Answered
Hello! I wondering is it possible to handle line adding/deletion in editor.
I found TypedActionHandler, but still have several questions:
1) How to handle newline adding to document? Can I just check if char passed as argument is CR\LF? Or should I do this somehow else?
2) How to handle line deletion? Didn't found anything for this case.
Thanks!
Please sign in to leave a comment.
More accurately I want to handle every action in editor, which affected line number in document.
Yes, you need a com.intellij.codeInsight.editorActions.BackspaceHandlerDelegate for backspace and typedHandler for enter. With checking if typed char is '\n'
Thank you for answer, Alexandr!
But how can I detect whole line detection in BackspaceHandler? And also is there a way for handling events like methods generation or methods reordering? Because I need to handle all these cases in my plugin.
More detailed, I am adding a custom LineExtensionInfo for each method first line. And because (if I understand logic correctly) line extensions are bounded to line number in document, I need somehow handle all these scenarios with addition/deletion of new lines in order to keep my line extension near first line of related method.
Unsure what do you mean with `whole line detection`. It handles backspace hits.
Why do you need to track anything for adding LineExtensionInfo? Shouldn't you just use com.intellij.openapi.editor.EditorLinePainter?
Yes, I am adding LineExtensionInfo with EditorLinePainter, but I need to keep this extensions on the first line of each method in class.
May be I can explain this better with example:
I have a method foo() staring on line 5 and a method bar() starting on line 10 and, relatively, line extensions on lines 5 and 10.
If I add a new line somewhere above line 5, method foo() will start on line 6 and method bar() will start on line 11, but related extensions will remain on lines 5 and 10 respectively.
I want to handle this kind of situations.
Thanks!
I've never used this facility, but i'd expect that platform refreshes information after modification.
Yes, extensions are re-drawed after modification, but because I'm saving start line number for each method, I need somehow trigger this data refresh on every action related with line-changes in document.
There are a lot of actions, which can modify line count in editor. It's hardly possible to track all of them.
If you need to track all possible cases, you might want to add a document listener (see Document.addDocumentListener) and detect cases which change line count in it, e.g. by checking the number of line breaks in old/new fragments passed in an event.