1 files changed,
34 insertions(+),
24 deletions(-)
Author:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2026-08-29 15:24:31 +0300
Authored at:
2026-08-28 22:49:34 +0300
Change ID:
mqwtkurvozwzrxkmnqvoxzvxmtzrzrpn
Parent:
87c9108
M
plugin/viye.lua
··· 46 46 local parts, lnum = {}, cmd_lnum 47 47 while lnum do 48 48 local l = vim.fn.getline(lnum) 49 - local line = l:match("^%s*[+-] (.*)") or vim.trim(l) 49 + local line = (l:match("^%s*[+-] (.*)") or vim.trim(l)):gsub("/$", "") 50 50 local words = tokenize(line) 51 51 for i = #words, 1, -1 do table.insert(parts, 1, words[i]) end 52 52 lnum = parent_of(lnum) ··· 70 70 return body 71 71 end 72 72 73 -local function run(cmd_lnum) 74 - local cmd = ancestors(cmd_lnum) 75 - if #cmd == 0 then return end 76 - table.insert(cmd, 1, "viye") 77 - local body = body_of(cmd_lnum) 78 - if #body > 0 then vim.list_extend(vim.list_extend(cmd, { "--" }), body) end 79 - local res = vim.system(cmd, { text = true }):wait(2000) 80 - if res.code ~= 0 then 81 - vim.notify(vim.trim(res.stderr), vim.log.levels.ERROR) 82 - return 83 - end 84 - return res.stdout 85 -end 86 - 87 73 local function insert_output(cmd_lnum, out) 88 74 local pad = string.rep(" ", indent(vim.fn.getline(cmd_lnum)) + 2) 89 75 local lines = vim.split(out, "\n", { trimempty = true }) 90 76 for i, l in ipairs(lines) do lines[i] = pad .. l end 91 - vim.fn.append(cmd_lnum, lines) 77 + vim.api.nvim_buf_set_lines(0, cmd_lnum, cmd_lnum, false, lines) 92 78 end 93 79 94 80 local function toggle_bullet(cmd_lnum) ··· 97 83 end, 1))) 98 84 end 99 85 86 +local function run(cmd_lnum) 87 + local cmd = ancestors(cmd_lnum) 88 + if #cmd == 0 then return end 89 + local body = body_of(cmd_lnum) 90 + if #body > 0 then vim.list_extend(vim.list_extend(cmd, { "--" }), body) end 91 + vim.system({ "viye", unpack(cmd) }, { text = true }, function(res) 92 + vim.schedule(function() 93 + if res.code ~= 0 then 94 + vim.notify(vim.trim(res.stderr), vim.log.levels.ERROR) 95 + return 96 + end 97 + local pos = vim.api.nvim_win_get_cursor(0) 98 + local first, last = children_of(cmd_lnum) 99 + if last then vim.api.nvim_buf_set_lines(0, first - 1, last, false, {}) end 100 + if res.stdout ~= "" then insert_output(cmd_lnum, res.stdout) end 101 + toggle_bullet(cmd_lnum) 102 + local row = math.min(pos[1], vim.api.nvim_buf_line_count(0)) 103 + local line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1] or "" 104 + vim.api.nvim_win_set_cursor(0, { row, math.min(pos[2], #line) }) 105 + end) 106 + end) 107 +end 108 + 100 109 local function exec() 101 110 local lnum = vim.fn.line(".") 102 111 local line = vim.fn.getline(lnum) 103 112 local is_out = line:match("^%s*|") 104 113 local cmd_lnum = (line:match("^%s*[|:]") and parent_of(lnum)) or lnum 105 114 if not cmd_lnum or vim.fn.getline(cmd_lnum):match("^==%s") then return end 106 - local out 107 - if not is_out then 108 - out = run(cmd_lnum) 109 - if not out then return end 115 + if is_out then 116 + local pos = vim.api.nvim_win_get_cursor(0) 117 + local first, last = children_of(cmd_lnum) 118 + if last then vim.api.nvim_buf_set_lines(0, first - 1, last, false, {}) end 119 + local row = math.min(pos[1], vim.api.nvim_buf_line_count(0)) 120 + local line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1] or "" 121 + vim.api.nvim_win_set_cursor(0, { row, math.min(pos[2], #line) }) 122 + return 110 123 end 111 - local first, last = children_of(cmd_lnum) 112 - if last then vim.cmd(string.format("%d,%ddelete _", first, last)) end 113 - if out and out ~= "" then insert_output(cmd_lnum, out) end 114 - if not is_out then toggle_bullet(cmd_lnum) end 124 + run(cmd_lnum) 115 125 end 116 126 117 127 vim.filetype.add { extension = { viye = "viye" } }