Skip to content

Commit

Permalink
Fully working single terminal
Browse files Browse the repository at this point in the history
x

y
  • Loading branch information
rohit-kumar-j committed May 28, 2023
1 parent 311ccca commit cdb4a8c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 25 deletions.
1 change: 1 addition & 0 deletions lua/cmake-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local Config = {
kit = nil,
configure_preset = nil,
build_preset = nil,
run_terminals = {} -- TODO Multiple terminals
}

function Config:new(const)
Expand Down
25 changes: 17 additions & 8 deletions lua/cmake-tools/const.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local const = {
cmake_build_directory = "", -- cmake generate directory
cmake_build_directory_prefix = "cmake_build_", -- when cmake_build_directory is "", this option will be activated
cmake_command = "cmake", -- cmake command path
cmake_build_directory = "", -- cmake generate directory
cmake_build_directory_prefix = "cmake_build_", -- when cmake_build_directory is "", this option will be activated
cmake_command = "cmake", -- cmake command path
cmake_generate_options = { "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" }, -- it will be activated when invoke `cmake.generate`
cmake_regenerate_on_save = true,
cmake_soft_link_compile_commands = true,
cmake_compile_commands_from_preset = false,
cmake_build_options = {}, -- it will be activated when invoke `cmake.build`
cmake_build_options = {}, -- it will be activated when invoke `cmake.build`
cmake_console_position = "belowright", -- "bottom", "top"
cmake_console_size = 10,
cmake_show_console = "always", -- "always", "only_on_error"
cmake_show_console = "always", -- "always", "only_on_error"
cmake_variants_message = {
short = { show = true },
long = { show = true, max_length = 40 }
Expand All @@ -24,11 +24,20 @@ local const = {
},
cmake_use_terminals = false,
cmake_terminal_opts = {
split_direction = "horizontal", -- "horizontal", "vertical"
split_direction = "horizontal", -- "horizontal", "vertical"
split_size = 10,
main_terminal_name = "CMake Main Terminal"
}
main_terminal_name = "CMake Main Terminal",

-- Window handling
display_single_terminal_arcoss_instance = true,
single_terminal_pet_tab = true,
keep_terminal_in_static_location = true,

-- Running Taaks
launch_task_in_a_child_process = true,
launch_executable_in_a_child_process = true,
launch_executable_from_build_directory = true
}
}

return const
9 changes: 6 additions & 3 deletions lua/cmake-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function cmake.generate(opt, callback)
end,
cmake_console_position = const.cmake_console_position,
cmake_show_console = const.cmake_show_console,
cmake_launch_path = vim.loop.cwd(),
cmake_console_size = const.cmake_console_size,
cmake_use_terminals = const.cmake_use_terminals,
cmake_terminal_opts = const.cmake_terminal_opts
Expand All @@ -104,7 +105,7 @@ function cmake.generate(opt, callback)

-- cmake kits, if cmake-kits.json doesn't exist, kit_option will
-- be {env={}, args={}}, so it's okay.
local kit_option = kits.build_env_and_args(config.kit, const.cmake_use_terminals)
local kit_option = kits.build_env_and_args(config.kit, const.cmake_terminal_opts)

if const.cmake_build_directory ~= "" then
config:update_build_dir(const.cmake_build_directory)
Expand Down Expand Up @@ -135,6 +136,7 @@ function cmake.generate(opt, callback)
cmake_console_position = const.cmake_console_position,
cmake_show_console = const.cmake_show_console,
cmake_console_size = const.cmake_console_size,
cmake_launch_path = vim.loop.cwd(),
cmake_use_terminals = const.cmake_use_terminals,
cmake_terminal_opts = const.cmake_terminal_opts
})
Expand Down Expand Up @@ -162,6 +164,7 @@ function cmake.clean(callback)
cmake_console_position = const.cmake_console_position,
cmake_show_console = const.cmake_show_console,
cmake_console_size = const.cmake_console_size,
cmake_launch_path = vim.loop.cwd(),
cmake_use_terminals = const.cmake_use_terminals,
cmake_terminal_opts = const.cmake_terminal_opts
})
Expand Down Expand Up @@ -210,8 +213,6 @@ function cmake.build(opt, callback)
vim.list_extend(args, fargs)
end

print('running from build')

return utils.run(const.cmake_command, {}, args, {
on_success = function()
if type(callback) == "function" then
Expand All @@ -221,6 +222,7 @@ function cmake.build(opt, callback)
cmake_console_position = const.cmake_console_position,
cmake_show_console = const.cmake_show_console,
cmake_console_size = const.cmake_console_size,
cmake_launch_path = vim.loop.cwd(),
cmake_use_terminals = const.cmake_use_terminals,
cmake_terminal_opts = const.cmake_terminal_opts
})
Expand Down Expand Up @@ -299,6 +301,7 @@ function cmake.install(opt)
cmake_console_position = const.cmake_console_position,
cmake_show_console = const.cmake_show_console,
cmake_console_size = const.cmake_console_size,
cmake_launch_path = vim.loop.cwd(),
cmake_use_terminals = const.cmake_use_terminals,
cmake_terminal_opts = const.cmake_terminal_opts
})
Expand Down
6 changes: 3 additions & 3 deletions lua/cmake-tools/kits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function kits.get_by_name(kit_name)
end

-- given a kit, build an argument list for CMake
function kits.build_env_and_args(kit_name, cmake_use_terminals)
function kits.build_env_and_args(kit_name, opts)
local kit = kits.get_by_name(kit_name)
local args = {}
local env = {}
Expand All @@ -84,10 +84,10 @@ function kits.build_env_and_args(kit_name, cmake_use_terminals)
-- if exists `compilers` option, then set variable for cmake
if kit.compilers then
for lang, compiler in pairs(kit.compilers) do
if cmake_use_terminals then
if opts.launch_task_in_a_child_process then
add_args({ "-DCMAKE_" .. lang .. "_COMPILER:FILEPATH=" .. "\\\"" .. compiler .. "\\\"" }) -- This is for passing to child process
else
add_args({ "-DCMAKE_" .. lang .. "_COMPILER:FILEPATH=" .. compiler })
add_args({ "-DCMAKE_" .. lang .. "_COMPILER:FILEPATH=\"" .. compiler .. "\"" })
end
end
end
Expand Down
75 changes: 64 additions & 11 deletions lua/cmake-tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,37 @@ function utils.execute(executable, opts)
vim.cmd("wall")

if opts.cmake_use_terminals then
print('testing from exectue()')
vim.print(opts.cmake_terminal_opts)
-- print('testing from exectue()')
-- vim.print(opts.cmake_terminal_opts)
-- print('opts.cmake_launch_path: ')
-- vim.print(opts.cmake_launch_path)
-- print('executable ')
-- vim.print(executable)

local _, buffer_idx = utils.create_terminal_if_not_created(opts.cmake_terminal_opts.main_terminal_name,
opts.cmake_terminal_opts)

if utils.check_if_term_is_running_child_procs(buffer_idx) then
notify('CMake task is running in terminal', vim.log.levels.ERROR)
return
end
utils.send_data_to_terminal(buffer_idx, executable, false)

-- Reposition the terminal buffer, before sending commands
utils.reposition_term(buffer_idx)

-- Prepare Launch path if sending to terminal
local launch_path = opts.cmake_launch_path
if opts.cmake_terminal_opts.launch_executable_in_a_child_process then
launch_path = "\\\"" .. opts.cmake_launch_path .. "\\\""
end

-- Launch form main directory
if opts.cmake_terminal_opts.launch_executable_from_build_directory == true then
executable = "cd " .. launch_path .. " && " .. executable
end

-- Send final cmd to terminal
utils.send_data_to_terminal(buffer_idx, executable, opts.cmake_terminal_opts.launch_executable_in_a_child_process)
else
-- print("EXECUTABLE", executable)
local set_bufname = "file " .. opts.bufname
Expand Down Expand Up @@ -153,18 +175,40 @@ function utils.run(cmd, env, args, opts)
vim.cmd("wall")

if opts.cmake_use_terminals then
print('testing from run()')
vim.print(opts.cmake_terminal_opts)
-- print('testing from run()')
-- vim.print(opts.cmake_terminal_opts)

local _, buffer_idx = utils.create_terminal_if_not_created(opts.cmake_terminal_opts.main_terminal_name,
opts.cmake_terminal_opts)

if utils.check_if_term_is_running_child_procs(buffer_idx) then
notify('CMake task is running in terminal', vim.log.levels.ERROR)
return
end

-- Reposition the terminal buffer, before sending commands
utils.reposition_term(buffer_idx)

-- Prepare Launch path form
local launch_path = opts.cmake_launch_path
if opts.cmake_terminal_opts.launch_task_in_a_child_process then
launch_path = "\\\"" .. opts.cmake_launch_path .. "\\\""
end

-- Launch form main directory
if opts.cmake_terminal_opts.launch_executable_from_build_directory then
cmd = "cd " .. launch_path .. " && " .. cmd
end

-- Add args to the cmd
for _, arg in ipairs(args) do
cmd = cmd .. " " .. arg
end
utils.send_data_to_terminal(buffer_idx, cmd, true)

-- Send final cmd to terminal
-- TODO: Find a way to use opts.on_success()
-- opts.on_success()
utils.send_data_to_terminal(buffer_idx, cmd, opts.cmake_terminal_opts.launch_task_in_a_child_process)
else
vim.fn.setqflist({}, " ", { title = cmd .. " " .. table.concat(args, " ") })
opts.cmake_show_console = opts.cmake_show_console == "always"
Expand Down Expand Up @@ -200,16 +244,16 @@ function utils.check_if_term_is_running_child_procs(terminal_buffer_idx)
local main_pid = vim.api.nvim_buf_get_var(terminal_buffer_idx, "terminal_job_pid")
local child_procs = vim.api.nvim_get_proc_children(main_pid)
if next(child_procs) then
print('child procs:')
vim.print(child_procs)
-- print('child procs:')
-- vim.print(child_procs)
return true
else
return false
end
end

function utils.send_data_to_terminal(buffer_idx, cmd, wrap)
print('buffer_idx: ' .. buffer_idx .. ', cmd: ' .. cmd)
-- print('buffer_idx: ' .. buffer_idx .. ', cmd: ' .. cmd)
local chan = vim.api.nvim_buf_get_var(buffer_idx, "terminal_job_id")
if vim.fn.has('win32') == 1 then
-- print('win32')
Expand All @@ -219,6 +263,10 @@ function utils.send_data_to_terminal(buffer_idx, cmd, wrap)
else
cmd = cmd .. " \r"
end
elseif vim.fn.has('macunix') == 1 then

Check warning on line 266 in lua/cmake-tools/utils.lua

View workflow job for this annotation

GitHub Actions / Lint

empty if branch
-- TODO: Process wrapper for mac
elseif vim.fn.has('wsl') == 1 then

Check warning on line 268 in lua/cmake-tools/utils.lua

View workflow job for this annotation

GitHub Actions / Lint

empty if branch
-- TODO: Process wrapper for unix
end
vim.api.nvim_chan_send(chan, cmd)
end
Expand Down Expand Up @@ -255,7 +303,7 @@ function utils.get_buffer_number_from_name(buffer_name)
return nil -- Buffer with the given name not found
end

function utils.delete_buffers_except(buffer_name, buffer_list)
function utils.delete_duplicate_terminal_buffers_except(buffer_name, buffer_list)
for _, buffer in ipairs(buffer_list) do
local name = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(buffer), ':t')
-- print('name....................' .. name)
Expand Down Expand Up @@ -321,13 +369,18 @@ function utils.start_local_shell(opts)
local diff_buffers_list = utils.symmetric_difference(buffers_before, new_buffers_list)
-- print('diff_buffers_list:')
-- vim.print(diff_buffers_list)
utils.delete_buffers_except(opts.main_terminal_name, diff_buffers_list)
utils.delete_duplicate_terminal_buffers_except(opts.main_terminal_name, diff_buffers_list)
utils.delete_scratch_buffers()

local new_buffer_idx = utils.get_buffer_number_from_name(opts.main_terminal_name)
return new_buffer_idx
end

function utils.reposition_term(buffer_idx)

Check warning on line 379 in lua/cmake-tools/utils.lua

View workflow job for this annotation

GitHub Actions / Lint

unused argument buffer_idx
-- TODO: Reposition Windows
-- print('Reposition! ... window: ' .. buffer_idx)
end

--- Check if exists active job.
-- @return true if not exists else false
function utils.has_active_job()
Expand Down

0 comments on commit cdb4a8c

Please sign in to comment.