zedapp/zed — pull request #126 by robru (2014-03-14 06:53:40+00:00)
Title: Implement getCursorIndex and setCursorIndex for the sandbox.

Here's a tidy pair of functions for getting and setting the cursor's position not with a `row,col` pair but instead with `characters from start of file` index.

This is going to be particularly useful for extensions that use `session.getText`, because when they already have the entire file as a single, linear string, knowing the cursor as `row,col` is not very helpful. Being able to know the string index of your cursor location will be very handy.

For example, one of my goals is to implement a function called "just one space", it sucks up all the whitespace adjacent to the cursor (including newlines), and then inserts a single space there. The way I would implement this would be to call `session.getText` followed by `session.getCursorIndex`. With that information I'd split the string into two: text before the cursor and text after the cursor. Then I'd regex away any whitespace at the end of the first string and at the start of the second string, concatenate them back together with a single space, and then call `session.setText` to put it all back in the buffer.

Thanks for reviewing this!