1

I am unable to get my folds to save and load between neovim sessions. Currently my sessions are being saved, and I ahve unable to load them through a autocmd, but if I run :loadview after loading into a file they appear.

Here is my tresitter.lua file.

return {
    "nvim-treesitter/nvim-treesitter",
    event = { "BufReadPre", "BufNewFile" },
    build = ":TSUpdate",
    dependencies = {
        "windwp/nvim-ts-autotag",
    },
    config = function()
        -- import nvim-treesitter plugin
        local treesitter = require("nvim-treesitter.configs")

        -- configure treesitter
        treesitter.setup({ -- enable syntax highlighting
            highlight = {
                enable = true,
            },
            -- enable indentation
            indent = { enable = true },
            -- enable autotagging (w/ nvim-ts-autotag plugin)
            autotag = {
                enable = true,
            },
            -- ensure these language parsers are installed
            ensure_installed = {
                "json",
                "javascript",
                "typescript",
                "tsx",
                "yaml",
                "html",
                "css",
                "prisma",
                "python",
                "markdown",
                "markdown_inline",
                "svelte",
                "graphql",
                "bash",
                "lua",
                "vim",
                "dockerfile",
                "gitignore",
                "query",
                "vimdoc",
                "c",
            },
            incremental_selection = {
                enable = true,
                keymaps = {
                    init_selection = "<C-space>",
                    node_incremental = "<C-space>",
                    scope_incremental = false,
                    node_decremental = "<bs>",
                },
            },
        })

        local opt = vim.opt

        -- Set fold method to 'expr' to allow expression-based folding (e.g., for Tree-sitter)
        opt.foldmethod = "expr"
        opt.foldexpr = "nvim_treesitter#foldexpr()"
        opt.foldlevel = 99 -- Prevent all folds from being closed when you open a file
        opt.foldlevelstart = 99
        opt.foldenable = true

        local map = vim.keymap

        map.set("n", "zR", "zR", { noremap = true }) -- open all folds
        map.set("n", "zM", "zM", { noremap = true }) -- close all folds
        map.set("n", "za", "za", { noremap = true }) -- toggle fold

        opt.foldtext = "v:lua.vim_fold_text()"
        function _G.vim_fold_text()
            local line = vim.fn.getline(vim.v.foldstart)
            local num_lines = vim.v.foldend - vim.v.foldstart + 1
            return "   " .. line .. "  [" .. num_lines .. " lines] "
        end

        opt.fillchars = {
            fold = "━",
            foldopen = "",
            foldclose = "",
            foldsep = "~",
        }

        vim.api.nvim_create_autocmd({ "ColorScheme", "BufWinEnter" }, {
            pattern = "*",
            group = vim.api.nvim_create_augroup("UserFoldHighlights", { clear = true }),
            callback = function()
                vim.cmd([[
      highlight! Folded guibg=NONE guifg=#275378
      highlight! FoldColumn guibg=NONE guifg=#7f849c
    ]])
            end,
        })

        require("nvim-treesitter")
        vim.api.nvim_create_autocmd("BufWinEnter", {
            pattern = "*.*",
            callback = function()
                print("loading view")
                vim.cmd("set foldmethod=expr")
                vim.cmd("setlocal foldmethod=expr")
                vim.cmd.loadview()
                vim.cmd("loadview")
            end,
        })
    end,
}

Here is my options.lua file where the save is setup.

vim.cmd("let g:netrw_liststyle = 3")

vim.api.nvim_create_autocmd("ColorScheme", {
    command = [[highlight CursorLine gui=bold guibg=NONE cterm=underline]],
})

local opt = vim.opt

opt.showtabline = 0

opt.relativenumber = true
opt.number = true

opt.tabstop = 2 -- 2 spaces for tabs (prettier default)
opt.shiftwidth = 2 -- 2 spaces for indent width
opt.expandtab = true -- expand tab to spaces
opt.autoindent = true -- copy indent from current line when starting new one

opt.wrap = false

opt.ignorecase = true
opt.smartcase = true

opt.cursorline = true

opt.termguicolors = true
opt.background = "dark"
opt.signcolumn = "yes"

opt.backspace = "indent,eol,start"

opt.clipboard:append("unnamedplus")

opt.splitright = true
opt.splitbelow = true

opt.swapfile = false

local autocmd = vim.api.nvim_create_autocmd

autocmd("BufWinLeave", {
    pattern = "*.*",
    callback = function()
        vim.cmd.mkview()
    end,
})

I am unsure why it workds when I manualy run :loadview and can't seem to figure out a solution.

1
  • are you sure that pattern=*.* is correct in autocmd? Can you check if your autocmd is being executed? Check this out stackoverflow.com/a/77180744/6482931 I would put autocmd code in some other file, out of plugin configuration Commented Jul 14 at 11:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.