Skip to content

Conversation

@Subjective
Copy link
Contributor

@Subjective Subjective commented Sep 15, 2023

Currently, there is no easy way (afaik) to retrieve session data for sessions not stored in the default save directory from within the session itself. It would be nice if the API exposed a function to do exactly that. One fairly clean way of doing this might be to add this functionality to the existing get_current(), by allowing it to have a second optional return string representing the save directory. This way, data for the current session can be retrieved (no matter the save directory) like this:

local resession = require "resession"
local files = require "resession.files"
local util = require "resession.util"

local filename = util.get_session_file(resession.get_current())
local data = files.load_json_file(filename)

If you're wondering what my use case is, I would like to have my Grapple tags specific to a session, and one way to do this by having a custom resolver that creates a scope based on the cwd of the current session. (Since projects.nvim autochanges the directory, it's necessary to have the scope resolved to global cwd stored by resession.nvim as opposed to the vim's cwd so that the tags don't get reset). This allows Grapple's builtin resession extension to function seamlessly in this kind of workflow.

Example Grapple setup
local scope = require "grapple.scope"
local resession = require "resession"
local files = require "resession.files"
local util = require "resession.util"

local get_session_path = function(session_name, dirname)
  if session_name then
    local filename = util.get_session_file(session_name, dirname)
    local data = files.load_json_file(filename)
    if data then
      local session_cwd = data.tab_scoped and data.tabs[1].cwd or data.global.cwd
      return string.format("%s", session_cwd)
    end
  end
end

local session_path = scope.resolver(function()
  if resession.get_current() then return string.format("%s", get_session_path(resession.get_current())) end
end, { cache = false, persist = false })

local resolver = scope.fallback {
  session_path,
  require("grapple.scope_resolvers").git,
}

local format_title = function()
  local current_session, session_dir = resession.get_current()
  if current_session then
    local title = string.format(" %s [%s] ", current_session, util.shorten_path(get_session_path(current_session, session_dir)))
    return #title > 50 and string.format(" %s ", current_session) or title
  else
    local git_root = require("grapple.state").ensure_loaded(require("grapple.scope_resolvers").git)
    return string.format(" %s ", git_root ~= vim.env.HOME and util.shorten_path(git_root) or git_root)
  end
end

resession.add_hook("pre_save", function()
  if require("resession").get_current() == nil then
    require("grapple").save()
    require("grapple.settings").integrations.resession = true
  end
end)
resession.add_hook("pre_load", function()
  require("grapple").save()
  require("grapple.settings").integrations.resession = true
end)

local unload_scopes = function()
  local grapple_state = require("grapple.state").state()
  for loaded_scope, _ in pairs(grapple_state) do
    if loaded_scope ~= get_session_path(resession.get_current()) then require("grapple.state").reset(loaded_scope, true) end
  end
end
resession.add_hook("post_save", function() unload_scopes() end)
resession.add_hook("post_load", function() unload_scopes() end)

@Subjective
Copy link
Contributor Author

Not sure why the generate script for docs formatted the return types like this:

Type Desc
string ?, string?

@Subjective Subjective changed the title feat!: get_current returns custom session save directory if available feat!: get_current() returns custom session save directory if available Sep 15, 2023
Copy link
Owner

@stevearc stevearc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll admit I don't think I fully understand your problem or how this solves it, but I'm fine with an API change that supports returning more data about the current session.

I think I would prefer this to go into a new method, something like get_current_session_info that can return an object with the name & dir of the session. That makes it much easier to extend in the future if we ever add more metadata about sessions.

@Subjective Subjective force-pushed the patch-7 branch 2 times, most recently from cfc4e52 to fbe1885 Compare September 18, 2023 06:01
Comment on lines 26 to 28
---@class resession.SessionInfo
---@field name nil|string Name of the current session
---@field directory nil|string Name of the directory that the current session is saved in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name should be non-nilable, and I believe directory should be as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set the directory in session_configs to the default value if it wasn't provided.

Comment on lines 75 to 81
local session_info = {}
local session = M.get_current()
if session_info[session] ~= nil then
session_info.dir = session_info[session].dir
end
return session_info
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never work because the local session_info will override the global one. if session_info[session] will always be nil. Did you test this at all?

Also, you aren't setting the session name on the return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't know what I was thinking at the time. It should be fixed now.

@stevearc
Copy link
Owner

Looks good, thanks!

@stevearc stevearc merged commit 5fc35f6 into stevearc:master Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants