Skip to content

Commit

Permalink
fix(window): fix winhl when reset by the other window
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 15, 2024
1 parent 1764b53 commit c40a55e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
8 changes: 5 additions & 3 deletions lua/edgy/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ local defaults = {
winbar = true,
winfixwidth = true,
winfixheight = false,
winhighlight = "WinBar:EdgyWinBar,Normal:EdgyNormal",
winhighlight = "WinBar:EdgyWinBar,WinBarNC:EdgyWinBarNC,Normal:EdgyNormal",
spell = false,
signcolumn = "no",
},
Expand Down Expand Up @@ -147,18 +147,20 @@ function M.setup(opts)
vim.api.nvim_set_hl(0, "EdgyIcon", { default = true, link = "SignColumn" })
vim.api.nvim_set_hl(0, "EdgyIconActive", { default = true, link = "Constant" })
vim.api.nvim_set_hl(0, "EdgyTitle", { default = true, link = "Title" })
vim.api.nvim_set_hl(0, "EdgyWinBar", { default = true, link = "Winbar" })
vim.api.nvim_set_hl(0, "EdgyWinBar", { default = true, link = "Title" })
vim.api.nvim_set_hl(0, "EdgyWinBarNC", { default = true, link = "EdgyWinBar" })
vim.api.nvim_set_hl(0, "EdgyNormal", { default = true, link = "NormalFloat" })

require("edgy.editor").setup()
require("edgy.state").setup()

local group = vim.api.nvim_create_augroup("layout", { clear = true })
local group = vim.api.nvim_create_augroup("edgy_layout", { clear = true })
vim.api.nvim_create_autocmd({ "BufWinEnter", "WinResized" }, {
group = group,
callback = Layout.update,
})
vim.api.nvim_create_autocmd({ "FileType", "VimResized" }, {
group = group,
callback = function()
vim.schedule(Layout.update)
end,
Expand Down
10 changes: 1 addition & 9 deletions lua/edgy/edgebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,12 @@ function M.new(pos, opts)
table.insert(self.views, View.new(v, self))
end
self:on_win_enter()
-- self:on_buf_win_enter()
return self
end

function M:on_buf_win_enter()
vim.api.nvim_create_autocmd("BufWinEnter", {
callback = function()
self:update({})
end,
})
end

function M:on_win_enter()
vim.api.nvim_create_autocmd("WinEnter", {
group = vim.api.nvim_create_augroup("edgy_edgebar_" .. self.pos, { clear = true }),
callback = function()
vim.schedule(function()
local win = vim.api.nvim_get_current_win()
Expand Down
11 changes: 7 additions & 4 deletions lua/edgy/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ local Config = require("edgy.config")

local M = {}

local uv = vim.uv or vim.loop

function M.setup()
local group = vim.api.nvim_create_augroup("edgy_track", { clear = true })
vim.api.nvim_create_autocmd({ "WinEnter", "BufEnter" }, {
group = vim.api.nvim_create_augroup("edgy_track", { clear = true }),
group = group,
callback = function(event)
if event.event == "BufEnter" then
vim.b.edgy_enter = vim.loop.hrtime()
vim.b.edgy_enter = uv.hrtime()
else
vim.w.edgy_enter = vim.loop.hrtime()
vim.w.edgy_enter = uv.hrtime()
end
end,
})

vim.api.nvim_create_autocmd("WinClosed", {
group = vim.api.nvim_create_augroup("edgy_track_closed", { clear = true }),
group = group,
nested = true,
callback = M.check_main,
})
Expand Down
4 changes: 4 additions & 0 deletions lua/edgy/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ function M.setup()
M.layout = M.layout_wins()
M.save()

local group = vim.api.nvim_create_augroup("edgy_state", { clear = true })

-- always update view state when the cursor moves
vim.api.nvim_create_autocmd("CursorMoved", {
group = group,
callback = function(ev)
if not M.is_enabled() then
return
Expand All @@ -31,6 +34,7 @@ function M.setup()
-- New windows mess up the view state, so we don't track changes after a new
-- window is created until the next restore.
vim.api.nvim_create_autocmd("WinScrolled", {
group = group,
callback = function()
if M.is_enabled() then
for win, info in pairs(vim.v.event) do
Expand Down
65 changes: 51 additions & 14 deletions lua/edgy/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local Config = require("edgy.config")
---@field width? number
---@field height? number
---@field idx integer
---@field wo vim.wo
local M = {}
M.__index = M

Expand All @@ -25,6 +26,7 @@ function M.new(win, view)

---@type vim.wo
local wo = vim.tbl_deep_extend("force", {}, Config.wo, view.edgebar.wo or {}, view.wo or {})
self.wo = wo

if wo.winbar == true then
if vim.api.nvim_win_get_height(win) == 1 then
Expand All @@ -35,30 +37,65 @@ function M.new(win, view)
wo.winbar = nil
end
for k, v in pairs(wo) do
-- special treatment for winhighlight
-- add to existing winhighlight
if k == "winhighlight" then
local whl = vim.split(vim.wo[self.win].winhighlight, ",")
vim.list_extend(whl, vim.split(v, ","))
whl = vim.tbl_filter(function(hl)
return hl ~= ""
end, whl)
v = table.concat(whl, ",")
if k ~= "winhighlight" then
vim.api.nvim_set_option_value(k, v, { scope = "local", win = win })
end
vim.api.nvim_set_option_value(k, v, { scope = "local", win = win })
end
vim.api.nvim_create_autocmd("WinClosed", {
callback = function(event)
if tonumber(event.match) == self.win then
self.view.edgebar:on_hide(self)
-- special treatment for winhighlight
-- add to existing winhighlight
self:fix_winhl()
local group = vim.api.nvim_create_augroup("edgy_window_" .. win, { clear = true })
vim.api.nvim_create_autocmd("WinEnter", {
group = group,
callback = function(ev)
if not vim.api.nvim_win_is_valid(self.win) then
return true
end
self:fix_winhl()
if ev.buf == vim.api.nvim_win_get_buf(self.win) then
vim.schedule(function()
self:fix_winhl()
end)
end
end,
})
vim.api.nvim_create_autocmd("WinClosed", {
group = group,
pattern = tostring(self.win),
callback = function()
self.view.edgebar:on_hide(self)
vim.api.nvim_del_augroup_by_id(group)
return true
end,
})
require("edgy.actions").setup(self)
return self
end

function M:fix_winhl()
if not vim.api.nvim_win_is_valid(self.win) then
return
end
local v = self.wo.winhighlight
-- special treatment for winhighlight
-- add to existing winhighlight
local whl = vim.split(vim.wo[self.win].winhighlight, ",")
vim.list_extend(whl, vim.split(v, ","))
local have = { [""] = true }
whl = vim.tbl_filter(function(hl)
if have[hl] then
return false
end
have[hl] = true
return true
end, whl)
local newv = table.concat(whl, ",")
if newv == v then
return
end
vim.api.nvim_set_option_value("winhighlight", newv, { scope = "local", win = self.win })
end

function M:__tostring()
return "Edgy.Window(" .. (self:is_pinned() and "pinned:" or "") .. self.win .. ")"
end
Expand Down

0 comments on commit c40a55e

Please sign in to comment.