Skip to content

Commit

Permalink
fix: Optimize the experience
Browse files Browse the repository at this point in the history
  • Loading branch information
denstiny committed Nov 6, 2022
2 parents a082ffa + 838445a commit 112c732
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 51 deletions.
64 changes: 35 additions & 29 deletions lua/colorful-winsep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,33 @@
-- @blog : https://denstiny.github.io

local api = vim.api
local fn = vim.fn
local M = {
lock = false
}
local view = require("colorful-winsep.view")
local M = { lock = false }

function M.NvimSeparatorShow()
view.lock = true
if view.create_dividing_win(true) then
M.lock = true
if view.create_dividing_win() then
view.set_buf_char()
view.start_timer()
--else
-- if view.move_dividing_win() then
-- view.set_buf_char()
-- end
end
view.lock = false
end

function M.SeparatorShow()
--M.NvimSeparatorDel()
view.lock = true
-- @todo
if view.create_dividing_win(false) then
view.set_buf_char()
view.start_timer()
else
if view.move_dividing_win() then
view.set_buf_char()
end
end
view.lock = false
M.lock = false
view.config.create_event()
end

function M.NvimSeparatorDel()
view.stop_timer()
view.close_dividing()
view.config.close_event()
end

function M.setup(opts)
view.set_config(opts)
view.highlight()
M.auto_group = api.nvim_create_augroup("NvimSeparator", { clear = true })
if view.config.auto_show then
api.nvim_create_autocmd({ "WinEnter" }, {
if view.config.enable then
api.nvim_create_autocmd({ "WinEnter", "WinScrolled", "WinNew", "VimResized" }, {
group = M.auto_group,
callback = function()
if M.lock then
Expand All @@ -55,15 +41,35 @@ function M.setup(opts)
end
})
end
api.nvim_create_autocmd({ "WinLeave", "CmdlineLeave", "WinClosed" }, {
api.nvim_create_autocmd({ "WinLeave", "BufModifiedSet" }, {
group = M.auto_group,
callback = function()
if M.lock then
callback = function(opts)
if ModifiedSet_no_closed_solt(opts) then
return
end
M.NvimSeparatorDel()
end
})
view.start_timer()
end

function ModifiedSet_no_closed_solt(opts)
if opts.event == "BufModifiedSet" then
local filetype_lock = false
for i = 1, #view.config.no_exec_files do
if view.config.no_exec_files[i] == vim.bo.filetype then
filetype_lock = true
end
end
if not filetype_lock then
return true
end
end
if M.lock then
return true
else
return false
end
end

return M
36 changes: 21 additions & 15 deletions lua/colorful-winsep/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ local M = {
no_exec_files = { "packer", "TelescopePrompt", "mason", "CompetiTest" },
highlight = { guifg = "#957CC6", guibg = api.nvim_get_hl_by_name("Normal", true)["background"] },
interval = 100,
auto_show = true
enable = true,
create_event = function()
vim.notify("create_event")
return true
end,
close_event = function()
vim.notify("close_event")
return true
end
},
direction = { left = 'h', right = 'l', up = 'k', down = 'j' },
c_win = -1
c_win = -1,
}

--- Judge whether the current can be started and colorful line
Expand All @@ -21,18 +29,18 @@ function M.can_create(no_exec_files)
M.c_win = api.nvim_get_current_win()
return false
end
--local win = api.nvim_get_current_win()
--if M.c_win == win then
-- return false
--end
local win = api.nvim_get_current_win()
if M.c_win == win then
return false
end
local cursor_win_filetype = bo.filetype
for i = 1, #no_exec_files do
if no_exec_files[i] == cursor_win_filetype then
M.c_win = api.nvim_get_current_win()
return false
end
end
--M.c_win = win
M.c_win = win
return true
end

Expand All @@ -52,9 +60,7 @@ end
---@return: boolean
function M.direction_have(direction)
local winnum = vim.fn.winnr()
vim.cmd.wincmd(direction)
if winnum ~= vim.fn.winnr() then
vim.cmd.wincmd(winnum .. "w")
if vim.fn.winnr(direction) ~= winnum then
return true
end
return false
Expand All @@ -64,7 +70,8 @@ end
---@param direction : { left = 'h', right = 'l', up = 'k', down = 'j' }
---@return:opts
function M.create_direction_win_option(direction)
local opts = { style = 'minimal', relative = 'editor', zindex = 10, focusable = false }
local opts = { style = 'minimal', relative = 'editor', zindex = 10, focusable = false, height = 0, width = 0, row = 0,
col = 0 }
local cursor_win_pos = api.nvim_win_get_position(0)
local cursor_win_width = fn.winwidth(0)
local cursor_win_height = fn.winheight(0)
Expand All @@ -78,6 +85,8 @@ function M.create_direction_win_option(direction)
opts.width = 1
if M.direction_have(M.direction.up) and (M.direction_have(M.direction.down) or vim.o.laststatus ~= 3) then
opts.height = cursor_win_height + 2
elseif not M.direction_have(M.direction.up) and not M.direction_have(M.direction.down) and vim.o.laststatus ~= 3 then
opts.height = cursor_win_height + 1
elseif not M.direction_have(M.direction.up) and not M.direction_have(M.direction.down) then
opts.height = cursor_win_height
else
Expand Down Expand Up @@ -111,7 +120,7 @@ function M.create_direction_win_option(direction)
end
opts.col = cursor_win_pos[2]
end
if opts.height == 0 and opts.width == 0 then
if opts.height == 0 and opts.width == 0 and opts.row == 0 and opts.col == 0 then
return nil
end
return opts
Expand All @@ -125,7 +134,4 @@ function M.set_user_config(opts)
end
end

function M.set_space_line()
end

return M
13 changes: 6 additions & 7 deletions lua/colorful-winsep/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ local api = vim.api
local fn = vim.fn
local utils = require('colorful-winsep.utils')
local M = {
config = utils.defaultopts,
wins = {},
bufs = {},
width = 0,
height = 0,
timers = {},
width = fn.winwidth(0),
height = fn.winheight(0),
timers = {}
}

--- Create floating win show line
---@return
function M.create_dividing_win(status)
function M.create_dividing_win()
if utils.can_create(M.config.no_exec_files) then
if status and utils.isCreated() then
return false
end
M.close_dividing()
local direction = utils.direction
for _, value in pairs(direction) do
local opts = utils.create_direction_win_option(value)
Expand Down Expand Up @@ -123,7 +122,7 @@ end

function M.resize_auto_show_float_win()
if M.width ~= fn.winwidth(0) or M.height ~= fn.winheight(0) then
if M.create_dividing_win(true) then
if M.create_dividing_win() then
M.set_buf_char()
elseif M.move_dividing_win() then
M.set_buf_char()
Expand Down

0 comments on commit 112c732

Please sign in to comment.