Skip to content

Commit

Permalink
fix: replaced deprecated api
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuyuanp committed Aug 18, 2024
1 parent 60bb175 commit 423130c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 66 deletions.
1 change: 1 addition & 0 deletions lua/yanil/decorators.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function M.readonly(node)
return ''
end

---@diagnostic disable-next-line: unused-local
function M.space(_node)
return ' '
end
Expand Down
1 change: 1 addition & 0 deletions lua/yanil/devicons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ function M.setup_highlight()
end
end

---@diagnostic disable-next-line: unused-local
function M.setup(_opts)
M.setup_highlight()
api.nvim_command('augroup yanil_devicons_highlight')
Expand Down
46 changes: 19 additions & 27 deletions lua/yanil/git.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local vim = vim
local api = vim.api
local loop = vim.loop
local loop = vim.uv

local utils = require('yanil/utils')

Expand Down Expand Up @@ -102,15 +102,15 @@ function M.update(cwd)

local git_root

local status_callback = vim.schedule_wrap(function(code, _signal, stdout, stderr)
if code > 0 then
api.nvim_err_writeln(string.format('git status failed: %s', stderr))
local status_callback = vim.schedule_wrap(function(res)
if res.code > 0 then
api.nvim_err_writeln(string.format('git status failed: %s', res.stderr))
return
end

local state = {}

stdout = stdout or ''
local stdout = res.stdout or ''
local lines = vim.split(stdout, delimiter)
local is_rename = false
for _, line in ipairs(lines) do
Expand Down Expand Up @@ -153,30 +153,20 @@ function M.update(cwd)
end
end)

local function root_callback(code, _signal, stdout, _stderr)
if code > 0 then
---@param res vim.SystemCompleted
local function root_callback(res)
if res.code > 0 then
return
end

git_root = vim.trim(stdout) .. '/'
git_root = vim.trim(res.stdout) .. '/'

utils.spawn('git', {
args = {
'status',
'--porcelain=v2',
'-z',
},
vim.system({ 'git', 'status', '--porcelain=v2', '-z' }, {
cwd = git_root,
}, status_callback)
end

utils.spawn('git', {
args = {
'rev-parse',
'--show-toplevel',
},
cwd = cwd,
}, root_callback)
vim.system({ 'git', 'rev-parse', '--show-toplevel' }, { cwd = cwd }, root_callback)
end

function M.get_icon_and_hl(path)
Expand Down Expand Up @@ -211,15 +201,15 @@ end

function M.apply_buf(bufnr)
bufnr = bufnr or 0
local patch = api.nvim_buf_get_lines(bufnr, 0, -1, false)
if not patch or #patch == 0 or (#patch == 1 and patch[1] == '') then
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
if not lines or #lines == 0 or (#lines == 1 and lines[1] == '') then
vim.fn.execute('q')
return
end
if patch[-1] ~= ' ' then
table.insert(patch, ' ')
if lines[-1] ~= ' ' then
table.insert(lines, ' ')
end
patch = table.concat(patch, '\n')
local patch = table.concat(lines, '\n')
local output = vim.fn.system({ 'git', 'apply', '--cached' }, patch)
if vim.v.shell_error > 0 then
api.nvim_err_writeln(string.format('git apply failed: %s', output))
Expand Down Expand Up @@ -248,18 +238,20 @@ function M.jump(tree, linenr, step)
local start, total = step, step * total_lines
for i = start, total, step do
local index = (i + linenr) % total_lines
local node = tree.root:get_node_by_index(index) -- TODO: optimaze
local node = tree.root:get_node_by_index(index)
if node.parent and M.get_icon_and_hl(node.abs_path) then
tree:go_to_node(node)
return
end
end
end

---@diagnostic disable-next-line: unused-local
function M.jump_next(tree, _node, _key, linenr)
M.jump(tree, linenr, 1)
end

---@diagnostic disable-next-line: unused-local
function M.jump_prev(tree, _node, _key, linenr)
M.jump(tree, linenr, -1)
end
Expand Down
3 changes: 3 additions & 0 deletions lua/yanil/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ end

do
for _, ft in ipairs(filetypes) do
---@diagnostic disable-next-line: assign-type-mismatch
Node['is_' .. ft] = function(n)
return n.ntype == ft
end
Expand Down Expand Up @@ -85,6 +86,7 @@ local classes = {

function DirNode:init()
local stat = loop.fs_stat(self.abs_path)
assert(stat)
self.last_modified = stat.mtime.sec
self.entries = {}

Expand Down Expand Up @@ -115,6 +117,7 @@ function LinkNode:init()

if self.link_to then
local stat = loop.fs_stat(self.link_to)
assert(stat)
self.link_to_type = stat.type
end
end
Expand Down
4 changes: 2 additions & 2 deletions lua/yanil/sections/tree.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local vim = vim
local api = vim.api
local loop = vim.loop
local uv = vim.uv

local Section = require('yanil/section')
local nodelib = require('yanil/node')
Expand Down Expand Up @@ -35,7 +35,7 @@ function M:setup(opts)
end

function M:set_cwd(cwd)
cwd = cwd or loop.cwd()
cwd = cwd or uv.cwd()
if not vim.endswith(cwd, utils.path_sep) then
cwd = cwd .. utils.path_sep
end
Expand Down
37 changes: 0 additions & 37 deletions lua/yanil/utils.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local vim = vim
local api = vim.api
local loop = vim.loop

local validate = vim.validate

Expand Down Expand Up @@ -38,42 +37,6 @@ function M.new_stack()
return stack
end

function M.spawn(path, options, callback)
local stdout = loop.new_pipe(false)
local stderr = loop.new_pipe(false)

local stdout_chunk, stderr_chunk

local handle
handle = loop.spawn(
path,
vim.tbl_deep_extend('keep', options, {
stdio = { nil, stdout, stderr },
}),
function(code, signal)
pcall(callback, code, signal, stdout_chunk, stderr_chunk)
handle:close()
stdout:close()
stderr:close()
end
)

stdout:read_start(function(err, data)
assert(not err, err)
if not data then
return
end
stdout_chunk = (stdout_chunk or '') .. data
end)
stderr:read_start(function(err, data)
assert(not err, err)
if not data then
return
end
stderr_chunk = (stderr_chunk or '') .. data
end)
end

function M.is_binary(path)
validate({ path = { path, 'string' } })

Expand Down

0 comments on commit 423130c

Please sign in to comment.