Skip to content

Commit

Permalink
style: update stylua.toml, reformat files
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Aug 10, 2023
1 parent d03db44 commit 09f6cc7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
22 changes: 16 additions & 6 deletions lua/flatten/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ function M.smart_open(focus)
local available_wins = vim
.iter(vim.api.nvim_list_wins())
:filter(function(win)
if win == curwin then return false end
if vim.api.nvim_win_get_config(win).zindex ~= nil then return false end
if win == curwin then
return false
end
if vim.api.nvim_win_get_config(win).zindex ~= nil then
return false
end

local winbuf = vim.api.nvim_win_get_buf(win)
return vim.bo[winbuf].buftype == "" and vim.bo[winbuf].buflisted == true
return vim.bo[winbuf].buftype == "" and vim.bo[winbuf].buflisted
end)
:fold({}, function(set, win)
set[win] = true
Expand Down Expand Up @@ -174,11 +178,15 @@ M.edit_files = function(opts)
-- 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
if winnr == nil and bufnr ~= nil then
winnr = vim.fn.bufwinid(bufnr)
end
elseif type(open) == "string" then
local focus = focus_first and files[1] or files[#files]
-- If there's an stdin buf, focus that
if stdin_buf then focus = stdin_buf end
if stdin_buf then
focus = stdin_buf
end
if open == "smart" then
M.smart_open(focus)
elseif open == "alternate" then
Expand All @@ -203,7 +211,9 @@ M.edit_files = function(opts)
end

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

local block = config.block_for[ft] or force_block

Expand Down
28 changes: 21 additions & 7 deletions lua/flatten/guest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ function M.exec_on_host(call, opts)
end

function M.maybe_block(block)
if not block then vim.cmd.qa({ bang = true }) end
if not block then
vim.cmd.qa({ bang = true })
end
vim.fn.chanclose(host)
while true do
vim.cmd.sleep(1)
end
end

local function send_files(files, stdin)
if #files < 1 and #stdin < 1 then return end
if #files < 1 and #stdin < 1 then
return
end

local force_block = vim.g.flatten_wait ~= nil
or config.callbacks.should_block(vim.v.argv)
Expand Down Expand Up @@ -68,7 +72,9 @@ M.init = function(host_pipe)
local ok
ok, host = M.sockconnect(host_pipe)
-- Return on connection error
if not ok then return end
if not ok then
return
end
end

if config.callbacks.should_nest and config.callbacks.should_nest(host) then
Expand All @@ -95,14 +101,20 @@ M.init = function(host_pipe)
local result = {}
for _, v in ipairs(tbl) do
local r = f(v)
if r ~= nil then table.insert(result, r) end
if r ~= nil then
table.insert(result, r)
end
end
return result
end
files = filter_map(vim.api.nvim_list_bufs(), function(buffer)
if not vim.api.nvim_buf_is_loaded(buffer) then return end
if not vim.api.nvim_buf_is_loaded(buffer) then
return
end
local buftype = vim.api.nvim_buf_get_option(buffer, "buftype")
if buftype ~= "" and buftype ~= "acwrite" then return end
if buftype ~= "" and buftype ~= "acwrite" then
return
end
local name = vim.api.nvim_buf_get_name(buffer)
if name ~= "" and vim.api.nvim_buf_get_option(buffer, "buflisted") then
return name
Expand All @@ -126,7 +138,9 @@ M.init = function(host_pipe)
should_block = result.should_block
end
end
if should_nest == true then return end
if should_nest == true then
return
end
M.maybe_block(should_block)
end

Expand Down
32 changes: 24 additions & 8 deletions lua/flatten/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,49 @@ function M.try_address(addr, startserver)
end
if vim.loop.fs_stat(addr) then
local ok, sock = require("flatten.guest").sockconnect(addr)
if ok then return sock end
if ok then
return sock
end
elseif startserver then
local ok = pcall(vim.fn.serverstart, addr)
if ok then return addr end
if ok then
return addr
end
end
end

---@return string | nil
function M.default_pipe_path()
-- If running in a terminal inside Neovim:
if vim.env.NVIM then return vim.env.NVIM end
if vim.env.NVIM then
return vim.env.NVIM
end
-- If running in a Kitty terminal,
-- all tabs/windows/os-windows in the same instance of kitty will open in the first neovim instance
if M.config.one_per.kitty and vim.env.KITTY_PID then
local ret = M.try_address("kitty.nvim-" .. vim.env.KITTY_PID, true)
if ret ~= nil then return ret end
if ret ~= nil then
return ret
end
end
-- If running in a Wezterm,
-- all tabs/windows/windows in the same instance of wezterm will open in the first neovim instance
if M.config.one_per.wezterm and vim.env.WEZTERM_UNIX_SOCKET then
local pid = vim.env.WEZTERM_UNIX_SOCKET:match("gui%-sock%-(%d+)")
local ret = M.try_address("wezterm.nvim-" .. pid, true)
if ret ~= nil then return ret end
if ret ~= nil then
return ret
end
end
end

---@param host channel
---@return boolean
function M.default_should_nest(host)
-- don't nest in a neovim terminal (unless nest_if_no_args is set)
if vim.env.NVIM ~= nil then return false end
if vim.env.NVIM ~= nil then
return false
end

-- If in a wezterm or kitty split, only open files in the first neovim instance
-- if their working directories are the same.
Expand All @@ -47,7 +59,9 @@ function M.default_should_nest(host)
local ok, host_cwd = pcall(vim.fn.rpcrequest, host, "nvim_exec_lua", call, {})

-- Yield to default behavior if RPC call fails
if not ok then return false end
if not ok then
return false
end

return host_cwd ~= vim.fn.getcwd(-1)
end
Expand Down Expand Up @@ -119,7 +133,9 @@ M.setup = function(opt)
M.config = vim.tbl_deep_extend("keep", opt or {}, M.config)

local pipe_path = M.config.pipe_path
if type(pipe_path) == "function" then pipe_path = pipe_path() end
if type(pipe_path) == "function" then
pipe_path = pipe_path()
end

if
pipe_path == nil or vim.tbl_contains(vim.fn.serverlist(), pipe_path, {})
Expand Down
4 changes: 2 additions & 2 deletions stylua.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
column_width = 80 # 120
column_width = 80 # 120
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
collapse_simple_statement = "ConditionalOnly"
collapse_simple_statement = "Never"

0 comments on commit 09f6cc7

Please sign in to comment.