Skip to content

Commit

Permalink
feat: add ability to run and build current file(with virtual text sup…
Browse files Browse the repository at this point in the history
…port)
  • Loading branch information
Civitasv committed May 3, 2024
1 parent 015be0e commit adfac36
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
35 changes: 30 additions & 5 deletions lua/cmake-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ end

local function get_targets(config, opt)
local targets, display_targets, paths, abs_paths = {}, {}, {}, {}
local sources = {}
if opt.has_all then
table.insert(targets, "all")
table.insert(display_targets, "all")
Expand Down Expand Up @@ -375,14 +376,30 @@ local function get_targets(config, opt)
table.insert(abs_paths, abs_path)
end
end
if opt.query_sources then -- get all source files related to this target
for _, source in ipairs(target_info["sources"]) do
local source_abs_path = config.cwd .. "/" .. source["path"]
table.insert(sources, { path = source_abs_path, type = type, name = target_name })
end
end
end
end

return Result:new(
Types.SUCCESS,
{ targets = targets, display_targets = display_targets, paths = paths, abs_paths = abs_paths },
"Success!"
)
if opt.query_sources then
return Result:new(Types.SUCCESS, {
targets = targets,
display_targets = display_targets,
paths = paths,
abs_paths = abs_paths,
sources = sources,
}, "Success!")
else
return Result:new(
Types.SUCCESS,
{ targets = targets, display_targets = display_targets, paths = paths, abs_paths = abs_paths },
"Success!"
)
end
end

function Config:get_code_model_info()
Expand Down Expand Up @@ -410,4 +427,12 @@ function Config:build_targets()
return get_targets(self, { has_all = true, only_executable = false })
end

function Config:launch_targets_with_sources()
return get_targets(self, { has_all = false, only_executable = true, query_sources = true })
end

function Config:build_targets_with_sources()
return get_targets(self, { has_all = false, only_executable = false, query_sources = true })
end

return Config
15 changes: 15 additions & 0 deletions lua/cmake-tools/hints.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local hints = {
ns_id = vim.api.nvim_create_namespace("cmaketools"),
}

function hints.show(buf, line, target_type, target)
vim.api.nvim_buf_clear_namespace(buf, hints.ns_id, 0, -1)
local vl = vim.fn.line("w0")
local mark_id = vim.api.nvim_buf_set_extmark(buf, hints.ns_id, vl, 0, {
virt_text = { { target_type .. "(" .. target .. ")", "@type" } },
virt_text_pos = "right_align",
hl_mode = "combine",
})
end

return hints
52 changes: 52 additions & 0 deletions lua/cmake-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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")
local environment = require("cmake-tools.environment")
Expand Down Expand Up @@ -1177,6 +1178,38 @@ function cmake.run_test(opt)
)
end

function cmake.run_current_file(opt)
local name
local file = vim.fn.expand("%:p")
local all_targets = config:launch_targets_with_sources()
for _, target in ipairs(all_targets.data["sources"]) do
if target.path == file then
name = target.name
break
end
end
if name == nil then
return log.error("Current file is not belong to any executable.")
end
return cmake.run({ target = name, args = opt.fargs })
end

function cmake.build_current_file(opt)
local name
local file = vim.fn.expand("%:p")
local all_targets = config:build_targets_with_sources()
for _, target in ipairs(all_targets.data["sources"]) do
if target.path == file then
name = target.name
break
end
end
if name == nil then
return log.error("Current file is not belong to any library.")
end
return cmake.build({ target = name, args = opt.fargs })
end

--[[ Getters ]]
function cmake.get_config()
return config
Expand Down Expand Up @@ -1462,6 +1495,25 @@ function cmake.register_autocmd()
vim.api.nvim_del_augroup_by_id(group)
end,
})

vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
group = group,
callback = function(ev)
local name, type
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
name = target.name
type = target.type
break
end
end
if name and type and config.build_type then
hints.show(ev.buf, 0, type, name)
end
end,
})
end
end

Expand Down
20 changes: 20 additions & 0 deletions plugin/cmake-tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ vim.api.nvim_create_user_command(
}
)

--- CMake run current file
vim.api.nvim_create_user_command(
"CMakeRunCurrentFile", -- name
cmake_tools.run_current_file, -- command
{ -- opts
nargs = "*",
desc = "CMake run current file",
}
)

--- CMake build current file
vim.api.nvim_create_user_command(
"CMakeBuildCurrentFile", -- name
cmake_tools.build_current_file, -- command
{ -- opts
nargs = "*",
desc = "CMake build current file",
}
)

--- CMake launch args
vim.api.nvim_create_user_command(
"CMakeLaunchArgs", -- name
Expand Down

0 comments on commit adfac36

Please sign in to comment.