Skip to content

Commit

Permalink
feat: migrate thread calls from work pool to threads/async calls
Browse files Browse the repository at this point in the history
work pool might block neovim exit `:q` if the thread is not done, for some
reason this does not happen if thread is started with new_thread and it
communicate back with async handlers
  • Loading branch information
mike325 committed Nov 6, 2024
1 parent 11b573e commit 94528f9
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 42 deletions.
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if not vim.keycode then
end
end

if not vim.version.gt(vim.version(), { 0, 9 }) then
if vim.version.lt(vim.version(), { 0, 9 }) then
vim.api.nvim_err_writeln 'Neovim version is too old!! please use update it'
end

Expand Down
9 changes: 7 additions & 2 deletions lua/threads/functions.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local M = {}

function M.find(thread_args)
function M.find(thread_args, async)
local encode = type(thread_args) == type ''
thread_args = require('threads').init(thread_args)

Expand Down Expand Up @@ -78,7 +78,12 @@ function M.find(thread_args)
thread_args.results = candidates
thread_args.functions = nil
thread_args.args = nil
return (vim.is_thread() and encode) and vim.json.encode(candidates) or candidates
local rt = (vim.is_thread() and encode) and vim.json.encode(candidates) or candidates
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.async_find(opts)
Expand Down
9 changes: 7 additions & 2 deletions lua/threads/git.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local M = {}

function M.get_hunks(thread_args)
function M.get_hunks(thread_args, async)
thread_args = require('threads').init(thread_args)

local utils = require 'utils.files'
Expand Down Expand Up @@ -52,7 +52,12 @@ function M.get_hunks(thread_args)
end
end

return vim.is_thread() and vim.json.encode(hunks) or hunks
local rt = vim.is_thread() and vim.json.encode(hunks) or hunks
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

return M
34 changes: 15 additions & 19 deletions lua/threads/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,31 +149,27 @@ function M.queue_thread(thread, cb, opts)
-- - copy of all global, buffer and tab variables vim ?
-- - pcall thread function and collect errors
-- - Parse thread opts/function/args before calling the thread function
local work = vim.uv.new_work(
thread,
vim.schedule_wrap(function(o)
if type(o) == type '' and o ~= '' then
local ok, data = pcall(vim.json.decode, o)
if ok then
cb(data)
else
vim.notify(
'Failed to decode return value from thread\nnot valid json data: ' .. o .. '\n' .. data,
vim.log.levels.ERROR,
{ title = 'Thread' }
)
end
local async = vim.uv.new_async(vim.schedule_wrap(function(o)
if type(o) == type '' and o ~= '' then
local ok, data = pcall(vim.json.decode, o)
if ok then
cb(data)
else
vim.notify(
'Something when wrong, got an empty string from another thread',
'Failed to decode return value from thread\nnot valid json data: ' .. o .. '\n' .. data,
vim.log.levels.ERROR,
{ title = 'Thread' }
)
end
end)
)

work:queue(vim.json.encode(M.add_thread_context(opts)))
else
vim.notify(
'Something when wrong, got an empty string from another thread',
vim.log.levels.ERROR,
{ title = 'Thread' }
)
end
end))
vim.uv.new_thread(thread, vim.json.encode(M.add_thread_context(opts)), async)
end

return M
39 changes: 31 additions & 8 deletions lua/threads/parsers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function M.includes(args)
end

-- TODO: Add support to inherit c/cpp source paths
function M.compile_flags(thread_args)
function M.compile_flags(thread_args, async)
thread_args = require('threads').init(thread_args)
local utils = require 'utils.files'

Expand Down Expand Up @@ -51,10 +51,16 @@ function M.compile_flags(thread_args)
flags_file = flags_file,
flags = compile_flags,
}
return vim.is_thread() and vim.json.encode(results) or results

local rt = vim.is_thread() and vim.json.encode(results) or results
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.compiledb(thread_args)
function M.compiledb(thread_args, async)
thread_args = require('threads').init(thread_args)
local utils = require 'utils.files'

Expand Down Expand Up @@ -115,10 +121,16 @@ function M.compiledb(thread_args)
flags_file = flags_file,
flags = compile_commands_dbs,
}
return vim.is_thread() and vim.json.encode(results) or results

local rt = vim.is_thread() and vim.json.encode(results) or results
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.sshconfig()
function M.sshconfig(_, async)
require('threads').init()

local utils = require 'utils.files'
Expand All @@ -143,10 +155,16 @@ function M.sshconfig()
end
end
end
return vim.is_thread() and vim.json.encode(hosts) or hosts

local rt = vim.is_thread() and vim.json.encode(hosts) or hosts
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.yaml(thread_args)
function M.yaml(thread_args, async)
thread_args = require('threads').init(thread_args)

local filename = thread_args.args.filename
Expand Down Expand Up @@ -205,7 +223,12 @@ function M.yaml(thread_args)
end
end

return vim.is_thread() and vim.json.encode(yaml_dict) or yaml_dict
local rt = vim.is_thread() and vim.json.encode(yaml_dict) or yaml_dict
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

return M
45 changes: 35 additions & 10 deletions lua/threads/related.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

-- NOTE: May save this to disk in a json cache file
function M.gather_srcs_headers(thread_args)
function M.gather_srcs_headers(thread_args, async)
thread_args = require('threads').init(thread_args)

local basename = require('utils.files').basename
Expand Down Expand Up @@ -92,10 +92,15 @@ function M.gather_srcs_headers(thread_args)
end
end

return vim.is_thread() and vim.json.encode(alternates) or alternates
local rt = vim.is_thread() and vim.json.encode(alternates) or alternates
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.gather_tests(thread_args)
function M.gather_tests(thread_args, async)
-- local encode = type(thread_args) == type ''
thread_args = require('threads').init(thread_args)

Expand Down Expand Up @@ -222,10 +227,15 @@ function M.gather_tests(thread_args)
end
end

return vim.is_thread() and vim.json.encode(tests) or tests
local rt = vim.is_thread() and vim.json.encode(tests) or tests
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.alternate_src_header(thread_args)
function M.alternate_src_header(thread_args, async)
thread_args = require('threads').init(thread_args)

local basename = require('utils.files').basename
Expand Down Expand Up @@ -292,10 +302,15 @@ function M.alternate_src_header(thread_args)
varname = 'alternates',
}

return vim.is_thread() and vim.json.encode(results) or results
local rt = vim.is_thread() and vim.json.encode(results) or results
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.alternate_test(thread_args)
function M.alternate_test(thread_args, async)
thread_args = require('threads').init(thread_args)

local basename = require('utils.files').basename
Expand Down Expand Up @@ -396,10 +411,15 @@ function M.alternate_test(thread_args)
varname = 'tests',
}

return vim.is_thread() and vim.json.encode(results) or results
local rt = vim.is_thread() and vim.json.encode(results) or results
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.related_makefiles(thread_args)
function M.related_makefiles(thread_args, async)
thread_args = require('threads').init(thread_args)

-- NOTE: current buffer's directory
Expand All @@ -415,7 +435,12 @@ function M.related_makefiles(thread_args)
varname = 'makefiles',
}

return vim.is_thread() and vim.json.encode(results) or results
local rt = vim.is_thread() and vim.json.encode(results) or results
if async then
vim.uv.async_send(async, rt)
return
end
return rt
end

function M.update_var_cb(opts)
Expand Down

0 comments on commit 94528f9

Please sign in to comment.