feat: neovim script now connects directly to socket
This commit is contained in:
parent
7e054722db
commit
ada4d35e39
1 changed files with 103 additions and 13 deletions
116
vim.lua
116
vim.lua
|
@ -1,22 +1,112 @@
|
|||
-- A script for vim to map the word under the cursor with portingtools by pressing m, and all
|
||||
-- mapped expressions in the file with M
|
||||
local function map(name)
|
||||
return vim.fn.systemlist("portingtools map", name)[1]
|
||||
local bit = require("bit")
|
||||
|
||||
local function map(sock, name, callback)
|
||||
--return vim.fn.systemlist("portingtools map", name)[1]
|
||||
local name_bytes = {
|
||||
string.char(bit.band(bit.rshift(#name, 24), 0xff)),
|
||||
string.char(bit.band(bit.rshift(#name, 16), 0xff)),
|
||||
string.char(bit.band(bit.rshift(#name, 8), 0xff)),
|
||||
string.char(bit.band(#name, 0xff)),
|
||||
}
|
||||
sock:write(table.concat(name_bytes) .. name)
|
||||
|
||||
local buf = ""
|
||||
sock:read_start(function(err, data)
|
||||
if err then
|
||||
error(err)
|
||||
end
|
||||
|
||||
if data then
|
||||
buf = buf .. data
|
||||
if #buf >= 4 then
|
||||
local len = bit.bor(
|
||||
bit.lshift(string.byte(buf, 1), 24),
|
||||
bit.lshift(string.byte(buf, 2), 16),
|
||||
bit.lshift(string.byte(buf, 3), 8),
|
||||
string.byte(buf, 4)
|
||||
)
|
||||
|
||||
if #buf >= len + 4 then
|
||||
sock:read_stop()
|
||||
callback(buf:sub(5))
|
||||
end
|
||||
end
|
||||
else
|
||||
callback(buf:sub(5))
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
local function connect()
|
||||
local sockpath = os.getenv("XDG_RUNTIME_DIR") .. "/portingtools.sock"
|
||||
local sock = vim.uv.new_pipe()
|
||||
vim.uv.pipe_connect(sock, sockpath)
|
||||
return sock
|
||||
end
|
||||
|
||||
vim.keymap.set("n", "m", function()
|
||||
local word = vim.fn.expand "<cword>"
|
||||
local mapped = map(word)
|
||||
local replaced = vim.fn.getline("."):gsub(word, mapped);
|
||||
vim.fn.setline(".", replaced)
|
||||
local sock = connect()
|
||||
local word = vim.fn.expand("<cword>")
|
||||
local curline = vim.api.nvim_win_get_cursor(0)[1]
|
||||
local curbuf = vim.api.nvim_get_current_buf()
|
||||
map(sock, word, function(mapped)
|
||||
sock:close()
|
||||
vim.schedule(function()
|
||||
local replaced = vim.api.nvim_buf_get_lines(curbuf, curline - 1, curline, true)[1]:gsub(word, mapped)
|
||||
vim.api.nvim_buf_set_lines(curbuf, curline - 1, curline, true, { replaced })
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
local function mapNext(sock, buf, total_lines, line)
|
||||
local curline = vim.api.nvim_buf_get_lines(buf, line - 1, line, true)[1]
|
||||
local unmapped = {}
|
||||
for w in curline:gmatch("field_%d+_[%a_]+") do
|
||||
table.insert(unmapped, w)
|
||||
end
|
||||
for w in curline:gmatch("func_%d+_[%a_]+") do
|
||||
table.insert(unmapped, w)
|
||||
end
|
||||
|
||||
local function replaceNextInLine()
|
||||
map(sock, unmapped[#unmapped], function(mapped)
|
||||
curline = curline:gsub(unmapped[#unmapped], mapped)
|
||||
table.remove(unmapped, #unmapped)
|
||||
|
||||
if #unmapped == 0 then
|
||||
vim.schedule(function()
|
||||
vim.api.nvim_buf_set_lines(buf, line - 1, line, true, { curline })
|
||||
print(line .. "/" .. total_lines)
|
||||
if total_lines > line then
|
||||
mapNext(sock, buf, total_lines, line + 1)
|
||||
else
|
||||
sock:close()
|
||||
end
|
||||
end)
|
||||
else
|
||||
replaceNextInLine()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
if #unmapped == 0 then
|
||||
print(line .. "/" .. total_lines)
|
||||
if total_lines > line then
|
||||
mapNext(sock, buf, total_lines, line + 1)
|
||||
else
|
||||
sock:close()
|
||||
end
|
||||
else
|
||||
replaceNextInLine()
|
||||
end
|
||||
end
|
||||
|
||||
vim.keymap.set("n", "M", function()
|
||||
local nlines = vim.api.nvim_buf_line_count(0)
|
||||
for i = 1, nlines do
|
||||
print(i .. "/" .. nlines)
|
||||
local line = vim.fn.getline(i)
|
||||
local mapped = line:gsub("field_%d+_[%a_]+", map):gsub("func_%d+_[%a_]+", map);
|
||||
vim.fn.setline(i, mapped)
|
||||
end
|
||||
local sock = connect()
|
||||
local nlines = vim.api.nvim_buf_line_count(0)
|
||||
local curbuf = vim.api.nvim_get_current_buf()
|
||||
|
||||
mapNext(sock, curbuf, nlines, 1)
|
||||
end)
|
||||
|
|
Loading…
Reference in a new issue