├── LICENSE ├── README.md ├── doc ├── nvim-projectconfig.txt └── tags ├── lua └── nvim-projectconfig.lua └── plugin └── nvim-projectconfig.vim /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 windwp 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## nvim-projectconfig 2 | 3 | Load config depend on current directory. 4 | 5 | ### Sample 6 | 7 | current directory is `/home/abcde/projects/awesome/`. 8 | you open vim in **awesome** directory. 9 | 10 | It will load a config file from `~/.config/nvim/projects/awesome.lua` or `~/.config/nvim/projects/awesome.vim` 11 | 12 | this config save outside of your git repo and you don't need to check security on that file. 13 | It work perfect if you are working on monorepo. 14 | 15 | 16 | ## Install 17 | ``` vim 18 | Plug 'windwp/nvim-projectconfig' 19 | ``` 20 | then add this in your init.lua 21 | 22 | ```lua 23 | require('nvim-projectconfig').setup() 24 | ``` 25 | 26 | ## FAQ 27 | * A command to open project config file 28 | 29 | Command: **EditProjectConfig** 30 | 31 | * I want to change projects-config directory 32 | 33 | ``` lua 34 | 35 | require('nvim-projectconfig').setup({ 36 | project_dir = "~/.config/projects-config/", 37 | }) 38 | 39 | ``` 40 | 41 | * I have 2 directory have same name. 42 | 43 | ``` lua 44 | require('nvim-projectconfig').setup({ 45 | project_dir = "~/.config/projects-config/", 46 | project_config={ 47 | { 48 | -- full path of your project or a lua regex string 49 | path = "projectconfig", 50 | -- use a function or a path to config file 51 | config = function () 52 | print("abcde") 53 | end 54 | }, 55 | }, 56 | silent = false,-- display message after load config file 57 | }) 58 | ``` 59 | 60 | 61 | * I want to change my directory inside neovim and load project config. 62 | 63 | ``` lua 64 | require('nvim-projectconfig').setup({autocmd=true}) 65 | 66 | ``` 67 | 68 | 69 | ## Bonus 70 | a function to load and save json file and do whatever you want. 71 | ```lua 72 | require('nvim-projectconfig').load_json() 73 | require('nvim-projectconfig').save_json(data) 74 | ``` 75 | 76 | -------------------------------------------------------------------------------- /doc/nvim-projectconfig.txt: -------------------------------------------------------------------------------- 1 | *nvim-projectconfig.txt* nvim-projectconfig.vim 2 | 3 | Author: Trieu Le 4 | License: Same terms as Vim itself (see |license|) 5 | 6 | 7 | Load config depend on current directory. 8 | 9 | 10 | ABOUT *nvim-projectconfig* 11 | 12 | Grab the latest version or report a bug on GitHub: 13 | 14 | https://github.com/windwp/nvim-projectconfig 15 | 16 | vim:tw=78:et:ft=help:norl: 17 | -------------------------------------------------------------------------------- /doc/tags: -------------------------------------------------------------------------------- 1 | nvim-projectconfig nvim-projectconfig.txt /*nvim-projectconfig* 2 | nvim-projectconfig.txt nvim-projectconfig.txt /*nvim-projectconfig.txt* 3 | -------------------------------------------------------------------------------- /lua/nvim-projectconfig.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | local config = { 4 | silent = true, 5 | autocmd = true, 6 | project_dir = vim.fn.stdpath("config") .. "/projects/", 7 | project_config = {}, 8 | } 9 | 10 | M.setup = function(opts) 11 | config = vim.tbl_extend("force", config, opts or {}) 12 | if not config.project_dir:match("/$") then 13 | config.project_dir = config.project_dir .. "/" 14 | end 15 | if config.autocmd then 16 | vim.api.nvim_create_autocmd("DirChanged", { 17 | group = vim.api.nvim_create_augroup("NvimProjectConfig", { clear = true }), 18 | pattern = "*", 19 | callback = function() 20 | M.load_project_config() 21 | end, 22 | }) 23 | end 24 | M.load_project_config() 25 | end 26 | 27 | local function execute(file_path) 28 | file_path = vim.fn.expand(file_path) 29 | if vim.fn.filereadable(file_path) == 1 then 30 | if config.silent == false then 31 | vim.defer_fn(function() 32 | vim.notify("[project-config] - " .. vim.fn.fnamemodify(file_path, ":t:r")) 33 | end, 100) 34 | end 35 | if file_path:match("%.vim$") then 36 | vim.cmd("source " .. file_path) 37 | return true 38 | elseif file_path:match("%.lua$") then 39 | vim.cmd("luafile " .. file_path) 40 | return true 41 | end 42 | end 43 | return false 44 | end 45 | 46 | local function load_from_directory() 47 | config.project_dir = vim.fn.expand(config.project_dir) 48 | if vim.fn.isdirectory(config.project_dir) == 0 then 49 | vim.fn.mkdir(config.project_dir, "p") 50 | end 51 | if execute(M.get_config_by_ext("lua")) then 52 | return true 53 | end 54 | if execute(M.get_config_by_ext("vim")) then 55 | return true 56 | end 57 | return false 58 | end 59 | 60 | local function load_from_config() 61 | local uv = vim.uv or vim.loop 62 | local cwd = uv.cwd() or "" 63 | for _, item in pairs(config.project_config) do 64 | local match = string.match(cwd, item.path) 65 | if cwd == item.path or match ~= nil and #match > 1 then 66 | if type(item.config) == "function" then 67 | item.config() 68 | return true 69 | end 70 | if type(item.config) == "string" then 71 | execute(item.config) 72 | return true 73 | end 74 | end 75 | end 76 | return false 77 | end 78 | 79 | function M.load_project_config(opts) 80 | if opts then 81 | config = vim.tbl_extend("force", config, opts or {}) 82 | end 83 | if load_from_directory() then 84 | return 85 | end 86 | if load_from_config() then 87 | return 88 | end 89 | end 90 | 91 | function M.edit_project_config() 92 | local conf = M.get_config_by_ext("vim") 93 | if vim.fn.filereadable(conf) == 1 then 94 | vim.cmd("edit " .. conf) 95 | else 96 | vim.cmd("edit " .. (M.get_config_by_ext("lua") or "")) 97 | end 98 | end 99 | 100 | ---get any config file with extension 101 | ---@return string|nil 102 | function M.get_config_by_ext(ext) 103 | local rootFolder = vim.fn.fnamemodify(vim.loop.cwd(), ":p:h:t") 104 | return config.project_dir .. rootFolder .. "." .. ext 105 | end 106 | 107 | ---comment 108 | ---@return table json setting 109 | function M.load_json() 110 | local json_decode = vim.json and vim.json.decode or vim.fn.json_decode 111 | local jsonfile = M.get_config_by_ext("json") 112 | if vim.fn.filereadable(jsonfile) == 1 then 113 | local f = io.open(jsonfile, "r") 114 | local data = f:read("*a") 115 | f:close() 116 | if data then 117 | local check, jdata = pcall(json_decode, data) 118 | if check then 119 | return jdata 120 | end 121 | end 122 | end 123 | end 124 | 125 | function M.save_json(json_table) 126 | local jsonfile = M.get_config_by_ext("json") 127 | local json_encode = vim.json and vim.json.encode or vim.fn.json_encode 128 | local fp = assert(io.open(jsonfile, "w")) 129 | fp:write(json_encode(json_table)) 130 | fp:close() 131 | end 132 | 133 | return M 134 | -------------------------------------------------------------------------------- /plugin/nvim-projectconfig.vim: -------------------------------------------------------------------------------- 1 | command! -nargs=* EditProjectConfig call v:lua.require("nvim-projectconfig").edit_project_config() 2 | --------------------------------------------------------------------------------