Skip to content

Commit

Permalink
improve: divide options into core and etc
Browse files Browse the repository at this point in the history
  • Loading branch information
zgpio committed Oct 18, 2020
1 parent cdc34dc commit 0fd26ff
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 72 deletions.
75 changes: 51 additions & 24 deletions runtime/lua/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ end

local M = {}

local default_etc_options = {
winheight=30,
winwidth=50,
split='no', -- {"vertical", "horizontal", "no", "tab", "floating"}
winrelative='editor',
buffer_name='default',
direction='',
search='',
new=false,
}
--- Resume tree window.
-- If the window corresponding to bufnrs is available, goto it;
-- otherwise, create a new window.
Expand Down Expand Up @@ -71,36 +81,38 @@ function M.resume(bufnrs, cfg)
end

local bufnr = treebufs[1]
local etc = M.etc_options[bufnr]
local resize_cmd, str
-- local no_split = false
-- if cfg.split == 'no' or cfg.split == 'tab' or cfg.split == 'floating' then
-- no_split = true
-- end
local vertical = ''
local command = 'sbuffer'
if cfg.split == 'tab' then
if etc.split == 'tab' then
cmd 'tabnew'
end
if cfg.split == 'vertical' then
if etc.split == 'vertical' then
vertical = 'vertical'
resize_cmd = string.format('vertical resize %d', cfg['winwidth'])
elseif cfg.split == 'horizontal' then
resize_cmd = string.format('resize %d', cfg.winheight)
elseif cfg.split == 'floating' then
resize_cmd = string.format('vertical resize %d', etc.winwidth)
elseif etc.split == 'horizontal' then
resize_cmd = string.format('resize %d', etc.winheight)
elseif etc.split == 'floating' then
local winid = a.nvim_open_win(bufnr, true, {
relative='editor',
row=cfg.winrow,
col=cfg.wincol,
width=cfg.winwidth,
height=cfg.winheight,
anchor='NW',
row=0, -- etc.winrow
col=0, -- etc.wincol
width=etc.winwidth,
height=etc.winheight,
})
else
command = 'buffer'
end

if cfg.split ~= 'floating' then
if etc.split ~= 'floating' then
local direction = 'topleft'
if cfg.direction == 'botright' then
if etc.direction == 'botright' then
direction = 'botright'
end
str = string.format("silent keepalt %s %s %s %d", direction, vertical, command, bufnr)
Expand Down Expand Up @@ -202,6 +214,8 @@ end
function M.buf_attach(buf)
a.nvim_buf_attach(buf, false, { on_detach = function()
rpcrequest('function', {"on_detach", buf}, true)
M.alive_buf_cnt = M.alive_buf_cnt - 1
M.etc_options[buf] = nil
end })
end

Expand Down Expand Up @@ -627,6 +641,7 @@ local function initialize()
M.tree_histories = {}
end

-- options = core + etc
local function user_var_options()
return {
wincol=math.modf(vim.o.columns/4),
Expand All @@ -637,25 +652,17 @@ function user_options()
return vim.tbl_extend('force', {
auto_cd=false,
auto_recursive_level=0,
buffer_name='default',
columns='mark:indent:icon:filename:size',
direction='',
ignored_files='.*',
listed=false,
new=false,
profile=false,
resume=false,
root_marker='[in]: ',
search='',
session_file='',
show_ignored_files=false,
split='no',
sort='filename',
toggle=false,
winheight=30,
winrelative='editor',
winwidth=90,
}, user_var_options())
}, user_var_options(), default_etc_options)
end

local function internal_options()
Expand All @@ -665,17 +672,18 @@ local function internal_options()
cmd('delmarks >')
return {
cursor=fn.line('.'),
drives={},
-- drives={},
prev_bufnr=fn.bufnr('%'),
prev_winid=fn.win_getid(),
visual_start=s,
visual_end=e,
}
end
-- 一些设置没有必要传输, action_ctx/setting_ctx
-- Transfer action context to server when perform action
-- Transfer core options when _tree_start
local function init_context(user_context)
local buffer_name = user_context.buffer_name or 'default'
local context = user_var_options()
local context = {} -- TODO: move user_var_options to etc options
local custom = vim.deepcopy(custom.get())
-- NOTE: Avoid empty custom.column being converted to vector
if vim.tbl_isempty(custom.column) then
Expand All @@ -702,13 +710,32 @@ end
-------------------- end of init.vim --------------------

-------------------- start of tree.vim --------------------
-- NOTE: The buffer creation is done by the lua side
M.alive_buf_cnt = 0
M.etc_options = {}
local count = 0
function start(paths, user_context)
initialize()
local context = init_context(user_context)
local paths = fn.map(paths, "fnamemodify(v:val, ':p')")
if #paths == 0 then
paths = {fn.expand('%:p:h')}
end
if M.alive_buf_cnt < 1 or user_context.new then
local buf = a.nvim_create_buf(false, true)
local bufname = "Tree-" .. tostring(count)
a.nvim_buf_set_name(buf, bufname);
count = count + 1
M.alive_buf_cnt = M.alive_buf_cnt + 1
local etc_opts = vim.deepcopy(default_etc_options)
for k, v in pairs(default_etc_options) do
if context[k] then
etc_opts[k] = context[k]
end
end
M.etc_options[buf] = etc_opts
context.bufnr = buf
end
rpcrequest('_tree_start', {paths, context}, false)
-- TODO: 检查 search 是否存在
-- if context['search'] !=# ''
Expand Down
25 changes: 6 additions & 19 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,10 @@ void App::init_highlight()
}
}

void App::createTree(string &path)
void App::createTree(int bufnr, string &path)
{
static int count = 0;
auto &b = m_nvim;

int bufnr = b->create_buf(false, true);
char name[64];
sprintf(name, "Tree-%d", count);
string bufname(name);
b->async_buf_set_name(bufnr, bufname);
count++;

int ns_id = b->create_namespace("tree_icon");
if (path.back() == '/') // path("/foo/bar/").parent_path(); // "/foo/bar"
path.pop_back();
Expand All @@ -90,9 +82,7 @@ void App::createTree(string &path)

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

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

Map tree_cfg = {
{"winwidth", tree.cfg.winwidth}, {"winheight", tree.cfg.winheight},
{"split", tree.cfg.split.c_str()}, {"new", tree.cfg.new_},
{"toggle", tree.cfg.toggle}, {"direction", tree.cfg.direction.c_str()},
{"winrow", tree.cfg.winrow}, {"wincol", tree.cfg.wincol},
{"toggle", tree.cfg.toggle},
};

b->async_execute_lua("tree.resume(...)", {bufnrs, tree_cfg});
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class App
public:
App(nvim::Nvim *, int);

void createTree(string &path);
void createTree(int bufnr, string &path);
void handleNvimNotification(const string &method, const vector<nvim::Object> &args);
void handleRequest(nvim::NvimRPC &rpc, uint64_t msgid, const string &method, const vector<nvim::Object> &args);
void init_highlight();
Expand Down
18 changes: 0 additions & 18 deletions src/app/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,20 +367,10 @@ void Config::update(const Map &ctx)
// cout << __FUNCTION__ << k << " type: "<< type_name(v)<< endl;
if (k == "auto_recursive_level") {
auto_recursive_level = v.as_int64_t();
} else if (k == "wincol") {
wincol = v.as_uint64_t();
} else if (k == "winheight") {
winheight = v.as_int64_t();
} else if (k == "winrow") {
winrow = v.as_uint64_t();
} else if (k == "winwidth") {
winwidth = v.as_uint64_t();
} else if (k == "auto_cd") {
auto_cd = v.as_bool();
} else if (k == "listed") {
listed = v.as_bool();
} else if (k == "new") {
new_ = v.as_bool();
} else if (k == "profile") {
profile = v.as_bool();
} else if (k == "show_ignored_files") {
Expand All @@ -389,10 +379,6 @@ void Config::update(const Map &ctx)
toggle = v.as_bool();
} else if (k == "root_marker") {
root_marker = v.as_string();
} else if (k == "buffer_name") {
buffer_name = v.as_string();
} else if (k == "direction") {
direction = v.as_string();
} else if (k == "ignored_files") {
ignored_files = v.as_string();
} else if (k == "search") {
Expand All @@ -401,10 +387,6 @@ void Config::update(const Map &ctx)
session_file = v.as_string();
} else if (k == "sort") {
sort = v.as_string();
} else if (k == "winrelative") {
winrelative = v.as_string();
} else if (k == "split") {
split = v.as_string();
} else if (k == "columns") {
vector<string> tokens;
ssplit(v.as_string(), tokens, ":");
Expand Down
11 changes: 1 addition & 10 deletions src/app/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,7 @@ class Config
string sort = "";

bool listed = false;
string buffer_name = "default";

string direction = "";
string split = "no"; // {"vertical", "horizontal", "no", "tab", "floating"}
string winrelative = "editor";
int winheight = 30;
int winwidth = 50;
int wincol = 0;
int winrow = 0;
bool new_ = false;

bool toggle = false;

int filename_colstop = 40;
Expand Down

0 comments on commit 0fd26ff

Please sign in to comment.