aboutsummaryrefslogtreecommitdiff
path: root/.config/nvim/lua/plugins/nvim-tree.lua
blob: 8afa105349d06ceff85798ca5078c210c2898564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
local state = require "state"

local function natural_cmp(_left, _right)
	local left = _left.name:lower()
	local right = _right.name:lower()

	if left == right then
		return false
	end

    local is_l_dir = _left.type ~= "directory"
    local is_r_dir = _right.type ~= "directory"
    if ((is_l_dir or is_r_dir) and not (is_l_dir and is_r_dir)) then
        return is_r_dir
    end

	for i = 1, math.max(string.len(left), string.len(right)), 1 do
		local l = string.sub(left, i, -1)
		local r = string.sub(right, i, -1)

		if type(tonumber(string.sub(l, 1, 1))) == "number" and type(tonumber(string.sub(r, 1, 1))) == "number" then
			local l_number = tonumber(string.match(l, "^[0-9]+"))
			local r_number = tonumber(string.match(r, "^[0-9]+"))

			if l_number ~= r_number then
				return l_number < r_number
			end
		elseif string.sub(l, 1, 1) ~= string.sub(r, 1, 1) then
			return l < r
		end
	end
end

return {
    "nvim-tree/nvim-tree.lua",
    opts = {
        sort_by = function (nodes)
            table.sort(nodes, natural_cmp)
        end,
        view = {
            width = 30,
        },
        renderer = {
            root_folder_label = function()
                return ".."
            end,
            indent_markers = {
                enable = true,
            },
            icons = {
                show = {
                    folder_arrow = false,
                },
                glyphs = {
                    git = {
                        unstaged = "-",
                        staged = "+",
                        unmerged = "",
                        renamed = "󰏪",
                        untracked = "",
                        deleted = "󰆴",
                        ignored = "",
                    },
                },
            },
        },
        filters = {
            git_ignored = false,
        },
    },
    config = function(_, opts)
        local api = require "nvim-tree.api"
        local Event = api.events.Event

        api.events.subscribe(Event.Ready, function()
            state.nvim_tree_root = api.tree.get_nodes().absolute_path
        end)

        api.events.subscribe(Event.TreeRendered, function()
            state.nvim_tree_root = api.tree.get_nodes().absolute_path
        end)

        require("nvim-tree").setup(vim.tbl_extend("force", opts, {
            -- on_attach = function ()
            --     local function opts(desc)
            --       return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
            --     end
            --
            --     local function edit_or_open()
            --       local node = api.tree.get_node_under_cursor()
            --
            --       if node.nodes ~= nil then
            --         -- expand or collapse folder
            --         api.node.open.edit()
            --       else
            --         -- open file
            --         api.node.open.edit()
            --         -- Close the tree if file was opened
            --         api.tree.close()
            --       end
            --     end
            --
            --     -- open as vsplit on current node
            --     local function vsplit_preview()
            --       local node = api.tree.get_node_under_cursor()
            --
            --       if node.nodes ~= nil then
            --         -- expand or collapse folder
            --         api.node.open.edit()
            --       else
            --         -- open file as vsplit
            --         api.node.open.vertical()
            --       end
            --
            --       -- Finally refocus on tree if it was lost
            --       api.tree.focus()
            --     end
            --
            --     vim.keymap.set("n", "l", edit_or_open,          opts("Edit Or Open"))
            --     vim.keymap.set("n", "h", api.tree.close,        opts("Close"))
            -- end
            --
        }))
    end,
    dependencies = {
        "nvim-tree/nvim-web-devicons",
    },
    keys = {
        { "<Leader>e", "<cmd>NvimTreeToggle<CR>", desc = "Toggle nvim-tree" },
    },
}