Skip to content

Commit

Permalink
feat: add split window on open support (#11)
Browse files Browse the repository at this point in the history
* add support for opening gitpad by splitting the current window

* add support for passing window type opts to functions

* fix bug in not using override window type from opts

* update README with configurations

* update for backwards compat

* update README.md

* use floating_win.opts.style for config

---------

Co-authored-by: Eugene Oliveros <[email protected]>
  • Loading branch information
RyanCallahan312 and yujinyuz authored Jul 11, 2024
1 parent 9685127 commit b5cbbcb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A minimal neovim plugin for taking down notes for git projects and per branch
## ✨ Features

- Provides a per repository / per branch way of note taking while working on your code with the help
of floating windows.
of standard or floating windows.
- Supports creating and toggling a separate `{branch}-branchpad.md` file for each branch,
if desired.
- Extensible note list (daily notes, per-file notes, etc.)
Expand Down Expand Up @@ -48,6 +48,14 @@ using [lazy.nvim](https://github.com/folke/lazy.nvim):
end,
desc = 'gitpad branch',
},
{
"<leader>pbv",
function()
require("gitpad").toggle_gitpad_branch({ window_type = "split", split_win_opts = { split = "right" } })
end,
desc = "gitpad branch vertical split",
},

-- Daily notes
{
'<leader>pd',
Expand Down Expand Up @@ -85,15 +93,23 @@ gitpad.nvim comes with the following defaults:
```lua
{
title = 'gitpad', -- The title of the floating window
border = 'single', -- The border style of the floating window. Possible values are `'single'`, `'double'`, `'shadow'`, `'rounded'`, and `''` (no border).
style = '', -- The style of the floating window. Possible values are `'minimal'` (no line numbers, statusline, or sign column. See :help nvim_open_win() '), and `''` (default Neovim style).
dir = vim.fn.stdpath('data') .. '/gitpad', -- The directory where the notes are stored. Possible value is a valid path ie '~/notes'
default_text = nil, -- Leave this nil if you want to use the default text
on_attach = function(bufnr)
-- You can also define a function to be called when the gitpad window is opened, by setting the `on_attach` option:
-- This is just an example
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'q', '<Cmd>wq<CR>', { noremap = true, silent = true })
end,
window_type = 'floating', -- Options are 'floating' or 'split'
floating_win_opts = {
relative = 'editor', -- where the floating window should appear. See :help nvim_open_win()
style = '', -- The style of the floating window. Possible values are `'minimal'` (no line numbers, statusline, or sign column. See :help nvim_open_win() '), and `''` (default Neovim style).
border = 'single', -- The border style of the floating window. Possible values are `'single'`, `'double'`, `'shadow'`, `'rounded'`, and `''` (no border).
focusable = false, -- Enables focus by user actions. See :help nvim_open_win()
},
split_win_opts = {
split = 'right', -- Controls split direction if window_type == 'split'. Options are 'left', 'right', 'above', or 'below'. See :help nvim_open_win()
},
}
```

Expand Down
63 changes: 46 additions & 17 deletions lua/gitpad/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ local gitpad_win_id = nil

M.config = {
title = 'gitpad',
border = 'single',
dir = vim.fs.normalize(vim.fn.stdpath('data') .. '/gitpad'),
style = '',
default_text = nil,
on_attach = nil,
window_type = 'floating',
floating_win_opts = {
relative = 'editor',
style = '',
border = 'single',
focusable = false,
},
split_win_opts = {
split = 'right',
},
}

function H.is_git_dir()
Expand Down Expand Up @@ -128,22 +136,41 @@ end
function M.open_window(opts)
local path = opts.path
local title = opts.title or M.config.title
local window_type = opts.window_type or M.config.window_type

local ui = vim.api.nvim_list_uis()[1]
local width = math.floor((ui.width * 0.5) + 0.5)
local height = math.floor((ui.height * 0.5) + 0.5)
local win_opts = {
relative = 'editor',
width = width,
height = height,
col = (ui.width - width) / 2,
row = (ui.height - height) / 2,
style = M.config.style,
border = M.config.border,
focusable = false,

-- Use this table for supporting more built-in window configurations
local switch_table = {
['floating'] = function()
local default_float_win_opts = {
width = width,
height = height,
col = (ui.width - width) / 2,
row = (ui.height - height) / 2,
}
local float_win_opts = vim.tbl_deep_extend('force', default_float_win_opts, M.config.floating_win_opts)

-- override with the following configurations for backwards compatability
float_win_opts.border = M.config.border or float_win_opts.border
float_win_opts.style = M.config.style or float_win_opts.style
return vim.tbl_deep_extend('force', float_win_opts, opts.floating_win_opts or {})
end,

['split'] = function()
return vim.tbl_deep_extend('force', M.config.split_win_opts, opts.split_win_opts or {})
end,
}

if vim.fn.has('nvim-0.9.0') == 1 then
-- default to floating window
local win_opts = switch_table['floating']()
if switch_table[window_type] then
win_opts = switch_table[window_type]()
end

if win_opts['relative'] and vim.fn.has('nvim-0.9.0') == 1 then
win_opts.title = ' ' .. title .. ' '
win_opts.title_pos = 'left'
end
Expand All @@ -156,11 +183,13 @@ function M.open_window(opts)
vim.api.nvim_set_option_value('filetype', 'markdown', { buf = bufnr })
vim.api.nvim_set_option_value('buflisted', false, { buf = bufnr })

vim.api.nvim_set_option_value(
'winhighlight',
'Normal:GitpadFloat,FloatBorder:GitpadFloatBorder,FloatTitle:GitpadFloatTitle',
{ win = gitpad_win_id }
)
if win_opts['relative'] then
vim.api.nvim_set_option_value(
'winhighlight',
'Normal:GitpadFloat,FloatBorder:GitpadFloatBorder,FloatTitle:GitpadFloatTitle',
{ win = gitpad_win_id }
)
end

-- These are all the options being set when style = minimal
-- But what's kinda annoying is the fact that using is would then
Expand Down

0 comments on commit b5cbbcb

Please sign in to comment.