blob: 13291af9cb2a6153e35c2eef97e1edb8dc5f2e2a [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)
ichizokf9d87fa2025-07-05 15:18:41 +020010" 2025 Jul 05 by Vim Project (update b:undo_ftplugin #17664)
Bram Moolenaarfb539272014-08-22 19:21:47 +020011
12if exists('b:did_ftplugin')
13 finish
14endif
15let b:did_ftplugin = 1
16
Eisuke Kawashimafbbaa6e2025-04-16 18:20:59 +020017let s:cpo_save = &cpo
18set cpo&vim
19
Bram Moolenaarfb539272014-08-22 19:21:47 +020020setlocal formatoptions-=t
Phạm Bình An62e82282025-03-07 19:19:31 +010021setlocal formatprg=gofmt
Bram Moolenaarfb539272014-08-22 19:21:47 +020022
23setlocal comments=s1:/*,mb:*,ex:*/,://
24setlocal commentstring=//\ %s
Phạm Bình An62e82282025-03-07 19:19:31 +010025setlocal keywordprg=:GoKeywordPrg
Bram Moolenaarfb539272014-08-22 19:21:47 +020026
Phạm Bình An62e82282025-03-07 19:19:31 +010027command! -buffer -nargs=* GoKeywordPrg call s:GoKeywordPrg()
28
29let b:undo_ftplugin = 'setl fo< com< cms< fp< kp<'
30 \ . '| delcommand -buffer GoKeywordPrg'
Bram Moolenaarfb539272014-08-22 19:21:47 +020031
markmacode3e07d5a2024-07-16 21:20:34 +020032if get(g:, 'go_recommended_style', 1)
33 setlocal noexpandtab softtabstop=0 shiftwidth=0
Rob B5ecee302025-07-02 18:46:44 +020034 let b:undo_ftplugin .= ' | setl et< sts< sw<'
markmacode3e07d5a2024-07-16 21:20:34 +020035endif
36
Rob B5ecee302025-07-02 18:46:44 +020037if !exists('*' . expand('<SID>') . 'GoKeywordPrg')
Phạm Bình An62e82282025-03-07 19:19:31 +010038 func! s:GoKeywordPrg()
39 let temp_isk = &l:iskeyword
40 setl iskeyword+=.
41 try
42 let cmd = 'go doc -C ' . shellescape(expand('%:h')) . ' ' . shellescape(expand('<cword>'))
Phạm Bình An11ab02c2025-03-18 21:05:35 +010043 if has('gui_running') || has('nvim')
44 exe 'hor term' cmd
Phạm Bình An62e82282025-03-07 19:19:31 +010045 else
46 exe '!' . cmd
47 endif
48 finally
49 let &l:iskeyword = temp_isk
50 endtry
51 endfunc
52endif
53
Rob B5ecee302025-07-02 18:46:44 +020054if !exists("no_plugin_maps") && !exists("no_go_maps")
55 noremap <silent> <buffer> ]] <Cmd>call <SID>GoFindSection('next_start', v:count1)<CR>
56 noremap <silent> <buffer> ][ <Cmd>call <SID>GoFindSection('next_end', v:count1)<CR>
57 noremap <silent> <buffer> [[ <Cmd>call <SID>GoFindSection('prev_start', v:count1)<CR>
58 noremap <silent> <buffer> [] <Cmd>call <SID>GoFindSection('prev_end', v:count1)<CR>
59 let b:undo_ftplugin .= ''
ichizokf9d87fa2025-07-05 15:18:41 +020060 \ . "| silent! exe 'unmap <buffer> ]]'"
61 \ . "| silent! exe 'unmap <buffer> ]['"
62 \ . "| silent! exe 'unmap <buffer> [['"
63 \ . "| silent! exe 'unmap <buffer> []'"
Rob B5ecee302025-07-02 18:46:44 +020064endif
65
66function! <SID>GoFindSection(dir, count)
67 mark '
68 let c = a:count
69 while c > 0
70 if a:dir == 'next_start'
71 keepjumps call search('^\(type\|func\)\>', 'W')
72 elseif a:dir == 'next_end'
73 keepjumps call search('^}', 'W')
74 elseif a:dir == 'prev_start'
75 keepjumps call search('^\(type\|func\)\>', 'bW')
76 elseif a:dir == 'prev_end'
77 keepjumps call search('^}', 'bW')
78 endif
79 let c -= 1
80 endwhile
81endfunction
82
Eisuke Kawashimafbbaa6e2025-04-16 18:20:59 +020083let &cpo = s:cpo_save
84unlet s:cpo_save
85
Bram Moolenaarfb539272014-08-22 19:21:47 +020086" vim: sw=2 sts=2 et