xaizek / vim-inccomplete (License: Vim) (since 2018-12-07)
Vim plugin for #include directive completion in C family of languages.
<root> / plugin / inccomplete.vim (e053608a9bd0335178658e5ce1dd4bb4e3a6ada0) (14KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
" Name:    inccomplete
" Author:  xaizek <xaizek@lavabit.com>
" Version: 1.6.31
" License: Same terms as Vim itself (see :help license)
"
" See :help inccomplete for documentation.

if exists('g:loaded_inccomplete')
    finish
endif

let g:loaded_inccomplete = 1
let g:inccomplete_cache = {}

if !exists('g:inccomplete_findcmd')
    let g:inccomplete_findcmd = ''
endif

if !exists('g:inccomplete_addclosebracket')
    let g:inccomplete_addclosebracket = 'always'
endif

if !exists('g:inccomplete_sort')
    let g:inccomplete_sort = ''
endif

if !exists('g:inccomplete_showdirs')
    let g:inccomplete_showdirs = 0
endif

if !exists('g:inccomplete_appendslash')
    let g:inccomplete_appendslash = 0
endif

autocmd FileType c,cpp,objc,objcpp call s:ICInit()

" maps <, ", / and \, sets 'omnifunc'
function! s:ICInit()
    " remap < and "
    inoremap <expr> <buffer> < ICCompleteInc('<')
    inoremap <expr> <buffer> " ICCompleteInc('"')
    if g:inccomplete_showdirs
        inoremap <expr> <buffer> / ICCompleteInc('/')
        inoremap <expr> <buffer> \ ICCompleteInc('\')
    endif

    " save current 'omnifunc'
    let l:curbuf = expand('%:p')
    if !exists('s:oldomnifuncs')
        let s:oldomnifuncs = {}
    endif
    let s:oldomnifuncs[l:curbuf] = &omnifunc

    " set our omnifunc
    setlocal omnifunc=ICComplete
endfunction

" checks whether we need to do completion after <, ", / or \ and starts it when
" we do.
function! ICCompleteInc(bracket)
    if a:bracket == '/' || a:bracket == '\'
        if getline('.') =~ '^\s*#\s*include\s*["<][^">]*$'
            return a:bracket."\<c-x>\<c-o>"
        endif
    endif

    " is it #include directive?
    if getline('.') !~ '^\s*#\s*include\s*$'
        return a:bracket
    endif

    if g:inccomplete_addclosebracket == 'always'
        " determine close bracket
        let l:closebracket = ['"', '>'][a:bracket == '<']

        " put brackets and start completion
        return a:bracket.l:closebracket."\<left>\<c-x>\<c-o>"
    else
        " put bracket and start completion
        return a:bracket."\<c-x>\<c-o>"
    endif
endfunction

" this is the 'omnifunc'
function! ICComplete(findstart, base)
    let l:curbuf = expand('%:p')
    if a:findstart
        " did user request #include completion?
        let s:passnext = getline('.') !~ '^\s*#\s*include\s*\%(<\|"\)'
        if !s:passnext
            return match(getline('.'), '<\|"') + 1
        endif

        " no, call other omnifunc if there is one
        if !has_key(s:oldomnifuncs, l:curbuf)
            return col('.') - 1
        endif
        return eval(s:oldomnifuncs[l:curbuf].
                  \ "(".a:findstart.",'".a:base."')")
    elseif exists('s:passnext') && s:passnext
        " call previous 'omnifunc' when needed
        if !has_key(s:oldomnifuncs, l:curbuf)
            return []
        endif
        return eval(s:oldomnifuncs[l:curbuf].
                  \ "(".a:findstart.",'".a:base."')")
    else
        let l:pos = match(getline('.'), '<\|"')
        let l:bracket = getline('.')[l:pos : l:pos]

        if empty(a:base) && l:bracket == '<' && exists('s:fullCached')
            return s:fullCached
        endif

        let l:old_cwd = getcwd()
        lcd %:p:h

        " get list of all candidates and reduce it to those starts with a:base
        let l:inclst = s:ICGetList(l:bracket == '"', a:base)
        let l:inclst = s:ICFilterIncLst(l:bracket == '"', l:inclst, a:base)

        if g:inccomplete_addclosebracket != 'always'
            " determine close bracket
            let l:closebracket = ['"', '>'][l:bracket == '<']
            if getline('.')[l:pos + 1 :] =~ l:closebracket.'\s*$'
                let l:closebracket = ''
            endif
        else
            let l:closebracket = ''
        endif

        let l:existend_list = s:ICListIncludes()[l:bracket == '"']

        " form list of dictionaries
        let [l:pos, l:sl1, l:sl2] = s:ICParsePath(a:base)
        let l:comlst = []
        for l:increc in l:inclst
            if empty(l:increc[1])
                continue
            endif

            if isdirectory(l:increc[0].'/'.l:increc[1])
                let l:slashidx = strridx(l:increc[1], l:sl2)
                if l:increc[1][l:slashidx + 1] == '.'
                    continue
                endif

                let l:strend = g:inccomplete_appendslash ? l:sl2 : ''
                let l:slash = l:sl2
            elseif index(l:existend_list, l:increc[1]) >= 0
                continue
            else
                let l:strend = l:closebracket
                let l:slash = ''
            endif

            let l:item = {
                        \ 'word': l:increc[1].l:strend,
                        \ 'abbr': l:increc[1].l:slash,
                        \ 'menu': s:ICModifyPath(l:increc[0]),
                        \ 'dup': 0
                        \}
            call add(l:comlst, l:item)
        endfor

        execute 'lcd' l:old_cwd

        let l:result = s:SortList(l:comlst)

        if empty(a:base) && l:bracket == '<'
            let s:fullCached = l:result
        endif

        return l:result
    endif
endfunction

function! s:ICListIncludes()
    let l:lines = getline(0, '$')
    call filter(l:lines, 'v:val =~? &include')
    call map(l:lines, 'matchstr(v:val, &include.''\s*\zs.*\ze$'')')

    let l:sys = []
    let l:usr = []
    for l:include in l:lines
        if l:include[0] == '<'
            call add(l:sys, l:include[1:])
        else
            call add(l:usr, l:include[1:])
        endif
    endfor
    unlet l:lines

    call map(l:sys, 'matchstr(v:val, ''^[^>]\+'')')
    call map(l:usr, 'matchstr(v:val, ''^[^"]\+'')')

    " echo l:sys
    " echo l:usr

    return [l:sys, l:usr]
endfunction

" sorts completion list
function s:SortList(lst)
    if g:inccomplete_sort == 'ignorecase'
        return sort(a:lst, 's:IgnoreCaseComparer')
    else
        return sort(a:lst, 's:Comparer')
    endif
endfunction
function s:IgnoreCaseComparer(i1, i2)
    return a:i1['abbr'] == a:i2['abbr'] ? 0 :
                \ (a:i1['abbr'] > a:i2['abbr'] ? 1 : -1)
endfunction
function s:Comparer(i1, i2)
    return a:i1['abbr'] ==# a:i2['abbr'] ? 0 :
                \ (a:i1['abbr'] ># a:i2['abbr'] ? 1 : -1)
endfunction

" modifies path correctly on Windows
function! s:ICModifyPath(path)
    let l:drive_regexp = '\C^[a-zA-Z]:'
    let l:modified = fnamemodify(a:path, ':p:.')
    let l:prefix = ''
    if has('win32') && a:path =~ l:drive_regexp && !empty(l:modified)
        let l:prefix = matchstr(a:path, l:drive_regexp)
    endif
    return l:prefix.l:modified
endfunction

" filters search results
function! s:ICFilterIncLst(user, inclst, base)
    let [l:pos, l:sl1, l:sl2] = s:ICParsePath(a:base)

    " filter by filename
    let l:filebegin = a:base[strridx(a:base, l:sl2) + 1:]
    let l:inclst = filter(copy(a:inclst),
                        \ '!empty(v:val[1]) && v:val[1] =~ "^".l:filebegin')

    " correct slashes in paths
    if l:sl1 == '/'
        call map(l:inclst, '[substitute(v:val[0], "\\\\", "/", "g"), v:val[1]]')
    else
        call map(l:inclst, '[substitute(v:val[0], "/", "\\\\", "g"), v:val[1]]')
    endif

    if l:pos >= 0
        " filter by subdirectory name
        let l:dirend0 = a:base[:l:pos]
        if a:user
            let l:dirend1 = fnamemodify(expand('%:p:h').'/'.l:dirend0, ':p')
        else
            let l:dirend1 = l:dirend0
        endif
        if l:sl1 == '/'
            let l:dirend2 = substitute(l:dirend1, "\\\\", "/", "g")
        else
            let l:dirend2 = escape(l:dirend1, '\')
        endif
        if a:user
            call filter(l:inclst, 'v:val[0] =~ "^".l:dirend2."[\\/]*$"')
        else
            call filter(l:inclst, 'v:val[0] =~ "'.l:sl1.'".l:dirend2."$"')
        endif

        " move end of each path to the beginning of filename
        let l:cutidx = - (l:pos + 2)
        if !empty(l:inclst) && l:inclst[0][0][l:cutidx + 1:] != l:dirend0
                    \ && a:user
            let l:path = expand('%:p:h')
            call map(l:inclst, '[l:path, l:dirend0.v:val[1]]')
        else
            call map(l:inclst, '[v:val[0][:l:cutidx], l:dirend0.v:val[1]]')
        endif
    endif

    return l:inclst
endfunction

" searches for files that can be included in path
" a:user determines search area, when it's not zero look only in '.', otherwise
" everywhere in path except '.'
function! s:ICGetList(user, base)
    if a:user
        let l:dir = expand('%:h:p')
        return s:ICFindIncludes(1, [l:dir] + s:ICGetSubDirs([l:dir], a:base))
    endif

    " prepare list of directories
    let l:pathlst = s:ICAddNoDupPaths(split(&path, ','), s:ICGetClangIncludes())
    let l:pathlst = s:ICAddNoDupPaths(l:pathlst,
                                    \ s:ICGetSubDirs(l:pathlst, a:base))
    call reverse(sort(l:pathlst))

    " divide it into sublists
    let l:noncached = filter(copy(l:pathlst),
                           \ '!has_key(g:inccomplete_cache, v:val)')
    let l:cached = filter(l:pathlst, 'has_key(g:inccomplete_cache, v:val)')

    " add noncached entries
    let l:result = s:ICFindIncludes(0, l:noncached)

    " add cached entries
    for l:incpath in l:cached
        call map(copy(g:inccomplete_cache[l:incpath]),
               \ 'add(l:result, [l:incpath, v:val])')
    endfor

    return l:result
endfunction

" gets list of header files using find
function! s:ICFindIncludes(user, pathlst)
    " test arguments
    if empty(a:pathlst)
        return []
    endif
    if !a:user
        if empty(g:inccomplete_findcmd)
            let l:regex = '.*[/\\][-_a-z0-9]\+\(\.hpp\|\.h\)\?$'
        else
            let l:regex = '.*[/\\][-_a-z0-9]+\(\.hpp\|\.h\)?$'
        endif
    else
        let l:regex = '.*\(\.hpp\|\.h\)$'
    endif

    " execute find
    if empty(g:inccomplete_findcmd)
        let l:pathstr = substitute(join(a:pathlst, ','), '\\', '/', 'g')
        let l:found = globpath(l:pathstr, '*', 1)
        let l:foundlst = split(l:found, '\n')

        if g:inccomplete_showdirs
            call filter(l:foundlst,
                      \ "v:val =~ '".l:regex."' || isdirectory(v:val)")
        else
            call filter(l:foundlst, "v:val =~ '".l:regex."'")
        endif
    else
        " substitute in the next command is for Windows (it removes backslash in
        " \" sequence, that can appear after escaping the path)
        let l:substcmd = 'substitute(shellescape(v:val), ''\(.*\)\\\"$'','.
                       \ ' "\\1\"", "")'


        let l:pathstr = join(map(copy(a:pathlst), l:substcmd), ' ')
        let l:iregex = ' -iregex '.shellescape(l:regex)
        let l:dirs = g:inccomplete_showdirs ? ' -or -type d' : ''
        let l:found = system(g:inccomplete_findcmd.' -L '.
                           \ l:pathstr.' -maxdepth 1 -type f'.l:iregex.l:dirs)
        let l:foundlst = split(l:found, '\n')
    endif
    unlet l:found " to free some memory

    " prepare a:pathlst by forming regexps
    for l:i in range(len(a:pathlst))
        let g:inccomplete_cache[a:pathlst[i]] = []
        let l:tmp = substitute(a:pathlst[i], '\', '/', 'g')
        let a:pathlst[i] = [a:pathlst[i], '^'.escape(l:tmp, '.')]
    endfor

    " process the results of find
    let l:result = []
    for l:file in l:foundlst
        let l:file = substitute(l:file, '\', '/', 'g')
        " find appropriate path
        let l:pathlst = filter(copy(a:pathlst), 'l:file =~ v:val[1]')
        if empty(l:pathlst)
            continue
        endif
        let l:incpath = l:pathlst[0]
        " add entry to list
        let l:left = l:file[len(l:incpath[0]):]
        if l:left[0] == '/' || l:left[0] == '\'
            let l:left = l:left[1:]
        endif
        call add(l:result, [l:incpath[0], l:left])
        " and to cache
        call add(g:inccomplete_cache[l:incpath[0]], l:left)
    endfor
    return l:result
endfunction

" retrieves include directories from b:clang_user_options and
" g:clang_user_options
function! s:ICGetClangIncludes()
    if !exists('b:clang_user_options') || !exists('g:clang_user_options')
        return []
    endif
    let l:lst = split(b:clang_user_options.' '.g:clang_user_options, ' ')
    let l:lst = filter(l:lst, 'v:val =~ "\\C^-I"')
    let l:lst = map(l:lst, 'v:val[2:]')
    let l:lst = map(l:lst, 'fnamemodify(v:val, ":p")')
    let l:lst = map(l:lst, 'substitute(v:val, "\\\\", "/", "g")')
    return l:lst
endfunction

" searches for existing subdirectories
function! s:ICGetSubDirs(pathlst, base)
    let [l:pos, l:sl, l:sl2] = s:ICParsePath(a:base)
    if l:pos < 0
        return []
    endif

    " search
    let l:dirend = a:base[:l:pos]
    let l:pathlst = join(a:pathlst, ',')
    let l:subdirs = finddir(l:dirend, l:pathlst, -1)

    " path expanding
    call map(l:subdirs, 'fnamemodify(v:val, ":p:h")')

    " ensure that path ends with slash
    let l:mapcmd = 'substitute(v:val, "\\([^'.l:sl.']\\)$", "\\1'.l:sl.'", "g")'
    call map(l:subdirs, l:mapcmd)

    return l:subdirs
endfunction

" returns list of three elements: [name_pos, slash_for_regexps, ordinary_slash]
function! s:ICParsePath(path)
    let l:iswindows = has('win16') || has('win32') || has('win64') ||
                    \ has('win95') || has('win32unix')

    " determine type of slash
    let l:path = a:path
    let l:pos = strridx(a:path, '/')
    let l:sl1 = '/'
    let l:sl2 = '/'
    if l:iswindows && (empty(a:path) || l:pos < 0)
        let l:pos = strridx(a:path, '\')
        let l:sl1 = '\\\\'
        let l:sl2 = '\'
    endif
    return [l:pos, l:sl1, l:sl2]
endfunction

" adds one list of paths to another without duplicating items
function! s:ICAddNoDupPaths(lista, listb)
    let l:result = []
    call s:ICPrepPaths(a:lista)
    call s:ICPrepPaths(a:listb)
    for l:item in a:lista + a:listb
        if index(l:result, l:item) == -1
            call add(l:result, l:item)
        endif
    endfor
    return l:result
endfunction

" converts list of paths to a list of absolute paths and excudes '.' directory
function! s:ICPrepPaths(lst)
    call filter(a:lst, '!empty(v:val) && v:val != "."')
    return map(a:lst, 'fnamemodify(v:val, ":p")')
endfunction

" vim: set foldmethod=syntax foldlevel=0 :
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vim-inccomplete

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vim-inccomplete

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master