Skip to content

Commit

Permalink
feat(config): Add settings for opening windows
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Mar 12, 2023
1 parent 438c8b8 commit 6e7a179
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ Flatten comes with the following defaults:
-- <String, Bool> dictionary of filetypes that should be blocking
block_for = {
gitcommit = true
},
-- Window options
window = {
-- Options:
-- tab -> opens in new tab (default)
-- split -> opens in split
-- vsplit -> opens in vsplit
-- current -> opens in current window
-- func(new_bufs) -> flatten will only open the files, allowing you to handle window opening yourself. Argument is an array of new buffer numbers.
open = "tab",
-- Affects which file gets focused when opening multiple at once
-- Options:
-- "first" -> opens first file of new files (default)
-- "last" -> opens last file of new files
focus = "first"
}
}
```
Expand Down
37 changes: 32 additions & 5 deletions lua/flatten/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,52 @@ end
M.edit_files = function(args, response_pipe, guest_cwd)
local config = require("flatten").config
local callbacks = config.callbacks
local focus_first = config.window.focus == "first"
local open = config.window.open

callbacks.pre_open()
if #args > 0 then
local argstr = ""
for _, arg in pairs(args) do
for _, arg in ipairs(args) do
local p = vim.loop.fs_realpath(arg) or guest_cwd .. '/' .. arg
if argstr == "" or argstr == nil then
argstr = p
else
argstr = argstr .. " " .. p
end
end
vim.cmd("0argadd " .. argstr)

vim.cmd("tab argument 1")
vim.cmd("0argadd " .. argstr)

vim.cmd("edit")
if type(open) == "function" then
-- Pass list of new buffer IDs
local bufs = vim.api.nvim_list_bufs()
local start = #bufs - #args
local newbufs = {}
for i, buf in ipairs(bufs) do
if i > start then
table.insert(newbufs, buf)
end
end
open(newbufs)
elseif type(open) == "string" then
local focus = vim.fn.argv(focus_first and 0 or (#args - 1))
if open == "current" then
vim.cmd("edit " .. focus)
elseif open == "split" then
vim.cmd("split " .. focus)
elseif open == "vsplit" then
vim.cmd("vsplit " .. focus)
else
vim.cmd("tab " .. focus)
end
else
vim.api.nvim_err_writeln("Flatten: 'config.open.focus' expects a function or string, got " .. type(open))
end
else
vim.cmd("tabnew")
-- If there weren't any args, don't open anything
-- and tell the guest not to block
return false
end
local ft = vim.bo.filetype

Expand Down
4 changes: 2 additions & 2 deletions lua/flatten/guest.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local M = {}

M.init = function(host_pipe)
local args = vim.call("argv")
local args = vim.fn.argv()

local host = vim.fn.sockconnect("pipe", host_pipe, { rpc = true })

local call =
"return require('flatten.core').edit_files("
"return require('flatten.core').edit_files("
.. vim.inspect(args) .. ','
.. "'" .. vim.v.servername .. "',"
.. "'" .. vim.fn.getcwd() .. "'" ..
Expand Down
6 changes: 5 additions & 1 deletion lua/flatten/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ M.config = {
end
},
block_for = {
["gitcommit"] = true,
gitcommit = true,
},
window = {
open = "tab",
focus = "first"
}
}

Expand Down

0 comments on commit 6e7a179

Please sign in to comment.