stevearc / resession.nvim

A replacement for mksession with a better API
MIT License
233 stars 19 forks source link

bug: colorscheme not loaded #67

Open uwla opened 3 months ago

uwla commented 3 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

v0.10.1

Operating system/version

ArchLinux

Describe the bug

Colorschemes for a given session are not restored.

What is the severity of this bug?

annoyance

Steps To Reproduce

  1. Open a new session
  2. Change the colorscheme with colorscheme
  3. Close nvim
  4. OPen the session again

Expected Behavior

With mksession, the colorscheme for a given session is restored.

I like to use distinct colorschemes for different projects because they fit better to that project and its programming language.

uwla commented 3 months ago

My fault, I misunderstood the plugin usage of session per directory. I was using such feature, which would save a per-directory session but when I tried to load require('resession').load('project_name') it did not work because was a different session type...

uwla commented 3 months ago

I reopened the issue because I started experience it again... but the bug only happens in the first launch.

I have a script that launches ROFI for me to pick a project and open it with neovim:

# show graphical menu, then get selected directory
folder=$(ls -1 "${HOME}/code" | \
    rofi -dmenu -window-title 'CODE' | \
    head -n 1)

# nothing selected, exit
[ -z "${folder}" ] && exit

# solve symbolic links
[ -s "${folder}" ] && folder=$(readlink "${folder}")

# get absolute path
path=$(realpath "${HOME}/code/${folder}")

# run code
cd "$path" || exit 1

if [ -f "${HOME}/.local/share/nvim/session/${folder}.json" ]; then
    exec neovide --no-fork -- \
        -c "silent! lua require('resession').load('${folder}')"
else
    exec neovide --no-fork -- \
        . \
        --cmd 'argdelete .' \
        -c "silent! lua require('resession').save('${folder}')"
fi

The last part is the most relevant.

If a session file exist, it opens the session with resession.load(), otherwise it saves new session.

For unknown reason, when I first open a session this way, the colorscheme is not restored... But if I close neovim (neovide actually) and relaunch it via the same script the colorscheme is restored!

To reproduce:

  1. Open a project with the script (change it if needed)
  2. It will open the project and load the session
  3. The colorscheme will likely not be restored (if it was not the default colorscheme)
  4. Close the project and open again with the script
  5. The colorscheme is now restored

There seems to be a delay when I first open the session... It also takes longer.

However, using the same script, this NEVER happened with other session plugins, such as xoloc's vim session which uses mksession. That is why I believe the bug is within this plugin and not something else. Otherwise, I would have experience such issue with other session plugins but this never happened.

SwimmingPolar commented 3 months ago

@uwla I'm seeing the same issue here. To be exact, is it the "color scheme" not loading or is it the "highlighting"? I thought it was the syntax highlighting that isn't working or at least it looks like it does not. Because, when I :TSConfigInfo, it showed highlight: { enable: false } so I :TSEnable highlight or :TSToggle highlight to get the highlighting back. But then, for some reason, the syntax highlight is gone even though highlight is enabled 😅. You can get your colors back with :e more conveniently? I guess.

I'm not familiar with neovim sessions so I might have missed out some of the steps I need to handle when dealing with sessions on my config though. Here's the resession config and neo-tree config.

return {
  "stevearc/resession.nvim",
  config = function()
    local resession = require("resession")
    resession.setup({})

    -- Get session name
    local function get_session_name()
      local is_git_repo = vim.trim(vim.fn.system("git rev-parse --is-inside-work-tree"))

      if is_git_repo == "true" then
        return vim.trim(vim.fn.system("git rev-parse --show-toplevel"))
      else
        return vim.trim(vim.fn.getcwd())
      end
    end

    -- Save session
    local function save_session()
      require("resession").save(get_session_name(), { dir = "dirsession", notify = false })
    end

    -- Load session
    ---@class LoadSessionOption
    ---@field name string?
    ---@param load_session_opts LoadSessionOption?
    local function load_session(load_session_opts)
      local session = load_session_opts and load_session_opts.name or nil

      -- load session
      local ok = pcall(function()
        resession.load(session or get_session_name(), { dir = "dirsession", notify = false })
      end)

      -- if didn't load, like in new session, instead save session
      if not ok then
        save_session()
      end

      -- @NOTE: might be a bug
      -- reload buffer because ts won't update highlight when session changes
      vim.cmd("silent! :e")
    end

    -- Delete session
    local function delete_session()
      pcall(function()
        resession.delete(get_session_name(), { dir = "dirsession" })
      end)
    end

    -- Show sessions list
    local function show_sessions()
      -- get sessions list
      local session_list = resession.list({ dir = "dirsession" })

      -- rename session names with "/" instead of "_"
      local new_session_list = vim.tbl_map(function(session)
        local name = string.gsub(session, "_", "/")
        return name
      end, session_list)

      -- show sessions a list
      vim.ui.select(new_session_list, {
        prompt = "Select Session",
        telescope = require("telescope.themes").get_dropdown({
          initial_mode = "normal",
        }),
      }, function(selected)
        if selected == nil then
          return
        end

        -- sync selected session and current session name to the same separator("/")
        local selected_session_name = selected and string.gsub(selected, "/", "_") or ""
        local current_session_name = require("resession").get_current()
        current_session_name = current_session_name and string.gsub(current_session_name, "/", "_") or ""

        -- do nothing if same session is selected
        if selected_session_name == current_session_name then
          return
        end

        -- write changes to the files that been modified before session changes
        vim.cmd("silent! :bufdo update!")
        -- before loading a new session, save the current session
        save_session()

        load_session({
          name = selected,
        })
      end)
    end

    vim.keymap.set("n", "<leader>qs", save_session, { desc = "save session" })
    vim.keymap.set("n", "<leader>ql", show_sessions, { desc = "show sessions" })
    vim.keymap.set("n", "<leader>qd", delete_session, { desc = "delete session" })

    vim.api.nvim_create_autocmd("VimEnter", {
      callback = function()
        -- Only load the session if nvim was started with no args
        if vim.fn.argc(-1) == 0 then
          load_session({})
        end
      end,
    })
    vim.api.nvim_create_autocmd("VimLeavePre", {
      callback = function()
        -- save_session()
        resession.save_all()
      end,
    })
  end,
}
return {
  "nvim-neo-tree/neo-tree.nvim",
  opts = {
    buffers = {
      follow_current_file = {
        enabled = true, -- This will find and focus the file in the active buffer every time
        leave_dirs_open = true, -- `false` closes auto expanded dirs, such as with `:Neotree reveal`
      },
    },
    filesystem = {
      bind_to_cwd = false,
    },
    ...

Made neotree(any plugin that might change cwd) file system bind to false. Thought that would be easier to manage sessions than jumping around cwds.

+ And it's not respecting the last buffer I exited again.

uwla commented 3 months ago

hi @SwimmingPolar ! The highlight works perfectly, but this plugin loads the first colorscheme that comes alphabetically (in my case, abyss) instead of the colorscheme I had set for the session (usually cyberdream or gotham). But when I open for the second time the colorscheme is restored!!

uwla commented 3 months ago

RESESSSION

SwimmingPolar commented 3 months ago

@uwla Yeah I see. Even though the issues may not seem exactly the same, it looks like to me, that we have to deal with or handle some left-over states of the prior session before/after loading a new session? Hence the hook functions. But not really sure though. Maybe @stevearc can help a little? Like are we saving modified buffers(for now, if I'm not mistaken it does not? so did it myself) before leaving the session or is there anything we have to clean up before or after loading a new session? Why some of us are having tree-sitter related issues?