Skip to content

Commit 04dcdb3

Browse files
sebu06Sebastian Uharek
authored andcommitted
Added option to override config with local file.
1 parent bac6ba2 commit 04dcdb3

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

docs/sessions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ local session = {
3939
```
4040

4141
per project.
42+
43+
Alternatively, cmake-tools will search for a file ".cmake-tools.lua" in the current working directory. If it is found, the configuration in this file will be used. If no configuration is found in the current working directory, it will continue looking in the parent directory. If no config can be found, it will use the sessions mentioned above.

lua/cmake-tools/session.lua

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
local osys = require("cmake-tools.osys")
22
local utils = require("cmake-tools.utils")
33

4+
local function find_file_in_dir_or_parent(dir, fname)
5+
local path = dir .. "/" .. fname
6+
local file = io.open(path, "r")
7+
if file then
8+
file:close()
9+
return path
10+
end
11+
12+
local parentDir = dir:match("(.+)/[^/]+$")
13+
if not parentDir then
14+
return nil
15+
end
16+
17+
return find_file_in_dir_or_parent(parentDir, fname)
18+
end
19+
420
local session = {
521
dir = {
622
unix = vim.fn.expand("~") .. "/.cache/cmake_tools_nvim/",
@@ -24,6 +40,12 @@ local function get_cache_path()
2440
end
2541
end
2642

43+
---@param cwd string neovim working directory
44+
---@return string
45+
local function get_local_config_path(cwd)
46+
return find_file_in_dir_or_parent(cwd, ".cmake-tools.lua")
47+
end
48+
2749
---@param cwd string neovim working directory
2850
---@return string
2951
local function get_current_path(cwd)
@@ -42,9 +64,12 @@ end
4264

4365
---@param cwd string neovim working directory
4466
local function init_session(cwd)
45-
init_cache()
67+
local path = get_local_config_path(cwd)
68+
if not path then
69+
init_cache()
70+
path = get_current_path(cwd)
71+
end
4672

47-
local path = get_current_path(cwd)
4873
if not utils.file_exists(path) then
4974
local file = io.open(path, "w")
5075
if file then
@@ -56,7 +81,10 @@ end
5681
---@param cwd string neovim working directory (used as cache key)
5782
---@return SerializedConfig raw session data, or empty table if none exists
5883
function session.load(cwd)
59-
local path = get_current_path(cwd)
84+
local path = get_local_config_path(cwd)
85+
if not path then
86+
path = get_current_path(cwd)
87+
end
6088

6189
if utils.file_exists(path) then
6290
local config = dofile(path)
@@ -92,9 +120,12 @@ end
92120
---@param cwd string neovim working directory (used as cache key)
93121
---@param config Config current config to persist
94122
function session.save(cwd, config)
95-
init_session(cwd)
123+
local path = get_local_config_path()
124+
if not path then
125+
init_session(cwd)
126+
path = get_current_path(cwd)
127+
end
96128

97-
local path = get_current_path(cwd)
98129
local file = io.open(path, "w")
99130

100131
---@class SerializedConfig

0 commit comments

Comments
 (0)