Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ local session = {
```

per project.

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.
41 changes: 36 additions & 5 deletions lua/cmake-tools/session.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
local osys = require("cmake-tools.osys")
local utils = require("cmake-tools.utils")

local function find_file_in_dir_or_parent(dir, fname)
local path = dir .. "/" .. fname
local file = io.open(path, "r")
if file then
file:close()
return path
end

local parentDir = dir:match("(.+)/[^/]+$")
if not parentDir then
return nil
end

return find_file_in_dir_or_parent(parentDir, fname)
end

local session = {
dir = {
unix = vim.fn.expand("~") .. "/.cache/cmake_tools_nvim/",
Expand All @@ -24,6 +40,12 @@ local function get_cache_path()
end
end

---@param cwd string neovim working directory
---@return string
local function get_local_config_path(cwd)
return find_file_in_dir_or_parent(cwd, ".cmake-tools.lua")
end

---@param cwd string neovim working directory
---@return string
local function get_current_path(cwd)
Expand All @@ -42,9 +64,12 @@ end

---@param cwd string neovim working directory
local function init_session(cwd)
init_cache()
local path = get_local_config_path(cwd)
if not path then
init_cache()
path = get_current_path(cwd)
end

local path = get_current_path(cwd)
if not utils.file_exists(path) then
local file = io.open(path, "w")
if file then
Expand All @@ -56,7 +81,10 @@ end
---@param cwd string neovim working directory (used as cache key)
---@return SerializedConfig raw session data, or empty table if none exists
function session.load(cwd)
local path = get_current_path(cwd)
local path = get_local_config_path(cwd)
if not path then
path = get_current_path(cwd)
end

if utils.file_exists(path) then
local config = dofile(path)
Expand Down Expand Up @@ -92,9 +120,12 @@ end
---@param cwd string neovim working directory (used as cache key)
---@param config Config current config to persist
function session.save(cwd, config)
init_session(cwd)
local path = get_local_config_path()
if not path then
init_session(cwd)
path = get_current_path(cwd)
end

local path = get_current_path(cwd)
local file = io.open(path, "w")

---@class SerializedConfig
Expand Down
Loading