summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Stankiewicz <sheerun@sher.pl>2020-09-01 23:02:36 +0200
committerAdam Stankiewicz <sheerun@sher.pl>2020-09-01 23:02:36 +0200
commitf2ef4cedecf554c0c9f0694f93df1d53a57bf70c (patch)
treeee95ac48f56aee9607aa3c92fe478cf63f9c26b7
parent45c1923f43f2bb2e626fe910654d4395a0e50a04 (diff)
downloadvim-polyglot-f2ef4cedecf554c0c9f0694f93df1d53a57bf70c.tar.gz
vim-polyglot-f2ef4cedecf554c0c9f0694f93df1d53a57bf70c.zip
Fix reason filetype detection, closes #532v4.7.1
-rw-r--r--autoload/polyglot.vim10
-rw-r--r--ftdetect/polyglot.vim12
-rw-r--r--heuristics.yaml8
-rw-r--r--packages.yaml1
-rwxr-xr-xscripts/build14
-rw-r--r--scripts/test_extensions.vim7
-rw-r--r--scripts/test_filetypes.vim2
7 files changed, 44 insertions, 10 deletions
diff --git a/autoload/polyglot.vim b/autoload/polyglot.vim
index 7d5aa867..3753dc8e 100644
--- a/autoload/polyglot.vim
+++ b/autoload/polyglot.vim
@@ -202,6 +202,16 @@ func! polyglot#DetectFsFiletype()
setf forth | return
endfunc
+func! polyglot#DetectReFiletype()
+ for lnum in range(1, min([line("$"), 50]))
+ let line = getline(lnum)
+ if line =~# '^\s*#\%(\%(if\|ifdef\|define\|pragma\)\s\+\w\|\s*include\s\+[<"]\|template\s*<\)'
+ setf cpp | return
+ endif
+ setf reason | return
+ endfor
+endfunc
+
" Restore 'cpoptions'
let &cpo = s:cpo_save
unlet s:cpo_save
diff --git a/ftdetect/polyglot.vim b/ftdetect/polyglot.vim
index 974aa924..9e86a7ad 100644
--- a/ftdetect/polyglot.vim
+++ b/ftdetect/polyglot.vim
@@ -218,6 +218,11 @@ if !has_key(s:disabled_packages, 'awk')
au BufNewFile,BufRead *.awk setf awk
endif
+if !has_key(s:disabled_packages, 'reason')
+ au BufNewFile,BufRead *.rei setf reason
+ au! BufNewFile,BufRead *.re call polyglot#DetectReFiletype()
+endif
+
if !has_key(s:disabled_packages, 'c/c++')
au BufNewFile,BufRead *.c setf c
au BufNewFile,BufRead *.cats setf c
@@ -237,9 +242,9 @@ if !has_key(s:disabled_packages, 'c/c++')
au BufNewFile,BufRead *.inl setf cpp
au BufNewFile,BufRead *.ino setf cpp
au BufNewFile,BufRead *.ipp setf cpp
- au BufNewFile,BufRead *.re setf cpp
au BufNewFile,BufRead *.tcc setf cpp
au BufNewFile,BufRead *.tpp setf cpp
+ au! BufNewFile,BufRead *.re call polyglot#DetectReFiletype()
endif
if !has_key(s:disabled_packages, 'caddyfile')
@@ -1045,11 +1050,6 @@ if !has_key(s:disabled_packages, 'razor')
au BufNewFile,BufRead *.razor setf razor
endif
-if !has_key(s:disabled_packages, 'reason')
- au BufNewFile,BufRead *.re setf reason
- au BufNewFile,BufRead *.rei setf reason
-endif
-
if !has_key(s:disabled_packages, 'rst')
au BufNewFile,BufRead *.rest setf rst
au BufNewFile,BufRead *.rest.txt setf rst
diff --git a/heuristics.yaml b/heuristics.yaml
index 28dff4ce..97999022 100644
--- a/heuristics.yaml
+++ b/heuristics.yaml
@@ -55,3 +55,11 @@ rules:
filetype: glsl
- override: "g:filetype_fs"
- filetype: forth
+---
+extensions: [re]
+rules:
+- lines: 50
+ rules:
+ - pattern: '^\s*#(?:(?:if|ifdef|define|pragma)\s+\w|\s*include\s+[<"]|template\s*<)'
+ filetype: cpp
+ - filetype: reason
diff --git a/packages.yaml b/packages.yaml
index 23489187..d4042c5e 100644
--- a/packages.yaml
+++ b/packages.yaml
@@ -1276,6 +1276,7 @@ filetypes:
---
name: reason
remote: reasonml-editor/vim-reason-plus
+after: c/c++
filetypes:
- name: reason
linguist: Reason
diff --git a/scripts/build b/scripts/build
index 384c8423..1fe148e7 100755
--- a/scripts/build
+++ b/scripts/build
@@ -202,9 +202,17 @@ def indent(str, amount)
end
def pattern_to_condition(rule)
+ if rule.has_key?("or")
+ return rule["or"].map { |p| pattern_to_condition(p) }.join(" || ")
+ end
+
+ if rule.has_key?("or")
+ return rule["and"].map { |p| pattern_to_condition(p) }.join(" && ")
+ end
+
operator = (rule["negative"] ? "!" : "=") + "~" + (rule["ignore_case"] ? "?" : "#")
- "line #{operator} '#{rule["pattern"]}'"
+ return "line #{operator} '#{rule["pattern"]}'"
end
def rules_to_code(rules)
@@ -250,10 +258,10 @@ def rule_to_code(rule)
return rule["rules"].map { |r| indent(rule_to_code(r), 0) }.join("\n")
end
- if rule.has_key?("pattern")
+ if rule.has_key?("pattern") || rule.has_key?("or") || rule.has_key?("and")
return <<~EOS
if #{pattern_to_condition(rule)}
- #{indent(rule_to_code(except(rule, "pattern", "ignore_case", "negative")), 2)}
+ #{indent(rule_to_code(except(rule, "pattern", "or", "and", "ignore_case", "negative")), 2)}
endif
EOS
end
diff --git a/scripts/test_extensions.vim b/scripts/test_extensions.vim
index cb52bef8..dfe2f3a3 100644
--- a/scripts/test_extensions.vim
+++ b/scripts/test_extensions.vim
@@ -207,3 +207,10 @@ call TestExtension('fsharp', 'fsharp.fs', "let myInt = 5")
call TestExtension('glsl', 'glsl.fs', "//#version 120\nvoid main() {}")
let g:filetype_fs = 'fizfuz'
call TestExtension('fizfuz', 'fizfuz.fs', '')
+
+" .re extension
+call TestExtension('reason', 'empty.re', '')
+call TestExtension('cpp', 'cpp.re', '#include "config.h"')
+call TestExtension('cpp', 'cpp2.re', '#ifdef HAVE_CONFIG_H')
+call TestExtension('cpp', 'cpp3.re', '#define YYCTYPE unsigned char')
+call TestExtension('reason', 'react.re', 'ReasonReact.Router.push("");')
diff --git a/scripts/test_filetypes.vim b/scripts/test_filetypes.vim
index 86e2ff36..d0bec4cc 100644
--- a/scripts/test_filetypes.vim
+++ b/scripts/test_filetypes.vim
@@ -44,6 +44,7 @@ call TestFiletype('atlas')
call TestFiletype('autoit')
call TestFiletype('ave')
call TestFiletype('awk')
+call TestFiletype('reason')
call TestFiletype('c')
call TestFiletype('cpp')
call TestFiletype('caddyfile')
@@ -175,7 +176,6 @@ call TestFiletype('ragel')
call TestFiletype('raku')
call TestFiletype('raml')
call TestFiletype('razor')
-call TestFiletype('reason')
call TestFiletype('rst')
call TestFiletype('ruby')
call TestFiletype('eruby')