Skip to content

Commit

Permalink
Write heuristics for perl, closes #550
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed Sep 16, 2020
1 parent 2716792 commit 17c2b63
Show file tree
Hide file tree
Showing 16 changed files with 1,035 additions and 42 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A collection of language packs for Vim.
> One to rule them all, one to find them, one to bring them all and in the darkness bind them.
- It **won't affect your startup time**, as scripts are loaded only on demand\*.
- It **installs and updates 120+ times faster** than the <!--Package Count-->196<!--/Package Count--> packages it consists of.
- It **installs and updates 120+ times faster** than the <!--Package Count-->199<!--/Package Count--> packages it consists of.
- It is more secure because scripts loaded for all extensions are generated by vim-polyglot (ftdetect).
- Solid syntax and indentation support (other features skipped). Only the best language packs.
- All unnecessary files are ignored (like enormous documentation from php support).
Expand Down Expand Up @@ -81,6 +81,7 @@ If you need full functionality of any plugin, please use it directly with your p
- [basic](https://github.com/vim/vim/tree/master/runtime)
- [blade](https://github.com/jwalton512/vim-blade)
- [brewfile](https://github.com/bfontaine/Brewfile.vim)
- [bzl](https://github.com/vim/vim/tree/master/runtime)
- [c/c++](https://github.com/vim-jp/vim-cpp)
- [caddyfile](https://github.com/isobit/vim-caddyfile)
- [carp](https://github.com/hellerve/carp-vim)
Expand Down Expand Up @@ -177,6 +178,7 @@ If you need full functionality of any plugin, please use it directly with your p
- [plantuml](https://github.com/aklt/plantuml-syntax)
- [pony](https://github.com/jakwings/vim-pony)
- [powershell](https://github.com/PProvost/vim-ps1)
- [prolog](https://github.com/vim/vim/tree/master/runtime)
- [protobuf](https://github.com/uarun/vim-protobuf)
- [pug](https://github.com/digitaltoad/vim-pug)
- [puppet](https://github.com/rodjek/vim-puppet)
Expand Down Expand Up @@ -213,6 +215,7 @@ If you need full functionality of any plugin, please use it directly with your p
- [swift](https://github.com/keith/swift.vim)
- [sxhkd](https://github.com/baskerville/vim-sxhkdrc)
- [systemd](https://github.com/wgwoods/vim-systemd-syntax)
- [tads](https://github.com/vim/vim/tree/master/runtime)
- [terraform](https://github.com/hashivim/vim-terraform)
- [textile](https://github.com/timcharper/textile.vim)
- [thrift](https://github.com/solarnz/thrift.vim)
Expand Down
103 changes: 94 additions & 9 deletions autoload/polyglot.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ func! s:Setf(ft)
endif
endfunc

func! polyglot#Heuristics()
func! polyglot#Shebang()
" Try to detect filetype from shebang
let l:filetype = polyglot#Shebang()
if l:filetype != ""
exec "setf " . l:filetype
let ft = polyglot#ShebangFiletype()
if ft != ""
call s:Setf(ft)
return 1
endif

Expand Down Expand Up @@ -52,6 +52,8 @@ let s:interpreters = {
\ 'cperl': 'perl',
\ 'perl': 'perl',
\ 'php': 'php',
\ 'swipl': 'prolog',
\ 'yap': 'prolog',
\ 'pwsh': 'ps1',
\ 'python': 'python',
\ 'python2': 'python',
Expand Down Expand Up @@ -95,7 +97,7 @@ let s:r_hashbang = '^#!\s*\(\S\+\)\s*\(.*\)\s*'
let s:r_envflag = '%(\S\+=\S\+\|-[iS]\|--ignore-environment\|--split-string\)'
let s:r_env = '^\%(\' . s:r_envflag . '\s\+\)*\(\S\+\)'

func! polyglot#Shebang()
func! polyglot#ShebangFiletype()
let l:line1 = getline(1)

if l:line1 !~# "^#!"
Expand Down Expand Up @@ -133,7 +135,7 @@ func! polyglot#Shebang()
endfunc

func! polyglot#DetectInpFiletype()
let line = getline(1)
let line = getline(nextnonblank(1))
if line =~# '^\*'
call s:Setf('abaqus') | return
endif
Expand Down Expand Up @@ -269,10 +271,10 @@ func! polyglot#DetectIdrFiletype()
if line =~# '^%access .*'
call s:Setf('idris') | return
endif
if exists("g:filetype_idr")
call s:Setf(g:filetype_idr) | return
endif
endfor
if exists("g:filetype_idr")
call s:Setf(g:filetype_idr) | return
endif
call s:Setf('idris2') | return
endfunc

Expand All @@ -296,6 +298,89 @@ func! polyglot#DetectBasFiletype()
call s:Setf('basic') | return
endfunc

func! polyglot#DetectPmFiletype()
let line = getline(nextnonblank(1))
if line =~# 'XPM2'
call s:Setf('xpm2') | return
endif
if line =~# 'XPM'
call s:Setf('xpm') | return
endif
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\s*\%(use\s\+v6\(\<\|\>\)\|\(\<\|\>\)module\(\<\|\>\)\|\(\<\|\>\)\%(my\s\+\)\=class\(\<\|\>\)\)'
call s:Setf('raku') | return
endif
if line =~# '\(\<\|\>\)use\s\+\%(strict\(\<\|\>\)\|v\=5\.\)'
call s:Setf('perl') | return
endif
endfor
if exists("g:filetype_pm")
call s:Setf(g:filetype_pm) | return
endif
call s:Setf('perl') | return
endfunc

func! polyglot#DetectPlFiletype()
let line = getline(nextnonblank(1))
if line =~# '^[^#]*:-' || line =~# '^\s*\%(%\|/\*\)' || line =~# '\.\s*$'
call s:Setf('prolog') | return
endif
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\s*\%(use\s\+v6\(\<\|\>\)\|\(\<\|\>\)module\(\<\|\>\)\|\(\<\|\>\)\%(my\s\+\)\=class\(\<\|\>\)\)'
call s:Setf('raku') | return
endif
if line =~# '\(\<\|\>\)use\s\+\%(strict\(\<\|\>\)\|v\=5\.\)'
call s:Setf('perl') | return
endif
endfor
if exists("g:filetype_pl")
call s:Setf(g:filetype_pl) | return
endif
call s:Setf('perl') | return
endfunc

func! polyglot#DetectTFiletype()
for lnum in range(1, min([line("$"), 5]))
let line = getline(lnum)
if line =~# '^\.'
call s:Setf('nroff') | return
endif
endfor
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\s*\%(use\s\+v6\(\<\|\>\)\|\(\<\|\>\)module\(\<\|\>\)\|\(\<\|\>\)\%(my\s\+\)\=class\(\<\|\>\)\)'
call s:Setf('raku') | return
endif
if line =~# '\(\<\|\>\)use\s\+\%(strict\(\<\|\>\)\|v\=5\.\)'
call s:Setf('perl') | return
endif
endfor
if exists("g:filetype_t")
call s:Setf(g:filetype_t) | return
endif
call s:Setf('perl') | return
endfunc

func! polyglot#DetectTt2Filetype()
for lnum in range(1, min([line("$"), 3]))
let line = getline(lnum)
if line =~? '<\%(!DOCTYPE HTML\|[%?]\|html\)'
call s:Setf('tt2html') | return
endif
endfor
call s:Setf('tt2') | return
endfunc

func! polyglot#DetectHtmlFiletype()
let line = getline(nextnonblank(1))
if line =~# '^\(%\|<[%&].*>\)'
call s:Setf('mason') | return
endif
call s:Setf('html') | return
endfunc

" Restore 'cpoptions'
let &cpo = s:cpo_save
unlet s:cpo_save
8 changes: 8 additions & 0 deletions autoload/sleuth.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let s:globs = {
\ 'basic': '*.basic',
\ 'blade': '*.blade,*.blade.php',
\ 'brewfile': 'Brewfile',
\ 'bzl': '*.bzl,BUCK,BUILD,BUILD.bazel,Tiltfile,WORKSPACE',
\ 'c': '*.c,*.cats,*.h,*.idc,*.qc',
\ 'caddyfile': 'Caddyfile',
\ 'carp': '*.carp',
Expand Down Expand Up @@ -129,6 +130,7 @@ let s:globs = {
\ 'mako': '*.mako,*.mao',
\ 'markdown': '*.md,*.markdown,*.mdown,*.mdwn,*.mkd,*.mkdn,*.mkdown,*.ronn,*.workbook,contents.lr',
\ 'markdown.mdx': '*.mdx',
\ 'mason': '*.mason,*.mhtml,*.comp',
\ 'meson': 'meson.build,meson_options.txt',
\ 'mma': '*.mathematica,*.cdf,*.m,*.ma,*.mt,*.nb,*.nbp,*.wl,*.wlt,*.wls,*.mma',
\ 'moon': '*.moon',
Expand All @@ -151,7 +153,9 @@ let s:globs = {
\ 'perl': '*.pl,*.al,*.cgi,*.fcgi,*.perl,*.ph,*.plx,*.pm,*.psgi,*.t,Makefile.PL,Rexfile,ack,cpanfile',
\ 'php': '*.php,*.aw,*.ctp,*.fcgi,*.inc,*.php3,*.php4,*.php5,*.phps,*.phpt,Phakefile',
\ 'plantuml': '*.puml,*.iuml,*.plantuml,*.uml,*.pu',
\ 'pod': '*.pod',
\ 'pony': '*.pony',
\ 'prolog': '*.pl,*.pro,*.prolog,*.yap',
\ 'proto': '*.proto',
\ 'ps1': '*.ps1,*.psd1,*.psm1,*.pssc',
\ 'ps1xml': '*.ps1xml',
Expand Down Expand Up @@ -190,13 +194,16 @@ let s:globs = {
\ 'sxhkdrc': '*.sxhkdrc,sxhkdrc',
\ 'systemd': '*.automount,*.mount,*.path,*.service,*.socket,*.swap,*.target,*.timer',
\ 'tablegen': '*.td',
\ 'tads': '*.t',
\ 'terraform': '*.hcl,*.nomad,*.tf,*.tfvars,*.workflow',
\ 'textile': '*.textile',
\ 'thrift': '*.thrift',
\ 'tmux': '',
\ 'toml': '*.toml,Cargo.lock,Gopkg.lock,poetry.lock,Pipfile',
\ 'tptp': '*.p,*.tptp,*.ax',
\ 'trasys': '*.inp',
\ 'tt2': '*.tt2',
\ 'tt2html': '*.tt2',
\ 'typescript': '*.ts',
\ 'typescriptreact': '*.tsx',
\ 'unison': '*.u,*.uu',
Expand All @@ -211,6 +218,7 @@ let s:globs = {
\ 'xdc': '*.xdc',
\ 'xml': '*.xml,*.adml,*.admx,*.ant,*.axml,*.builds,*.ccproj,*.ccxml,*.clixml,*.cproject,*.cscfg,*.csdef,*.csl,*.csproj,*.ct,*.depproj,*.dita,*.ditamap,*.ditaval,*.dll.config,*.dotsettings,*.filters,*.fsproj,*.fxml,*.glade,*.gml,*.gmx,*.grxml,*.gst,*.iml,*.ivy,*.jelly,*.jsproj,*.kml,*.launch,*.mdpolicy,*.mjml,*.mm,*.mod,*.mxml,*.natvis,*.ncl,*.ndproj,*.nproj,*.nuspec,*.odd,*.osm,*.pkgproj,*.pluginspec,*.proj,*.props,*.ps1xml,*.psc1,*.pt,*.rdf,*.resx,*.rss,*.sch,*.scxml,*.sfproj,*.shproj,*.srdf,*.storyboard,*.sublime-snippet,*.targets,*.tml,*.ui,*.urdf,*.ux,*.vbproj,*.vcxproj,*.vsixmanifest,*.vssettings,*.vstemplate,*.vxml,*.wixproj,*.workflow,*.wsdl,*.wsf,*.wxi,*.wxl,*.wxs,*.x3d,*.xacro,*.xaml,*.xib,*.xlf,*.xliff,*.xmi,*.xml.dist,*.xproj,*.xsd,*.xspec,*.xul,*.zcml,*.cdxml,App.config,NuGet.config,Settings.StyleCop,Web.Debug.config,Web.Release.config,Web.config,packages.config',
\ 'xml.twig': '*.xml.twig',
\ 'xs': '*.xs',
\ 'xsl': '*.xslt,*.xsl',
\ 'yaml': '*.yml,*.mir,*.reek,*.rviz,*.sublime-syntax,*.syntax,*.yaml,*.yaml-tmlanguage,*.yaml.sed,*.yml.mysql,glide.lock,yarn.lock,fish_history,fish_read_history',
\ 'yaml.ansible': 'playbook.y{a,}ml,site.y{a,}ml,main.y{a,}ml,local.y{a,}ml,requirements.y{a,}ml,tasks.*.y{a,}ml,roles.*.y{a,}ml,handlers.*.y{a,}ml',
Expand Down
Loading

0 comments on commit 17c2b63

Please sign in to comment.