Skip to content

Commit

Permalink
feat: make floating window behavior configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
toupeira committed May 28, 2024
1 parent c29547c commit ec6ca80
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ require('smart-splits').setup({
-- NOTE: `at_edge = 'wrap'` is not supported on Kitty terminal
-- multiplexer, as there is no way to determine layout via the CLI
at_edge = 'wrap',
-- Desired behavior when the current window is floating:
-- 'previous' => Focus previous Vim window and perform action
-- 'mux' => Always forward action to multiplexer
float_win_behavior = 'previous',
-- when moving cursor between splits left or right,
-- place the cursor on the same row of the *screen*
-- regardless of line numbers. False by default.
Expand Down
62 changes: 35 additions & 27 deletions lua/smart-splits/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local mux_utils = require('smart-splits.mux.utils')
local types = require('smart-splits.types')
local Direction = types.Direction
local AtEdgeBehavior = types.AtEdgeBehavior
local FloatWinBehavior = types.FloatWinBehavior

local M = {}

Expand Down Expand Up @@ -194,20 +195,39 @@ local function compute_direction_horizontal(direction)
return result
end

---@param mux_callback fun()|nil
---@return boolean
local function handle_floating_window(mux_callback)
if utils.is_floating_window() then
if config.float_win_behavior == FloatWinBehavior.previous then
-- focus the last accessed window.
-- if it's also floating, do not attempt to perform the action.
local prev_win = vim.fn.win_getid(vim.fn.winnr('#'))
if utils.is_floating_window(prev_win) then
return true
end

vim.api.nvim_set_current_win(prev_win)
return false
elseif config.float_win_behavior == FloatWinBehavior.mux then
-- always forward the action to the multiplexer
if mux_callback then
mux_callback()
end
return true
end
end
end

---@param direction SmartSplitsDirection
---@param amount number
local function resize(direction, amount)
amount = amount or config.default_amount

-- ignore currently focused floating windows by focusing the last accessed window
-- if the last accessed window is also floating, do not attempt to resize it
if utils.is_floating_window() then
local prev_win = vim.fn.win_getid(vim.fn.winnr('#'))
if utils.is_floating_window(prev_win) then
return
end

vim.api.nvim_set_current_win(prev_win)
if handle_floating_window(function()
mux.resize_pane(direction, amount)
end) then
return
end

-- if a full width window and horizontall resize check if we can resize with multiplexer
Expand Down Expand Up @@ -336,15 +356,10 @@ local function move_cursor(direction, opts)
end
end

-- ignore currently focused floating windows by focusing the last accessed window
-- if the last accessed window is also floating, do not attempt to move the cursor
if utils.is_floating_window() then
local prev_win = vim.fn.win_getid(vim.fn.winnr('#'))
if utils.is_floating_window(prev_win) then
return
end

vim.api.nvim_set_current_win(prev_win)
if handle_floating_window(function()
mux.move_pane(direction, true, at_edge)
end) then
return
end

local offset = vim.fn.winline() + vim.api.nvim_win_get_position(0)[1]
Expand Down Expand Up @@ -421,15 +436,8 @@ end
local function swap_bufs(direction, opts)
opts = opts or {}

-- ignore currently focused floating windows by focusing the last accessed window
-- if the last accessed window is also floating, do not attempt to swap buffers
if utils.is_floating_window() then
local prev_win = vim.fn.win_getid(vim.fn.winnr('#'))
if utils.is_floating_window(prev_win) then
return
end

vim.api.nvim_set_current_win(prev_win)
if handle_floating_window() then
return
end

local buf_1 = vim.api.nvim_get_current_buf()
Expand Down
3 changes: 3 additions & 0 deletions lua/smart-splits/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local lazy = require('smart-splits.lazy')
local log = lazy.require_on_exported_call('smart-splits.log') --[[@as SmartSplitsLogger]]
local types = require('smart-splits.types')
local AtEdgeBehavior = types.AtEdgeBehavior
local FloatWinBehavior = types.FloatWinBehavior
local Multiplexer = types.Multiplexer
local mux_utils = require('smart-splits.mux.utils')

Expand All @@ -20,6 +21,7 @@ local mux_utils = require('smart-splits.mux.utils')
---@field ignored_filetypes string[]
---@field default_amount number
---@field at_edge SmartSplitsAtEdgeBehavior
---@field float_win_behavior SmartSplitsFloatWinBehavior
---@field move_cursor_same_row boolean
---@field cursor_follows_swapped_bufs boolean
---@field resize_mode SmartResizeModeConfig
Expand All @@ -45,6 +47,7 @@ local config = { ---@diagnostic disable-line:missing-fields
},
default_amount = 3,
at_edge = AtEdgeBehavior.wrap,
float_win_behavior = FloatWinBehavior.previous,
move_cursor_same_row = false,
cursor_follows_swapped_bufs = false,
resize_mode = {
Expand Down
8 changes: 8 additions & 0 deletions lua/smart-splits/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
---@alias SmartSplitsDirection 'left'|'right'|'up'|'down'

---@alias SmartSplitsAtEdgeBehavior 'split'|'wrap'|'stop'|function
---
---@alias SmartSplitsFloatWinBehavior 'previous'|'mux'

---@alias SmartSplitsMultiplexerType 'tmux'|'wezterm'|'kitty'

Expand Down Expand Up @@ -40,6 +42,12 @@ local M = {
---@type SmartSplitsAtEdgeBehavior
stop = 'stop',
},
FloatWinBehavior = {
---@type SmartSplitsFloatWinBehavior
previous = 'previous',
---@type SmartSplitsFloatWinBehavior
mux = 'mux',
},
Multiplexer = {
---@type SmartSplitsMultiplexerType
tmux = 'tmux',
Expand Down

0 comments on commit ec6ca80

Please sign in to comment.