Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not Bug Report] Improved a little - add current path to json file #15

Open
thohnb opened this issue Mar 19, 2024 · 1 comment
Open

Comments

@thohnb
Copy link

thohnb commented Mar 19, 2024

I find idea when see your cd-project.nvim.json file
I think it will useful for someone need it - I call it "CdProjectAddCurrentDir" -> it will automatically add current directory that currently loading into cd-project.nvim.json with current path and name.

-- D:/test/myproject
-- D:/test/myproject -> [path] 
-- myproject -> [name]

Required: vim.notifiy plugin

-- Define a wrapper function for CdProjectAdd
function CdProjectAddCurrentDir()
  -- Get the directory of the current file
  local current_file_directory = vim.fn.expand('%:p:h')
  
  -- Call CdProjectAdd with the current file directory as an argument
  CdProjectAdd(current_file_directory)
end
-- Define CdProjectAdd function
function CdProjectAdd(directory)
  -- Read existing projects from cd-project.nvim.json
  local projects_config_filepath = vim.fn.stdpath("config") .. "/cd-project.nvim.json"
  local file = io.open(projects_config_filepath, "r")
  local content = file:read("*all")
  file:close()
  
  -- Decode JSON content if it's not empty, otherwise initialize projects as an empty table
  local projects = content ~= "" and vim.fn.json_decode(content) or {}
  
  -- Prepare the new project object
  local project = {}
  project["path"] = directory
  project["name"] = directory:match("([^/\\]*)$")
  
  -- Check if the project already exists in the projects table
  for _, existing_project in ipairs(projects) do
    if existing_project["path"] == project["path"] then
      -- The project already exists, so we return without adding it
      require("notify")("Project already exists: " .. project["path"], 'Notice')
      return
    end
  end
  
  -- Add the new project object to projects
  table.insert(projects, project)
  
  -- Encode projects back to JSON
  local new_content = vim.fn.json_encode(projects)
  
  -- Write updated JSON content back to cd-project.nvim.json
  file = io.open(projects_config_filepath, "w")
  file:write(new_content)
  file:close()
  -- Display a notification
  require("notify")("Project is saved: " .. project["path"] .. " as " .. project["name"], 'Notice')
end
-- Set key mapping for CdProjectAddCurrentDir function
vim.api.nvim_set_keymap('n', 'pa', '<cmd>lua CdProjectAddCurrentDir()<CR>', {noremap=true, silent=true})
@vague2k
Copy link
Contributor

vague2k commented Apr 2, 2024

wouldnt using the hooks functionality cease the need for another dependency like notify?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants