@@ -1271,8 +1271,6 @@ local function update_cursor(event)
1271
1271
end
1272
1272
1273
1273
local function handle_init_event (event )
1274
- -- TODO: make sure only init once
1275
-
1276
1274
assert (vim .api .nvim_win_is_valid (event .window ))
1277
1275
update_cursor (event )
1278
1276
@@ -1315,30 +1313,38 @@ local function handle_init_event(event)
1315
1313
language_tree :register_cbs ({ on_changedtree = on_changedtree_callback })
1316
1314
mark_all_lines_changed (event .buffer )
1317
1315
1318
- if module .config .public .folds and vim .api .nvim_win_is_valid (event .window ) then
1319
- local wo = vim .wo [event .window ]
1320
- wo .foldmethod = " expr"
1321
- wo .foldexpr = vim .treesitter .foldexpr and " v:lua.vim.treesitter.foldexpr()" or " nvim_treesitter#foldexpr()"
1322
- wo .foldtext = " "
1323
-
1324
- local init_open_folds = module .config .public .init_open_folds
1325
- local function open_folds ()
1326
- vim .cmd (" normal! zR" )
1327
- end
1328
-
1329
- if init_open_folds == " always" then
1330
- open_folds ()
1331
- elseif init_open_folds == " never" then -- luacheck:ignore 542
1332
- -- do nothing
1333
- else
1334
- if init_open_folds ~= " auto" then
1335
- log .warn (' "init_open_folds" must be "auto", "always", or "never"' )
1316
+ if
1317
+ module .config .public .folds
1318
+ and vim .api .nvim_win_is_valid (event .window )
1319
+ and vim .api .nvim_buf_is_valid (event .buffer )
1320
+ then
1321
+ vim .api .nvim_buf_call (event .buffer , function ()
1322
+ -- NOTE(vhyrro): `vim.wo` only supports `wo[winid][0]`,
1323
+ -- hence the `buf_call` here.
1324
+ local wo = vim .wo [event .window ][0 ]
1325
+ wo .foldmethod = " expr"
1326
+ wo .foldexpr = vim .treesitter .foldexpr and " v:lua.vim.treesitter.foldexpr()" or " nvim_treesitter#foldexpr()"
1327
+ wo .foldtext = " "
1328
+
1329
+ local init_open_folds = module .config .public .init_open_folds
1330
+ local function open_folds ()
1331
+ vim .cmd (" normal! zR" )
1336
1332
end
1337
1333
1338
- if wo . foldlevel == 0 then
1334
+ if init_open_folds == " always " then
1339
1335
open_folds ()
1336
+ elseif init_open_folds == " never" then -- luacheck:ignore 542
1337
+ -- do nothing
1338
+ else
1339
+ if init_open_folds ~= " auto" then
1340
+ log .warn (' "init_open_folds" must be "auto", "always", or "never"' )
1341
+ end
1342
+
1343
+ if wo .foldlevel == 0 then
1344
+ open_folds ()
1345
+ end
1340
1346
end
1341
- end
1347
+ end )
1342
1348
end
1343
1349
end
1344
1350
0 commit comments