[vim/vim] Improve filetype.vim perfomance by moving autocmd to vim9script (PR #18507)

6 views
Skip to first unread message

BenYip

unread,
1:36 AM (9 hours ago) 1:36 AM
to vim/vim, Subscribed

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()

You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/18507

Commit Summary

  • 2555f1d clean filetype.vim
  • a18aab8 If we can detect filetype from name and extension, do it in vim9

File Changes

(2 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/18507@github.com>

BenYip

unread,
1:54 AM (9 hours ago) 1:54 AM
to vim/vim, Subscribed

Closed #18507.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/18507/issue_event/20132495779@github.com>

Reply all
Reply to author
Forward
0 new messages