fix: folding when a file is opened (#1643)

* fix: folding after a file is opened

fixes #559

* fix: wrap nvim_win_set_cursor in autocmd

* chore: Cleanup folding fix

* explain the reason behind the autocmd

Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
Abel Mulugeta
2022-01-09 20:49:19 +03:00
committed by GitHub
parent 24046b2361
commit 01426c491b
2 changed files with 30 additions and 9 deletions

View File

@@ -12,7 +12,6 @@
local a = vim.api local a = vim.api
local log = require "telescope.log"
local Path = require "plenary.path" local Path = require "plenary.path"
local state = require "telescope.state" local state = require "telescope.state"
@@ -129,19 +128,22 @@ action_set.edit = function(prompt_bufnr, command)
vim.api.nvim_buf_set_option(entry_bufnr, "buflisted", true) vim.api.nvim_buf_set_option(entry_bufnr, "buflisted", true)
end end
edit_buffer(command, entry_bufnr) edit_buffer(command, entry_bufnr)
require("telescope.actions.utils").__jump_to(row, col)
else else
-- check if we didn't pick a different buffer -- check if we didn't pick a different buffer
-- prevents restarting lsp server -- prevents restarting lsp server
if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then
filename = Path:new(vim.fn.fnameescape(filename)):normalize(vim.loop.cwd()) filename = Path:new(vim.fn.fnameescape(filename)):normalize(vim.loop.cwd())
pcall(vim.cmd, string.format("%s %s", command, filename)) -- Make sure we wait till we are back in insert mode before opening the file
end -- This fixes foldes
end vim.cmd(string.format(
[[%s :lua require("telescope.actions.utils").__open_file_at("%s", "%s", %s, %s)]],
if row and col then "autocmd InsertLeave * ++once ++nested", -- open file as soon as we have left insert mode (fixes folding)
local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, { row, col }) command,
if not ok then filename,
log.debug("Failed to move to cursor:", err_msg, row, col) row,
col
))
end end
end end
end end

View File

@@ -6,6 +6,7 @@
--- Generally used from within other |telescope.actions| --- Generally used from within other |telescope.actions|
---@brief ]] ---@brief ]]
local log = require "telescope.log"
local action_state = require "telescope.actions.state" local action_state = require "telescope.actions.state"
local utils = {} local utils = {}
@@ -102,4 +103,22 @@ function utils.get_registered_mappings(prompt_bufnr)
return ret return ret
end end
--- Internal function for autocmd
--- Please dont call (subject to change)
function utils.__jump_to(row, col)
if row and col then
local ok, err_msg = pcall(vim.api.nvim_win_set_cursor, 0, { row, col })
if not ok then
log.debug("Failed to move to cursor:", err_msg, row, col)
end
end
end
--- Internal function for autocmd
--- Please dont call (subject to change)
function utils.__open_file_at(command, filename, row, col)
vim.cmd(string.format([[:%s %s]], command, filename))
utils.__jump_to(row, col)
end
return utils return utils