aboutsummaryrefslogtreecommitdiff
path: root/lua/cmake-explorer.lua
blob: eec4f5dd959986cab3d89c3dc3d282d2260d1bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
local config = require("cmake-explorer.config")
local runner = require("cmake-explorer.runner")
local Project = require("cmake-explorer.project")
local capabilities = require("cmake-explorer.capabilities")
local utils = require("cmake-explorer.utils")
local Path = require("plenary.path")

local M = {}

local project = nil

local format_build_dir = function()
	if Path:new(config.build_dir):is_absolute() then
		return function(v)
			return Path:new(v.path):make_relative(vim.env.HOME)
		end
	else
		return function(v)
			return Path:new(v.path):make_relative(project.path)
		end
	end
end

function M.list_build_dirs()
	if project then
		vim.print(project:list_build_dirs())
	end
end

function M.configure()
	assert(project)
	local generators = capabilities.generators()
	table.insert(generators, 1, "Default")
	vim.ui.select(generators, { prompt = "Select generator" }, function(generator)
		if not generator then
			return
		end
		-- TODO: handle default generator from env (or from anywhere else)
		generator = utils.is_neq(generator, "Default")
		vim.ui.select(config.build_types, { prompt = "Select build type" }, function(build_type)
			if not build_type then
				return
			end
			vim.ui.input({ prompt = "Input additional args" }, function(args)
				if not build_type then
					return
				end
				local task = project:configure({ generator = generator, build_type = build_type, args = args })
				runner.start(task)
			end)
		end)
	end)
end

function M.configure_dir()
	assert(project)

	vim.ui.select(
		project:list_build_dirs(),
		{ prompt = "Select directory to build", format_item = format_build_dir() },
		function(dir)
			if not dir then
				return
			end
			local task = project:configure(dir.path)
			runner.start(task)
		end
	)
end

function M.configure_last()
	local task = project:configure_last()
	runner.start(task)
end

M.setup = function(cfg)
	cfg = cfg or {}

	config.setup(cfg)
	capabilities.setup()

	project = Project:new(vim.loop.cwd())
	if not project then
		print("cmake-explorer: no CMakeLists.txt file found. Aborting setup")
		return
	end
	project:scan_build_dirs()

	local cmd = vim.api.nvim_create_user_command

	cmd("CMakeConfigure", M.configure, { -- opts
		nargs = 0,
		bang = true,
		desc = "CMake configure with parameters",
	})

	cmd(
		"CMakeConfigureLast",
		M.configure_last,
		{ nargs = 0, bang = true, desc = "CMake configure last if exists. Otherwise default" }
	)

	cmd(
		"CMakeConfigureDir",
		M.configure_dir,
		{ nargs = 0, bang = true, desc = "CMake configure last if exists. Otherwise default" }
	)

	cmd("CMakeListBuilds", M.list_build_dirs, { nargs = 0 })
end

return M