Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix: do not change current window
Browse files Browse the repository at this point in the history
  • Loading branch information
aznhe21 committed Nov 8, 2022
1 parent 5554562 commit 2d3085e
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions lua/hop/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@ local hint = require'hop.hint'
local M = {}

local function window_context(win_handle, cursor_pos)
-- get a bunch of information about the window and the cursor
vim.api.nvim_set_current_win(win_handle)
local win_info = vim.fn.getwininfo(win_handle)[1]
local win_view = vim.fn.winsaveview()
local top_line = win_info.topline - 1
local bot_line = win_info.botline

-- NOTE: due to an (unknown yet) bug in neovim, the sign_width is not correctly reported when shifting the window
-- view inside a non-wrap window, so we can’t rely on this; for this reason, we have to implement a weird hack that
-- is going to disable the signs while hop is running (I’m sorry); the state is restored after jump
-- local left_col_offset = win_info.variables.context.number_width + win_info.variables.context.sign_width
local win_width = nil

-- hack to get the left column offset in nowrap
if not vim.wo.wrap then
vim.api.nvim_win_set_cursor(win_handle, { cursor_pos[1], 0 })
local left_col_offset = vim.fn.wincol() - 1
vim.fn.winrestview(win_view)
win_width = win_info.width - left_col_offset
end
-- get window context without changing current window
return vim.api.nvim_win_call(win_handle, function()
-- get a bunch of information about the window and the cursor
local win_info = vim.fn.getwininfo(win_handle)[1]
local win_view = vim.fn.winsaveview()
local top_line = win_info.topline - 1
local bot_line = win_info.botline

-- NOTE: due to an (unknown yet) bug in neovim, the sign_width is not correctly reported when shifting the window
-- view inside a non-wrap window, so we can’t rely on this; for this reason, we have to implement a weird hack that
-- is going to disable the signs while hop is running (I’m sorry); the state is restored after jump
-- local left_col_offset = win_info.variables.context.number_width + win_info.variables.context.sign_width
local win_width = nil

-- hack to get the left column offset in nowrap
if not vim.wo.wrap then
vim.api.nvim_win_set_cursor(win_handle, { cursor_pos[1], 0 })
local left_col_offset = vim.fn.wincol() - 1
vim.fn.winrestview(win_view)
win_width = win_info.width - left_col_offset
end

return {
hwin = win_handle,
cursor_pos = cursor_pos,
top_line = top_line,
bot_line = bot_line,
win_width = win_width,
col_offset = win_view.leftcol
}
return {
hwin = win_handle,
cursor_pos = cursor_pos,
top_line = top_line,
bot_line = bot_line,
win_width = win_width,
col_offset = win_view.leftcol
}
end)
end

-- Collect all multi-windows's context:
Expand Down

0 comments on commit 2d3085e

Please sign in to comment.