Add heuristics for .h extension

This commit is contained in:
Adam Stankiewicz
2020-09-06 23:37:24 +02:00
parent 48254ec7ad
commit 46040f3ffd
8 changed files with 107 additions and 25 deletions

View File

@@ -154,6 +154,25 @@ func! polyglot#DetectAspFiletype()
setf aspvbs | return
endfunc
func! polyglot#DetectHFiletype()
for lnum in range(1, min([line("$"), 200]))
let line = getline(lnum)
if line =~# '^\s*\(@\(interface\|class\|protocol\|property\|end\|synchronised\|selector\|implementation\)\(\<\|\>\)\|#import\s\+.\+\.h[">]\)'
if exists("g:c_syntax_for_h")
setf objc | return
endif
setf objcpp | return
endif
endfor
if exists("g:c_syntax_for_h")
setf c | return
endif
if exists("g:ch_syntax_for_h")
setf ch | return
endif
setf cpp | return
endfunc
func! polyglot#DetectMFiletype()
let saw_comment = 0
for lnum in range(1, min([line("$"), 100]))

View File

@@ -10,7 +10,7 @@ let s:globs = {
\ 'abc': '*.abc',
\ 'abel': '*.abl',
\ 'acedb': '*.wrm',
\ 'ada': '*.adb,*.ads,*.ada,*.gpr',
\ 'ada': '*.adb,*.ads,*.ada,*.adc,*.gpr,*.ada_m',
\ 'ahdl': '*.tdf',
\ 'aidl': '*.aidl',
\ 'alsaconf': '',
@@ -34,17 +34,17 @@ let s:globs = {
\ 'autoit': '*.au3',
\ 'automake': '[Mm]akefile.am,GNUmakefile.am',
\ 'ave': '*.ave',
\ 'awk': '*.awk',
\ 'awk': '*.awk,*.gawk',
\ 'basic': '*.basic',
\ 'blade': '*.blade,*.blade.php',
\ 'brewfile': 'Brewfile',
\ 'c': '*.c,*.cats,*.h,*.idc',
\ 'c': '*.c,*.cats,*.h,*.idc,*.qc',
\ 'caddyfile': 'Caddyfile',
\ 'carp': '*.carp',
\ 'clojure': '*.clj,*.boot,*.cl2,*.cljc,*.cljs,*.cljs.hl,*.cljscm,*.cljx,*.hic,*.edn,riemann.config,build.boot,profile.boot',
\ 'cmake': '*.cmake,*.cmake.in,CMakeLists.txt',
\ 'coffee': '*.coffee,*._coffee,*.cake,*.cjsx,*.iced,*.coffeekup,Cakefile',
\ 'cpp': '*.cpp,*.c++,*.cc,*.cp,*.cxx,*.h,*.h++,*.hh,*.hpp,*.hxx,*.inc,*.inl,*.ipp,*.tcc,*.tpp',
\ 'cpp': '*.cpp,*.c++,*.cc,*.cp,*.cxx,*.h,*.h++,*.hh,*.hpp,*.hxx,*.inc,*.inl,*.ipp,*.tcc,*.tpp,*.moc',
\ 'cql': '*.cql',
\ 'cryptol': '*.cry,*.cyl,*.lcry,*.lcyl',
\ 'crystal': '*.cr,Projectfile',
@@ -52,7 +52,7 @@ let s:globs = {
\ 'cucumber': '*.feature,*.story',
\ 'cuesheet': '*.cue',
\ 'd': '*.d,*.di',
\ 'dart': '*.dart',
\ 'dart': '*.dart,*.drt',
\ 'dcov': '*.lst',
\ 'dd': '*.dd',
\ 'ddoc': '*.ddoc',

View File

@@ -134,7 +134,7 @@ if !has_key(s:disabled_packages, 'awk')
endif
if !has_key(s:disabled_packages, 'c/c++')
au! BufRead,BufNewFile *.c,*.cpp,*.tpp
au! BufRead,BufNewFile *.cpp,*.tpp,*.c
endif
if !has_key(s:disabled_packages, 'cmake')
@@ -343,7 +343,9 @@ endif
if !has_key(s:disabled_packages, 'ada')
au BufNewFile,BufRead *.ada setf ada
au BufNewFile,BufRead *.ada_m setf ada
au BufNewFile,BufRead *.adb setf ada
au BufNewFile,BufRead *.adc setf ada
au BufNewFile,BufRead *.ads setf ada
au BufNewFile,BufRead *.gpr setf ada
endif
@@ -465,6 +467,7 @@ endif
if !has_key(s:disabled_packages, 'awk')
au BufNewFile,BufRead *.awk setf awk
au BufNewFile,BufRead *.gawk setf awk
endif
if !has_key(s:disabled_packages, 'reason')
@@ -473,16 +476,11 @@ if !has_key(s:disabled_packages, 'reason')
endif
if !has_key(s:disabled_packages, 'c/c++')
au BufNewFile,BufRead *.c setf c
au BufNewFile,BufRead *.cats setf c
au BufNewFile,BufRead *.h setf c
au BufNewFile,BufRead *.idc setf c
au BufNewFile,BufRead *.c++ setf cpp
au BufNewFile,BufRead *.cc setf cpp
au BufNewFile,BufRead *.cp setf cpp
au BufNewFile,BufRead *.cpp setf cpp
au BufNewFile,BufRead *.cxx setf cpp
au BufNewFile,BufRead *.h setf cpp
au BufNewFile,BufRead *.h++ setf cpp
au BufNewFile,BufRead *.hh setf cpp
au BufNewFile,BufRead *.hpp setf cpp
@@ -490,8 +488,14 @@ if !has_key(s:disabled_packages, 'c/c++')
au BufNewFile,BufRead *.inc setf cpp
au BufNewFile,BufRead *.inl setf cpp
au BufNewFile,BufRead *.ipp setf cpp
au BufNewFile,BufRead *.moc setf cpp
au BufNewFile,BufRead *.tcc setf cpp
au BufNewFile,BufRead *.tpp setf cpp
au BufNewFile,BufRead *.c setf c
au BufNewFile,BufRead *.cats setf c
au BufNewFile,BufRead *.idc setf c
au BufNewFile,BufRead *.qc setf c
au! BufNewFile,BufRead *.h call polyglot#DetectHFiletype()
endif
if !has_key(s:disabled_packages, 'caddyfile')
@@ -566,6 +570,7 @@ endif
if !has_key(s:disabled_packages, 'dart')
au BufNewFile,BufRead *.dart setf dart
au BufNewFile,BufRead *.drt setf dart
endif
if !has_key(s:disabled_packages, 'dhall')
@@ -1065,7 +1070,7 @@ if !has_key(s:disabled_packages, 'nix')
endif
if !has_key(s:disabled_packages, 'objc')
au BufNewFile,BufRead *.h setf objc
au! BufNewFile,BufRead *.h call polyglot#DetectHFiletype()
au! BufNewFile,BufRead *.m call polyglot#DetectMFiletype()
endif

View File

@@ -23,6 +23,20 @@ rules:
filetype: aspperl
- filetype: aspvbs
---
extensions: [h]
rules:
- lines: 200
pattern: '^\s*(@(interface|class|protocol|property|end|synchronised|selector|implementation)\b|#import\s+.+\.h[">])'
rules:
- if_exists: "g:c_syntax_for_h"
filetype: objc
- filetype: objcpp
- if_exists: "g:c_syntax_for_h"
filetype: c
- if_exists: "g:ch_syntax_for_h"
filetype: ch
- filetype: cpp
---
extensions: [m]
rules:
- lines: 100

View File

@@ -93,7 +93,9 @@ filetypes:
- adb
- ads
- ada
- adc
- gpr
- ada_m
---
name: ahdl
remote: vim/vim:runtime
@@ -191,6 +193,11 @@ filetypes:
- '*/.aptitude/config'
- '*/etc/apt/apt.conf.d/[^.]*'
- '*/etc/apt/apt.conf.d/*.conf'
ignored_warnings:
# some kind of bugs in filetype detection algorithm
- '*/etc/apt/apt.conf.d/{[-_[:alnum:]]\+,[-_.[:alnum:]]\+.conf}'
- '*/etc/apt/apt.conf.d/]\+'
- '*/etc/apt/apt.conf.d/]\+.conf'
---
name: arch
remote: vim/vim:runtime
@@ -302,19 +309,25 @@ filetypes:
- name: awk
extensions:
- awk
- gawk
---
name: c/c++
remote: vim-jp/vim-cpp
filetypes:
- name: c
linguist: C
- name: cpp
linguist: C++
extra_extensions:
- moc
ignored_extensions:
# conflicts with more popular reason, remove after heuristics work
- re
# implemented by arduino
- ino
- name: c
linguist: C
extra_extensions:
# Quake C
- qc
---
name: caddyfile
remote: isobit/vim-caddyfile
@@ -358,10 +371,9 @@ filetypes:
extra_extensions:
# 17 results: https://github.com/search?q=extension%3Acoffeekup+html&type=Code
- coffeekup
ignored_filenames:
ignored_warnings:
# Probably mistake
- '*Cakefile'
ignored_warnings:
# Used by C#
- csx
# Used by Chuck
@@ -430,6 +442,8 @@ remote: dart-lang/dart-vim-plugin
filetypes:
- name: dart
linguist: Dart
extra_extensions:
- drt
---
name: dhall
remote: vmchale/dhall-vim
@@ -470,7 +484,7 @@ filetypes:
filenames:
- 'docker-compose*.yaml'
- 'docker-compose*.yml'
ignored_filenames:
ignored_warnings:
# Probably mistakes?
- 'docker-compose*.yaml*'
- 'docker-compose*.yml*'

View File

@@ -268,10 +268,6 @@ def rule_to_code(rule)
end
end
if rule.has_key?("rules")
return rule["rules"].map { |r| indent(rule_to_code(r), 0) }.join("\n")
end
if rule.has_key?("pattern") || rule.has_key?("or") || rule.has_key?("and")
return <<~EOS
if #{pattern_to_condition(rule)}
@@ -280,6 +276,10 @@ def rule_to_code(rule)
EOS
end
if rule.has_key?("rules")
return rule["rules"].map { |r| indent(rule_to_code(r), 0) }.join("\n")
end
if rule.has_key?("if_set")
return <<~EOS
if #{rule["negative"] ? "!" : ""}#{rule["if_set"]}
@@ -288,6 +288,14 @@ def rule_to_code(rule)
EOS
end
if rule.has_key?("if_exists")
return <<~EOS
if #{rule["negative"] ? "!" : ""}exists("#{rule["if_exists"]}")
#{indent(rule_to_code(except(rule, "if_exists", "negative")), 2)}
endif
EOS
end
if rule.has_key?("set")
return <<~EOS
let #{rule["set"]} = 1
@@ -455,7 +463,7 @@ def generate_ftdetect(packages, heuristics)
extensions = filetype["extensions"]
filenames = filetype["filenames"]
expected_extensions = all_filetypes.has_key?(name) ? all_filetypes.fetch(name)[:extensions] : []
expected_extensions = (all_filetypes.has_key?(name) ? all_filetypes.fetch(name)[:extensions] : []).map(&:downcase)
ignored_extensions = expand_all(filetype.fetch("ignored_extensions", []))
ignored_warnings = expand_all(filetype.fetch("ignored_warnings", []))

View File

@@ -181,6 +181,18 @@ call TestExtension("ave", "foobar.ave", "")
" Awk
call TestExtension("awk", "foobar.awk", "")
" C++
call TestExtension("c", "foobar.c", "")
call TestExtension("cpp", "foobar.cxx", "")
call TestExtension("cpp", "foobar.c++", "")
call TestExtension("cpp", "foobar.hh", "")
call TestExtension("cpp", "foobar.hxx", "")
call TestExtension("cpp", "foobar.hpp", "")
call TestExtension("cpp", "foobar.ipp", "")
call TestExtension("cpp", "foobar.moc", "")
call TestExtension("cpp", "foobar.tcc", "")
call TestExtension("cpp", "foobar.inl", "")
" vim-polyglot only
call TestExtension("blade", "test.blade.php", "")
call TestExtension("yaml.ansible", "playbook.yml", "")
@@ -233,7 +245,7 @@ call TestExtension("vb", "vb.dsm", "")
call TestExtension("vb", "vb.dsm", "")
call TestExtension("vb", "vb.ctl", "")
" Idris
" .idr extension
call TestExtension("idris", "lowercase.idr", "--idris1")
call TestExtension("idris", "uppercase.idr", "--Idris1")
call TestExtension("idris", "start-space-l.idr", "-- idris1")
@@ -253,7 +265,17 @@ call TestExtension("idris2", "two-spaces-u.idr", "-- Idris 2")
call TestExtension("idris2", "mypkg.ipkg", "package mypkg\n\ndepends = effects")
call TestExtension("idris2", "use-post-proj.idr", "%language PostfixProjections")
" Literate Idris
" .lidr extension
call TestExtension("lidris", "lidris-1.lidr", "Some test plaintext\n\n> --idris1\n> myfact : Nat -> Nat\n> myfact Z = 1\n> myfact (S k) = (S k) * myfact k\n\nMore plaintext")
call TestExtension("lidris2", "lidris-2.lidr", "Some test plaintext\n\n> --idris2\n> myfact : Nat -> Nat\n> myfact Z = 1\n> myfact (S k) = (S k) * myfact k\n\nMore plaintext")
" .h extension
call TestExtension("objcpp", "foo.h", "@interface MTNavigationController : UINavigationController")
call TestExtension("cpp", "foo.h", "")
let g:c_syntax_for_h = 1
call TestExtension("objc", "foo.h", "@interface MTNavigationController : UINavigationController")
call TestExtension("c", "foo.h", "")
unlet g:c_syntax_for_h
let g:ch_syntax_for_h = 1
call TestExtension("ch", "foo.h", "")

View File

@@ -45,8 +45,8 @@ call TestFiletype('autoit')
call TestFiletype('ave')
call TestFiletype('awk')
call TestFiletype('reason')
call TestFiletype('c')
call TestFiletype('cpp')
call TestFiletype('c')
call TestFiletype('caddyfile')
call TestFiletype('carp')
call TestFiletype('clojure')