Get text from cursor to start of node

I’m trying to implement auto-indentation in my code blocks, and my first idea to do this is just get the text from the cursor and go back until it finds a newline character, and count the number of tabs inbetween. Then when the user presses enter it makes a new line and puts that many tabs.

What would be the easiest way to do this?

You could bind a command to Enter that checks whether the cursor is in a code block, and if so, gets its textContent, computes the cursor offset into that (which should just be $head.parentOffset), and computes the text to insert from those values.