[nvim] check for URLs in FormatOnSave

This commit is contained in:
Dmytro Meleshko 2020-03-08 02:49:27 +02:00
parent b8ba43a562
commit f8e9206692

View file

@ -110,13 +110,16 @@ nnoremap <silent><expr> <CR> &buftype is# '' ? ":write<bar>wall\<CR>" : "\<CR>"
" on save (BufWritePre) {{{ " on save (BufWritePre) {{{
function s:IsUrl(str)
return a:str =~# '\v^\w+://'
endfunction
" create directory {{{ " create directory {{{
" Creates the parent directory of the file if it doesn't exist " Creates the parent directory of the file if it doesn't exist
function s:CreateDirOnSave() function s:CreateDirOnSave()
" <afile> is the filename of the buffer where the autocommand is executed
let l:file = expand('<afile>') let l:file = expand('<afile>')
" check if this is a regular file and its path is not a URL " check if this is a regular file and its path is not a URL
if empty(&buftype) && l:file !~# '\v^\w+://' if empty(&buftype) && !s:IsUrl(l:file)
let l:dir = fnamemodify(l:file, ':h') let l:dir = fnamemodify(l:file, ':h')
if !isdirectory(l:dir) | call mkdir(l:dir, 'p') | endif if !isdirectory(l:dir) | call mkdir(l:dir, 'p') | endif
endif endif
@ -137,7 +140,8 @@ nnoremap <silent><expr> <CR> &buftype is# '' ? ":write<bar>wall\<CR>" : "\<CR>"
" auto-format with Coc.nvim {{{ " auto-format with Coc.nvim {{{
let g:coc_format_on_save_ignore = [] let g:coc_format_on_save_ignore = []
function s:FormatOnSave() function s:FormatOnSave()
if IsCocEnabled() && index(g:coc_format_on_save_ignore, &filetype) < 0 let l:file = expand('<afile>')
if IsCocEnabled() && !s:IsUrl(l:file) && index(g:coc_format_on_save_ignore, &filetype) < 0
silent CocFormat silent CocFormat
endif endif
endfunction endfunction