├── README.md ├── compiler └── golang.vim ├── demo.png ├── ftdetect └── go.vim └── ftplugin └── go.vim /README.md: -------------------------------------------------------------------------------- 1 | Vim compiler file for Go (golang) 2 | ================================= 3 | 4 | Compiles Go files in the background and usefully underlines and reports 5 | errors to the QuickFix window: 6 | 7 | ![](demo.png?raw=true) 8 | 9 | Installation: 10 | ------------- 11 | 12 | ### Pathogen 13 | 14 | Just clone this repo into your bundles directory: 15 | 16 | git clone https://github.com/rjohnsondev/vim-compiler-go.git ~/.vim/bundle/vim-compiler-go 17 | 18 | Either ensure the $GOROOT environment variable is set, or otherwise set the 19 | g:golang_goroot variable in .vimrc to where your go installation can be found. 20 | This must be an absolute path: 21 | 22 | let g:golang_goroot = "/home/richard/go" 23 | 24 | ### Manual installation 25 | 26 | Drop golang.vim in ~/.vim/compiler directory. 27 | 28 | Add the following line to the autocmd section of .vimrc 29 | 30 | autocmd FileType go compiler golang 31 | 32 | Set the g:golang_goroot variable to where your go installation can be 33 | found. This must be an absolute path 34 | 35 | let g:golang_goroot = "/home/richard/go" 36 | 37 | The plugin assumes a standard project layout with the files stored in a 38 | src directory. The GOPATH is set to one directory below the src folder. 39 | 40 | Usage: 41 | ------ 42 | 43 | Golang is called after a buffer with Go code is saved. The QuickFix 44 | window is opened to show errors, warnings and hints provided by Golang. 45 | 46 | To disable calling Golang every time a buffer is saved, put into .vimrc file: 47 | 48 | let g:golang_onwrite = 0 49 | 50 | The QuickFix window can be disabled with: 51 | 52 | let g:golang_cwindow = 0 53 | 54 | Setting highlights for the lines can be disabled with: 55 | 56 | let g:golang_inline_highlight = 0 57 | 58 | Of course, standard :make command can be used as is the case with every 59 | other compiler. 60 | -------------------------------------------------------------------------------- /compiler/golang.vim: -------------------------------------------------------------------------------- 1 | " Vim compiler file for Go (golang) 2 | " Compiler: Go (golang) 3 | " Maintainer: Richard Johnson 4 | " Last Change: 2012 Nov 24 5 | " Version: 0.1 6 | " Contributors: 7 | " This is mostly based on the excellent pylint compiler plugin 8 | " 9 | " Installation: 10 | " Drop golang.vim in ~/.vim/compiler directory. 11 | " 12 | " Add the following line to the autocmd section of .vimrc 13 | " 14 | " autocmd FileType go compiler golang 15 | " 16 | " Set the $GOROOT environment variable or the g:golang_goroot Vim variable 17 | " to where your go installation can be found. This must be an absolute path 18 | " 19 | " let g:golang_goroot = "/home/richard/go" 20 | " 21 | " The plugin assumes a standard project layout with the files stored in a 22 | " src directory. The GOPATH is set to one directory below the src folder. 23 | " 24 | " Usage: 25 | " Golang is called after a buffer with Golang code is saved. QuickFix 26 | " window is opened to show errors, warnings and hints provided by Golang. 27 | " 28 | " This is realized with :Golang command. To disable calling Golang every 29 | " time a buffer is saved put into .vimrc file 30 | " 31 | " let g:golang_onwrite = 0 32 | " 33 | " Displaying code rate calculated by Golang can be avoided by setting 34 | " 35 | " Openning of QuickFix window can be disabled with 36 | " 37 | " let g:golang_cwindow = 0 38 | " 39 | " Of course, standard :make command can be used as in case of every 40 | " other compiler. 41 | " 42 | " Setting highlights for the lines can be disabled with 43 | " 44 | " let g:golang_inline_highlight = 0 45 | " 46 | 47 | if exists('current_compiler') 48 | finish 49 | endif 50 | let current_compiler = 'golang' 51 | 52 | if !exists('g:golang_onwrite') 53 | let g:golang_onwrite = 1 54 | endif 55 | 56 | if !exists('g:golang_cwindow') 57 | let g:golang_cwindow = 1 58 | endif 59 | 60 | if !exists('g:golang_inline_highlight') 61 | let g:golang_inline_highlight = 1 62 | endif 63 | 64 | if exists(':Golang') != 2 65 | command Golang :call Golang(0) 66 | endif 67 | 68 | if exists(":CompilerSet") != 2 " older Vim always used :setlocal 69 | command -nargs=* CompilerSet setlocal 70 | endif 71 | 72 | au CursorHold call s:GetGolangMessage() 73 | au CursorMoved call s:GetGolangMessage() 74 | 75 | if exists('golang_goroot') 76 | let $GOROOT=golang_goroot 77 | endif 78 | if empty($GOPATH) 79 | let $GOPATH=substitute(expand("%:p:h"),"\\(.*\\)/src.*","\\1",'g') 80 | endif 81 | let $PATHESCAPED=substitute(expand("%:p:h"),"\/","\\\\/",'g') 82 | CompilerSet makeprg=cd\ %:p:h;\ $GOROOT/bin/go\ build\ 2>&1\\\|sed\ -e\ \'s\/^\\(.*\\)\.go/$PATHESCAPED\\/\\1.go\/g\' 83 | CompilerSet efm=%f:%l:%m 84 | 85 | if g:golang_onwrite 86 | augroup python 87 | au! 88 | au BufWritePost * call Golang(1) 89 | augroup end 90 | endif 91 | 92 | if !exists("*s:Golang") 93 | function! Golang(writing) 94 | if has('win32') || has('win16') || has('win95') || has('win64') 95 | setlocal sp=>%s 96 | else 97 | setlocal sp=>%s\ 2>&1 98 | endif 99 | 100 | " If check is executed by buffer write - do not jump to first error 101 | if !a:writing 102 | silent make 103 | else 104 | silent make! 105 | endif 106 | 107 | if g:golang_inline_highlight 108 | call GolangHighlight() 109 | endif 110 | 111 | if g:golang_cwindow 112 | cwindow 113 | endif 114 | 115 | endfunction 116 | endif 117 | 118 | if !exists("*s:GolangHighlight") 119 | function! GolangHighlight() 120 | highlight link GoError SpellBad 121 | 122 | "clear all already highlighted 123 | if exists("b:cleared") 124 | if b:cleared == 0 125 | silent call s:ClearHighlight() 126 | let b:cleared = 1 127 | endif 128 | else 129 | let b:cleared = 1 130 | endif 131 | 132 | let b:matchedlines = {} 133 | 134 | " get all messages from qicklist 135 | let l:list = getqflist() 136 | for l:item in l:list 137 | " highlight lines with errors (only word characters) without end 138 | " of line 139 | let l:matchDict = {} 140 | let l:matchDict['linenum'] = l:item.lnum 141 | let l:matchDict['message'] = l:item.text 142 | if bufnr('%') == item.bufnr 143 | if !has_key(b:matchedlines, l:item.lnum) 144 | let b:matchedlines[l:item.lnum] = l:matchDict 145 | call matchadd("GoError", '\w\%' . l:item.lnum . 'l\n\@!') 146 | endif 147 | endif 148 | endfor 149 | let b:cleared = 0 150 | endfunction 151 | endif 152 | 153 | " keep track of whether or not we are showing a message 154 | let b:showing_message = 0 155 | 156 | " WideMsg() prints [long] message up to (&columns-1) length 157 | " guaranteed without "Press Enter" prompt. 158 | if !exists("*s:WideMsg") 159 | function s:WideMsg(msg) 160 | let x=&ruler | let y=&showcmd 161 | set noruler noshowcmd 162 | redraw 163 | echo a:msg 164 | let &ruler=x | let &showcmd=y 165 | endfun 166 | endif 167 | 168 | if !exists('*s:GetGolangMessage') 169 | function s:GetGolangMessage() 170 | let l:cursorPos = getpos(".") 171 | 172 | " Bail if Golang hasn't been called yet. 173 | if !exists('b:matchedlines') 174 | return 175 | endif 176 | " if there's a message for the line the cursor is currently on, echo 177 | " it to the console 178 | if has_key(b:matchedlines, l:cursorPos[1]) 179 | let l:golangMatch = get(b:matchedlines, l:cursorPos[1]) 180 | call s:WideMsg(l:golangMatch['message']) 181 | let b:showing_message = 1 182 | return 183 | endif 184 | " otherwise, if we're showing a message, clear it 185 | if b:showing_message == 1 186 | echo 187 | let b:showing_message = 0 188 | endif 189 | endfunction 190 | endif 191 | 192 | if !exists('*s:ClearHighlight') 193 | function s:ClearHighlight() 194 | let l:matches = getmatches() 195 | for l:matchId in l:matches 196 | call matchdelete(l:matchId['id']) 197 | endfor 198 | let b:matchedlines = {} 199 | let b:cleared = 1 200 | endfunction 201 | endif 202 | 203 | au BufRead * call s:ClearHighlight() 204 | -------------------------------------------------------------------------------- /demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rjohnsondev/vim-compiler-go/4aad97aad2b914e1a6c33b8eedb4620c8ffd2102/demo.png -------------------------------------------------------------------------------- /ftdetect/go.vim: -------------------------------------------------------------------------------- 1 | autocmd FileType go compiler golang 2 | -------------------------------------------------------------------------------- /ftplugin/go.vim: -------------------------------------------------------------------------------- 1 | if !empty($GOROOT) && !exists('g:golang_goroot') 2 | let g:golang_goroot = $GOROOT 3 | endif 4 | if !exists('g:golang_goroot') 5 | let g:golang_goroot = '/usr/bin' 6 | echoerr 'Please set $GOROOT or g:golang_goroot' 7 | endif 8 | --------------------------------------------------------------------------------