Skip to content

Fix minlen of builtin commands #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2016
Merged

Fix minlen of builtin commands #20

merged 2 commits into from
Sep 27, 2016

Conversation

haya14busa
Copy link
Member

ref: #19

@coveralls
Copy link

coveralls commented Sep 27, 2016

Coverage Status

Coverage remained the same at 84.016% when pulling cf0fe1bc7ab0e6c5c56a8083110b940dc6da3eb3 on fix-minlen into f607c09 on master.

@haya14busa
Copy link
Member Author

" create builtin command table
"
let s:ex_cmds_h = '/home/haya14busa/src/github.com/vim/vim/src/ex_cmds.h'


let s:Trie = {
\   'data': {},
\ }

function! s:Trie.new() abort
  return deepcopy(self)
endfunction

function! s:Trie.add(s) abort
  let d = self.data
  for c in split(a:s, '\zs')
    if !has_key(d, c)
      let d[c] = {}
    endif
    let d = d[c]
  endfor
endfunction

function! s:Trie.in(s) abort
  let d = self.data
  for c in split(a:s, '\zs')
    if has_key(d, c)
      let d = d[c]
    else
      return v:false
    endif
  endfor
  return v:true
endfunction

function! s:Trie.common(s) abort
  let d = self.data
  let common = []
  for c in split(a:s, '\zs')
    if has_key(d, c)
      let common = add(common, c)
      let d = d[c]
    else
      break
    endif
  endfor
  return join(common, '')
endfunction

function! s:Trie.remove(s) abort
  let d = self.data
  let kss = [[]]
  for c in split(a:s, '\zs')
    if has_key(d, c)
      let kss = add(kss, copy(kss[-1]) + [c])
      let d = d[c]
    else
      return v:false
    endif
  endfor
  let kss = kss[1:]
  let kss = reverse(kss)
  for ks in kss
    let d = self.data
    for [i, k] in map(copy(ks[:-2]), {i, k->[i,k]})
      let pd = d
      let nk = ks[i+1]
      let d = d[k]
    endfor
    if empty(pd[k][nk])
      unlet pd[k][nk]
    else
      return v:true
    endif
  endfor
  return v:true
endfunction


function! s:test() abort
  let t = s:Trie.new()
  call t.add('vi')
  call t.add('vim')
  call t.add('neovim')
  call t.add('vimtutor')
  call t.add('vimgrep')
  echo PP(t.data)

  echo '---'
  echo t.in('vi')
  echo t.in('vimtu')
  echo t.in('vimg')
  echo t.in('vimgrepppp')
  echo t.common('vimgrepppp')
  echo t.common('hoge')
  echo '---remove'
  echo t.remove('vimgrep')
  echo t.in('vimgrep')
  echo t.data
  echo '---'
endfunction
" call s:test()

function! s:gen(ex_cmds_h) abort
  let lines = readfile(a:ex_cmds_h)

  " { 'name': string, 'flags': string, 'minlen': int, 'parser': string}
  let cmds = []

  let trie = s:Trie.new()

  let cumname = ''
  for [i, line] in map(copy(lines), {i, l -> [i, l]})
    if line =~# '^EX('
      let name = matchstr(line, '"\zs.*\ze",')
      let flags = matchstr(lines[i+1], '\t\+\zs.*\ze,$')

      let minlen = len(trie.common(name)) + 1
      call trie.add(name)

      let cmd = {
      \   'name': name,
      \   'flags': flags,
      \   'minlen': minlen,
      \ }
      let cmds = add(cmds, cmd)
    endif
  endfor
  return cmds
endfunction

let s:latest = s:gen(s:ex_cmds_h)

let s:vimlparser = vimlparser#import()

function! s:gen_new_builtin(existing, latest) abort
  let existing_names = {}
  for cmd in a:existing
    let existing_names[cmd.name] = v:true
  endfor
  let newcmds = []
  for cmd in filter(copy(a:latest), {_, c -> !has_key(existing_names, c.name)})
    let newcmds = add(newcmds, extend(cmd, {'parser': 'parse_cmd_common'}))
  endfor
  return newcmds
endfunction


function! s:gen_viml(newcmds) abort
  let lines = []
  for c in a:newcmds
    let lines = add(lines, '      \ ' . string(c))
  endfor
  return join(lines, "\n")
endfunction

" -- main

function! s:vimlparser_new_cmds() abort
  let vimlparser = vimlparser#import()
  let latest = s:gen(s:ex_cmds_h)
  let new_cmds = s:gen_new_builtin(s:vimlparser.VimLParser.builtin_commands, s:latest)
  echo s:gen_viml(new_cmds)
endfunction
" call s:vimlparser_new_cmds()

function! s:minlen_table() abort
  let newtable = {}
  for c in s:gen(s:ex_cmds_h)
    let newtable[c.name] = c
  endfor
  let orig = vimlparser#import().VimLParser.builtin_commands

  for c in orig
    if !has_key(newtable, c.name)
      echo 'skip: ' . c.name
      continue
    endif
    let new = newtable[c.name]
    if new.minlen != c.minlen
      echo [c.minlen, ':' . c.name]
    endif
  endfor
endfunction
call s:minlen_table()
[5, ':bufdo']
[5, ':caddbuffer']
[3, ':caddexpr']
[8, ':diffthis']
[3, ':keepmarks']
[3, ':list']
[4, ':lopen']
[5, ':scriptnames']
[3, ':simalt']
skip: sniff
[3, ':srewind']
[5, ':tabdo']
[5, ':windo']

@coveralls
Copy link

coveralls commented Sep 27, 2016

Coverage Status

Coverage remained the same at 84.016% when pulling fa6208e on fix-minlen into f607c09 on master.

@haya14busa haya14busa merged commit b757ec1 into master Sep 27, 2016
@haya14busa haya14busa deleted the fix-minlen branch September 27, 2016 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants