Skip to content

Commit 0fd1f6e

Browse files
committed
feat(api): add public API module
1 parent 8dc2144 commit 0fd1f6e

File tree

4 files changed

+289
-140
lines changed

4 files changed

+289
-140
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ Basic commands:
9898

9999
`:NvimTreeCollapse` Collapses the nvim-tree recursively.
100100

101+
## Api
102+
103+
nvim-tree exposes a public api; see [:help nvim-tree-api](doc/nvim-tree-lua.txt). This is a stable non breaking api.
104+
101105
## Mappings
102106

103107
nvim-tree comes with number of mappings; for default mappings please see [:help nvim-tree-default-mappings](doc/nvim-tree-lua.txt), for way of configuring mappings see [:help nvim-tree-mappings](doc/nvim-tree-lua.txt)

doc/nvim-tree-lua.txt

Lines changed: 144 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ CONTENTS *nvim-tree*
1010
3. Commands |nvim-tree-commands|
1111
4. Setup/Configuration |nvim-tree-setup|
1212
4.1 Vinegar Style |nvim-tree-vinegar|
13-
5. Mappings |nvim-tree-mappings|
14-
6. Highlight Groups |nvim-tree-highlight|
15-
7. Events |nvim-tree-events|
16-
7.1 Available Events |nvim-tree.events|
17-
8. Bookmarks |nvim-tree-bookmarks|
13+
5. Api |nvim-tree-api|
14+
6. Mappings |nvim-tree-mappings|
15+
7. Highlight Groups |nvim-tree-highlight|
16+
8. Events |nvim-tree-events|
17+
9. Bookmarks |nvim-tree-bookmarks|
1818

1919
==============================================================================
2020
1. INTRODUCTION *nvim-tree-introduction*
@@ -944,7 +944,99 @@ You'll also need to set |nvim-tree.hijack_netrw| to `true` during setup.
944944
A good functionnality to enable is |nvim-tree.hijack_directories|.
945945

946946
==============================================================================
947-
5. MAPPINGS *nvim-tree-mappings*
947+
5. API *nvim-tree-api*
948+
949+
Nvim-tree's public API can be used to access features.
950+
>
951+
local nt_api = require("nvim-tree.api")
952+
953+
nt_api.tree.toggle()
954+
<
955+
This module exposes stable functionalities, it is advised to use this in order
956+
to avoid breaking configurations due to internal breaking changes.
957+
958+
The api is separated in multiple modules, which can be accessed with
959+
`require("nvim-tree.api").moduleName.functionality`.
960+
961+
Functions that needs a tree node parameter are exposed with an abstraction
962+
that injects the node from the cursor position in the tree when calling
963+
the function. It will use the node you pass as an argument in priority if it
964+
exists.
965+
966+
- api.tree: *nvim-tree.api.tree*
967+
- open `(path?: string)`
968+
- close
969+
- toggle `(find_file?: bool, no_focus?: bool, path?: string)`
970+
- focus
971+
- reload
972+
- change_root `(path: string)`
973+
- change_root_to_parent
974+
- get_node_under_cursor
975+
- find_file `(filename: string)`
976+
- search_node
977+
- collapse_all `(keep_buffers?: bool)`
978+
- expand_all
979+
- toggle_gitignore_filter
980+
- toggle_custom_filter
981+
- toggle_hidden_filter
982+
- toggle_help
983+
984+
- api.fs: *nvim-tree.api.fs*
985+
- create
986+
- remove
987+
- trash
988+
- rename
989+
- rename_sub
990+
- copy
991+
- cut
992+
- paste
993+
- print_clipboard
994+
- copy.absolute_path
995+
- copy.filename
996+
- copy.relative_path
997+
998+
- api.node: *nvim-tree.api.node*
999+
- open.edit
1000+
- open.vertical
1001+
- open.horizontal
1002+
- open.tab
1003+
- open.preview
1004+
- show_info_popup
1005+
- run.cmd
1006+
- run.system
1007+
- navigate.sibling.next
1008+
- navigate.sibling.prev
1009+
- navigate.sibling.first
1010+
- navigate.sibling.last
1011+
- navigate.parent
1012+
- navigate.parent_close
1013+
- navigate.git.next
1014+
- navigate.git.prev
1015+
- navigate.diagnostics.next
1016+
- navigate.diagnostics.prev
1017+
1018+
- api.git: *nvim-tree.api.git*
1019+
- reload
1020+
1021+
- api.events: *nvim-tree.api.events*
1022+
- subscribe `(eventType: Event, callback: function(...args))`
1023+
- Event (enum type, please see |nvim_tree_events_kind|)
1024+
1025+
- api.live_filter: *nvim-tree.api.live_filter*
1026+
- start
1027+
- clear
1028+
1029+
- api.marks: *nvim-tree.api.marks*
1030+
- get
1031+
- list
1032+
- toggle
1033+
- bulk.move
1034+
- navigate.next
1035+
- navigate.prev
1036+
- navigate.select
1037+
1038+
==============================================================================
1039+
6. MAPPINGS *nvim-tree-mappings*
9481040

9491041
The `list` option in `view.mappings.list` is a table of
9501042

@@ -1071,7 +1163,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
10711163
} -- END_DEFAULT_MAPPINGS
10721164
<
10731165
==============================================================================
1074-
6. HIGHLIGHT GROUPS *nvim-tree-highlight*
1166+
7. HIGHLIGHT GROUPS *nvim-tree-highlight*
10751167

10761168
All the following highlight groups can be configured by hand. Aside from
10771169
`NvimTreeWindowPicker`, it is not advised to colorize the background of these
@@ -1145,129 +1237,85 @@ NvimTreeBookmark
11451237

11461238

11471239
==============================================================================
1148-
7. EVENTS *nvim-tree-events*
1240+
8. EVENTS *nvim-tree-events*
11491241

11501242
|nvim_tree_events|
11511243

11521244
nvim-tree will dispatch events whenever an action is made. These events can be
11531245
subscribed to through handler functions. This allows for even further
11541246
customization of nvim-tree.
11551247

1156-
A handler for an event is just a function which receives one argument -
1157-
the payload of the event. The payload is different for each event type. Refer
1248+
A handler for an event is just a function which receives one argument, the
1249+
payload of the event. The payload is different for each event type. Refer
11581250
to |nvim_tree_registering_handlers| for more information.
1159-
<
11601251

11611252
|nvim_tree_registering_handlers|
11621253

1163-
Handlers are registered by calling the `on_*` functions available in the
1164-
`require('nvim-tree.events')` module. See |nvim-tree.events|.
1254+
Handlers are registered by calling the `events.subscribe` function available in the
1255+
`require("nvim-tree.api")` module.
11651256

1257+
For example, registering a handler for when a node is renamed is done like this:
1258+
>
1259+
local api = require('nvim-tree.api')
11661260
1167-
For example, registering a handler for when a node is renamed is done like this: >
1168-
1169-
local events = require('nvim-tree.events')
1170-
1171-
events.on_node_renamed(function(data)
1261+
api.events.subscribe(Event.NodeRenamed, function(data)
11721262
print("Node renamed from " .. data.old_name .. " to " .. data.new_name)
11731263
end)
1264+
<
11741265

1175-
==============================================================================
1176-
7.1 Lua module: nvim-tree.events *nvim-tree.events*
1266+
|nvim_tree_events_kind|
1267+
1268+
You can access the event enum with:
1269+
>
1270+
local Event = require('nvim-tree.api').events.Event
1271+
<
1272+
Here is the list of available variant of this enum:
11771273

1178-
*nvim-tree.events.on_nvim_tree_ready()*
1179-
on_nvim_tree_ready({handler})
1180-
Registers a handler for when NvimTree has been initialized.
1274+
- Event.Ready
1275+
When NvimTree has been initialized
1276+
• Note: Handler takes no parameter.
11811277

1182-
Parameters: ~
1183-
{handler} `{function}` Handler function, with the
1184-
signature `function()`.
1278+
- Event.TreeOpen
1279+
• Note: Handler takes no parameter.
11851280

1186-
*nvim-tree.events.on_node_renamed()*
1187-
on_node_renamed({handler})
1188-
Registers a handler for when a node is renamed.
1189-
• Note: A node can either be a file or a directory.
1281+
- Event.TreeClose
1282+
• Note: Handler takes no parameter.
11901283

1191-
Parameters: ~
1192-
{handler} `{function}` Handler function, with the
1193-
signature `function(payload)`.
1194-
payload: ~
1284+
- Event.Resize - When NvimTree is resized.
1285+
handler parameters: ~
1286+
size: `number` size of the view in columns.
1287+
1288+
- Event.NodeRenamed
1289+
• Note: A node can either be a file or a directory.
1290+
handler parameters: ~
11951291
{old_name} `{string}` Absolute path to the old node location.
11961292
{new_name} `{string}` Absolute path to the new node location.
11971293

1198-
*nvim-tree.events.on_file_created()*
1199-
on_file_created({handler})
1200-
Registers a handler for when a file is created.
1201-
1202-
Parameters: ~
1203-
{handler} `{function}` Handler function, with the
1204-
signature `function(payload)`.
1205-
payload: ~
1294+
- Event.FileCreated
1295+
handler parameters: ~
12061296
{fname} `{string}` Absolute path to the created file
12071297

1208-
*nvim-tree.events.on_file_removed()*
1209-
on_file_removed({handler})
1210-
Registers a handler for when a file is removed.
1211-
1212-
Parameters: ~
1213-
{handler} `{function}` Handler function, with the
1214-
signature `function(payload)`.
1215-
payload: ~
1298+
- Event.FileRemoved
1299+
handler parameters: ~
12161300
{fname} `{string}` Absolute path to the removed file.
12171301

1218-
*nvim-tree.events.on_folder_created()*
1219-
on_folder_created({handler})
1220-
Registers a handler for when a folder is created.
1221-
1222-
Parameters: ~
1223-
{handler} `{function}` Handler function, with the
1224-
signature `function(payload)`.
1225-
payload: ~
1302+
- Event.FolderCreated
1303+
handler parameters: ~
12261304
{folder_name} `{string}` Absolute path to the created folder.
12271305

1228-
*nvim-tree.events.on_folder_removed()*
1229-
on_folder_removed({handler})
1230-
Registers a handler for when a folder is removed.
1231-
1232-
Parameters: ~
1233-
{handler} `{function}` Handler function, with the
1234-
signature `function(payload)`.
1235-
payload: ~
1306+
- Event.FolderRemoved
1307+
handler parameters: ~
12361308
{folder_name} `{string}` Absolute path to the removed folder.
12371309

1238-
*nvim-tree.events.on_tree_open()*
1239-
on_tree_open({handler})
1240-
Registers a handler for when NvimTree is opened.
1241-
1242-
Parameters: ~
1243-
{handler} `{function}` Handler function, with the
1244-
signature `function()`.
1245-
1246-
*nvim-tree.events.on_tree_close()*
1247-
on_tree_close({handler})
1248-
Registers a handler for when NvimTree is closed.
1249-
1250-
Parameters: ~
1251-
{handler} `{function}` Handler function, with the
1252-
signature `function()`.
1253-
1254-
*nvim-tree.events.on_tree_resize()*
1255-
on_tree_resize({handler})
1256-
Registers a handler for when NvimTree is resized.
1257-
1258-
Parameters: ~
1259-
{handler} `{function}` Handler function, with the
1260-
signature `function(size)`.
1261-
12621310
==============================================================================
1263-
8. BOOKMARKS *nvim-tree-bookmarks*
1311+
9. BOOKMARKS *nvim-tree-bookmarks*
12641312

12651313
You can toggle marks on files/folders with
1266-
`require("nvim-tree.marks").toggle_mark(node)` which is bound to `m` by
1314+
`require("nvim-tree.api").marks.toggle(node)` which is bound to `m` by
12671315
default.
12681316

12691317
To get the list of marked paths, you can call
1270-
`require("nvim-tree.marks").get_marks()`. This will return `{node}`.
1318+
`require("nvim-tree.api").marks.list()`. This will return `{node}`.
12711319

12721320
*nvim-tree.bookmarks.navigation*
12731321

@@ -1277,8 +1325,8 @@ want to focus the tree view each time we wish to switch to another mark.
12771325
This requires binding bookmark navigation yourself.
12781326

12791327
-- in your lua configuration
1280-
vim.keymap.set("n", "<leader>mn", require("nvim-tree.marks.navigation").next)
1281-
vim.keymap.set("n", "<leader>mp", require("nvim-tree.marks.navigation").prev)
1282-
vim.keymap.set("n", "<leader>ms", require("nvim-tree.marks.navigation").select)
1328+
vim.keymap.set("n", "<leader>mn", require("nvim-tree.api").marks.navigate.next)
1329+
vim.keymap.set("n", "<leader>mp", require("nvim-tree.api").marks.navigate.prev)
1330+
vim.keymap.set("n", "<leader>ms", require("nvim-tree.api").marks.navigate.select)
12831331

12841332
vim:tw=78:ts=4:sw=4:et:ft=help:norl:

0 commit comments

Comments
 (0)