Code Monkey home page Code Monkey logo

Comments (8)

FelixKratz avatar FelixKratz commented on August 27, 2024

This is also a problem if I, for example, edit a file by entering its absolute path (without calling chdir). The session will not be loaded when I edit the file from a different directory. This could be fixed by checking all session files if the current file is contained in any of the sessions, loading the first session where the current file is contained. This would also fix my initial problem with having two distinct sessions for different files in the same folder.

from trailblazer.nvim.

LeonHeidelbach avatar LeonHeidelbach commented on August 27, 2024

Sessions are usually dependent on the current working directory as this is the best solution for most users. Checking all session files for the first occurence of the opened file and loading only those trail marks is not the way to go since you could have multiple sessions with the same file which would introduce inconsistencies in the loading behavior once the order changes.

If your workflow requires a more granular approach to sessions you can simply use the API to create a named session with only the marks you need and load them on demand.

TrailBlazer sessions are stored in this directory:

local session_dir = vim.fn.stdpath("data") .. "/trailblazer/"

You could then create a new session for the currently open file like this:

local file_name = vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0)))
require("trailblazer").save_trailblazer_state_to_file(session_dir .. file_name .. ".tbsv", nil, false)

If you want to auto-save per file sessions on exit you can simply add the the above to a custom auto command that is called on the VimLeavePre event. You could also implement a solution that would save per file sessions on BufUnload. This would ensure that all open files have their own session and automatically save the current session state for each individual file, meaning that you would be able to open any of the files that are currently in your session at a later point and all other files would automatically be opend.

You can just as easily load the session for the current file like this:

local file_name = vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0)))
require("trailblazer").load_trailblazer_state_from_file(session_dir .. file_name .. ".tbsv", false)

If you want to auto load sessions like this on startup you can simply call the above after TrailBlazer's setup function or add a custom auto command to the BufFilePost event. Obviously you would have to disable auto-saving/loading sessions in your custom config.

from trailblazer.nvim.

FelixKratz avatar FelixKratz commented on August 27, 2024

I would like to use the configured

custom_session_storage_dir = "~/tb_sessions/",

without hardcoding it in my keybinding, would it perhaps be better to separate dir and filename arguments in both signatures, such that I dont have to specify the path but only the name of the file?

from trailblazer.nvim.

FelixKratz avatar FelixKratz commented on August 27, 2024

This doesn't seem to work:

vim.api.nvim_set_keymap("n", "<Esc>l", ':lua local file = vim.fn.stdpath("data") .. "/trailblazer/" .. vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0))) require("trailblazer").load_trailblazer_state_from_file(file, false) require("trailblazer").open_trail_mark_list()<CR>:wincmd p<CR>', opts)
vim.api.nvim_set_keymap("n", "<Esc>s", ':lua local file = vim.fn.stdpath("data") .. "/trailblazer/" .. vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0))) require("trailblazer").save_trailblazer_state_to_file(file, nil, false) print("Saved Session")<CR>', opts)

It creates a file without the .tbsv file ending which I guess can not be loaded again? There is no error indicating this however.

This works:

vim.api.nvim_set_keymap("n", "<Esc>l", ':lua local file = vim.fn.stdpath("data") .. "/trailblazer/" .. vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0))) .. ".tbsv" require("trailblazer").load_trailblazer_state_from_file(file, false) require("trailblazer").open_trail_mark_list()<CR>:wincmd p<CR>', opts)
vim.api.nvim_set_keymap("n", "<Esc>s", ':lua local file = vim.fn.stdpath("data") .. "/trailblazer/" .. vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0))) .. ".tbsv" require("trailblazer").save_trailblazer_state_to_file(file, nil, false) print("Saved Session")<CR>', opts)

I would suggest that the .tbsv extension is added automatically if not specified and that either the path is split into separate dir and file name arguments, or that relative paths, e.g. just the name, will automatically be adapted to include the configured/default storage dir.

from trailblazer.nvim.

LeonHeidelbach avatar LeonHeidelbach commented on August 27, 2024

I would like to use the configured

custom_session_storage_dir = "~/tb_sessions/",

without hardcoding it in my keybinding, would it perhaps be better to separate dir and filename arguments in both signatures, such that I dont have to specify the path but only the name of the file?

Having two separate arguments would unnecessarily complicate things. You can simpy get the default value from TrailBlazers configuration table like this:

local default_storage_path = require("trailblazer").options.custom_session_storage_dir

This doesn't seem to work:

vim.api.nvim_set_keymap("n", "<Esc>l", ':lua local file = vim.fn.stdpath("data") .. "/trailblazer/" .. vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0))) require("trailblazer").load_trailblazer_state_from_file(file, false) require("trailblazer").open_trail_mark_list()<CR>:wincmd p<CR>', opts)
vim.api.nvim_set_keymap("n", "<Esc>s", ':lua local file = vim.fn.stdpath("data") .. "/trailblazer/" .. vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0))) require("trailblazer").save_trailblazer_state_to_file(file, nil, false) print("Saved Session")<CR>', opts)

It creates a file without the .tbsv file ending which I guess can not be loaded again? There is no error indicating this however.

Oh yes, I forgot about that. This is actually the inteded behavior and the reason for this is detailed in the documentation. You will just have to add any file extension to your save path for it to work properly. There is no error message for this as this is not a bug but enables you to dynamically create session directories or save files depending on whether or not you add a file type extension.

from trailblazer.nvim.

FelixKratz avatar FelixKratz commented on August 27, 2024

If you pass either a directory or file name to :TrailBlazerSaveSession or :TrailBlazerLoadSession, the session will be saved into or loaded from the given directory or file. Note that you have to append any file extension (e.g. ".tbsv") to the file name argument when saving a session. Otherwise TrailBlazer will create a directory with the given name and save the session file named as the hashed path of the current working directory. Passing a directory name to :TrailBlazerLoadSession will cause TrailBlazer to search for a session file which matches the name of the hashed path of the current working directory.

Even after reading it again I have no clue what you want to tell me with this paragraph. I will just use the default_storage_path in my keybindings.

from trailblazer.nvim.

LeonHeidelbach avatar LeonHeidelbach commented on August 27, 2024

It's actually quite simple:

  • :TrailBlazerSaveSession sessions will create the directory "sessions" in the current cwd and save all trail marks to a file in this directory named after the hashed value of the expanded cwd (i.e. /home/<username>/projects/your_project -> 5c33a1e23599584ac47ea3914ecec378ca4ab49f3fb9d9a6e54f0491a5dc8620.tbsv) or create it if it doesn't already exist.
  • :TrailBlazerSaveSession sessions/session_file.tbsv will save all trail marks to a file named session_file.tbsv in the directory "sessions" or create it if it does not already exist within the current cwd.

from trailblazer.nvim.

FelixKratz avatar FelixKratz commented on August 27, 2024

It's actually quite simple:

  • :TrailBlazerSaveSession sessions will create the directory "sessions" in the current cwd and save all trail marks to a file in this directory named after the hashed value of the expanded cwd (i.e. /home/<username>/projects/your_project -> 5c33a1e23599584ac47ea3914ecec378ca4ab49f3fb9d9a6e54f0491a5dc8620.tbsv) or create it if it doesn't already exist.
  • :TrailBlazerSaveSession sessions/session_file.tbsv will save all trail marks to a file named session_file.tbsv in the directory "sessions" or create it if it does not already exist within the current cwd.

So why exactly doesn't this work then and why would that be expected behavior?

local file = vim.fn.stdpath("data") .. "/trailblazer/" .. vim.fn.sha256(vim.fn.expand(vim.api.nvim_buf_get_name(0)))
require("trailblazer").save_trailblazer_state_to_file(file, nil, false) 

followed by

require("trailblazer").load_trailblazer_state_from_file(file, false)

I just find this super unintuitive.

from trailblazer.nvim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.