this post was submitted on 11 Jun 2023
10 points (100.0% liked)

Neovim

496 readers
1 users here now

Neovim is a modal text editor forked off of Vim in 2014. Being modal means that you do not simply type text on screen, but the behavior and functionality of the editor changes entirely depending on the mode.

The most common and most used mode, the "normal mode" for Neovim is to essentially turn your keyboard in to hotkeys with which you can navigate and manipulate text. Several modes exist, but two other most common ones are "insert mode" where you type in text directly as if it was a traditional text editor, and "visual mode" where you select text.

Neovim seeks to enable further community participation in its development and to make drastic changes without turning it in to something that is "not Vim". Neovim also seeks to enable embedding the editor within GUI applications.

The Neovim logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.

founded 3 years ago
MODERATORS
 

So, instead of running to reddit, i'll give this a shot here. I'm in the process of making my own NeoVim Configuration from scratch. One of the tools i use is sidebar-nvim . This allows me to make my own Sections. I'm pretty new to lua and have never dabbled in plugin programming before. Through reading the source of sidebar-nvim and neovim-session-manager I have gotten to a point where i get a list of my sessions in the sidebar.

local session_manager = require("session_manager.utils")
local sessions = session_manager.get_sessions()
local dirs = {}
local filenames = {}

for _, file, _ in pairs(sessions) do
  local directory = session_manager.shorten_path(file.dir)
  table.insert(dirs, directory)
  table.insert(filenames, file.filename)
end

local section = {
  title = "Sessions",
  icon = " ",
  draw = function()
    return dirs
  end,
  bindings = {
    ["l"] = function(line, col)
      local filename = filenames[line]
      session_manager.load_session(filename, false)

    end,
  }
}

return {
  "sidebar-nvim/sidebar.nvim",
  config = function()
    require("sidebar-nvim").setup({
      open = true,
      side = "right",
      sections = {
        "datetime",
        "symbols",
        section,
        "git",
        "todos",
        "diagnostics"
      },
    })
  end
}

The thing that isn't working is the "bindings". session_manager.load_session takes the filename out of the original sessions table. I have no clue how the original structure looks. My attempt currently returns an empty value. Anyone have an idea how to access the data i need to get the session loaded properly?

top 3 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 year ago* (last edited 1 year ago) (1 children)

Ok, found a few errors, but only one in your code. I have a workaround though for the bug in nvim-session-manager.

There's, at the very least, a bug in the code of neovim-session-manager which errors out even if you get the correct name while sidebar is open, before crashing. This may be an issue with sidebar though but session manager shouldn't crash neovim at all.

For your bug, in your binding, you should add one to the line number to get the correct index. So, local filename = filenames(line + 1) will work. This isn't what caused your issue however

The second issue, is with you having the sidebar open. This, for some reason, crashes neovim for me. The following code closes the sidebar before running the command.


bindings = {
  ["l"] = function(line, col)
    vim.cmd("SidebarNvinClose")
    local filename = filenames[line + 1]
    session_manager.load_session(filename, false)
  end,

This, for me, works. I'll update later with my full code snippet along with how to output tables in lua.

Edit: Here's the code that worked for me, incase I missed something. You can use the dump function at the top to format the table into a string. Use print(dump(file)) to view it:


local function dump(o)
 if type(o) == 'table' then
    local s = '{ '
    for k,v in pairs(o) do
       if type(k) ~= 'number' then k = '"'..k..'"' end
       s = s .. '['..k..'] = ' .. dump(v) .. ','
    end
    return s .. '} '
 else
    return tostring(o)
 end
end
local sessionman = require'session_manager.utils'
local sessions = sessionman.get_sessions()

local dirs = {}

local filenames = {}

for _, file, _ in pairs(sessions) do
  print(file.filename)
  local directory = sessionman.shorten_path(file.dir)
    table.insert(dirs, directory);
    table.insert(filenames, file.filename);
end


local section = {
  title = "Section Title",
  icon = "->",
  setup = function(ctx)
    -- called only once and if the section is being used
  end,
  end,
  draw = function(ctx)
    return dirs;
  end,
  bindings = {
    ["l"] = function (line, col)

	  vim.cmd("SidebarNvimClose")
	    local filename = filenames[line + 1];
		sessionman.load_session(filename, true);
        end,
	}
}

return {
	"/sidebar-nvim/sidebar.nvim",
	config = {
		sections = {
			section,
		}
	}

}

[–] [email protected] 2 points 1 year ago

Oh man, i can't believe it was only me being off by one. I'm pretty surprised i got as far as i did, considering my lua knowledge boils down to some config functions in Lazy Packages.

I appreciate you looking into this. This works perfectly. I'll probably play around with coloring stuff a bit, but the functionality works.

[–] [email protected] 1 points 1 year ago* (last edited 1 year ago)

Huh, never heard about sidebar-nvim. No clue if I'll be able to help, I'll see what I can find, but either way thanks for the cool plugin

load more comments
view more: next ›