<commit_before>" Copyright 2011 The Go Authors. All rights reserved.
" Use of this source code is governed by a BSD-style
" license that can be found in the LICENSE file.
"
" godoc.vim: Vim command to see godoc.
"
"
" Commands:
"
"   :GoDoc
"
"       Open the relevant Godoc for either the word[s] passed to the command or
"       the, by default, the word under the cursor.
"
" Options:
"
"   g:go_godoc_commands [default=1]
"
"       Flag to indicate whether to enable the commands listed above.

if exists("g:loaded_godoc")
    finish
endif
let g:loaded_godoc = 1

let s:buf_nr = -1

if !exists('g:go_godoc_commands')
    let g:go_godoc_commands = 1
endif

if g:go_godoc_commands
    command! -nargs=* -range -complete=customlist,go#package#Complete GoDoc :call s:Godoc('leftabove new', <f-args>)
    command! -nargs=* -range -complete=customlist,go#package#Complete GoDocBrowser :call s:GodocBrowser(<f-args>)
endif

nnoremap <silent> <Plug>(go-doc) :<C-u>call <SID>Godoc("leftabove new")<CR>
nnoremap <silent> <Plug>(go-doc-tab) :<C-u>call <SID>Godoc("tabnew")<CR>
nnoremap <silent> <Plug>(go-doc-vertical) :<C-u>call <SID>Godoc("vnew")<CR>
nnoremap <silent> <Plug>(go-doc-split) :<C-u>call <SID>Godoc("split")<CR>

function! s:GodocBrowser(...)
    if !executable('godoc')
        echohl WarningMsg
        echo "godoc command not found."
        echo "  install with: go get code.google.com/p/go.tools/cmd/godoc"
        echohl None
        return -1
    endif

    if !len(a:000)
        let oldiskeyword = &iskeyword
        setlocal iskeyword+=.
        let word = expand('<cword>')
        let &iskeyword = oldiskeyword
        let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
        let words = split(word, '\.\ze[^./]\+$')
    else
        let words = a:000
    endif

    if !len(words)
        return
    endif

    let pkg = words[0]
    let packages = GoImports() 

    if has_key(packages, pkg)
        let pkg = packages[pkg]
    endif

    let command = 'godoc ' . pkg

    " example url: https://godoc.org/github.com/fatih/set#Set
    let godoc_url = "https://godoc.org/" . pkg . "#" . words[1]
    call GoOpenBrowser(godoc_url)
endfunction

function! s:Godoc(mode, ...)
    if !executable('godoc')
        echohl WarningMsg
        echo "godoc command not found."
        echo "  install with: go get code.google.com/p/go.tools/cmd/godoc"
        echohl None
        return -1
    endif

    if !len(a:000)
        let oldiskeyword = &iskeyword
        setlocal iskeyword+=.
        let word = expand('<cword>')
        let &iskeyword = oldiskeyword
        let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
        let words = split(word, '\.\ze[^./]\+$')
    else
        let words = a:000
    endif

    if !len(words)
        return
    endif

    let pkg = words[0]
    let packages = GoImports() 

    if has_key(packages, pkg)
        let command = 'godoc ' . packages[pkg]
    else
        let command = 'godoc ' . pkg
    endif

    silent! let content = system(command)
    if v:shell_error || !len(content)
        echo 'No documentation found for "' . pkg . '".'
        return -1
    endif

    call s:GodocView(a:mode, content)

    if len(words) < 2
        return
        echo 'No documentation found for "' . pkg . '".'
    endif 

    if search('^\%(const\|var\|type\|\s\+\) ' . pkg . '\s\+=\s')
        silent! normal zt
        return -1
    endif

    if search('^func ' . words[1] . '(')
        silent! normal zt
        return -1
    endif

endfunction

function! s:GodocView(position, content)
    " reuse existing buffer window if it exists othwersie create a new one
    if !bufexists(s:buf_nr)
        execute a:position
        file `="[Godoc]"`
        let s:buf_nr = bufnr('%')
    elseif bufwinnr(s:buf_nr) == -1
        execute a:position<commit_msg>Improve GoDoc and refactor lookup function<commit_after>" Copyright 2011 The Go Authors. All rights reserved.
" Use of this source code is governed by a BSD-style
" license that can be found in the LICENSE file.
"
" godoc.vim: Vim command to see godoc.
"
"
" Commands:
"
"   :GoDoc
"
"       Open the relevant Godoc for either the word[s] passed to the command or
"       the, by default, the word under the cursor.
"
" Options:
"
"   g:go_godoc_commands [default=1]
"
"       Flag to indicate whether to enable the commands listed above.

if exists("g:loaded_godoc")
    finish
endif
let g:loaded_godoc = 1

let s:buf_nr = -1

if !exists('g:go_godoc_commands')
    let g:go_godoc_commands = 1
endif

if g:go_godoc_commands
    command! -nargs=* -range -complete=customlist,go#package#Complete GoDoc :call s:Godoc('leftabove new', <f-args>)
    command! -nargs=* -range -complete=customlist,go#package#Complete GoDocBrowser :call s:GodocBrowser(<f-args>)
endif

nnoremap <silent> <Plug>(go-doc) :<C-u>call <SID>Godoc("leftabove new")<CR>
nnoremap <silent> <Plug>(go-doc-tab) :<C-u>call <SID>Godoc("tabnew")<CR>
nnoremap <silent> <Plug>(go-doc-vertical) :<C-u>call <SID>Godoc("vnew")<CR>
nnoremap <silent> <Plug>(go-doc-split) :<C-u>call <SID>Godoc("split")<CR>

" returns the package and exported name. exported name might be empty.
" ie: fmt and Println
" ie: github.com/fatih/set and New
function! s:GodocWord(args)
    if !executable('godoc')
        echohl WarningMsg
        echo "godoc command not found."
        echo "  install with: go get code.google.com/p/go.tools/cmd/godoc"
        echohl None
        return []
    endif

    if !len(a:args)
        let oldiskeyword = &iskeyword
        setlocal iskeyword+=.
        let word = expand('<cword>')
        let &iskeyword = oldiskeyword
        let word = substitute(word, '[^a-zA-Z0-9\\/._~-]', '', 'g')
        let words = split(word, '\.\ze[^./]\+$')
    else
        let words = a:args
    endif

    if !len(words) 
        return []
    endif

    let pkg = words[0]
    if len(words) == 1
        let exported_name = ""
    else
        let exported_name = words[1]
    endif

    let packages = GoImports() 

    if has_key(packages, pkg)
        let pkg = packages[pkg]
    endif

    return [pkg, exported_name]
endfunction

function! s:GodocBrowser(...)
    let pkgs = s:GodocWord(a:000)
    if empty(pkgs)
        return
    endif

    let pkg = pkgs[0]
    let exported_name = pkgs[1]

    " example url: https://godoc.org/github.com/fatih/set#Set
    let godoc_url = "https://godoc.org/" . pkg . "#" . exported_name
    call GoOpenBrowser(godoc_url)
endfunction

function! s:Godoc(mode, ...)
    let pkgs = s:GodocWord(a:000)
    if empty(pkgs)
        return
    endif

    let pkg = pkgs[0]
    let exported_name = pkgs[1]

    let command = 'godoc ' . pkg

    silent! let content = system(command)
    if v:shell_error || !len(content)
        echo 'No documentation found for "' . pkg . '".'
        return -1
    endif

    call s:GodocView(a:mode, content)

    " jump to the specified name
    if search('^\%(const\|var\|type\|\s\+\) ' . pkg . '\s\+=\s')
        silent! normal zt
        return -1
    endif

    if search('^func ' . exported_name . '(')
        silent! normal zt
        return -1
    endif

    " nothing found, jump to top
    silent! normal gg
endfunction

function! s:GodocView(position, content)
    " reuse existing buffer window if it exists othwersie create a new one
    if !bufexists(s:buf_nr)
        execute a:position
        file `="[Godoc]"`
        let s:buf_nr = bufnr('%')
    elseif bufwinnr(s:buf_nr) == -1
        execute a:position
{'fraction_non_alphanumeric': 0.13523987705465723, 'fraction_numerical': 0.008285447013229988, 'mean_word_length': 2.5587256300523062, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 50, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '572489', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2564, 'n_words': 712}