Skip to content

Commit

Permalink
Merge pull request #3 from EricDriussi/IndianBoy42-main
Browse files Browse the repository at this point in the history
Add Forget functionality
  • Loading branch information
EricDriussi authored Jul 31, 2023
2 parents 195aa61 + 63e9364 commit c446f76
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 7 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,43 @@ I'm either working on a project, taking notes/writing or messing with dotfiles/s
Sessions only make sense to me when working in a project.

- Default (n)vim session management is cumbersome.

- Session management [plugins](https://github.com/natecraddock/sessions.nvim#related) are overkill since I just want part of that functionality and only when inside a project.

- Project management plugins are overkill since I don't really need to manage my projects, I usually navigate to them using [z.lua](https://github.com/skywind3000/z.lua).

Since nothing felt right, I started writing stuff in my config to fit my needs:

- I just want sessions when inside a project.
- Even inside a project, I don't want sessions if I'm explicitly opening a file, editing a commit or reading a `man` page.
- I don't want any overhead. Set the thing up and forget about it.
- I want a quick way to remove the current session if I decide it's useless.

## Usage

As such, this plugin has a *set it and forget it* approach to session handling.

Aucmds are already set up to handle its behavior when opening and closing nvim.
Aucmds are already set up to handle the described behavior when opening and closing nvim.

If that's not enough and/or you wish to extend the behavior, you can save and load sessions manually with the user commands:
If that's not enough and/or you wish to extend the behavior, you can save, load and forget sessions manually with the user commands:

```
```vim
:Memorize
:Recall
:Forget
```

Or by calling these functions directly from you `lua` config:

```lua
require("remember_me").memorize()
require("remember_me").recall()
require("remember_me").forget()
```

### A note on forgetting

Using the "forget" functionality will both delete the current session **and** turn off the `aucmds`: No automatic session saving will occur on exit.
To turn the `aucmds` back on again, just use the "recall" functionality.

## Setup

As usual, `require("remember_me").setup()` needs to be added to your config.
Expand All @@ -65,7 +71,7 @@ You can pass a table to `setup()` with your desired config, as explained below.
}
```

Full config table can be found [here](https://github.com/EricDriussi/remember-me.nvim/blob/5f3a874fb54794d324f97713d012fb328f6a10e5/lua/remember_me/config.lua#L1).
Full config table can be found [here](https://github.com/EricDriussi/remember-me.nvim/blob/main/lua/remember_me/config.lua#L1).

### Options

Expand Down
12 changes: 12 additions & 0 deletions lua/remember_me.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ M.recall = function()
if project:is_valid() and no_args then
local session = Session.new(project.name, project.path)
session:load()
aucmds.clear()
aucmds.create(M.memorize, M.recall)
end
end

M.forget = function()
local project = Project.new()

if project:is_valid() then
local session = Session.new(project.name, project.path)
session:delete()
aucmds.clear()
end
end

return M
7 changes: 6 additions & 1 deletion lua/remember_me/aucmds.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local new_au_cmd = vim.api.nvim_create_autocmd
local remember_me_group = vim.api.nvim_create_augroup("RememberMe", {})
local augroup_name = "RememberMe"
local remember_me_group = vim.api.nvim_create_augroup(augroup_name, {})

local M = {}

Expand All @@ -25,4 +26,8 @@ M.create = function(save_func, load_func)
on_close(load_func)
end

M.clear = function()
vim.api.nvim_create_augroup(augroup_name, {})
end

return M
9 changes: 9 additions & 0 deletions lua/remember_me/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ function Session:load()
end
end

function Session:delete()
local session = self.store .. self.name .. self.ext
local session_exists = vim.fn.filereadable(vim.fn.expand(session)) == 1
if session_exists then
local rm_cmd = vim.api.nvim_parse_cmd("!rm " .. session, {})
vim.api.nvim_cmd(rm_cmd, {})
end
end

return Session
6 changes: 6 additions & 0 deletions plugin/remember_me.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ vim.api.nvim_create_user_command("Remember", function()
end, {
desc = "Try to load last session",
})

vim.api.nvim_create_user_command("Forget", function()
require("remember_me").forget()
end, {
desc = "Try to delete last session",
})
13 changes: 13 additions & 0 deletions tests/lua/remember_me/session_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ describe("session should", function()
assert.stub(api.nvim_cmd).was.called()
mock.revert(api)
end)

it("delete if exists", function()
local api = mock(vim.api, false)
local existing_session = session.store .. session.name .. session.ext
os.execute("mkdir " .. session.store)
os.execute("touch " .. existing_session)

session:delete()

assert.stub(api.nvim_parse_cmd).was.called_with("!rm " .. existing_session, {})
assert.stub(api.nvim_cmd).was.called()
mock.revert(api)
end)
end)
18 changes: 18 additions & 0 deletions tests/lua/remember_me_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,22 @@ describe("in a valid project, remember_me should", function()
mock.revert(session_mock.save)
mock.revert(session_mock.load)
end)

it("save and forget given a valid ft", function()
vim.api.nvim_set_current_dir(vcs_path)
vim.cmd(":vs " .. vcs_path .. "fixture")

local session_mock = mock(require("remember_me.session"), false)
plugin.memorize()
plugin.forget()
plugin.recall()

assert.stub(session_mock.save).was.called()
assert.stub(session_mock.forget).was.called()
assert.stub(session_mock.load).was_not.called()
mock.revert(session_mock.save)
mock.revert(session_mock.forget)
mock.revert(session_mock.load)
end)

end)
6 changes: 6 additions & 0 deletions tests/plugin/command_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ describe("plugin should", function()
vim.cmd("Remember")
end)
end)

it("create Forget user command", function()
assert.has_no.errors(function()
vim.cmd("Forget")
end)
end)
end)

0 comments on commit c446f76

Please sign in to comment.