From 6184ca819279c7e44f470a731fcca3d469f40a01 Mon Sep 17 00:00:00 2001
From: Daniil Rozanov <daniilrozzanov@gmail.com>
Date: Mon, 18 Mar 2024 01:49:23 +0300
Subject: feat: new CMakeConfigureLast and CMakeConfigureDir commands.
 CMakeConfigure and CMakeConfigureDir now have vim.ui.* interface to edit

---
 lua/cmake-explorer.lua | 118 +++++++++++++++++++++++++++++++++----------------
 1 file changed, 81 insertions(+), 37 deletions(-)

(limited to 'lua/cmake-explorer.lua')

diff --git a/lua/cmake-explorer.lua b/lua/cmake-explorer.lua
index 8e01a57..eec4f5d 100644
--- a/lua/cmake-explorer.lua
+++ b/lua/cmake-explorer.lua
@@ -1,68 +1,112 @@
-local globals = require("cmake-explorer.globals")
 local config = require("cmake-explorer.config")
 local runner = require("cmake-explorer.runner")
 local Project = require("cmake-explorer.project")
-local Build = require("cmake-explorer.build")
+local capabilities = require("cmake-explorer.capabilities")
+local utils = require("cmake-explorer.utils")
+local Path = require("plenary.path")
 
 local M = {}
 
-local projects = {}
+local project = nil
 
-local current_project = nil
-
-local function set_current_project(path)
-	if path then
-		for _, v in ipairs(projects) do
-			-- print(v.path:absolute() .. " ? " .. path)
-			if v.path:absolute() == path then
-				current_project = v
-				return
-			end
+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
-	end
-	if #projects ~= 0 then
-		current_project = projects[1]
 	else
-		print("set_current_project. no projects available")
+		return function(v)
+			return Path:new(v.path):make_relative(project.path)
+		end
 	end
 end
 
 function M.list_build_dirs()
-	if current_project then
-		vim.print(current_project:list_build_dirs_names())
+	if project then
+		vim.print(project:list_build_dirs())
 	end
 end
 
-function M.configure(opts)
-	print("configure. #projects " .. #projects)
-	if current_project then
-		runner.start(current_project:configure(opts))
-	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 {}
-	globals.setup()
+
 	config.setup(cfg)
+	capabilities.setup()
 
-	projects = { Project:new(vim.loop.cwd()) }
-	set_current_project()
+	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", function(opts)
-		if #opts.fargs ~= 0 then
-			M.configure({ build_type = opts.fargs[1] })
-		else
-			M.configure()
-		end
-	end, { -- opts
-		nargs = "*",
+	cmd("CMakeConfigure", M.configure, { -- opts
+		nargs = 0,
 		bang = true,
-		desc = "CMake configure",
+		desc = "CMake configure with parameters",
 	})
 
-	cmd("CMakeListBuildDirs", M.list_build_dirs, { nargs = 0 })
+	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
-- 
cgit v1.2.3