Compare commits

...

1 Commits

Author SHA1 Message Date
Adam Stankiewicz
9243367ba3 Automatically detect script filetype when typing 2020-09-10 15:40:27 +02:00
4 changed files with 43 additions and 26 deletions

View File

@@ -7,4 +7,4 @@ test:
@ scripts/test
dev:
@ echo "packages.yaml\nheuristics.yaml\nscripts/test\nscripts/build\nscripts/test_extensions.vim" | DEV=1 entr bash -c 'make && make test'
@ (ls && find scripts) | DEV=1 entr bash -c 'make && make test'

View File

@@ -4,11 +4,13 @@ set cpo&vim
func! polyglot#Heuristics()
" Try to detect filetype from shebang
let l:filetype = polyglot#Shebang()
if l:filetype != ""
exec "setf " . l:filetype
return
let filetype = polyglot#Shebang()
if filetype != ""
exec "setf " . filetype
return 1
endif
return 0
endfunc
let s:interpreters = {
@@ -86,37 +88,41 @@ let s:r_envflag = '%(\S\+=\S\+\|-[iS]\|--ignore-environment\|--split-string\)'
let s:r_env = '^\%(\' . s:r_envflag . '\s\+\)*\(\S\+\)'
func! polyglot#Shebang()
let l:line1 = getline(1)
let line1 = getline(1)
if l:line1 !~# "^#!"
if line1 !~# "^#!"
return
endif
let l:pathrest = matchlist(l:line1, s:r_hashbang)
let pathrest = matchlist(line1, s:r_hashbang)
if len(l:pathrest) == 0
if len(pathrest) == 0
return
endif
let [_, l:path, l:rest; __] = l:pathrest
let [_, path, rest; __] = pathrest
let l:script = split(l:path, "/")[-1]
let script = split(path, "/")[-1]
if l:script == "env"
let l:argspath = matchlist(l:rest, s:r_env)
if len(l:argspath) == 0
if len(script) == 0
return
endif
let l:script = l:argspath[1]
if script == "env"
let argspath = matchlist(rest, s:r_env)
if len(argspath) == 0
return
endif
if has_key(s:interpreters, l:script)
return s:interpreters[l:script]
let script = argspath[1]
endif
if has_key(s:interpreters, script)
return s:interpreters[script]
endif
for interpreter in keys(s:interpreters)
if l:script =~# '^' . interpreter
if script =~# '^' . interpreter
return s:interpreters[interpreter]
endif
endfor

View File

@@ -1911,12 +1911,11 @@ endif
" end filetypes
augroup END
au BufNewFile,BufRead,StdinReadPost *
\ if !did_filetype() && expand("<afile>") !~ g:ft_ignore_pat
\ | call polyglot#Heuristics() | endif
augroup END
if !has_key(s:disabled_packages, 'autoindent')
" Code below re-implements sleuth for vim-polyglot
@@ -2088,10 +2087,10 @@ if !has_key(s:disabled_packages, 'autoindent')
endif
endfunction
augroup polyglot
autocmd!
autocmd FileType * call s:detect_indent()
autocmd User Flags call Hoist('buffer', 5, 'SleuthIndicator')
augroup polyglot-sleuth
au!
au FileType * call s:detect_indent()
au User Flags call Hoist('buffer', 5, 'SleuthIndicator')
augroup END
command! -bar -bang Sleuth call s:detect_indent()
@@ -2109,7 +2108,17 @@ func! s:verify()
endif
endfunc
autocmd VimEnter * call s:verify()
au VimEnter * call s:verify()
func! s:observe_filetype()
augroup polyglot-observer
au! CursorHold,CursorHoldI <buffer>
\ if polyglot#Heuristics() | au! polyglot-observer CursorHold,CursorHoldI | endif
augroup END
endfunc
au BufEnter * if &ft == "" && expand("<afile>") !~ g:ft_ignore_pat
\ | call s:observe_filetype() | endif
" restore Vi compatibility settings
let &cpo = s:cpo_save

View File

@@ -559,8 +559,10 @@ def generate_ftdetect(packages, heuristics)
let l:filetype = polyglot#Shebang()
if l:filetype != ""
exec "setf " . l:filetype
return
return 1
endif
return 0
endfunc
let s:interpreters = {