@@ -15,10 +15,11 @@ local MAX_VIEWPORT_LINES = 500
1515--- @return integer start_line 0-indexed inclusive
1616--- @return integer end_line 0-indexed exclusive (-1 means all )
1717--- @return integer line_offset lines skipped from the start
18+ --- @return integer total total line count of the source buffer
1819function M .compute_viewport (source_bufnr , target_line )
1920 local total = vim .api .nvim_buf_line_count (source_bufnr )
2021 if total <= MAX_VIEWPORT_LINES then
21- return 0 , - 1 , 0
22+ return 0 , - 1 , 0 , total
2223 end
2324
2425 local half = math.floor (MAX_VIEWPORT_LINES / 2 )
@@ -28,7 +29,7 @@ function M.compute_viewport(source_bufnr, target_line)
2829 start_line = math.max (0 , end_line - MAX_VIEWPORT_LINES )
2930 end
3031
31- return start_line , end_line , start_line
32+ return start_line , end_line , start_line , total
3233end
3334
3435--- @param bufnr integer
5960
6061--- @param location PeekstackLocation
6162--- @param opts ? table
62- --- @return { bufnr : integer , source_bufnr : integer , buffer_mode : " copy" | " source" , line_offset : integer }?
63+ --- @return { bufnr : integer , source_bufnr : integer , buffer_mode : " copy" | " source" , line_offset : integer , viewport ?: PeekstackPopupViewport }?
6364function M .prepare (location , opts )
6465 local buffer_mode = M .resolve_buffer_mode (opts or {})
6566
@@ -95,7 +96,7 @@ function M.prepare(location, opts)
9596 vim .bo [bufnr ].readonly = false
9697
9798 local target_line = location .range .start .line or 0
98- local vp_start , vp_end , line_offset = M .compute_viewport (source_bufnr , target_line )
99+ local vp_start , vp_end , line_offset , total = M .compute_viewport (source_bufnr , target_line )
99100 local ok_lines , lines = pcall (vim .api .nvim_buf_get_lines , source_bufnr , vp_start , vp_end , false )
100101 if not ok_lines then
101102 notify .warn (" Failed to read buffer contents: " .. fname )
@@ -106,11 +107,25 @@ function M.prepare(location, opts)
106107 vim .api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , lines )
107108 configure_popup_buffer (bufnr , source_bufnr , opts )
108109
110+ --- @type PeekstackPopupViewport ?
111+ local viewport = nil
112+ local effective_end = vp_end == - 1 and total or vp_end
113+ local skipped_before = vp_start
114+ local skipped_after = math.max (0 , total - effective_end )
115+ if skipped_before > 0 or skipped_after > 0 then
116+ viewport = {
117+ total = total ,
118+ skipped_before = skipped_before ,
119+ skipped_after = skipped_after ,
120+ }
121+ end
122+
109123 return {
110124 bufnr = bufnr ,
111125 source_bufnr = source_bufnr ,
112126 buffer_mode = buffer_mode ,
113127 line_offset = line_offset ,
128+ viewport = viewport ,
114129 }
115130end
116131
0 commit comments