blob: e3f947c091c3fc0708722d617e659718efb02678 [file] [log] [blame]
Bram Moolenaarfb539272014-08-22 19:21:47 +02001" Vim filetype plugin file
2" Language: Go
Phạm Bình An11ab02c2025-03-18 21:05:35 +01003" Maintainer: David Barnett (https://github.com/google/vim-ft-go is archived)
Bram Moolenaarfb539272014-08-22 19:21:47 +02004" Last Change: 2014 Aug 16
markmacode3e07d5a2024-07-16 21:20:34 +02005" 2024 Jul 16 by Vim Project (add recommended indent style)
Phạm Bình An62e82282025-03-07 19:19:31 +01006" 2025 Mar 07 by Vim Project (add formatprg and keywordprg option #16804)
Phạm Bình An11ab02c2025-03-18 21:05:35 +01007" 2025 Mar 18 by Vim Project (use :term for 'keywordprg' #16911)
Eisuke Kawashimafbbaa6e2025-04-16 18:20:59 +02008" 2025 Apr 16 by Vim Project (set 'cpoptions' for line continuation, #17121)
Rob B5ecee302025-07-02 18:46:44 +02009" 2025 Jul 02 by Vim Project (add section movement mappings #17641)
Bram Moolenaarfb539272014-08-22 19:21:47 +020010
11if exists('b:did_ftplugin')
12 finish
13endif
14let b:did_ftplugin = 1
15
Eisuke Kawashimafbbaa6e2025-04-16 18:20:59 +020016let s:cpo_save = &cpo
17set cpo&vim
18
Bram Moolenaarfb539272014-08-22 19:21:47 +020019setlocal formatoptions-=t
Phạm Bình An62e82282025-03-07 19:19:31 +010020setlocal formatprg=gofmt
Bram Moolenaarfb539272014-08-22 19:21:47 +020021
22setlocal comments=s1:/*,mb:*,ex:*/,://
23setlocal commentstring=//\ %s
Phạm Bình An62e82282025-03-07 19:19:31 +010024setlocal keywordprg=:GoKeywordPrg
Bram Moolenaarfb539272014-08-22 19:21:47 +020025
Phạm Bình An62e82282025-03-07 19:19:31 +010026command! -buffer -nargs=* GoKeywordPrg call s:GoKeywordPrg()
27
28let b:undo_ftplugin = 'setl fo< com< cms< fp< kp<'
29 \ . '| delcommand -buffer GoKeywordPrg'
Bram Moolenaarfb539272014-08-22 19:21:47 +020030
markmacode3e07d5a2024-07-16 21:20:34 +020031if get(g:, 'go_recommended_style', 1)
32 setlocal noexpandtab softtabstop=0 shiftwidth=0
Rob B5ecee302025-07-02 18:46:44 +020033 let b:undo_ftplugin .= ' | setl et< sts< sw<'
markmacode3e07d5a2024-07-16 21:20:34 +020034endif
35
Rob B5ecee302025-07-02 18:46:44 +020036if !exists('*' . expand('<SID>') . 'GoKeywordPrg')
Phạm Bình An62e82282025-03-07 19:19:31 +010037 func! s:GoKeywordPrg()
38 let temp_isk = &l:iskeyword
39 setl iskeyword+=.
40 try
41 let cmd = 'go doc -C ' . shellescape(expand('%:h')) . ' ' . shellescape(expand('<cword>'))
Phạm Bình An11ab02c2025-03-18 21:05:35 +010042 if has('gui_running') || has('nvim')
43 exe 'hor term' cmd
Phạm Bình An62e82282025-03-07 19:19:31 +010044 else
45 exe '!' . cmd
46 endif
47 finally
48 let &l:iskeyword = temp_isk
49 endtry
50 endfunc
51endif
52
Rob B5ecee302025-07-02 18:46:44 +020053if !exists("no_plugin_maps") && !exists("no_go_maps")
54 noremap <silent> <buffer> ]] <Cmd>call <SID>GoFindSection('next_start', v:count1)<CR>
55 noremap <silent> <buffer> ][ <Cmd>call <SID>GoFindSection('next_end', v:count1)<CR>
56 noremap <silent> <buffer> [[ <Cmd>call <SID>GoFindSection('prev_start', v:count1)<CR>
57 noremap <silent> <buffer> [] <Cmd>call <SID>GoFindSection('prev_end', v:count1)<CR>
58 let b:undo_ftplugin .= ''
59 \ . '| unmap <buffer> ]]'
60 \ . '| unmap <buffer> ]['
61 \ . '| unmap <buffer> [['
62 \ . '| unmap <buffer> []'
63endif
64
65function! <SID>GoFindSection(dir, count)
66 mark '
67 let c = a:count
68 while c > 0
69 if a:dir == 'next_start'
70 keepjumps call search('^\(type\|func\)\>', 'W')
71 elseif a:dir == 'next_end'
72 keepjumps call search('^}', 'W')
73 elseif a:dir == 'prev_start'
74 keepjumps call search('^\(type\|func\)\>', 'bW')
75 elseif a:dir == 'prev_end'
76 keepjumps call search('^}', 'bW')
77 endif
78 let c -= 1
79 endwhile
80endfunction
81
Eisuke Kawashimafbbaa6e2025-04-16 18:20:59 +020082let &cpo = s:cpo_save
83unlet s:cpo_save
84
Bram Moolenaarfb539272014-08-22 19:21:47 +020085" vim: sw=2 sts=2 et