Migrate ftdetect generation to ruby

This commit is contained in:
Adam Stankiewicz
2020-08-23 23:25:06 +02:00
parent f3ab28a287
commit 114a93bb7c
7 changed files with 862 additions and 660 deletions

131
build
View File

@@ -95,8 +95,6 @@ def load_languages
data = URI.open(url) { |io| YAML.load(io.read) }
end
# heuristics, languages = parallel(:load_heuristics, :load_languages)
def parse_remote(remote)
match = remote.match(/(?<repo>[^@:]+)(?:@(?<branch>[^:]+))?(?::(?<path>.*))?/)
[match[:repo], match[:branch] || "master", match[:path]]
@@ -123,16 +121,18 @@ end
def download
FileUtils.rm_rf('tmp')
PACKAGES.map do |package|
Thread.new do
repo, branch, path = parse_remote(package.fetch("remote"))
dir = "tmp/" + repo.split('/')[1]
FileUtils.mkdir_p(dir)
url = "https://codeload.github.com/#{repo}/tar.gz/#{branch}"
`curl --silent -fL #{url} | tar -zx -C "#{dir}" --strip 1`
progress
end
end.map(&:join)
PACKAGES.each_slice(20) do |batch|
batch.map do |package|
Thread.new do
repo, branch, path = parse_remote(package.fetch("remote"))
dir = "tmp/" + repo.split('/')[1]
FileUtils.mkdir_p(dir)
url = "https://codeload.github.com/#{repo}/tar.gz/#{branch}"
`curl --silent -fL #{url} | tar -zx -C "#{dir}" --strip 1`
progress
end
end.map(&:join)
end
end
$i = 0
@@ -191,7 +191,114 @@ def extract
File.write('README.md', readme)
end
def generate_ftdetect
heuristics, languages = parallel(:load_heuristics, :load_languages)
output = <<~EOS
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
if !exists('g:polyglot_disabled')
let g:polyglot_disabled = []
endif
function! s:SetDefault(name, value)
if !exists(a:name)
let {a:name} = a:value
endif
endfunction
call s:SetDefault('g:markdown_enable_spell_checking', 0)
call s:SetDefault('g:markdown_enable_input_abbreviations', 0)
call s:SetDefault('g:markdown_enable_mappings', 0)
" Enable jsx syntax by default
call s:SetDefault('g:jsx_ext_required', 0)
" Make csv loading faster
call s:SetDefault('g:csv_start', 1)
call s:SetDefault('g:csv_end', 2)
" Disable json concealing by default
call s:SetDefault('g:vim_json_syntax_conceal', 0)
call s:SetDefault('g:filetype_euphoria', 'elixir')
if !exists('g:python_highlight_all')
call s:SetDefault('g:python_highlight_builtins', 1)
call s:SetDefault('g:python_highlight_builtin_objs', 1)
call s:SetDefault('g:python_highlight_builtin_types', 1)
call s:SetDefault('g:python_highlight_builtin_funcs', 1)
call s:SetDefault('g:python_highlight_builtin_funcs_kwarg', 1)
call s:SetDefault('g:python_highlight_exceptions', 1)
call s:SetDefault('g:python_highlight_string_formatting', 1)
call s:SetDefault('g:python_highlight_string_format', 1)
call s:SetDefault('g:python_highlight_string_templates', 1)
call s:SetDefault('g:python_highlight_indent_errors', 1)
call s:SetDefault('g:python_highlight_space_errors', 1)
call s:SetDefault('g:python_highlight_doctests', 1)
call s:SetDefault('g:python_highlight_func_calls', 1)
call s:SetDefault('g:python_highlight_class_vars', 1)
call s:SetDefault('g:python_highlight_operators', 1)
call s:SetDefault('g:python_highlight_file_headers_as_comments', 1)
call s:SetDefault('g:python_slow_sync', 1)
endif
EOS
for package in PACKAGES
name = package.fetch("name")
output << if name == "jsx"
"if !(index(g:polyglot_disabled, 'typescript') != -1 || index(g:polyglot_disabled, 'typescript') != -1 || index(g:polyglot_disabled, 'jsx') != -1)\n"
else
"if index(g:polyglot_disabled, '#{name}') == -1\n"
end
filetypes = package["filetypes"] or raise "Unknown filetype for: #{package["name"]}"
for filetype in filetypes
syntax = filetype["syntax"] ? " syntax=#{filetype["syntax"]}" : ""
set_command = package.fetch("custom_set", "set ft=#{filetype.fetch("name")}#{syntax}")
linguist = filetype["linguist"] ? languages.fetch(filetype["linguist"]) : {}
extensions = filetype["extensions"] || linguist.fetch("extensions", []).map { |e| e[1..] }
extensions = (extensions | filetype.fetch("extra_extensions", [])) - filetype.fetch("ignored_extensions", [])
filenames = filetype["filenames"] || linguist.fetch("filenames", [])
filenames = (filenames | filetype.fetch("extra_filenames", [])) - filetype.fetch("ignored_filenames", [])
for extension in extensions.sort
outer_filetype = filetype["outer_filetype"]
if outer_filetype
output << " au BufNewFile *.*.#{extension} execute \"do BufNewFile filetypedetect \" . expand(\"<afile>:r\") | #{outer_filetype}"
output << " au BufReadPre *.*#{extension} execute \"do BufRead filetypedetect \" . expand(\"<afile>:r\") | #{outer_filetype}"
end
output << " au BufNewFile,BufRead *.#{extension} #{set_command}\n"
end
for filename in filenames.sort
if filename[0] == "."
filename = "{.,}" + filename[1..]
end
output << " au BufNewFile,BufRead #{filename} #{set_command}\n"
end
end
output << "endif\n\n"
end
output << <<~EOS
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
EOS
File.write('ftdetect/polyglot.vim', output)
end
download
extract
generate_ftdetect
puts(" Bye! Have a wonderful time!")
FileUtils.rm_rf("tmp")