|
| 1 | +" create builtin command table |
| 2 | +let s:Trie = { |
| 3 | +\ 'data': {}, |
| 4 | +\ } |
| 5 | + |
| 6 | +function! s:Trie.new() abort |
| 7 | + return deepcopy(self) |
| 8 | +endfunction |
| 9 | + |
| 10 | +function! s:Trie.add(s) abort |
| 11 | + let d = self.data |
| 12 | + for c in split(a:s, '\zs') |
| 13 | + if !has_key(d, c) |
| 14 | + let d[c] = {} |
| 15 | + endif |
| 16 | + let d = d[c] |
| 17 | + endfor |
| 18 | +endfunction |
| 19 | + |
| 20 | +function! s:Trie.in(s) abort |
| 21 | + let d = self.data |
| 22 | + for c in split(a:s, '\zs') |
| 23 | + if has_key(d, c) |
| 24 | + let d = d[c] |
| 25 | + else |
| 26 | + return v:false |
| 27 | + endif |
| 28 | + endfor |
| 29 | + return v:true |
| 30 | +endfunction |
| 31 | + |
| 32 | +function! s:Trie.common(s) abort |
| 33 | + let d = self.data |
| 34 | + let common = [] |
| 35 | + for c in split(a:s, '\zs') |
| 36 | + if has_key(d, c) |
| 37 | + let common = add(common, c) |
| 38 | + let d = d[c] |
| 39 | + else |
| 40 | + break |
| 41 | + endif |
| 42 | + endfor |
| 43 | + return join(common, '') |
| 44 | +endfunction |
| 45 | + |
| 46 | +function! s:Trie.remove(s) abort |
| 47 | + let d = self.data |
| 48 | + let kss = [[]] |
| 49 | + for c in split(a:s, '\zs') |
| 50 | + if has_key(d, c) |
| 51 | + let kss = add(kss, copy(kss[-1]) + [c]) |
| 52 | + let d = d[c] |
| 53 | + else |
| 54 | + return v:false |
| 55 | + endif |
| 56 | + endfor |
| 57 | + let kss = kss[1:] |
| 58 | + let kss = reverse(kss) |
| 59 | + for ks in kss |
| 60 | + let d = self.data |
| 61 | + for [i, k] in map(copy(ks[:-2]), {i, k->[i,k]}) |
| 62 | + let pd = d |
| 63 | + let nk = ks[i+1] |
| 64 | + let d = d[k] |
| 65 | + endfor |
| 66 | + if empty(pd[k][nk]) |
| 67 | + unlet pd[k][nk] |
| 68 | + else |
| 69 | + return v:true |
| 70 | + endif |
| 71 | + endfor |
| 72 | + return v:true |
| 73 | +endfunction |
| 74 | + |
| 75 | +function! s:gen(ex_cmds_h) abort |
| 76 | + let lines = readfile(a:ex_cmds_h) |
| 77 | + |
| 78 | + " { 'name': string, 'flags': string, 'minlen': int, 'parser': string} |
| 79 | + let cmds = [] |
| 80 | + |
| 81 | + let trie = s:Trie.new() |
| 82 | + |
| 83 | + let cumname = '' |
| 84 | + for [i, line] in map(copy(lines), {i, l -> [i, l]}) |
| 85 | + if line =~# '^EX(' |
| 86 | + let name = matchstr(line, '"\zs.*\ze",') |
| 87 | + let flags = matchstr(lines[i+1], '\t\+\zs.*\ze,$') |
| 88 | + |
| 89 | + let minlen = len(trie.common(name)) + 1 |
| 90 | + call trie.add(name) |
| 91 | + |
| 92 | + let cmd = { |
| 93 | + \ 'name': name, |
| 94 | + \ 'flags': flags, |
| 95 | + \ 'minlen': minlen, |
| 96 | + \ } |
| 97 | + let cmds = add(cmds, cmd) |
| 98 | + endif |
| 99 | + endfor |
| 100 | + return cmds |
| 101 | +endfunction |
| 102 | + |
| 103 | +function! s:gen_new_builtin(existing, latest) abort |
| 104 | + let existing_names = {} |
| 105 | + for cmd in a:existing |
| 106 | + let existing_names[cmd.name] = v:true |
| 107 | + endfor |
| 108 | + let newcmds = [] |
| 109 | + for cmd in filter(copy(a:latest), {_, c -> !has_key(existing_names, c.name)}) |
| 110 | + let newcmds = add(newcmds, extend(cmd, {'parser': 'parse_cmd_common'})) |
| 111 | + endfor |
| 112 | + return newcmds |
| 113 | +endfunction |
| 114 | + |
| 115 | +function! s:gen_viml(newcmds) abort |
| 116 | + let lines = [] |
| 117 | + for c in a:newcmds |
| 118 | + let lines = add(lines, ' \ ' . string(c) . ',') |
| 119 | + endfor |
| 120 | + return join(lines, "\n") |
| 121 | +endfunction |
| 122 | + |
| 123 | +" -- main |
| 124 | + |
| 125 | +" ex_cmds_h: path to vim/src/ex_cmds.h |
| 126 | +function! g:VimLParserNewCmds(ex_cmds_h) abort |
| 127 | + let vimlparser = vimlparser#import() |
| 128 | + let latest = s:gen(a:ex_cmds_h) |
| 129 | + let new_cmds = s:gen_new_builtin(vimlparser#import().VimLParser.builtin_commands, latest) |
| 130 | + let generated_text = s:gen_viml(new_cmds) |
| 131 | + if generated_text == '' |
| 132 | + verbose echo 's:VimLParser.builtin_commands in autoload/vimlparser.vim is up-to-date.' |
| 133 | + else |
| 134 | + verbose echo "Append following lines to s:VimLParser.builtin_commands in autoload/vimlparser.vim\n" |
| 135 | + verbose echo generated_text |
| 136 | + endif |
| 137 | +endfunction |
| 138 | +" call s:vimlparser_new_cmds('/home/haya14busa/src/github.com/vim/vim/src/ex_cmds.h') |
0 commit comments