Skip to content

Commit

Permalink
remove external plugin-specific stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
zwhitchcox committed Nov 7, 2022
1 parent 60d409a commit b22064d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 90 deletions.
64 changes: 24 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ Auto Session takes advantage of Neovim's existing session management capabilitie
:warning: Please note that if there are errors in your config, restoring the session might fail, if that happens, auto session will then disable auto saving for the current session.
Manually saving a session can still be done by calling `:SaveSession`.

AutoSession now tracks `cwd` changes!
By default, handling is as follows:
DirChangedPre (before the cwd actually changes):
- Save the current session
- Clear all buffers `%bd!`. This guarantees buffers don't bleed to the
next session.
- Clear jumps. Also done so there is no bleading between sessions.
- Run the `pre_cwd_changed_hook`
DirChanged (after the cwd has changed):
- Restore session using new cwd
- Run the `post_cwd_changed_hook`

AutoSession now tracks `cwd` changes!
By default, handling is as follows:
DirChangedPre (before the cwd actually changes):
- Save the current session
- Clear all buffers `%bd!`. This guarantees buffers don't bleed to the
next session.
- Clear jumps. Also done so there is no bleading between sessions.
- Run the `pre_cwd_changed_hook`
DirChanged (after the cwd has changed):
- Restore session using new cwd
- Run the `post_cwd_changed_hook`
Now when the user changes the cwd with `:cd some/new/dir` auto-session handles it gracefully, saving the current session so there aren't losses and loading the session for the upcoming cwd if it exists.

Hooks are available for custom actions _before_ and _after_ the `cwd` is changed. These hooks can be configured through the `cwd_change_handling` key as follows:
Expand Down Expand Up @@ -113,19 +113,18 @@ require('lualine').setup{

### Options

| Config | Options | Default | Description |
| ------------------------------------------| ------------------------ | ------------------------------------ | --------------------------------------------------------------- |
| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin |
| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist |
| auto_session_enable_file_tree_integration | false, true | false | Fixes problems with nvim-tree and NERDTree compatibility |
| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions |
| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features |
| auto_session_create_enabled | false, true | true | Enables/disables the plugin's session auto creation |
| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving |
| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring |
| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs |
| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs |
| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name |
| Config | Options | Default | Description |
| -------------------------------- | ------------------------ | ------------------------------------ | --------------------------------------------------------------- |
| log_level | 'debug', 'info', 'error' | 'info' | Sets the log level of the plugin |
| auto_session_enable_last_session | false, true | false | Loads the last loaded session if session for cwd does not exist |
| auto_session_root_dir | "/some/path/you/want" | vim.fn.stdpath('data').."/sessions/" | Changes the root dir for sessions |
| auto_session_enabled | false, true | true | Enables/disables the plugin's auto save _and_ restore features |
| auto_session_create_enabled | false, true | true | Enables/disables the plugin's session auto creation |
| auto_save_enabled | false, true, nil | nil | Enables/disables auto saving |
| auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring |
| auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs |
| auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs |
| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name |

#### Notes

Expand Down Expand Up @@ -177,21 +176,6 @@ require('auto-session').setup {

:warning: WARNING :warning: If the directory does not exist, default directory will be used and an error message will be printed.

### File Tree Integration

This fixes a lot of conflics with [`nvim-tree`](https://github.com/nvim-tree/nvim-tree.lua) and [`NERDTree`](https://github.com/preservim/nerdtree),
as they both try to control the `cwd`, which causes infinite loops and corrupted session data.
This feature synchronizes the `cwd` between `auto-session` and the file tree plugin, which turns out to be very powerful and fun to use.
:warning: This feature is still experimental. Bugs to be expected.

```lua
require('auto-session').setup {
auto_session_enable_file_tree_integration=true,
}
```

:warning: Note :warning: If you already have broken session data, this will not fix it. You will have to delete that session and start again

# 📢 Commands

Auto Session exposes two commands that can be used or mapped to any keybindings for manually saving and restoring sessions.
Expand Down
23 changes: 12 additions & 11 deletions lua/auto-session-autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local Lib = require "auto-session-library"

local M = {}

-- don't autorestore if there are open buffers (indicating a failed save session
-- don't autorestore if there are open buffers (indicating auto save session failed)
local function has_open_buffers()
local result = false
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
Expand Down Expand Up @@ -32,10 +32,7 @@ M.setup_autocmds = function(config, AutoSession)
end

local conf = config.cwd_change_handling
local scopes = { "global" }
if config.auto_session_enable_file_tree_integration then
table.insert(scopes, "window")
end
local scopes = { "global", "tabpage"}

for _, pattern in ipairs(scopes) do
vim.api.nvim_create_autocmd("DirChangedPre", {
Expand All @@ -46,12 +43,11 @@ M.setup_autocmds = function(config, AutoSession)
Lib.logger.debug(" changed window: " .. tostring(vim.v.event.changed_window))
Lib.logger.debug(" scope: " .. vim.v.event.scope)

if vim.v.event.scope == "window" then
-- don't save session for all `lcd`'s (local change directory), just file tree explorers
if not Lib.tree_buf_type(vim.api.nvim_get_current_buf()) then
Lib.logger.debug(" not file tree event, returning")
return
end
-- Don't want to save session if dir change was triggered
-- by a window change. This will corrupt the session data,
-- mixing the two different directory sessions
if vim.v.event.changed_window then
return
end

AutoSession.AutoSaveSession()
Expand All @@ -75,6 +71,11 @@ M.setup_autocmds = function(config, AutoSession)
Lib.logger.debug(" changed window: " .. tostring(vim.v.event.changed_window))
Lib.logger.debug(" scope: " .. vim.v.event.scope)

-- see above
if vim.v.event.changed_window then
return
end

-- all buffers should've been deleted in `DirChangedPre`, something probably went wrong
if has_open_buffers() then
Lib.logger.debug("Cancelling session restore")
Expand Down
25 changes: 0 additions & 25 deletions lua/auto-session-library.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,4 @@ function Lib.logger.error(...)
vim.notify(vim.fn.join({ "error: ", tostring(...) }, " "), vim.log.levels.ERROR)
end

-- return type of file tree explorer or false if not a file tree explorers
function Lib.tree_buf_type(bufnr)
if bufnr == nil then
bufnr = 0
end
if vim.fn.bufexists(bufnr) then
local bufname = vim.api.nvim_buf_get_name(bufnr)
local filename = vim.fn.fnamemodify(bufname, ":t")
if filename:match "^NvimTree_[0-9]+$" then
if vim.bo[bufnr].filetype == "NvimTree" then
return "nvimtree"
elseif vim.fn.filereadable(bufname) == 0 then
return "nvimtree"
end
elseif filename:match "^NERD_tree_[0-9]+$" then
if vim.bo[bufnr].filetype == "nerdtree" then
return "nerdtree"
elseif vim.fn.filereadable(bufname) == 0 then
return "nerdtree"
end
end
end
return false
end

return Lib
14 changes: 0 additions & 14 deletions lua/auto-session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ local AutoSession = {
local defaultConf = {
log_level = vim.g.auto_session_log_level or AutoSession.conf.logLevel or AutoSession.conf.log_level or "info", -- Sets the log level of the plugin (debug, info, error). camelCase logLevel for compatibility.
auto_session_enable_last_session = vim.g.auto_session_enable_last_session or false, -- Enables/disables the "last session" feature
auto_session_enable_file_tree_integration = vim.g.auto_session_enable_file_tree_integration or false, -- Enables/disables the "last session" feature
auto_session_root_dir = vim.fn.stdpath "data" .. "/sessions/", -- Root dir where sessions will be stored
auto_session_enabled = true, -- Enables/disables auto creating, saving and restoring
auto_session_create_enabled = nil, -- Enables/disables auto creating new sessions
Expand Down Expand Up @@ -494,19 +493,6 @@ local function post_restore_refresh()
vim.cmd 'filetype detect'
end)

if AutoSession.conf.auto_session_enable_file_tree_integration then
-- refresh file trees
local tree_type = Lib.tree_buf_type(bufnr)
-- we only open the tree if it was open before
if (tree_type == "nvimtree") then
pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
-- want no focus to avoid "'modifiable' is off" errors
require('nvim-tree').toggle(false, true)
elseif (tree_type == "nerdtree") then
pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
vim.cmd 'NERDTreeOpen'
end
end
::continue::
end
end
Expand Down

0 comments on commit b22064d

Please sign in to comment.