This PR moves a lot of autocmd in filetype.vim into this function in autoload/dist/ft.vim:
export def DetectByNameAndExt() const amatch = expand("<amatch>") const ext = fnamemodify(amatch, ':e') const name = fnamemodify(amatch, ':p:t') const ft = get(byname, name, get(byextesion, ext, '')) if ft != '' execute "setf " .. ft endif enddef const byextesion = { # 8th (Firth-derivative) "8th": "8th", ... } const byname = { # Ant "build.xml": "ant", ... }
In filetype.vim, these autocmd is replace by:
au BufNewFile,BufRead * call dist#ft#DetectByNameAndExt()
After this PR, autocmd in filetype.vim reduces from 1137 to 489.
On my machine, the load time of filetype.vim reduces 7.55ms to 3.86ms.
byextesion
, byname
and the new filetype.vim are generated from this python script:
import re from pathlib import Path lines = open(Path("filetype.vim")).readlines() ext_regex = r"^au BufNewFile,BufRead (\*\.[a-zA-Z0-9_+-]+(?:,\*\.[a-zA-Z0-9_+-]+)*)\s+setf\s+(\S+)$" name_regex = r"^au BufNewFile,BufRead ([a-zA-Z0-9_.+-,]+)\s+setf\s+(\S+)$" outfiletype = open("next-filetype.vim", "w") outvim9 = open("vim9ft.vim", 'w') i = 0 N = len(lines) extlines = ["const byextesion = {"] namelines = ["const byname = {"] indent = " " while lines[i] != "\n": outfiletype.write(lines[i]) i += 1 outfiletype.write(lines[i]) i += 1 while i < N: if lines[i].startswith('"'): comments = [] while i < N and lines[i][0] == '"': comments.append(lines[i][1:-1]) i+=1 vim8comment = '\n'.join(('"' + x for x in comments)) vim9comment = '\n'.join((indent + '#' + x for x in comments)) if i >= N or lines[i] == '\n': outfiletype.write(vim8comment + "\n") continue ext_has_commnet = False name_has_commnet = False while i < N: if mat := re.match(ext_regex, lines[i]): ft = mat.group(2) if not ext_has_commnet: extlines.append(vim9comment) ext_has_commnet = True for ext in mat.group(1).split(","): ext = ext[2:] extlines.append(f'{indent}"{ext}": "{ft}",') i += 1 elif mat := re.match(name_regex, lines[i]): name = mat.group(1) ft = mat.group(2) if not name_has_commnet: namelines.append(vim9comment) name_has_commnet = True for name in mat.group(1).split(","): namelines.append(f'{indent}"{name}": "{ft}",') i += 1 elif lines[i] == "\n": i += 1 break else: outfiletype.write(vim8comment + "\n") break else: outfiletype.write(lines[i]) i += 1 extlines.append("}") namelines.append("}") outvim9.write('vim9script\n') outvim9.write("\n".join(extlines)) outvim9.write('\n') outvim9.write("\n".join(namelines)) # outvim9.write('\ndefcompile\n') outvim9.close() outfiletype.close()
https://github.com/vim/vim/pull/18507
(2 files)
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
Closed #18507.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.