@@ -171,18 +171,30 @@ function win:get_width()
171
171
return vim .api .nvim_win_get_width (self :get_win ()) + self :get_border_size ().horizontal
172
172
end
173
173
174
- --- @return { bufnr : number , start_line : number , end_line : number , horizontal_offset : number }
175
- function win .get_screen_scroll_range ()
176
- local bufnr = vim .api .nvim_win_get_buf (0 )
177
- local line_count = vim .api .nvim_buf_line_count (bufnr )
178
-
179
- -- Get the scrolled range (start and end line)
180
- local start_line = math.max (1 , vim .fn .line (' w0' ) - 1 )
181
- local end_line = math.max (start_line , math.min (line_count , vim .fn .line (' w$' ) + 1 ))
174
+ --- Gets the cursor's distance from the top and bottom of the window
175
+ --- @return { distance_from_top : number , distance_from_bottom : number }
176
+ function win .get_cursor_screen_position ()
177
+ local win_height = vim .api .nvim_win_get_height (0 )
178
+ local cursor_line = vim .api .nvim_win_get_cursor (0 )[1 ]
179
+
180
+ -- Calculate the visual distance from top of the window sinc the vim.fn.line()
181
+ -- func gives the literal line number but there can be folds.
182
+ -- HACK: ideally there's a more generic solution since vertical conceal
183
+ -- will be added in the future
184
+ local distance_from_top = 0
185
+ local line = math.max (1 , vim .fn .line (' w0' ))
186
+ while line < cursor_line do
187
+ distance_from_top = distance_from_top + 1
188
+ if vim .fn .foldclosedend (line ) ~= - 1 then line = vim .fn .foldclosedend (line ) end
189
+ line = line + 1
190
+ end
182
191
183
- local horizontal_offset = vim . fn . winsaveview (). leftcol
192
+ local distance_from_bottom = win_height - distance_from_top - 1
184
193
185
- return { bufnr = bufnr , start_line = start_line , end_line = end_line , horizontal_offset = horizontal_offset }
194
+ return {
195
+ distance_from_bottom = distance_from_bottom ,
196
+ distance_from_top = distance_from_top ,
197
+ }
186
198
end
187
199
188
200
return win
0 commit comments