Skip to content

Commit

Permalink
fix: terminal executor and runner don't need different operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Civitasv committed May 3, 2024
1 parent b29e0ed commit a2b0d35
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 115 deletions.
95 changes: 21 additions & 74 deletions lua/cmake-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ local variants = require("cmake-tools.variants")
local kits = require("cmake-tools.kits")
local presets = require("cmake-tools.presets")
local log = require("cmake-tools.log")
local terminal = require("cmake-tools.terminal")
local hints = require("cmake-tools.hints")
local _session = require("cmake-tools.session")
local window = require("cmake-tools.window")
Expand Down Expand Up @@ -35,13 +34,6 @@ function cmake.setup(values)
const.cmake_runner.opts or {}
)

if const.cmake_executor.name == "terminal" then
const.cmake_notifications.executor.enabled = false
end
if const.cmake_runner.name == "terminal" then
const.cmake_notifications.runner.enabled = false
end

config = Config:new(const)

-- auto reload previous session
Expand Down Expand Up @@ -121,9 +113,6 @@ function cmake.generate(opt, callback)

local env = environment.get_build_environment(config, config.executor.name == "terminal")
local cmd = const.cmake_command
if config.executor.name == "terminal" then
cmd = terminal.prepare_cmd_for_execute(const.cmake_command, env, args)
end
return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function()
if type(callback) == "function" then
callback()
Expand Down Expand Up @@ -185,9 +174,6 @@ function cmake.generate(opt, callback)

local env = environment.get_build_environment(config, config.executor.name == "terminal")
local cmd = const.cmake_command
if config.executor.name == "terminal" then
cmd = terminal.prepare_cmd_for_execute(const.cmake_command, env, args)
end
env = vim.tbl_extend("keep", env, kit_option.env)
return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function()
if type(callback) == "function" then
Expand All @@ -213,9 +199,6 @@ function cmake.clean(callback)

local env = environment.get_build_environment(config, config.executor.name == "terminal")
local cmd = const.cmake_command
if config.executor.name == "terminal" then
cmd = terminal.prepare_cmd_for_execute(const.cmake_command, env, args)
end
return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function()
if type(callback) == "function" then
callback()
Expand Down Expand Up @@ -281,9 +264,6 @@ function cmake.build(opt, callback)

local env = environment.get_build_environment(config, config.executor.name == "terminal")
local cmd = const.cmake_command
if config.executor.name == "terminal" then
cmd = terminal.prepare_cmd_for_execute(const.cmake_command, env, args)
end
return utils.execute(cmd, config.env_script, env, args, config.cwd, config.executor, function()
if type(callback) == "function" then
callback()
Expand Down Expand Up @@ -325,7 +305,6 @@ end

function cmake.stop_executor()
if not utils.has_active_job(config.runner, config.executor) then
log.error("CMake Tools isn't running")
return
end

Expand All @@ -334,7 +313,6 @@ end

function cmake.stop_runner()
if not utils.has_active_job(config.runner, config.executor) then
log.error("CMake Tools isn't running")
return
end

Expand Down Expand Up @@ -444,9 +422,6 @@ function cmake.run(opt)
environment.get_run_environment(config, opt.target, config.runner.name == "terminal")
local _args = opt.args and opt.args or config.target_settings[opt.target].args
local cmd = target_path
if config.runner.name == "terminal" then
cmd = terminal.prepare_cmd_for_run(target_path, _args, launch_path, opt.wrap_call, env)
end
utils.run(
cmd,
config.env_script,
Expand All @@ -462,15 +437,10 @@ function cmake.run(opt)
local result = config:get_launch_target()
local result_code = result.code
if result_code == Types.NOT_CONFIGURED or result_code == Types.CANNOT_FIND_CODEMODEL_FILE then
if config.executor.name == "terminal" then
log.error("You need to firstly invoke CMakeGenerate.")
return
else
-- Configure it
return cmake.generate({ bang = false, fargs = utils.deepcopy(opt.fargs) }, function()
cmake.run(opt)
end)
end
-- Configure it
return cmake.generate({ bang = false, fargs = utils.deepcopy(opt.fargs) }, function()
cmake.run(opt)
end)
elseif
result_code == Types.NOT_SELECT_LAUNCH_TARGET
or result_code == Types.NOT_A_LAUNCH_TARGET
Expand All @@ -496,15 +466,6 @@ function cmake.run(opt)
config.runner.name == "terminal"
)
local cmd = target_path
if config.runner.name == "terminal" then
cmd = terminal.prepare_cmd_for_run(
target_path,
cmake:get_launch_args(),
launch_path,
opt.wrap_call,
env
)
end
utils.run(
cmd,
config.env_script,
Expand All @@ -521,9 +482,6 @@ function cmake.run(opt)
end
end

if config.has_telescope then
end

function cmake.quick_run(opt)
-- if no target was supplied, query via ui select
if opt.fargs[1] == nil then
Expand All @@ -548,12 +506,12 @@ function cmake.quick_run(opt)
if not idx then
return
end
cmake.run({ target = targets[idx], wrap_call = opt.wrap_call })
cmake.run({ target = targets[idx] })
end)
)
else
local target = table.remove(opt.fargs, 1)
cmake.run({ target = target, args = opt.fargs, wrap_call = opt.wrap_call })
cmake.run({ target = target, args = opt.fargs })
end
end

Expand Down Expand Up @@ -777,12 +735,7 @@ function cmake.select_build_target(callback, regenerate)
if targets_res.code ~= Types.SUCCESS then
-- try again
if not regenerate then
if config.executor.name == "terminal" then
log.error("You need to firstly invoke CMakeGenerate.")
return
else
return
end
return
else
return cmake.generate({ bang = true, fargs = {} }, function()
cmake.select_build_target(callback, false)
Expand Down Expand Up @@ -831,12 +784,7 @@ function cmake.select_launch_target(callback, regenerate)
if targets_res.code ~= Types.SUCCESS then
-- try again
if not regenerate then
if config.executor.name == "terminal" then
log.error("You need to firstly invoke CMakeGenerate.")
return
else
return
end
return
else
return cmake.generate({ bang = true, fargs = {} }, function()
cmake.select_launch_target(callback, false)
Expand Down Expand Up @@ -1364,12 +1312,14 @@ function cmake.register_autocmd()
local targets = {}
local file = ev.file
local all_targets = config:build_targets_with_sources()
for _, target in ipairs(all_targets.data["sources"]) do
if target.path == file then
table.insert(targets, { name = target.name, type = target.type })
if all_targets and all_targets.data and all_targets.data["sources"] then
for _, target in ipairs(all_targets.data["sources"]) do
if target.path == file then
table.insert(targets, { name = target.name, type = target.type })
end
end
hints.show(ev.buf, targets)
end
hints.show(ev.buf, targets)
end,
})
end
Expand All @@ -1384,7 +1334,9 @@ end

function cmake.register_scratch_buffer(executor, runner)
if cmake.is_cmake_project() then
scratch.create(executor, runner)
vim.schedule(function()
scratch.create(executor, runner)
end)
end
end

Expand Down Expand Up @@ -1443,15 +1395,10 @@ function cmake.register_dap_function()
if
result_code == Types.NOT_CONFIGURED or result_code == Types.CANNOT_FIND_CODEMODEL_FILE
then
if config.executor.name == "terminal" then
log.error("You need to firstly invoke CMakeGenerate.")
return
else
-- Configure it
return cmake.generate({ bang = false, fargs = utils.deepcopy(opt.fargs) }, function()
cmake.debug(opt, callback)
end)
end
-- Configure it
return cmake.generate({ bang = false, fargs = utils.deepcopy(opt.fargs) }, function()
cmake.debug(opt, callback)
end)
elseif
result_code == Types.NOT_SELECT_LAUNCH_TARGET
or result_code == Types.NOT_A_LAUNCH_TARGET
Expand Down
12 changes: 5 additions & 7 deletions lua/cmake-tools/scratch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ local scratch = {
function scratch.create(executor, runner)
scratch.buffer = vim.api.nvim_create_buf(true, true) -- can be search, and is a scratch buffer
vim.api.nvim_buf_set_name(scratch.buffer, scratch.name)
vim.schedule_wrap(function()
vim.api.nvim_buf_set_lines(scratch.buffer, 0, 0, false, {
"THIS IS A SCRATCH BUFFER FOR cmake-tools.nvim, YOU CAN SEE WHICH COMMAND THIS PLUGIN EXECUTES HERE.",
"EXECUTOR: " .. executor .. " RUNNER: " .. runner,
})
vim.api.nvim_buf_set_option(scratch.buffer, "buflisted", false)
end)
vim.api.nvim_buf_set_lines(scratch.buffer, 0, 0, false, {
"THIS IS A SCRATCH BUFFER FOR cmake-tools.nvim, YOU CAN SEE WHICH COMMAND THIS PLUGIN EXECUTES HERE.",
"EXECUTOR: " .. executor .. " RUNNER: " .. runner,
})
vim.api.nvim_buf_set_option(scratch.buffer, "buflisted", false)
end

function scratch.append(cmd)
Expand Down
44 changes: 10 additions & 34 deletions lua/cmake-tools/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,11 @@ function _terminal.get_buffers_with_prefix(prefix)
return filtered_buffers
end

function _terminal.prepare_cmd_for_run(executable, args, launch_path, wrap_call, env)
function _terminal.prepare_cmd_for_run(cmd, env, args, cwd)
local full_cmd = ""
-- executable = vim.fn.fnamemodify(executable, ":t")

-- Launch form executable's build directory by default
full_cmd = 'cd "' .. launch_path .. '" &&'
full_cmd = 'cd "' .. cwd .. '" &&'

if osys.iswin32 then
for _, v in ipairs(env) do
Expand All @@ -485,26 +484,15 @@ function _terminal.prepare_cmd_for_run(executable, args, launch_path, wrap_call,
full_cmd = full_cmd .. " " .. table.concat(env, " ")
end

-- prepend wrap_call args
if wrap_call then
for _, arg in ipairs(wrap_call) do
full_cmd = full_cmd .. " " .. arg
end
end

full_cmd = full_cmd .. " "
full_cmd = full_cmd .. " " .. cmd

if osys.islinux or osys.iswsl or osys.ismac then
full_cmd = " " .. full_cmd -- adding a space in front of the command prevents bash from recording the command in the history (if configured)
end

full_cmd = full_cmd .. '"' .. executable .. '"'

-- Add args to the cmd
if args then
for _, arg in ipairs(args) do
full_cmd = full_cmd .. " " .. arg
end
for _, arg in ipairs(args) do
full_cmd = full_cmd .. " " .. arg
end

if osys.iswin32 then -- wrap in sub process to prevent env vars from being persited
Expand All @@ -514,22 +502,6 @@ function _terminal.prepare_cmd_for_run(executable, args, launch_path, wrap_call,
return full_cmd
end

function _terminal.prepare_cmd_for_execute(cmd, env, args)
local full_cmd = ""
if next(env) then
full_cmd = full_cmd .. cmd .. " -E " .. " env " .. table.concat(env, " ") .. " " .. cmd
else
full_cmd = full_cmd .. cmd
end

-- Add args to the cmd
for _, arg in ipairs(args) do
full_cmd = full_cmd .. " " .. arg
end

return full_cmd
end

local get_tmp_dir = function()
return vim.fn.stdpath("data") .. "/cmake-tools-tmp"
end
Expand Down Expand Up @@ -595,6 +567,8 @@ end
---@param on_exit function|nil function to be called on exit the terminal will pass commands exit code as an argument
---@param on_output any !unused here added for the sake of unification
function _terminal.run(cmd, env_script, env, args, cwd, opts, on_exit, on_output)
local full_cmd = _terminal.prepare_cmd_for_run(cmd, env, args, cwd)

local prefix = opts.prefix_name -- [CMakeTools]
create_lock_file()
-- prefix is added to the terminal name because the reposition_term() function needs to find it
Expand Down Expand Up @@ -623,7 +597,7 @@ function _terminal.run(cmd, env_script, env, args, cwd, opts, on_exit, on_output
end

-- Send final cmd to terminal
_terminal.send_data_to_terminal(buffer_idx, cmd .. ";" .. get_command_handling_on_exit(), {
_terminal.send_data_to_terminal(buffer_idx, full_cmd .. ";" .. get_command_handling_on_exit(), {
win_id = final_win_id,
prefix = opts.prefix_name,
split_direction = opts.split_direction,
Expand Down Expand Up @@ -689,6 +663,7 @@ end
function _terminal.is_installed()
return true
end

--- Handle when a terminal process exists
---@param opts any
---@param on_exit function|nil function to be executed on exit
Expand All @@ -701,4 +676,5 @@ function _terminal.handle_exit(opts, on_exit, close_on_exit)
on_exit(get_last_exit_code()) -- always return success
end
end

return _terminal
2 changes: 2 additions & 0 deletions lua/cmake-tools/toggleterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ function _toggleterm.run(cmd, env_script, env, args, cwd, opts, on_exit, on_outp
end, -- callback for processing output on stderr
on_exit = function(t, job, exit_code, name)
on_exit(exit_code)
_toggleterm.chan_id = nil
_toggleterm.cmd = nil
end, -- function to run when terminal process exits
})
_toggleterm.term:toggle()
Expand Down

0 comments on commit a2b0d35

Please sign in to comment.