natecraddock / workspaces.nvim

a simple plugin to manage workspace directories in neovim
MIT License
309 stars 15 forks source link

Option auto_open failed to take effect #31

Closed Parsifa1 closed 7 months ago

Parsifa1 commented 7 months ago

I couldn't find the error in this function, but it didn't work properly on my device. When I entered nvim on the path of the existing Workspace, it couldn't automatically load the session:

local enable_autoload = function()
    -- create autocmd for every file at the start of neovim that checks the current working directory
    -- and if the cwd  matches a workspace directory then activate the corresponding workspace
      vim.api.nvim_create_autocmd({ "VimEnter" }, {
          pattern = "*",
          callback = function()
              for _, workspace in pairs(get_workspaces_and_dirs().workspaces) do
                  if workspace.path == cwd() then
                      M.open(workspace.name)
              end
            end
          end,
      })
end

and this is my config:

return {
    "natecraddock/workspaces.nvim",
    dependencies = { "natecraddock/sessions.nvim" },
    event = "VeryLazy",
    config = function()
        require("telescope").load_extension "workspaces"
        require("sessions").setup {
            events = { "WinEnter", "User", "VimLeavePre" },
            absolute = true,
            session_filepath = vim.fn.stdpath "data" .. "/sessions",
        }
        require("workspaces").setup {
            auto_open = true,
            sort = true,
            mru_sort = true,
            hooks = {
                add = {
                    "SessionsSave",
                },
                open_pre = {
                    "SessionsStop",
                    "silent %bdelete!",
                },
                open = function()
                    require("sessions").load(nil, { silent = true })
                end,
            },
        }
    end,
}

As an aside, is it possible to add feature that automatically stop session recording when my current working directory is no longer the working path I set? Thank you very much. Your plug-in is very light and easy to use.

Parsifa1 commented 7 months ago

I know the problem, close lazyload

Parsifa1 commented 7 months ago

Oh, there is another problem. In the workspace opened in this way, the lsp of the corresponding file cannot be attached. like this: image

Parsifa1 commented 7 months ago

File type not detected image

natecraddock commented 7 months ago

Sorry I didn't get around to replying yesterday. Did you resolve the issue?

Parsifa1 commented 7 months ago

Yes! Your plugin is very light and easy to use. I replaced the plugin that handles sessions with another one and it works very smoothly! Thank you for yout work🥰

elken commented 1 month ago

Have just hit the second issue myself with a potential fix :)

According to Justin, the auto command needs to be nested and surely enough with the below lazy recipe all works as expected vs setting auto_open.

{
    "natecraddock/workspaces.nvim",
    init = function()
      vim.api.nvim_create_autocmd({ "VimEnter" }, {
        pattern = "*",
        nested = true,
        callback = function()
          for _, workspace in pairs(require("workspaces").get()) do
            -- dont autoload if nvim start with arg
            if vim.fn.argc(-1) > 0 then
              return
            end

            if workspace.path == vim.fn.fnamemodify(vim.fn.getcwd(), ":p") then
              require("workspaces").open(workspace.name)
            end
          end
        end,
      })
    end,
    opts = {
      cd_type = "local",
      hooks = {
        open_pre = {
          -- If recording, save current session state and stop recording
          "SessionsStop",

          -- delete all buffers (does not save changes)
          ":Bdelete!",
        },
        open = function()
          require("sessions").load(nil, { silent = true })
        end,
      },
    },
  }

Happy to PR the tiny fix @natecraddock, thanks for a lovely set of plugins :)

EDIT:

Also with that PR now checked out, auto_open works as intended!