Skip to content

Commit 0fd26ff

Browse files
committed
improve: divide options into core and etc
1 parent cdc34dc commit 0fd26ff

File tree

5 files changed

+59
-72
lines changed

5 files changed

+59
-72
lines changed

runtime/lua/tree.lua

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ end
3232

3333
local 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)
@@ -202,6 +214,8 @@ end
202214
function 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 })
206220
end
207221

@@ -627,6 +641,7 @@ local function initialize()
627641
M.tree_histories = {}
628642
end
629643

644+
-- options = core + etc
630645
local 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)
659666
end
660667

661668
local 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
}
674681
end
675-
-- 一些设置没有必要传输, action_ctx/setting_ctx
682+
-- Transfer action context to server when perform action
683+
-- Transfer core options when _tree_start
676684
local 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
705717
function 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'] !=# ''

src/app/app.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,10 @@ void App::init_highlight()
6363
}
6464
}
6565

66-
void App::createTree(string &path)
66+
void App::createTree(int bufnr, string &path)
6767
{
68-
static int count = 0;
6968
auto &b = m_nvim;
7069

71-
int bufnr = b->create_buf(false, true);
72-
char name[64];
73-
sprintf(name, "Tree-%d", count);
74-
string bufname(name);
75-
b->async_buf_set_name(bufnr, bufname);
76-
count++;
77-
7870
int ns_id = b->create_namespace("tree_icon");
7971
if (path.back() == '/') // path("/foo/bar/").parent_path(); // "/foo/bar"
8072
path.pop_back();
@@ -90,9 +82,7 @@ void App::createTree(string &path)
9082

9183
b->async_buf_set_option(bufnr, "buflisted", tree.cfg.listed);
9284
nvim::Dictionary tree_cfg{
93-
{"winwidth", tree.cfg.winwidth}, {"winheight", tree.cfg.winheight}, {"split", tree.cfg.split.c_str()},
94-
{"new", tree.cfg.new_}, {"toggle", tree.cfg.toggle}, {"direction", tree.cfg.direction.c_str()},
95-
{"winrow", tree.cfg.winrow}, {"wincol", tree.cfg.wincol},
85+
{"toggle", tree.cfg.toggle},
9686
};
9787
b->execute_lua("tree.resume(...)", {m_ctx.prev_bufnr, tree_cfg});
9888
}
@@ -171,10 +161,10 @@ void App::handleRequest(nvim::NvimRPC &rpc, uint64_t msgid, const string &method
171161
m_cfgmap = args[1].as_multimap();
172162
auto *b = m_nvim;
173163

174-
auto search = m_cfgmap.find("new");
175-
if (trees.size() < 1 || (search != m_cfgmap.end() && search->second.as_bool())) {
164+
auto search = m_cfgmap.find("bufnr");
165+
if (search != m_cfgmap.end()) {
176166
// TODO: createTree时存在request和此处的response好像产生冲突
177-
createTree(path);
167+
createTree(search->second.as_uint64_t(), path);
178168
} else {
179169
// NOTE: Resume tree buffer by default.
180170
// TODO: consider to use treebufs[0]
@@ -197,10 +187,7 @@ void App::handleRequest(nvim::NvimRPC &rpc, uint64_t msgid, const string &method
197187
bufnrs.push_back(item);
198188

199189
Map tree_cfg = {
200-
{"winwidth", tree.cfg.winwidth}, {"winheight", tree.cfg.winheight},
201-
{"split", tree.cfg.split.c_str()}, {"new", tree.cfg.new_},
202-
{"toggle", tree.cfg.toggle}, {"direction", tree.cfg.direction.c_str()},
203-
{"winrow", tree.cfg.winrow}, {"wincol", tree.cfg.wincol},
190+
{"toggle", tree.cfg.toggle},
204191
};
205192

206193
b->async_execute_lua("tree.resume(...)", {bufnrs, tree_cfg});

src/app/app.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class App
1212
public:
1313
App(nvim::Nvim *, int);
1414

15-
void createTree(string &path);
15+
void createTree(int bufnr, string &path);
1616
void handleNvimNotification(const string &method, const vector<nvim::Object> &args);
1717
void handleRequest(nvim::NvimRPC &rpc, uint64_t msgid, const string &method, const vector<nvim::Object> &args);
1818
void init_highlight();

src/app/column.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -367,20 +367,10 @@ void Config::update(const Map &ctx)
367367
// cout << __FUNCTION__ << k << " type: "<< type_name(v)<< endl;
368368
if (k == "auto_recursive_level") {
369369
auto_recursive_level = v.as_int64_t();
370-
} else if (k == "wincol") {
371-
wincol = v.as_uint64_t();
372-
} else if (k == "winheight") {
373-
winheight = v.as_int64_t();
374-
} else if (k == "winrow") {
375-
winrow = v.as_uint64_t();
376-
} else if (k == "winwidth") {
377-
winwidth = v.as_uint64_t();
378370
} else if (k == "auto_cd") {
379371
auto_cd = v.as_bool();
380372
} else if (k == "listed") {
381373
listed = v.as_bool();
382-
} else if (k == "new") {
383-
new_ = v.as_bool();
384374
} else if (k == "profile") {
385375
profile = v.as_bool();
386376
} else if (k == "show_ignored_files") {
@@ -389,10 +379,6 @@ void Config::update(const Map &ctx)
389379
toggle = v.as_bool();
390380
} else if (k == "root_marker") {
391381
root_marker = v.as_string();
392-
} else if (k == "buffer_name") {
393-
buffer_name = v.as_string();
394-
} else if (k == "direction") {
395-
direction = v.as_string();
396382
} else if (k == "ignored_files") {
397383
ignored_files = v.as_string();
398384
} else if (k == "search") {
@@ -401,10 +387,6 @@ void Config::update(const Map &ctx)
401387
session_file = v.as_string();
402388
} else if (k == "sort") {
403389
sort = v.as_string();
404-
} else if (k == "winrelative") {
405-
winrelative = v.as_string();
406-
} else if (k == "split") {
407-
split = v.as_string();
408390
} else if (k == "columns") {
409391
vector<string> tokens;
410392
ssplit(v.as_string(), tokens, ":");

src/app/column.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,7 @@ class Config
132132
string sort = "";
133133

134134
bool listed = false;
135-
string buffer_name = "default";
136-
137-
string direction = "";
138-
string split = "no"; // {"vertical", "horizontal", "no", "tab", "floating"}
139-
string winrelative = "editor";
140-
int winheight = 30;
141-
int winwidth = 50;
142-
int wincol = 0;
143-
int winrow = 0;
144-
bool new_ = false;
135+
145136
bool toggle = false;
146137

147138
int filename_colstop = 40;

0 commit comments

Comments
 (0)