Skip to content

Commit adfb3f8

Browse files
committed
initial commit
0 parents  commit adfb3f8

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2021 cseickel (https://github.com/cseickel) and nvim-neo-tree
4+
maintaners.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Neo-tree.nvim: Example source
2+
3+
This is just an example of how build a custom source for [Neo-tree](https://github.com/nvim-neo-tree/neo-tree.nvim)
4+
as an external plugin.
5+
6+
7+
## Quickstart
8+
9+
10+
```lua
11+
use {
12+
"nvim-neo-tree/neo-tree.nvim",
13+
branch = "v2.x",
14+
requires = {
15+
"nvim-lua/plenary.nvim",
16+
"kyazdani42/nvim-web-devicons",
17+
"MunifTanjim/nui.nvim",
18+
"nvim-neo-tree/example-source" -- <-- THIS IS US! Add This line to your existing config.
19+
},
20+
config = function ()
21+
require("neo-tree").setup({
22+
sources = {
23+
"filesystem",
24+
"buffers",
25+
"git_status",
26+
"example" -- <-- external sources need to be a fully qualified path to the module
27+
--"my.name.example" <-- Feel free to add to your folder structure to create a namespace,
28+
-- The name of the modeul will be the last part, or whatever your module
29+
-- experts as the `name` field.
30+
},
31+
example = {
32+
-- The config for your source goes here. This is the same as any other source, plus whatever
33+
-- special config options you add.
34+
--window = {...}
35+
--renderers = { ..}
36+
--etc
37+
},
38+
})
39+
40+
vim.cmd([[nnoremap \ :Neotree reveal<cr>]])
41+
end
42+
}
43+
```
44+
45+
After installing, run:
46+
```
47+
:Neotree example
48+
```
49+
50+
...to see the example source in all it's glory!

lua/example/commands.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--This file should contain all commands meant to be used by mappings.
2+
local cc = require("neo-tree.sources.common.commands")
3+
4+
local vim = vim
5+
6+
local M = {}
7+
8+
M.example_command = function(state)
9+
local tree = state.tree
10+
local node = tree:get_node()
11+
local id = node:get_id()
12+
local name = node.name
13+
print(string.format("example_command: id=%s, name=%s", id, name))
14+
end
15+
16+
M.show_debug_info = function(state)
17+
print(vim.inspect(state))
18+
end
19+
20+
cc._add_common_commands(M)
21+
return M

lua/example/components.lua

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- This file contains the built-in components. Each componment is a function
2+
-- that takes the following arguments:
3+
-- config: A table containing the configuration provided by the user
4+
-- when declaring this component in their renderer config.
5+
-- node: A NuiNode object for the currently focused node.
6+
-- state: The current state of the source providing the items.
7+
--
8+
-- The function should return either a table, or a list of tables, each of which
9+
-- contains the following keys:
10+
-- text: The text to display for this item.
11+
-- highlight: The highlight group to apply to this text.
12+
13+
local highlights = require("neo-tree.ui.highlights")
14+
local common = require("neo-tree.sources.common.components")
15+
16+
local M = {}
17+
18+
M.custom = function(config, node, state)
19+
local text = node.extra.custom_text or ""
20+
local highlight = highlights.DIM_TEXT
21+
return {
22+
text = text .. " ",
23+
highlight = highlight,
24+
}
25+
end
26+
27+
M.icon = function(config, node, state)
28+
local icon = config.default or " "
29+
local padding = config.padding or " "
30+
local highlight = config.highlight or highlights.FILE_ICON
31+
if node.type == "directory" then
32+
highlight = highlights.DIRECTORY_ICON
33+
if node:is_expanded() then
34+
icon = config.folder_open or "-"
35+
else
36+
icon = config.folder_closed or "+"
37+
end
38+
elseif node.type == "file" then
39+
local success, web_devicons = pcall(require, "nvim-web-devicons")
40+
if success then
41+
local devicon, hl = web_devicons.get_icon(node.name, node.ext)
42+
icon = devicon or icon
43+
highlight = hl or highlight
44+
end
45+
end
46+
return {
47+
text = icon .. padding,
48+
highlight = highlight,
49+
}
50+
end
51+
52+
M.name = function(config, node, state)
53+
local highlight = config.highlight or highlights.FILE_NAME
54+
if node.type == "directory" then
55+
highlight = highlights.DIRECTORY_NAME
56+
end
57+
if node:get_depth() == 1 then
58+
highlight = highlights.ROOT_NAME
59+
end
60+
return {
61+
text = node.name,
62+
highlight = highlight,
63+
}
64+
end
65+
66+
return vim.tbl_deep_extend("force", common, M)

lua/example/init.lua

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--This file should have all functions that are in the public api and either set
2+
--or read the state of this source.
3+
4+
local vim = vim
5+
local renderer = require("neo-tree.ui.renderer")
6+
local manager = require("neo-tree.sources.manager")
7+
local events = require("neo-tree.events")
8+
9+
local M = { name = "example" }
10+
11+
---Navigate to the given path.
12+
---@param path string Path to navigate to. If empty, will navigate to the cwd.
13+
M.navigate = function(state, path)
14+
if path == nil then
15+
path = vim.fn.getcwd()
16+
end
17+
state.path = path
18+
19+
-- Do something useful here to get items
20+
local items = {
21+
{
22+
id = "1",
23+
name = "root",
24+
type = "directory",
25+
children = {
26+
{
27+
id = "1.1",
28+
name = "child1",
29+
type = "directory",
30+
children = {
31+
{
32+
id = "1.1.1",
33+
name = "child1.1 (you'll need a custom renderer to display this properly)",
34+
type = "custom",
35+
extra = { custom_text = "HI!" },
36+
},
37+
{
38+
id = "1.1.2",
39+
name = "child1.2",
40+
type = "file"
41+
},
42+
},
43+
},
44+
},
45+
},
46+
}
47+
renderer.show_nodes(items, state)
48+
end
49+
50+
---Configures the plugin, should be called before the plugin is used.
51+
---@param config table Configuration table containing any keys that the user
52+
--wants to change from the defaults. May be empty to accept default values.
53+
M.setup = function(config, global_config)
54+
-- You most likely want to use this function to subscribe to events
55+
if config.use_libuv_file_watcher then
56+
manager.subscribe(M.name, {
57+
event = events.FS_EVENT,
58+
handler = function(args)
59+
manager.refresh(M.name)
60+
end,
61+
})
62+
end
63+
end
64+
65+
return M

0 commit comments

Comments
 (0)