Skip to content

Commit

Permalink
style: add stylua.toml, run stylua
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Jul 31, 2023
1 parent 6d93630 commit 07e9496
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 347 deletions.
321 changes: 162 additions & 159 deletions lua/flatten/core.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
local M = {}

local function unblock_guest(guest_pipe)
local response_sock = vim.fn.sockconnect("pipe", guest_pipe, { rpc = true })
vim.fn.rpcnotify(response_sock, "nvim_exec_lua", "vim.cmd.qa({ bang = true })", {})
vim.fn.chanclose(response_sock)
local response_sock = vim.fn.sockconnect("pipe", guest_pipe, { rpc = true })
vim.fn.rpcnotify(
response_sock,
"nvim_exec_lua",
"vim.cmd.qa({ bang = true })",
{}
)
vim.fn.chanclose(response_sock)
end

local function notify_when_done(pipe, bufnr, callback, ft)
vim.api.nvim_create_autocmd({ "QuitPre", "BufUnload", "BufDelete" }, {
buffer = bufnr,
once = true,
group = M.augroup,
callback = function()
vim.api.nvim_del_augroup_by_id(M.augroup)
unblock_guest(pipe)
callback(ft)
end,
})
vim.api.nvim_create_autocmd({ "QuitPre", "BufUnload", "BufDelete" }, {
buffer = bufnr,
once = true,
group = M.augroup,
callback = function()
vim.api.nvim_del_augroup_by_id(M.augroup)
unblock_guest(pipe)
callback(ft)
end,
})
end

---@class EditFilesOptions
Expand All @@ -30,152 +35,150 @@ end
---@param opts EditFilesOptions
---@return boolean
M.edit_files = function(opts)
local files = opts.files
local response_pipe = opts.response_pipe
local guest_cwd = opts.guest_cwd
local stdin = opts.stdin
local force_block = opts.force_block
local argv = opts.argv
local config = require("flatten").config
local callbacks = config.callbacks
local focus_first = config.window.focus == "first"
local open = config.window.open

local nfiles = #files
local stdin_lines = #stdin

--- commands passed through with +<cmd>, to be executed after opening files
local postcmds = {}

if nfiles == 0 and stdin_lines == 0 then
-- If there are no new bufs, don't open anything
-- and tell the guest not to block
return false
end

local is_cmd = false
if config.allow_cmd_passthrough then
for _, arg in ipairs(argv) do
if is_cmd then
is_cmd = false
-- execute --cmd <cmd> commands
if vim.api.nvim_exec2 then
-- nvim_exec2 only exists in nvim 0.9+
vim.api.nvim_exec2(arg, {})
else
vim.api.nvim_exec(arg, false)
end
elseif arg:sub(1, 1) == "+" then
local cmd = string.sub(arg, 2, -1)
table.insert(postcmds, cmd)
elseif arg == "--cmd" then
-- next arg is the actual command
is_cmd = true
end
end
end

callbacks.pre_open()

-- Open files
if nfiles > 0 then
local argstr = ""
for i, fname in ipairs(files) do
local is_absolute = string.find(fname, "^/")
local fpath = vim.fn.fnameescape(is_absolute and fname or (guest_cwd .. "/" .. fname))
files[i] = fpath
if argstr == "" or argstr == nil then
argstr = fpath
else
argstr = argstr .. " " .. fpath
end
end
-- Hack to work around https://github.com/vim/vim/issues/4610
local wildignore = vim.o.wildignore
vim.o.wildignore = ""
vim.cmd("0argadd " .. argstr)
vim.o.wildignore = wildignore
end

-- Create buffer for stdin pipe input
local stdin_buf = nil
if stdin_lines > 0 then
-- Create buffer for stdin
stdin_buf = vim.api.nvim_create_buf(true, false)
-- Add text to buffer
vim.api.nvim_buf_set_lines(stdin_buf, 0, 0, true, stdin)
-- Set buffer name based on the first line of stdin
local name = stdin[1]:sub(1, 12):gsub("[^%w%.]", "")
-- Ensure the name isn't empty or a duplicate
if vim.fn.bufname(name) ~= "" or name == "" then
local i = 1
local newname = name .. i
while vim.fn.bufname(newname) ~= "" do
i = i + 1
newname = name .. i
end
name = newname
end
vim.api.nvim_buf_set_name(stdin_buf, name)
end

local winnr
local bufnr

-- Open window
if type(open) == "function" then
bufnr, winnr = open(files, argv, stdin_buf, guest_cwd)
if winnr == nil and bufnr ~= nil then
winnr = vim.fn.bufwinid(bufnr)
end
elseif type(open) == "string" then
local focus = vim.fn.argv(focus_first and 0 or (#files - 1))
-- If there's an stdin buf, focus that
if stdin_buf then
focus = vim.api.nvim_buf_get_name(stdin_buf)
end
if open == "current" then
vim.cmd("edit " .. focus)
elseif open == "alternate" then
winnr = vim.fn.win_getid(vim.fn.winnr("#"))
vim.api.nvim_win_set_buf(winnr, vim.fn.bufnr(focus))
vim.api.nvim_set_current_win(winnr)
elseif open == "split" then
vim.cmd("split " .. focus)
elseif open == "vsplit" then
vim.cmd("vsplit " .. focus)
else
vim.cmd("tabedit " .. focus)
end
winnr = vim.api.nvim_get_current_win()
bufnr = vim.api.nvim_get_current_buf()
else
vim.api.nvim_err_writeln("Flatten: 'config.open.focus' expects a function or string, got " .. type(open))
return false
end

local ft
if bufnr ~= nil then
ft = vim.api.nvim_buf_get_option(bufnr, "filetype")
end

local block = config.block_for[ft] or force_block

for _, cmd in ipairs(postcmds) do
if vim.api.nvim_exec2 then
vim.api.nvim_exec2(cmd, {})
else
vim.api.nvim_exec(cmd, false)
end
end

callbacks.post_open(bufnr, winnr, ft, block)

if block then
M.augroup = vim.api.nvim_create_augroup("flatten_notify", { clear = true })
notify_when_done(response_pipe, bufnr, callbacks.block_end, ft)
end
return block
local files = opts.files
local response_pipe = opts.response_pipe
local guest_cwd = opts.guest_cwd
local stdin = opts.stdin
local force_block = opts.force_block
local argv = opts.argv
local config = require("flatten").config
local callbacks = config.callbacks
local focus_first = config.window.focus == "first"
local open = config.window.open

local nfiles = #files
local stdin_lines = #stdin

--- commands passed through with +<cmd>, to be executed after opening files
local postcmds = {}

if nfiles == 0 and stdin_lines == 0 then
-- If there are no new bufs, don't open anything
-- and tell the guest not to block
return false
end

local is_cmd = false
if config.allow_cmd_passthrough then
for _, arg in ipairs(argv) do
if is_cmd then
is_cmd = false
-- execute --cmd <cmd> commands
if vim.api.nvim_exec2 then
-- nvim_exec2 only exists in nvim 0.9+
vim.api.nvim_exec2(arg, {})
else
vim.api.nvim_exec(arg, false)
end
elseif arg:sub(1, 1) == "+" then
local cmd = string.sub(arg, 2, -1)
table.insert(postcmds, cmd)
elseif arg == "--cmd" then
-- next arg is the actual command
is_cmd = true
end
end
end

callbacks.pre_open()

-- Open files
if nfiles > 0 then
local argstr = ""
for i, fname in ipairs(files) do
local is_absolute = string.find(fname, "^/")
local fpath =
vim.fn.fnameescape(is_absolute and fname or (guest_cwd .. "/" .. fname))
files[i] = fpath
if argstr == "" or argstr == nil then
argstr = fpath
else
argstr = argstr .. " " .. fpath
end
end
-- Hack to work around https://github.com/vim/vim/issues/4610
local wildignore = vim.o.wildignore
vim.o.wildignore = ""
vim.cmd("0argadd " .. argstr)
vim.o.wildignore = wildignore
end

-- Create buffer for stdin pipe input
local stdin_buf = nil
if stdin_lines > 0 then
-- Create buffer for stdin
stdin_buf = vim.api.nvim_create_buf(true, false)
-- Add text to buffer
vim.api.nvim_buf_set_lines(stdin_buf, 0, 0, true, stdin)
-- Set buffer name based on the first line of stdin
local name = stdin[1]:sub(1, 12):gsub("[^%w%.]", "")
-- Ensure the name isn't empty or a duplicate
if vim.fn.bufname(name) ~= "" or name == "" then
local i = 1
local newname = name .. i
while vim.fn.bufname(newname) ~= "" do
i = i + 1
newname = name .. i
end
name = newname
end
vim.api.nvim_buf_set_name(stdin_buf, name)
end

local winnr
local bufnr

-- Open window
if type(open) == "function" then
bufnr, winnr = open(files, argv, stdin_buf, guest_cwd)
if winnr == nil and bufnr ~= nil then winnr = vim.fn.bufwinid(bufnr) end
elseif type(open) == "string" then
local focus = vim.fn.argv(focus_first and 0 or (#files - 1))
-- If there's an stdin buf, focus that
if stdin_buf then focus = vim.api.nvim_buf_get_name(stdin_buf) end
if open == "current" then
vim.cmd("edit " .. focus)
elseif open == "alternate" then
winnr = vim.fn.win_getid(vim.fn.winnr("#"))
vim.api.nvim_win_set_buf(winnr, vim.fn.bufnr(focus))
vim.api.nvim_set_current_win(winnr)
elseif open == "split" then
vim.cmd("split " .. focus)
elseif open == "vsplit" then
vim.cmd("vsplit " .. focus)
else
vim.cmd("tabedit " .. focus)
end
winnr = vim.api.nvim_get_current_win()
bufnr = vim.api.nvim_get_current_buf()
else
vim.api.nvim_err_writeln(
"Flatten: 'config.open.focus' expects a function or string, got "
.. type(open)
)
return false
end

local ft
if bufnr ~= nil then ft = vim.api.nvim_buf_get_option(bufnr, "filetype") end

local block = config.block_for[ft] or force_block

for _, cmd in ipairs(postcmds) do
if vim.api.nvim_exec2 then
vim.api.nvim_exec2(cmd, {})
else
vim.api.nvim_exec(cmd, false)
end
end

callbacks.post_open(bufnr, winnr, ft, block)

if block then
M.augroup = vim.api.nvim_create_augroup("flatten_notify", { clear = true })
notify_when_done(response_pipe, bufnr, callbacks.block_end, ft)
end
return block
end

return M
Loading

0 comments on commit 07e9496

Please sign in to comment.