feat: neovim script now connects directly to socket

This commit is contained in:
LordMZTE 2024-04-28 15:35:27 +02:00
parent 7e054722db
commit ada4d35e39
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6

116
vim.lua
View file

@ -1,22 +1,112 @@
-- A script for vim to map the word under the cursor with portingtools by pressing m, and all -- 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 -- mapped expressions in the file with M
local function map(name) local bit = require("bit")
return vim.fn.systemlist("portingtools map", name)[1]
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 end
vim.keymap.set("n", "m", function() vim.keymap.set("n", "m", function()
local word = vim.fn.expand "<cword>" local sock = connect()
local mapped = map(word) local word = vim.fn.expand("<cword>")
local replaced = vim.fn.getline("."):gsub(word, mapped); local curline = vim.api.nvim_win_get_cursor(0)[1]
vim.fn.setline(".", replaced) 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) end)
vim.keymap.set("n", "M", function() local function mapNext(sock, buf, total_lines, line)
local nlines = vim.api.nvim_buf_line_count(0) local curline = vim.api.nvim_buf_get_lines(buf, line - 1, line, true)[1]
for i = 1, nlines do local unmapped = {}
print(i .. "/" .. nlines) for w in curline:gmatch("field_%d+_[%a_]+") do
local line = vim.fn.getline(i) table.insert(unmapped, w)
local mapped = line:gsub("field_%d+_[%a_]+", map):gsub("func_%d+_[%a_]+", map); end
vim.fn.setline(i, mapped) 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
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 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)