From 336909e742c33ba8b31502a41a9e02b22703d1cc Mon Sep 17 00:00:00 2001 From: olimorris Date: Thu, 3 Mar 2022 21:15:37 +0000 Subject: [PATCH] chore: formatting with stylua --- README.md | 2 + lua/persisted/config.lua | 12 ++--- lua/persisted/init.lua | 102 +++++++++++++++++++-------------------- 3 files changed, 59 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 20f266c..d77232f 100644 --- a/README.md +++ b/README.md @@ -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. Some example keybindings for the plugins functions are contained below: + ```lua -- restore the session for the current directory and current branch (if `git_use_branch` is enabled) vim.api.nvim_set_keymap("n", "qr", [[lua require("persisted").load()]]) @@ -87,4 +88,5 @@ vim.api.nvim_set_keymap("n", "qt", [[lua require("persisted").toggl ``` ### 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). diff --git a/lua/persisted/config.lua b/lua/persisted/config.lua index f447117..27453e2 100644 --- a/lua/persisted/config.lua +++ b/lua/persisted/config.lua @@ -2,18 +2,18 @@ local M = {} ---@class PersistedOptions local defaults = { - 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 - autosave = true, -- automatically save session files - options = { "buffers", "curdir", "tabpages", "winsize" } -- session options used for saving + 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 + autosave = true, -- automatically save session files + options = { "buffers", "curdir", "tabpages", "winsize" }, -- session options used for saving } ---@type PersistedOptions M.options = {} function M.setup(opts) - M.options = vim.tbl_deep_extend("force", {}, defaults, opts or {}) - vim.fn.mkdir(M.options.dir, "p") + M.options = vim.tbl_deep_extend("force", {}, defaults, opts or {}) + vim.fn.mkdir(M.options.dir, "p") end return M diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 7efcbea..1628068 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -1,99 +1,99 @@ -local Config = require("persisted.config") +local config = require("persisted.config") local M = {} local e = vim.fn.fnameescape function M.get_current() - local pattern = "/" - if vim.fn.has("win32") == 1 then - pattern = "[\\:]" - end - local name = vim.fn.getcwd():gsub(pattern, "%%") - return Config.options.dir .. name .. get_branch() .. ".vim" + local pattern = "/" + if vim.fn.has("win32") == 1 then + pattern = "[\\:]" + end + local name = vim.fn.getcwd():gsub(pattern, "%%") + return config.options.dir .. name .. get_branch() .. ".vim" end 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 - local branch = vim.api.nvim_exec( - [[!git rev-parse --abbrev-ref HEAD 2>/dev/null]], - true) + if config.options.use_git_branch and git_enabled then + local branch = vim.api.nvim_exec([[!git rev-parse --abbrev-ref HEAD 2>/dev/null]], true) - -- The branch command returns two lines. We only need the second one - local lines = {} - for s in branch:gmatch("[^\r\n]+") do - table.insert(lines, "_" .. s) - end - return lines[2]:gsub("/", "%%") + -- The branch command returns two lines. We only need the second one + local lines = {} + for s in branch:gmatch("[^\r\n]+") do + table.insert(lines, "_" .. s) end + return lines[2]:gsub("/", "%%") + end - return "" + return "" end function M.get_last() - local sessions = M.list() - table.sort(sessions, function(a, b) - return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec - end) - return sessions[1] + local sessions = M.list() + table.sort(sessions, function(a, b) + return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec + end) + return sessions[1] end function M.setup(opts) - Config.setup(opts) - if Config.options.autosave then - M.start() - end + config.setup(opts) + if config.options.autosave then + M.start() + end end function M.start() - vim.cmd([[ + vim.cmd([[ augroup Persisted autocmd! autocmd VimLeavePre * lua require("persisted").save() augroup end ]]) - vim.g.persisting = true + vim.g.persisting = true end function M.stop() - vim.cmd([[ + vim.cmd([[ autocmd! Persisted augroup! Persisted ]]) - vim.g.persisting = false + vim.g.persisting = false end function M.save() - local tmp = vim.o.sessionoptions - vim.o.sessionoptions = table.concat(Config.options.options, ",") - vim.cmd("mks! " .. e(M.get_current())) - vim.o.sessionoptions = tmp - vim.g.persisting = true + local tmp = vim.o.sessionoptions + vim.o.sessionoptions = table.concat(config.options.options, ",") + vim.cmd("mks! " .. e(M.get_current())) + vim.o.sessionoptions = tmp + vim.g.persisting = true +end + end function M.load(opt) - opt = opt or {} - local sfile = opt.last and M.get_last() or M.get_current() - if sfile and vim.fn.filereadable(sfile) ~= 0 then - vim.cmd("source " .. e(sfile)) - vim.g.persisting = true - end + opt = opt or {} + local session = opt.last and M.get_last() or M.get_current() + if session and vim.fn.filereadable(session) ~= 0 then + vim.cmd("source " .. e(session)) + vim.g.persisting = true + end end function M.list() - return vim.fn.glob(Config.options.dir .. "*.vim", true, true) + return vim.fn.glob(config.options.dir .. "*.vim", true, true) end function M.toggle() - if vim.g.persisting == nil then - return M.load() - end - if vim.g.persisting then - return M.stop() - end - return M.start() + if vim.g.persisting == nil then + return M.load() + end + if vim.g.persisting then + return M.stop() + end + return M.start() end return M