Modularize lazy loading of polyglot parts

This commit is contained in:
Adam Stankiewicz
2020-09-29 21:48:38 +02:00
parent 2f133372bc
commit 543e8c917b
9 changed files with 480 additions and 788 deletions

View File

@@ -13,8 +13,11 @@ Dir.chdir(File.dirname(__dir__))
BASE_URL = 'https://raw.githubusercontent.com/github/linguist/master'
def read_section(name)
"\" Please do not edit this file directly, instead modify polyglot.vim or scripts/build\n\n" +
File.read('polyglot.vim').match(/""" #{Regexp.escape(name)}\n*(.*)\n(?=\n""")/m)[1]
section = File.read('polyglot.vim').split('""" ').find { |e| e.start_with?(name) }
if section.nil?
raise StandardError.new("Section not found: #{name}")
end
"\" Please do not edit this file directly, instead modify polyglot.vim or scripts/build\n" + section[(section.index("\n") + 1)..-1]
end
def camelize(str)
@@ -390,10 +393,17 @@ def rule_to_code(rule)
EOS
end
if rule.has_key?("shebang")
return <<~EOS
if polyglot#shebang#Detect() | return | endif
#{indent(rule_to_code(except(rule, "shebang")), 0)}
EOS
end
if rule.has_key?("filetype")
if rule.has_key?("fallback")
return <<~EOS
set ft=#{rule["filetype"]} | au! BufWritePost <buffer> ++once call polyglot#Detect#{camelize(rule["extensions"].first)}Filetype()
set ft=#{rule["filetype"]} | au! BufWritePost <buffer> ++once call polyglot#detect##{camelize(rule["extensions"].first)}()
return
EOS
end
@@ -489,6 +499,8 @@ def extract(packages)
end
def generate_ftdetect(packages, heuristics)
FileUtils.mkdir_p('autoload/polyglot')
output = "\n"
all_filetypes = packages.flat_map { |f| f["filetypes"] || [] }
@@ -561,7 +573,7 @@ def generate_ftdetect(packages, heuristics)
for heuristic in package_heuristics.uniq
extensions = heuristic["extensions"].map { |e| "*.#{e}" }
autocommands << " au! BufNewFile,BufRead #{extensions.join(",")} call polyglot#Detect#{camelize(heuristic["extensions"].first)}Filetype()\n"
autocommands << " au! BufNewFile,BufRead #{extensions.join(",")} call polyglot#detect##{camelize(heuristic["extensions"].first)}()\n"
end
if autocommands != ""
@@ -580,6 +592,18 @@ def generate_ftdetect(packages, heuristics)
ftdetect.gsub('" scripts/build inserts here filetype detection autocommands') { output }
)
output = []
for heuristic in heuristics
output << <<~EOS
func! polyglot#detect##{camelize(heuristic["extensions"].first)}()
#{indent(rules_to_code(heuristic), 2)}
endfunc
EOS
end
File.write('autoload/polyglot/detect.vim', output.join("\n"))
output = <<~EOS
let s:interpreters = {
EOS
@@ -590,24 +614,10 @@ def generate_ftdetect(packages, heuristics)
end
end
output << <<~EOS
\\ }
output << " \\ }"
EOS
for heuristic in heuristics
output << <<~EOS
func! polyglot#Detect#{camelize(heuristic["extensions"].first)}Filetype()
#{indent(rules_to_code(heuristic), 2)}
endfunc
EOS
end
autoload_script = read_section('autoload/polyglot.vim')
autoload_script["\" scripts/build generates heuristics functions here\n"] = output
File.write('autoload/polyglot.vim', autoload_script)
autoload_script = read_section('autoload/polyglot/shebang.vim')
File.write('autoload/polyglot/shebang.vim', autoload_script + "\n\n" + output)
end
def generate_tests(packages)
@@ -744,13 +754,13 @@ def generate_plugins(packages)
output << <<~EOS
func! sleuth#GlobForFiletype(type)
func! polyglot#sleuth#GlobForFiletype(type)
return get(s:globs, a:type, '')
endfunc
EOS
File.write('autoload/sleuth.vim', output)
File.write('autoload/polyglot/sleuth.vim', output)
end
def process_list(list, extras)