aboutsummaryrefslogtreecommitdiff
path: root/lua/cmake-explorer/utils.lua
blob: 61b5c9823a6f7088f0f6cd67442c17e3c96a7f10 (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
local config = require("cmake-explorer.config")
local capabilities = require("cmake-explorer.capabilities")
local Path = require("plenary.path")

local utils = {}

utils.build_path = function(build_dir, source_dir)
	local build_path = Path:new(config.build_dir)
	if build_path:is_absolute() then
		return (build_path / build_dir):absolute()
	else
		return Path:new(build_path, build_dir):normalize()
	end
end

utils.substitude = function(str, subs)
	local ret = str
	for k, v in pairs(subs) do
		ret = ret:gsub(k, v)
	end
	return ret
end

function utils.symlink_compile_commands(src_path, dst_path)
	local src = Path:new(src_path, "compile_commands.json")
	if src:exists() then
		vim.cmd(
			'silent exec "!'
			.. config.cmake_path
			.. " -E create_symlink "
			.. src:normalize()
			.. " "
			.. Path:new(dst_path, "compile_commands.json"):normalize()
			.. '"'
		)
	end
end

utils.is_eq = function(val, cmp, if_eq, if_not_eq)
	if val == cmp then
		if if_eq then
			return if_eq
		else
			return val
		end
	else
		if if_not_eq then
			return if_not_eq
		else
			return nil
		end
	end
end

utils.is_neq = function(val, cmp, if_eq, if_not_eq)
	if val ~= cmp then
		if if_eq then
			return if_eq
		else
			return val
		end
	else
		if if_not_eq then
			return if_not_eq
		else
			return nil
		end
	end
end

utils.make_maplike_list = function(proj)
	local mt = {}
	mt.__index = function(t, k)
		for _, value in ipairs(t) do
			if proj(value) == k then
				return value
			end
		end
	end
	mt.__newindex = function(t, k, v)
		for key, value in ipairs(t) do
			if proj(value) == k then
				rawset(t, key, v)
				return
			end
		end
		rawset(t, #t + 1, v)
	end
	return mt
end

return utils