chore: formatting with stylua

main
olimorris 2022-03-03 21:15:37 +00:00
parent 4770626aec
commit 336909e742
3 changed files with 59 additions and 57 deletions

View File

@ -66,6 +66,7 @@ Persisted comes with the following defaults:
**Persisted** works well with plugins like `startify` or `dashboard`. It will never restore a session automatically, but you can of course write an autocmd that does exactly that. **Persisted** works well with plugins like `startify` or `dashboard`. It will never restore a session automatically, but you can of course write an autocmd that does exactly that.
Some example keybindings for the plugins functions are contained below: Some example keybindings for the plugins functions are contained below:
```lua ```lua
-- restore the session for the current directory and current branch (if `git_use_branch` is enabled) -- restore the session for the current directory and current branch (if `git_use_branch` is enabled)
vim.api.nvim_set_keymap("n", "<leader>qr", [[<cmd>lua require("persisted").load()<cr>]]) vim.api.nvim_set_keymap("n", "<leader>qr", [[<cmd>lua require("persisted").load()<cr>]])
@ -87,4 +88,5 @@ vim.api.nvim_set_keymap("n", "<leader>qt", [[<cmd>lua require("persisted").toggl
``` ```
### Helpers ### Helpers
**Persisted** sets a global variable, `vim.g.persisting`, which is set to `true` when the plugin is enabled. The author uses this to display an icon in their [statusline](https://github.com/olimorris/dotfiles/blob/0cdaee183c64f872778952f90f62b9366851101c/.config/nvim/lua/Oli/plugins/statusline.lua#L257). **Persisted** sets a global variable, `vim.g.persisting`, which is set to `true` when the plugin is enabled. The author uses this to display an icon in their [statusline](https://github.com/olimorris/dotfiles/blob/0cdaee183c64f872778952f90f62b9366851101c/.config/nvim/lua/Oli/plugins/statusline.lua#L257).

View File

@ -5,7 +5,7 @@ local defaults = {
dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
use_git_branch = false, -- create session files based on the branch of the git enabled repository use_git_branch = false, -- create session files based on the branch of the git enabled repository
autosave = true, -- automatically save session files autosave = true, -- automatically save session files
options = { "buffers", "curdir", "tabpages", "winsize" } -- session options used for saving options = { "buffers", "curdir", "tabpages", "winsize" }, -- session options used for saving
} }
---@type PersistedOptions ---@type PersistedOptions

View File

@ -1,4 +1,4 @@
local Config = require("persisted.config") local config = require("persisted.config")
local M = {} local M = {}
@ -10,16 +10,14 @@ function M.get_current()
pattern = "[\\:]" pattern = "[\\:]"
end end
local name = vim.fn.getcwd():gsub(pattern, "%%") local name = vim.fn.getcwd():gsub(pattern, "%%")
return Config.options.dir .. name .. get_branch() .. ".vim" return config.options.dir .. name .. get_branch() .. ".vim"
end end
function get_branch() function get_branch()
local git_enabled = (vim.fn.isdirectory(vim.fn.getcwd() .. "/.git") == 1) local git_enabled = (vim.fn.isdirectory(vim.fn.getcwd() .. "/.git") == 1)
if Config.options.use_git_branch and git_enabled then if config.options.use_git_branch and git_enabled then
local branch = vim.api.nvim_exec( local branch = vim.api.nvim_exec([[!git rev-parse --abbrev-ref HEAD 2>/dev/null]], true)
[[!git rev-parse --abbrev-ref HEAD 2>/dev/null]],
true)
-- The branch command returns two lines. We only need the second one -- The branch command returns two lines. We only need the second one
local lines = {} local lines = {}
@ -41,8 +39,8 @@ function M.get_last()
end end
function M.setup(opts) function M.setup(opts)
Config.setup(opts) config.setup(opts)
if Config.options.autosave then if config.options.autosave then
M.start() M.start()
end end
end end
@ -67,23 +65,25 @@ end
function M.save() function M.save()
local tmp = vim.o.sessionoptions local tmp = vim.o.sessionoptions
vim.o.sessionoptions = table.concat(Config.options.options, ",") vim.o.sessionoptions = table.concat(config.options.options, ",")
vim.cmd("mks! " .. e(M.get_current())) vim.cmd("mks! " .. e(M.get_current()))
vim.o.sessionoptions = tmp vim.o.sessionoptions = tmp
vim.g.persisting = true vim.g.persisting = true
end end
end
function M.load(opt) function M.load(opt)
opt = opt or {} opt = opt or {}
local sfile = opt.last and M.get_last() or M.get_current() local session = opt.last and M.get_last() or M.get_current()
if sfile and vim.fn.filereadable(sfile) ~= 0 then if session and vim.fn.filereadable(session) ~= 0 then
vim.cmd("source " .. e(sfile)) vim.cmd("source " .. e(session))
vim.g.persisting = true vim.g.persisting = true
end end
end end
function M.list() function M.list()
return vim.fn.glob(Config.options.dir .. "*.vim", true, true) return vim.fn.glob(config.options.dir .. "*.vim", true, true)
end end
function M.toggle() function M.toggle()