1
1
local Extensions = require (" harpoon.extensions" )
2
2
local Logger = require (" harpoon.logger" )
3
+ local List = require (" harpoon.list" )
3
4
local Utils = require (" harpoon.utils" )
4
5
5
6
local function to_exact_name (value )
@@ -102,13 +103,11 @@ function M.get_default_config()
102
103
return
103
104
end
104
105
106
+ list :sync_cursor ()
105
107
options = options or {}
106
108
107
109
local bufnr = vim .fn .bufnr (to_exact_name (list_item .value ))
108
- local set_position = false
109
110
if bufnr == - 1 then -- must create a buffer!
110
- set_position = true
111
- -- bufnr = vim.fn.bufnr(list_item.value, true)
112
111
bufnr = vim .fn .bufadd (list_item .value )
113
112
end
114
113
if not vim .api .nvim_buf_is_loaded (bufnr ) then
@@ -128,39 +127,42 @@ function M.get_default_config()
128
127
129
128
vim .api .nvim_set_current_buf (bufnr )
130
129
131
- if set_position then
132
- local lines = vim .api .nvim_buf_line_count (bufnr )
130
+ local lines = vim .api .nvim_buf_line_count (bufnr )
133
131
134
- local edited = false
135
- if list_item .context .row > lines then
136
- list_item .context .row = lines
137
- edited = true
138
- end
132
+ local edited = false
133
+ if list_item .context .row > lines then
134
+ list_item .context .row = lines
135
+ edited = true
136
+ end
139
137
140
- local row = list_item .context .row
141
- local row_text =
142
- vim .api .nvim_buf_get_lines (0 , row - 1 , row , false )
143
- local col = # row_text [1 ]
138
+ local row = list_item .context .row
139
+ local row_text =
140
+ vim .api .nvim_buf_get_lines (0 , row - 1 , row , false )
141
+ local col = # row_text [1 ]
144
142
145
- if list_item .context .col > col then
146
- list_item .context .col = col
147
- edited = true
148
- end
143
+ if list_item .context .col > col then
144
+ list_item .context .col = col
145
+ edited = true
146
+ end
149
147
150
- vim .api .nvim_win_set_cursor (0 , {
151
- list_item .context .row or 1 ,
152
- list_item .context .col or 0 ,
153
- })
148
+ vim .api .nvim_win_set_cursor (0 , {
149
+ list_item .context .row or 1 ,
150
+ list_item .context .col or 0 ,
151
+ })
154
152
155
- if edited then
156
- Extensions .extensions :emit (
157
- Extensions .event_names .POSITION_UPDATED ,
158
- {
159
- list_item = list_item ,
160
- }
161
- )
162
- end
153
+ if edited then
154
+ Extensions .extensions :emit (
155
+ Extensions .event_names .POSITION_UPDATED ,
156
+ {
157
+ list_item = list_item ,
158
+ }
159
+ )
163
160
end
161
+ -- The stored cursor position could be outside of the file bounds.
162
+ pcall (vim .api .nvim_win_set_cursor , 0 , {
163
+ list_item .context .row or 1 ,
164
+ list_item .context .col or 0 ,
165
+ })
164
166
165
167
Extensions .extensions :emit (Extensions .event_names .NAVIGATE , {
166
168
buffer = bufnr ,
@@ -224,7 +226,7 @@ function M.get_default_config()
224
226
local item = list :get_by_value (bufname )
225
227
226
228
if item then
227
- local pos = vim . api . nvim_win_get_cursor ( 0 )
229
+ local pos = List . _sync_cursor ( item )
228
230
229
231
Logger :log (
230
232
" config_default#BufLeave updating position" ,
0 commit comments