From 146ef5d0b86ca777f0ecd4eb5982f8beb4ea6e12 Mon Sep 17 00:00:00 2001 From: jdhao Date: Thu, 13 Jan 2022 20:27:48 +0800 Subject: [PATCH] use lua for may_create_dir --- autoload/utils.vim | 9 --------- core/autocommands.vim | 2 +- lua/utils.lua | 10 ++++++++++ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/autoload/utils.vim b/autoload/utils.vim index 2fa692e..580c1cd 100644 --- a/autoload/utils.vim +++ b/autoload/utils.vim @@ -207,12 +207,3 @@ function! utils#add_pack(name) abort return l:status endfunction - -" Create the parent dir for current file if it does not exist -function! utils#may_create_dir(path) abort - let l:parent_dir = fnamemodify(a:path, ':p:h') - - if !isdirectory(l:parent_dir) - call mkdir(l:parent_dir, "p") - endif -endfunction diff --git a/core/autocommands.vim b/core/autocommands.vim index c3943da..d929637 100644 --- a/core/autocommands.vim +++ b/core/autocommands.vim @@ -121,5 +121,5 @@ augroup END augroup auto_create_dir autocmd! - autocmd BufWritePre * call utils#may_create_dir(expand("")) + autocmd BufWritePre * lua require('utils').may_create_dir() augroup END diff --git a/lua/utils.lua b/lua/utils.lua index a4c02f2..7deb322 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -14,4 +14,14 @@ function M.executable(name) return false end +function M.may_create_dir() + local fpath = vim.fn.expand('') + local parent_dir = vim.fn.fnamemodify(fpath, ":p:h") + local res = vim.fn.isdirectory(parent_dir) + + if res == 0 then + vim.fn.mkdir(parent_dir, 'p') + end +end + return M