diff options
author | Adam Stankiewicz <sheerun@sher.pl> | 2020-09-10 15:40:27 +0200 |
---|---|---|
committer | Adam Stankiewicz <sheerun@sher.pl> | 2020-09-10 15:40:27 +0200 |
commit | 9243367ba376050621e4c05e8f0439742c1f0f82 (patch) | |
tree | da72230c3a5258fe2be8c90ef0301ac1edd7d547 | |
parent | 1eed30b2af57538496943da09e298a6facc33002 (diff) | |
download | vim-polyglot-9243367ba376050621e4c05e8f0439742c1f0f82.tar.gz vim-polyglot-9243367ba376050621e4c05e8f0439742c1f0f82.zip |
Automatically detect script filetype when typingv4.10.0
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | autoload/polyglot.vim | 40 | ||||
-rw-r--r-- | ftdetect/polyglot.vim | 23 | ||||
-rwxr-xr-x | scripts/build | 4 |
4 files changed, 43 insertions, 26 deletions
@@ -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' diff --git a/autoload/polyglot.vim b/autoload/polyglot.vim index d54cb87e..d89108aa 100644 --- a/autoload/polyglot.vim +++ b/autoload/polyglot.vim @@ -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 script = split(path, "/")[-1] - let l:script = split(l:path, "/")[-1] + if len(script) == 0 + return + endif - if l:script == "env" - let l:argspath = matchlist(l:rest, s:r_env) - if len(l:argspath) == 0 + if script == "env" + let argspath = matchlist(rest, s:r_env) + if len(argspath) == 0 return endif - let l:script = l:argspath[1] + let script = argspath[1] endif - if has_key(s:interpreters, l:script) - return s:interpreters[l:script] + 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 diff --git a/ftdetect/polyglot.vim b/ftdetect/polyglot.vim index 5a95caa1..0fa71e9c 100644 --- a/ftdetect/polyglot.vim +++ b/ftdetect/polyglot.vim @@ -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 diff --git a/scripts/build b/scripts/build index 03c005c4..982e0f3a 100755 --- a/scripts/build +++ b/scripts/build @@ -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 = { |