3232
3333local M = {}
3434
35+ local default_etc_options = {
36+ winheight = 30 ,
37+ winwidth = 50 ,
38+ split = ' no' , -- {"vertical", "horizontal", "no", "tab", "floating"}
39+ winrelative = ' editor' ,
40+ buffer_name = ' default' ,
41+ direction = ' ' ,
42+ search = ' ' ,
43+ new = false ,
44+ }
3545--- Resume tree window.
3646-- If the window corresponding to bufnrs is available, goto it;
3747-- otherwise, create a new window.
@@ -71,36 +81,38 @@ function M.resume(bufnrs, cfg)
7181 end
7282
7383 local bufnr = treebufs [1 ]
84+ local etc = M .etc_options [bufnr ]
7485 local resize_cmd , str
7586 -- local no_split = false
7687 -- if cfg.split == 'no' or cfg.split == 'tab' or cfg.split == 'floating' then
7788 -- no_split = true
7889 -- end
7990 local vertical = ' '
8091 local command = ' sbuffer'
81- if cfg .split == ' tab' then
92+ if etc .split == ' tab' then
8293 cmd ' tabnew'
8394 end
84- if cfg .split == ' vertical' then
95+ if etc .split == ' vertical' then
8596 vertical = ' vertical'
86- resize_cmd = string.format (' vertical resize %d' , cfg [ ' winwidth' ] )
87- elseif cfg .split == ' horizontal' then
88- resize_cmd = string.format (' resize %d' , cfg .winheight )
89- elseif cfg .split == ' floating' then
97+ resize_cmd = string.format (' vertical resize %d' , etc . winwidth )
98+ elseif etc .split == ' horizontal' then
99+ resize_cmd = string.format (' resize %d' , etc .winheight )
100+ elseif etc .split == ' floating' then
90101 local winid = a .nvim_open_win (bufnr , true , {
91102 relative = ' editor' ,
92- row = cfg .winrow ,
93- col = cfg .wincol ,
94- width = cfg .winwidth ,
95- height = cfg .winheight ,
103+ anchor = ' NW' ,
104+ row = 0 , -- etc.winrow
105+ col = 0 , -- etc.wincol
106+ width = etc .winwidth ,
107+ height = etc .winheight ,
96108 })
97109 else
98110 command = ' buffer'
99111 end
100112
101- if cfg .split ~= ' floating' then
113+ if etc .split ~= ' floating' then
102114 local direction = ' topleft'
103- if cfg .direction == ' botright' then
115+ if etc .direction == ' botright' then
104116 direction = ' botright'
105117 end
106118 str = string.format (" silent keepalt %s %s %s %d" , direction , vertical , command , bufnr )
202214function M .buf_attach (buf )
203215 a .nvim_buf_attach (buf , false , { on_detach = function ()
204216 rpcrequest (' function' , {" on_detach" , buf }, true )
217+ M .alive_buf_cnt = M .alive_buf_cnt - 1
218+ M .etc_options [buf ] = nil
205219 end })
206220end
207221
@@ -627,6 +641,7 @@ local function initialize()
627641 M .tree_histories = {}
628642end
629643
644+ -- options = core + etc
630645local function user_var_options ()
631646 return {
632647 wincol = math.modf (vim .o .columns / 4 ),
@@ -637,25 +652,17 @@ function user_options()
637652 return vim .tbl_extend (' force' , {
638653 auto_cd = false ,
639654 auto_recursive_level = 0 ,
640- buffer_name = ' default' ,
641655 columns = ' mark:indent:icon:filename:size' ,
642- direction = ' ' ,
643656 ignored_files = ' .*' ,
644657 listed = false ,
645- new = false ,
646658 profile = false ,
647659 resume = false ,
648660 root_marker = ' [in]: ' ,
649- search = ' ' ,
650661 session_file = ' ' ,
651662 show_ignored_files = false ,
652- split = ' no' ,
653663 sort = ' filename' ,
654664 toggle = false ,
655- winheight = 30 ,
656- winrelative = ' editor' ,
657- winwidth = 90 ,
658- }, user_var_options ())
665+ }, user_var_options (), default_etc_options )
659666end
660667
661668local function internal_options ()
@@ -665,17 +672,18 @@ local function internal_options()
665672 cmd (' delmarks >' )
666673 return {
667674 cursor = fn .line (' .' ),
668- drives = {},
675+ -- drives={},
669676 prev_bufnr = fn .bufnr (' %' ),
670677 prev_winid = fn .win_getid (),
671678 visual_start = s ,
672679 visual_end = e ,
673680 }
674681end
675- -- 一些设置没有必要传输, action_ctx/setting_ctx
682+ -- Transfer action context to server when perform action
683+ -- Transfer core options when _tree_start
676684local function init_context (user_context )
677685 local buffer_name = user_context .buffer_name or ' default'
678- local context = user_var_options ()
686+ local context = {} -- TODO: move user_var_options to etc options
679687 local custom = vim .deepcopy (custom .get ())
680688 -- NOTE: Avoid empty custom.column being converted to vector
681689 if vim .tbl_isempty (custom .column ) then
@@ -702,13 +710,32 @@ end
702710---- ---------------- end of init.vim --------------------
703711
704712---- ---------------- start of tree.vim --------------------
713+ -- NOTE: The buffer creation is done by the lua side
714+ M .alive_buf_cnt = 0
715+ M .etc_options = {}
716+ local count = 0
705717function start (paths , user_context )
706718 initialize ()
707719 local context = init_context (user_context )
708720 local paths = fn .map (paths , " fnamemodify(v:val, ':p')" )
709721 if # paths == 0 then
710722 paths = {fn .expand (' %:p:h' )}
711723 end
724+ if M .alive_buf_cnt < 1 or user_context .new then
725+ local buf = a .nvim_create_buf (false , true )
726+ local bufname = " Tree-" .. tostring (count )
727+ a .nvim_buf_set_name (buf , bufname );
728+ count = count + 1
729+ M .alive_buf_cnt = M .alive_buf_cnt + 1
730+ local etc_opts = vim .deepcopy (default_etc_options )
731+ for k , v in pairs (default_etc_options ) do
732+ if context [k ] then
733+ etc_opts [k ] = context [k ]
734+ end
735+ end
736+ M .etc_options [buf ] = etc_opts
737+ context .bufnr = buf
738+ end
712739 rpcrequest (' _tree_start' , {paths , context }, false )
713740 -- TODO: 检查 search 是否存在
714741 -- if context['search'] !=# ''
0 commit comments