Skip to content

fix: cwd not set upon loading first buffer#20

Merged
stevearc merged 3 commits intostevearc:masterfrom
Subjective:fix-incorrect-cwd
Aug 20, 2023
Merged

fix: cwd not set upon loading first buffer#20
stevearc merged 3 commits intostevearc:masterfrom
Subjective:fix-incorrect-cwd

Conversation

@Subjective
Copy link
Copy Markdown
Contributor

@Subjective Subjective commented Aug 19, 2023

Problem: Normally, <c-g> and :w will output a message with the current buffer's filename relative the the cwd. However, when the buffer is restored from a session, the message (as well as the default statusline) show the filename relative the home directory instead. This doesn't seem to happen in tab-scoped sessions where each tab has its own cwd.

Before saving session (cwd set to ~/Documents):
image

After restoring session:
image

In the "after" screenshot, the cwd is set correctly, but the filename is displayed as if it were set to the home directory.

Solution: This PR resolves the issue by setting global options upon loading the first buffer. It also uses the lua api to set the cwd instead of vim.cmd.

Comment on lines +448 to +452
-- Set global options before loading the first buffer
if i == 1 and not data.tab_scoped then
util.restore_global_options(data.global.options)
vim.api.nvim_set_current_dir(data.global.cwd)
end
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This doesn't feel like the right way to do this. If the first buffer in the restore list was unloaded, then we will never restore the global options.

Could this be fixed by just adding another

if not data.tab_scoped then
  vim.api.nvim_set_current_dir(data.global.cwd)
end

after the tabs and buffers are all restored?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah nvm, I found an edgecase where if one of the buffers in the restore list is not under the cwd, then the relative file path does not show correctly for all the subsequent buffers, even those buffers are under the cwd. Removing the check for the first buffer fixes this, but it feels redundant to repeatedly set global options for each buffer.

I'll try out your suggestion

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@stevearc setting the cwd again after loading all buffers/tabs seems to work

@stevearc
Copy link
Copy Markdown
Owner

Thanks for the PR!

@stevearc stevearc merged commit d0ad06e into stevearc:master Aug 20, 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