Sublime Forum

Visible line's position

#1

Say I have a line long enough to be wrapped into 10 visible lines.
Is there an API to get the text positions of the beginning/end of visual line with my current position?

So if there is no text wrap and I scroll to the end of the line, these positions would be for the last portion of the line that fits
And if there is text wrap and the cursor is at the beginning of visual line 5, then these positions would be just for visual line 5, not the whole 10-visual-line line

0 Likes

#2

just to be clear, you’re after the text position (Point as the API defines it) - as in “character number from the beginning of the file”?

I would guess you’d need to play with text_to_layout to get the y position of visual line 5, then viewport_extent or layout_extent to get the max x position, then layout_to_text…

0 Likes

#3

Thanks for the tip, though as far as I understand, the layout_extent doesn’t measure the extent of a given text region, but rather the whole layout, so if my line’s visual width is small due to some long word being wrapped without breaks, then I won’t get the x-pixel value equivalent to 4 chars as length, but whatever the max width of the second/third visual line, so on line 1 I can’t relly jump to the middle point between chars 2 and 3
These APIs don’t accept a selection region as an argument, and there isn’t text_region_extent API

(| is cursor position below, it’s a single long “physical” line that should be split into multiple visual lines)

|1234 longwrappedwordaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

0 Likes

#4

I don’t entirely understand why my proposal doesn’t work for you… You can use different logic depending on whether word wrap is enabled for the view or not. And it shouldn’t matter where the physical line is wrapped as you should be able to work out where the physical line starts and ends in terms of visual lines…

view.layout_to_text((view.layout_extent()[0], 129))
227

view.line_height()
32

for visual_line_y in range(1, 129 + 1, int(view.line_height())): print(view.layout_to_text((view.layout_extent()[0], visual_line_y)))
55
110
165
220
227

2 Likes

#5

Oh, I see, collapsing back to text is what I was missing, that will give me the end of 7 symbols aaaaaaa on the last line, not the full potential visual width as I was thinking, and similarly I can get the beginning of the same line in symbol coordinates and thus get the middle

Thanks for clarificaion, think this should work!

0 Likes

#6

By the way, do you know how to get different layout coordinates for these identical text offsets?

  • the 1st happens when you press End at visual line 1 and
  • the 2nd — Home at visual line 2 (though you can’t get there via cursor movements)

Otherwise I get identical visual line coordinates even though visually the lines are different, and it’s weird to end up in he middle of visual line 1 when your cursor is at the visual line 2
subl%20end

subl%20end-beg

0 Likes

#7

For having the caret at (soft) BOL, you need to set xpos=0.0 on the region

1 Like

#8

Thanks again! I didn’t need to set it, just detect it, but xpos works for that as well: if

  • xpos is 0 and
  • caret text offset is bigger than the offset of the start of the physical line

it means it’s at the start of a visual line

1 Like