Home Download Buy Blog Forum Support

how to check if cursor is at the end of the line ?

how to check if cursor is at the end of the line ?

Postby rsk82 on Tue Oct 15, 2013 10:34 pm

I'm beginnig to learn architecture of st plugin. And I don't know how approach to take to this issue. It should be fast without getting contents of the line.
What is the function to get line length in which cursor exists. And second how to get cursor position, that is line number and column number ?
rsk82
 
Posts: 8
Joined: Sat Feb 09, 2013 6:43 pm

Re: how to check if cursor is at the end of the line ?

Postby quodlibet on Wed Oct 16, 2013 11:01 am

Have you seen https://www.sublimetext.com/docs/3/api_reference.html ?

rsk82 wrote:What is the function to get line length in which cursor exists.


Look at the `line()` function in the API docs. Something like this:

Code: Select all
rg = view.line( view.sel()[0] )
length_of_rg = rg.b - rg.a


Hmm. It seems that the line function seems to ignore indentation (whitespace at the beginning of the line). I'm not sure what the best alternative is if this is an issue for you.

rsk82 wrote:And second how to get cursor position, that is line number and column number ?


rowcol(point) (int, int) Calculates the 0 based line and column numbers of the point.


Hope this helps,
Alex
quodlibet
 
Posts: 542
Joined: Sat Jun 18, 2011 5:53 pm

Re: how to check if cursor is at the end of the line ?

Postby randy3k on Tue Oct 22, 2013 3:49 am

rg.end()-rg.begin()

will be a better code, since rg.a can be larger than rg.b.
randy3k
 
Posts: 47
Joined: Sun Nov 18, 2012 5:16 am

Re: how to check if cursor is at the end of the line ?

Postby quodlibet on Tue Oct 22, 2013 6:26 am

@randy3k You're right, of course :)
quodlibet
 
Posts: 542
Joined: Sat Jun 18, 2011 5:53 pm

Re: how to check if cursor is at the end of the line ?

Postby bizoo on Tue Oct 22, 2013 8:47 am

In ST3, I suppose the fastest way is:
Code: Select all
is_cursor_at_the_end_of_the_line = view.classify(view.sel()[0].a) & sublime.CLASS_LINE_END != 0
bizoo
 
Posts: 886
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: how to check if cursor is at the end of the line ?

Postby lunixbochs on Sat Oct 26, 2013 9:17 am

The actual "cursor" for a selection is sel.b, not sel.a: view.sel()[0].b

This matters if you do an area selection.

I also urge you to iterate over the cursors and do proper multiple cursor support in your plugins instead of hard-coding view.sel()[0].
lunixbochs
 
Posts: 91
Joined: Fri Oct 08, 2010 10:18 pm

Re: how to check if cursor is at the end of the line ?

Postby sapphirehamster on Sat Oct 26, 2013 8:06 pm

lunixbochs wrote:The actual "cursor" for a selection is sel.b, not sel.a: view.sel()[0].b

This matters if you do an area selection.


This is not correct, as randy3k said just above. The "cursor" is at region.end().
sapphirehamster
 
Posts: 90
Joined: Sun Jul 01, 2012 11:19 pm

Re: how to check if cursor is at the end of the line ?

Postby lunixbochs on Sat Oct 26, 2013 10:10 pm

sapphirehamster wrote:This is not correct, as randy3k said just above. The "cursor" is at region.end().

Wrong. region.end() gives you the side of the region closest to the end of the buffer. region.b is the cursor.

To demonstrate, open a view in sublime, make a few lines, then drag select from the end of the view to the beginning. Your "cursor" (the blinking dot) will be at the beginning of the view, while the selection will span to the end of the view.

Open the console and run this:

Code: Select all
print(view.size(), view.sel()[0].b, view.sel()[0].end())

view.size() will equal region.end() and *not* region.b.

region.end() is only accurate when you do selections toward the end of the view, and wrong when you do selections toward the beginning of the view.

You should also be careful of using (region.begin(), region.end()) to save and restore cursor state, as you will potentially reverse the selection (like using abs() on a negative number).
Last edited by lunixbochs on Tue Oct 29, 2013 4:29 am, edited 1 time in total.
lunixbochs
 
Posts: 91
Joined: Fri Oct 08, 2010 10:18 pm

Re: how to check if cursor is at the end of the line ?

Postby sapphirehamster on Mon Oct 28, 2013 12:33 am

lunixbochs wrote:
sapphirehamster wrote:This is not correct, as randy3k said just above. The "cursor" is at region.end().

Wrong. region.end() gives you the side of the region closest to the end of the buffer. region.b is the cursor.


Oh, wow, sorry. I got mixed up. Thanks!
sapphirehamster
 
Posts: 90
Joined: Sun Jul 01, 2012 11:19 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests