From 12c0ca55ace7f349b1e55df68137fe8e57de8ccd Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Tue, 25 Nov 2014 01:49:13 +0900
Subject: [PATCH 01/19] initial commit

---
 README.md                  | 32 ++++++++++++++++
 addon-info.json            |  6 +++
 autoload/go/coverlay.vim   | 77 ++++++++++++++++++++++++++++++++++++++
 ftplugin/go/coverlay.vim   | 14 +++++++
 plugin/vim-go-coverlay.vim |  6 +++
 5 files changed, 135 insertions(+)
 create mode 100644 README.md
 create mode 100644 addon-info.json
 create mode 100644 autoload/go/coverlay.vim
 create mode 100644 ftplugin/go/coverlay.vim
 create mode 100644 plugin/vim-go-coverlay.vim

diff --git a/README.md b/README.md
new file mode 100644
index 0000000000..1fe92da6f4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,32 @@
+# vim-go-coverlay
+Go (golang) code coverage overlays support for Vim.
+
+## Install
+For Vundle add these lines to your vimrc:
+
+```
+Plugin 'fatih/vim-go' "prerequisites
+Plugin 't-yuki/vim-go-coverlay'
+```
+
+Then run :PluginInstall
+
+## Mappings
+For example, add these lines to your vimrc:
+
+```vim
+au FileType go nmap <leader>c <Plug>(go-coverlay) "test coverage then overlay covered lines
+au FileType go nmap <leader>C <Plug>(go-clearlay) "clear overlay
+```
+
+## Thanks and Credits
+
+* Thanks for basic structure and some of codes: https://github.com/fatih/vim-go and related projects.
+* Thanks for inspiration: https://github.com/twada/coverlay.el
+* Thanks for documentation: http://vim-jp.org/vimdoc-ja/eval.html
+ * http://vim-jp.org/vimdoc-ja/eval.html#matchadd%28%29
+ * http://vim-jp.org/vimdoc-ja/pattern.html
+ * http://vim-jp.org/vimdoc-ja/options.html#%27highlight%27
+ * http://www.vim.org/scripts/script.php?script_id=1238
+* Thanks for parser: https://godoc.org/golang.org/x/tools/cover#ParseProfiles
+
diff --git a/addon-info.json b/addon-info.json
new file mode 100644
index 0000000000..4467648d33
--- /dev/null
+++ b/addon-info.json
@@ -0,0 +1,6 @@
+{
+  "name": "vim-go-coverlay",
+  "description": "Go (golang) code coverage overlays support for Vim.",
+  "author": "Yukinari Toyota <xxseyxx@gmail.com>",
+  "repository" : {"type": "git", "url": "https://github.com/t-yuki/vim-go-coverlay.git"}
+}
diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
new file mode 100644
index 0000000000..68bd1919e6
--- /dev/null
+++ b/autoload/go/coverlay.vim
@@ -0,0 +1,77 @@
+function! go#coverlay#Coverlay(...)
+    let l:tmpname=tempname()
+
+    let command = "go test -coverprofile=".l:tmpname
+
+    let out = go#tool#ExecuteInDir(command)
+    if v:shell_error
+        call go#tool#ShowErrors(out)
+    else
+        " clear previous quick fix window
+        call setqflist([])
+
+        highlight covered term=bold ctermbg=green guibg=green
+        highlight uncover term=bold ctermbg=red guibg=red
+
+        let lines = readfile(l:tmpname,100)
+        let openHTML = 'go tool cover -html='.l:tmpname
+        let mode = lines[0]
+        for line in lines[1:]
+            " file:startline.col,endline.col numstmt count
+            let mx = '\([^:]\+\):\(\d\+\)\.\(\d\+\),\(\d\+\)\.\(\d\+\)\s\(\d\+\)\s\(\d\+\)'
+            let l = matchstr(line, mx)
+            let file = substitute(l, mx, '\1', '')
+            let startline  = substitute(l, mx, '\2', '')
+            let startcol = substitute(l, mx, '\3', '')
+            let endline = substitute(l, mx, '\4', '')
+            let endcol = substitute(l, mx, '\5', '')
+            let numstmt = substitute(l, mx, '\6', '')
+            let cnt = substitute(l, mx, '\7', '')
+
+            let curnr = bufnr("%")
+            let iscur = 0
+            if file[0] == "_"
+                if bufnr(file[1:]) != curnr
+                    let iscur = 1
+                endif
+            else
+                for path in split($GOPATH, ':')
+                    if bufnr(path . '/src/' . file) == curnr
+                        let iscur = 1
+                    endif
+                endfor
+            endif
+            if !iscur
+                continue
+            endif
+
+            "TODO: handle cols
+            "let pat1 = '\%>' . startline . 'l\%' . startcol . 'c'
+            "let pat2 = '\%<' . endline . 'l\%' . endcol . 'c'
+            let pat1 = '\%>' . startline . 'l'
+            let pat2 = '\%<' . endline . 'l'
+            let color = 'covered'
+            if cnt == 0
+                let color = 'uncover'
+            endif
+            silent! call matchadd(color, pat1 . '\_^\s\+' . pat2)
+        endfor
+    endif
+    cwindow
+
+    let errors = getqflist()
+    if !empty(errors)
+        if g:go_jump_to_error
+            cc 1 "jump to first error if there is any
+        endif
+    endif
+
+    call delete(l:tmpname)
+endfunction
+
+function! go#coverlay#Clearlay(...)
+    "TODO: clear locally
+    call clearmatches()
+endfunction
+
+" vim:ts=4:sw=4:et
diff --git a/ftplugin/go/coverlay.vim b/ftplugin/go/coverlay.vim
new file mode 100644
index 0000000000..cda0a16157
--- /dev/null
+++ b/ftplugin/go/coverlay.vim
@@ -0,0 +1,14 @@
+if exists("b:did_ftplugin_go_coverlay")
+	finish
+endif
+let b:did_ftplugin_go_coverlay = 1
+
+" Some handy plug mappings
+nnoremap <silent> <Plug>(go-coverlay) :<C-u>call go#coverlay#Coverlay('')<CR>
+nnoremap <silent> <Plug>(go-clearlay) :<C-u>call go#coverlay#Clearlay('')<CR>
+
+" coverlay
+command! -nargs=* GoCoverlay call go#coverlay#Coverlay(<f-args>)
+command! -nargs=* GoClearlay call go#coverlay#Clearlay(<f-args>)
+
+" vim:ts=4:sw=4:et
diff --git a/plugin/vim-go-coverlay.vim b/plugin/vim-go-coverlay.vim
new file mode 100644
index 0000000000..681b70a0f1
--- /dev/null
+++ b/plugin/vim-go-coverlay.vim
@@ -0,0 +1,6 @@
+if exists("g:loaded_vim_go_coverlay")
+	finish
+endif
+
+let g:loaded_vim_go_coverlay = 1
+" vim:ts=4:sw=4:et

From 5b990f01cd3f1d855246d225a80487e3e78b7367 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Tue, 25 Nov 2014 13:13:39 +0900
Subject: [PATCH 02/19] fix to match inclusive

---
 autoload/go/coverlay.vim | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 68bd1919e6..52ffc38d73 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -50,11 +50,12 @@ function! go#coverlay#Coverlay(...)
             "let pat2 = '\%<' . endline . 'l\%' . endcol . 'c'
             let pat1 = '\%>' . startline . 'l'
             let pat2 = '\%<' . endline . 'l'
+            let pat3 = '\|\%' . startline . 'l\_^\s\+\|\%' . endline . 'l\_^\s\+'
             let color = 'covered'
             if cnt == 0
                 let color = 'uncover'
             endif
-            silent! call matchadd(color, pat1 . '\_^\s\+' . pat2)
+            silent! call matchadd(color, pat1 . '\_^\s\+' . pat2 . pat3)
         endfor
     endif
     cwindow

From 38bfa6bbf024268f6f01e996514e261b9917652f Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Tue, 25 Nov 2014 13:15:07 +0900
Subject: [PATCH 03/19] open quickfix only when errored

---
 autoload/go/coverlay.vim | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 52ffc38d73..39bf402fc1 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -58,10 +58,10 @@ function! go#coverlay#Coverlay(...)
             silent! call matchadd(color, pat1 . '\_^\s\+' . pat2 . pat3)
         endfor
     endif
-    cwindow
 
     let errors = getqflist()
     if !empty(errors)
+        cwindow
         if g:go_jump_to_error
             cc 1 "jump to first error if there is any
         endif

From 768ca0243f1bbc2bf22d75d627fa9a75aacd9556 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Wed, 26 Nov 2014 09:25:52 +0900
Subject: [PATCH 04/19] unlimit on readfile

---
 autoload/go/coverlay.vim | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 39bf402fc1..409526aa25 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -13,8 +13,7 @@ function! go#coverlay#Coverlay(...)
         highlight covered term=bold ctermbg=green guibg=green
         highlight uncover term=bold ctermbg=red guibg=red
 
-        let lines = readfile(l:tmpname,100)
-        let openHTML = 'go tool cover -html='.l:tmpname
+        let lines = readfile(l:tmpname)
         let mode = lines[0]
         for line in lines[1:]
             " file:startline.col,endline.col numstmt count

From 32b7a065928f3498a8d360039e7b843249b76c6a Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sat, 29 Nov 2014 14:21:41 +0900
Subject: [PATCH 05/19] refactor and add tests

---
 Gemfile                            |  3 +
 Rakefile                           | 13 +++++
 autoload/go/coverlay.vim           | 91 +++++++++++++++++-------------
 plugin/vim-go-coverlay.vim         |  2 +
 t/fixtures/src/pkg1/sample.go      | 10 ++++
 t/fixtures/src/pkg1/sample.out     |  4 ++
 t/fixtures/src/pkg1/sample_test.go |  7 +++
 t/test.vim                         | 45 +++++++++++++++
 8 files changed, 137 insertions(+), 38 deletions(-)
 create mode 100644 Gemfile
 create mode 100644 Rakefile
 create mode 100644 t/fixtures/src/pkg1/sample.go
 create mode 100644 t/fixtures/src/pkg1/sample.out
 create mode 100644 t/fixtures/src/pkg1/sample_test.go
 create mode 100644 t/test.vim

diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000000..a87f4e1a2d
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gem 'vim-flavor', '~> 1.1'
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000000..3026d06c93
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,13 @@
+#!/usr/bin/env rake
+
+task :ci => [:dump, :test]
+
+task :dump do
+  sh 'vim --version'
+end
+
+# Firstly, `bundle install`
+# Then, `rake test`
+task :test do
+  sh 'bundle exec vim-flavor test'
+end
diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 409526aa25..47ed042fda 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -1,3 +1,51 @@
+if !exists("g:go_gopath")
+    let g:go_gopath = $GOPATH
+endif
+
+function! go#coverlay#isopenedon(file, bufnr)
+    if a:file[0] == "_"
+        if bufnr(a:file[1:]) == a:bufnr
+            return 1
+        endif
+        return 0
+    endif
+    for path in split(g:go_gopath, ':')
+        if bufnr(path . '/src/' . a:file) == a:bufnr
+            return 1
+        endif
+    endfor
+    return 0
+endfunction
+
+function! go#coverlay#parsegocoverline(line)
+    " file:startline.col,endline.col numstmt count
+    let mx = '\([^:]\+\):\(\d\+\)\.\(\d\+\),\(\d\+\)\.\(\d\+\)\s\(\d\+\)\s\(\d\+\)'
+    let l = matchstr(a:line, mx)
+    let ret = {}
+    let ret.file = substitute(l, mx, '\1', '')
+    let ret.startline  = substitute(l, mx, '\2', '')
+    let ret.startcol = substitute(l, mx, '\3', '')
+    let ret.endline = substitute(l, mx, '\4', '')
+    let ret.endcol = substitute(l, mx, '\5', '')
+    let ret.numstmt = substitute(l, mx, '\6', '')
+    let ret.cnt = substitute(l, mx, '\7', '')
+    return ret
+endfunction
+
+function! go#coverlay#genmatch(cov)
+    "TODO: handle colovs
+    "let pat1 = '\%>' . startline . 'l\%' . startcol . 'c'
+    "let pat2 = '\%<' . endline . 'l\%' . endcol . 'c'
+    let pat1 = '\%>' . a:cov.startline . 'l'
+    let pat2 = '\%<' . a:cov.endline . 'l'
+    let pat3 = '\|\%' . a:cov.startline . 'l\_^\s\+\|\%' . a:cov.endline . 'l\_^\s\+'
+    let color = 'covered'
+    if a:cov.cnt == 0
+        let color = 'uncover'
+    endif
+    return {'group': color, 'pattern': pat1 . '\_^\s\+' . pat2 . pat3}
+endfunction
+
 function! go#coverlay#Coverlay(...)
     let l:tmpname=tempname()
 
@@ -16,46 +64,13 @@ function! go#coverlay#Coverlay(...)
         let lines = readfile(l:tmpname)
         let mode = lines[0]
         for line in lines[1:]
-            " file:startline.col,endline.col numstmt count
-            let mx = '\([^:]\+\):\(\d\+\)\.\(\d\+\),\(\d\+\)\.\(\d\+\)\s\(\d\+\)\s\(\d\+\)'
-            let l = matchstr(line, mx)
-            let file = substitute(l, mx, '\1', '')
-            let startline  = substitute(l, mx, '\2', '')
-            let startcol = substitute(l, mx, '\3', '')
-            let endline = substitute(l, mx, '\4', '')
-            let endcol = substitute(l, mx, '\5', '')
-            let numstmt = substitute(l, mx, '\6', '')
-            let cnt = substitute(l, mx, '\7', '')
-
-            let curnr = bufnr("%")
-            let iscur = 0
-            if file[0] == "_"
-                if bufnr(file[1:]) != curnr
-                    let iscur = 1
-                endif
-            else
-                for path in split($GOPATH, ':')
-                    if bufnr(path . '/src/' . file) == curnr
-                        let iscur = 1
-                    endif
-                endfor
-            endif
-            if !iscur
+            let c = go#coverlay#parsegocoverline(line)
+            if !go#coverlay#isopenedon(c.file, bufnr("%"))
                 continue
             endif
-
-            "TODO: handle cols
-            "let pat1 = '\%>' . startline . 'l\%' . startcol . 'c'
-            "let pat2 = '\%<' . endline . 'l\%' . endcol . 'c'
-            let pat1 = '\%>' . startline . 'l'
-            let pat2 = '\%<' . endline . 'l'
-            let pat3 = '\|\%' . startline . 'l\_^\s\+\|\%' . endline . 'l\_^\s\+'
-            let color = 'covered'
-            if cnt == 0
-                let color = 'uncover'
-            endif
-            silent! call matchadd(color, pat1 . '\_^\s\+' . pat2 . pat3)
-        endfor
+            let m = go#coverlay#genmatch(c)
+            silent! call matchadd(m.group, m.pattern)
+       endfor
     endif
 
     let errors = getqflist()
diff --git a/plugin/vim-go-coverlay.vim b/plugin/vim-go-coverlay.vim
index 681b70a0f1..830ed52e8f 100644
--- a/plugin/vim-go-coverlay.vim
+++ b/plugin/vim-go-coverlay.vim
@@ -1,3 +1,5 @@
+" vim-go-coverlay - Go (golang) code coverage overlays support
+"
 if exists("g:loaded_vim_go_coverlay")
 	finish
 endif
diff --git a/t/fixtures/src/pkg1/sample.go b/t/fixtures/src/pkg1/sample.go
new file mode 100644
index 0000000000..6a1ff8139e
--- /dev/null
+++ b/t/fixtures/src/pkg1/sample.go
@@ -0,0 +1,10 @@
+// set gopath before
+//go:generate go test --coverprofile=sample.out
+package pkg1
+
+func Sample() int {
+	if false {
+		return 0
+	}
+	return 1
+}
diff --git a/t/fixtures/src/pkg1/sample.out b/t/fixtures/src/pkg1/sample.out
new file mode 100644
index 0000000000..19aa51e94a
--- /dev/null
+++ b/t/fixtures/src/pkg1/sample.out
@@ -0,0 +1,4 @@
+mode: set
+pkg1/sample.go:5.19,6.11 1 1
+pkg1/sample.go:9.2,9.10 1 1
+pkg1/sample.go:6.11,8.3 1 0
diff --git a/t/fixtures/src/pkg1/sample_test.go b/t/fixtures/src/pkg1/sample_test.go
new file mode 100644
index 0000000000..e39d7ba835
--- /dev/null
+++ b/t/fixtures/src/pkg1/sample_test.go
@@ -0,0 +1,7 @@
+package pkg1
+
+import "testing"
+
+func TestSample(t *testing.T) {
+	Sample()
+}
diff --git a/t/test.vim b/t/test.vim
new file mode 100644
index 0000000000..f4897ebc63
--- /dev/null
+++ b/t/test.vim
@@ -0,0 +1,45 @@
+describe 'go#coverlay#isopenedon'
+    before
+        new
+	let g:curdir = expand('<sfile>:p:h') . '/'
+	let g:srcpath = 't/fixtures/src/'
+	let g:sample = 'pkg1/sample.go'
+	let g:sampleabs = g:curdir . g:srcpath . 'pkg1/sample.go'
+	let g:go_gopath = g:curdir . 't/fixtures'
+	execute "badd " . g:srcpath . g:sample
+    end
+    after
+	execute "bdelete " . g:srcpath . g:sample
+        close!
+    end
+
+    it 'returns 1 if FILE is opened at BUFNR'
+        Expect go#coverlay#isopenedon('_' . g:sampleabs, bufnr("$")) == 1
+        Expect go#coverlay#isopenedon(g:sample, bufnr("$")) == 1
+    end
+
+    it 'returns 0 if FILE is not opened at BUFNR'
+        Expect go#coverlay#isopenedon('_' . g:sampleabs, 42) == 0
+        Expect go#coverlay#isopenedon(g:sample, 42) == 0
+    end
+
+    it 'returns 0 if FILE is not exists'
+        Expect go#coverlay#isopenedon('_' . g:curdir . g:srcpath . 'pkg1/NOTEXISTS', bufnr("$")) == 0
+        Expect go#coverlay#isopenedon('pkg1/NOTEXISTS.go', bufnr("$")) == 0
+    end
+end
+
+describe 'go#coverlay#parsegocoverline'
+    it 'parses a go cover output line and returns as dict'
+        let d = {'file': 'f',"startline": "1", "startcol": "2", "endline": "3", "endcol": "4", "numstmt": "5", "cnt": "6"}
+        " file:startline.col,endline.col numstmt count
+        Expect go#coverlay#parsegocoverline("f:1.2,3.4 5 6") == d
+    end
+end
+
+describe 'go#coverlay#genmatch'
+    it 'generate mark pattern from cover data'
+        let d = {'file': 'f',"startline": "1", "startcol": "2", "endline": "3", "endcol": "4", "numstmt": "5", "cnt": "6"}
+        Expect go#coverlay#genmatch(d) == {'group': 'covered', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+'}
+    end
+end

From 319539dbb9e6c80e9bd2f568657cac75cae81cc9 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sun, 30 Nov 2014 21:54:00 +0900
Subject: [PATCH 06/19] refactor and add tests. delete matches selectively on
 Clearlay

---
 VimFlavor                |  1 +
 autoload/go/coverlay.vim | 49 +++++++++++++++++++++++++++-------------
 t/test.vim               | 39 ++++++++++++++++++++++++++++----
 3 files changed, 69 insertions(+), 20 deletions(-)
 create mode 100644 VimFlavor

diff --git a/VimFlavor b/VimFlavor
new file mode 100644
index 0000000000..038b3194ed
--- /dev/null
+++ b/VimFlavor
@@ -0,0 +1 @@
+flavor 't-yuki/vim-go', '~> 0.0.1'
diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 47ed042fda..aaa7d38d05 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -46,6 +46,27 @@ function! go#coverlay#genmatch(cov)
     return {'group': color, 'pattern': pat1 . '\_^\s\+' . pat2 . pat3}
 endfunction
 
+function! go#coverlay#overlay(file)
+    if !exists("b:go_coverlay_matches")
+        let b:go_coverlay_matches = []
+    endif
+
+    highlight covered term=bold ctermbg=green guibg=green
+    highlight uncover term=bold ctermbg=red guibg=red
+
+    let lines = readfile(a:file)
+    let mode = lines[0]
+    for line in lines[1:]
+        let c = go#coverlay#parsegocoverline(line)
+        if !go#coverlay#isopenedon(c.file, bufnr("%"))
+            continue
+        endif
+        let m = go#coverlay#genmatch(c)
+        let id = matchadd(m.group, m.pattern)
+        call add(b:go_coverlay_matches, id)
+   endfor
+endfunction
+
 function! go#coverlay#Coverlay(...)
     let l:tmpname=tempname()
 
@@ -57,20 +78,7 @@ function! go#coverlay#Coverlay(...)
     else
         " clear previous quick fix window
         call setqflist([])
-
-        highlight covered term=bold ctermbg=green guibg=green
-        highlight uncover term=bold ctermbg=red guibg=red
-
-        let lines = readfile(l:tmpname)
-        let mode = lines[0]
-        for line in lines[1:]
-            let c = go#coverlay#parsegocoverline(line)
-            if !go#coverlay#isopenedon(c.file, bufnr("%"))
-                continue
-            endif
-            let m = go#coverlay#genmatch(c)
-            silent! call matchadd(m.group, m.pattern)
-       endfor
+        call go#coverlay#overlay(l:tmpname)
     endif
 
     let errors = getqflist()
@@ -85,8 +93,17 @@ function! go#coverlay#Coverlay(...)
 endfunction
 
 function! go#coverlay#Clearlay(...)
-    "TODO: clear locally
-    call clearmatches()
+    if !exists("b:go_coverlay_matches")
+        return
+    endif
+    for id in b:go_coverlay_matches
+        call matchdelete(id)
+    endfor
+    let b:go_coverlay_matches = []
+endfunction
+
+function! go#coverlay#matches()
+    return b:go_coverlay_matches
 endfunction
 
 " vim:ts=4:sw=4:et
diff --git a/t/test.vim b/t/test.vim
index f4897ebc63..08d581cf65 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -1,3 +1,34 @@
+describe 'go#coverlay#Coverlay'
+    before
+        new
+	let g:curdir = expand('<sfile>:p:h') . '/'
+	let g:srcpath = 't/fixtures/src/'
+	let g:sample = 'pkg1/sample.go'
+	let g:sampleabs = g:curdir . g:srcpath . 'pkg1/sample.go'
+	let g:samplecover = g:curdir . g:srcpath . 'pkg1/sample.out'
+	let g:go_gopath = g:curdir . 't/fixtures'
+	execute "badd " . g:srcpath . g:sample
+	execute "buffer " . bufnr("$")
+    end
+    after
+	execute "bprev"
+	execute "bdelete " . g:srcpath . g:sample
+        close!
+    end
+
+    it 'puts match to the list'
+        call go#coverlay#Coverlay()
+        Expect len(go#coverlay#matches()) == 3
+        call go#coverlay#Clearlay()
+        Expect len(go#coverlay#matches()) == 0
+
+        call go#coverlay#Coverlay()
+        Expect len(go#coverlay#matches()) == 3
+        call go#coverlay#Clearlay()
+        Expect len(go#coverlay#matches()) == 0
+    end
+end
+
 describe 'go#coverlay#isopenedon'
     before
         new
@@ -14,8 +45,8 @@ describe 'go#coverlay#isopenedon'
     end
 
     it 'returns 1 if FILE is opened at BUFNR'
-        Expect go#coverlay#isopenedon('_' . g:sampleabs, bufnr("$")) == 1
-        Expect go#coverlay#isopenedon(g:sample, bufnr("$")) == 1
+        Expect go#coverlay#isopenedon('_' . g:sampleabs, bufnr(g:sampleabs)) == 1
+        Expect go#coverlay#isopenedon(g:sample, bufnr(g:sampleabs)) == 1
     end
 
     it 'returns 0 if FILE is not opened at BUFNR'
@@ -24,8 +55,8 @@ describe 'go#coverlay#isopenedon'
     end
 
     it 'returns 0 if FILE is not exists'
-        Expect go#coverlay#isopenedon('_' . g:curdir . g:srcpath . 'pkg1/NOTEXISTS', bufnr("$")) == 0
-        Expect go#coverlay#isopenedon('pkg1/NOTEXISTS.go', bufnr("$")) == 0
+        Expect go#coverlay#isopenedon('_' . g:curdir . g:srcpath . 'pkg1/NOTEXISTS', bufnr(g:sampleabs)) == 0
+        Expect go#coverlay#isopenedon('pkg1/NOTEXISTS.go', bufnr(g:sampleabs)) == 0
     end
 end
 

From aabd71306b95a17b7a9c8337876ba143c5317f72 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Mon, 1 Dec 2014 20:12:10 +0900
Subject: [PATCH 07/19] set higher priority for covered line to beat the red

---
 autoload/go/coverlay.vim       | 6 ++++--
 t/fixtures/src/pkg1/sample.go  | 2 ++
 t/fixtures/src/pkg1/sample.out | 4 +++-
 t/test.vim                     | 8 +++++---
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index aaa7d38d05..aaab9770cc 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -40,10 +40,12 @@ function! go#coverlay#genmatch(cov)
     let pat2 = '\%<' . a:cov.endline . 'l'
     let pat3 = '\|\%' . a:cov.startline . 'l\_^\s\+\|\%' . a:cov.endline . 'l\_^\s\+'
     let color = 'covered'
+    let prio = 11
     if a:cov.cnt == 0
         let color = 'uncover'
+        let prio = 10
     endif
-    return {'group': color, 'pattern': pat1 . '\_^\s\+' . pat2 . pat3}
+    return {'group': color, 'pattern': pat1 . '\_^\s\+' . pat2 . pat3, 'priority': prio}
 endfunction
 
 function! go#coverlay#overlay(file)
@@ -62,7 +64,7 @@ function! go#coverlay#overlay(file)
             continue
         endif
         let m = go#coverlay#genmatch(c)
-        let id = matchadd(m.group, m.pattern)
+        let id = matchadd(m.group, m.pattern, m.priority)
         call add(b:go_coverlay_matches, id)
    endfor
 endfunction
diff --git a/t/fixtures/src/pkg1/sample.go b/t/fixtures/src/pkg1/sample.go
index 6a1ff8139e..6ce94b394b 100644
--- a/t/fixtures/src/pkg1/sample.go
+++ b/t/fixtures/src/pkg1/sample.go
@@ -5,6 +5,8 @@ package pkg1
 func Sample() int {
 	if false {
 		return 0
+	} else if false {
+		return 0
 	}
 	return 1
 }
diff --git a/t/fixtures/src/pkg1/sample.out b/t/fixtures/src/pkg1/sample.out
index 19aa51e94a..909a63f142 100644
--- a/t/fixtures/src/pkg1/sample.out
+++ b/t/fixtures/src/pkg1/sample.out
@@ -1,4 +1,6 @@
 mode: set
 pkg1/sample.go:5.19,6.11 1 1
-pkg1/sample.go:9.2,9.10 1 1
+pkg1/sample.go:11.2,11.10 1 1
 pkg1/sample.go:6.11,8.3 1 0
+pkg1/sample.go:8.3,8.18 1 1
+pkg1/sample.go:8.18,10.3 1 0
diff --git a/t/test.vim b/t/test.vim
index 08d581cf65..bc91ab39e7 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -18,12 +18,12 @@ describe 'go#coverlay#Coverlay'
 
     it 'puts match to the list'
         call go#coverlay#Coverlay()
-        Expect len(go#coverlay#matches()) == 3
+        Expect len(go#coverlay#matches()) == 5
         call go#coverlay#Clearlay()
         Expect len(go#coverlay#matches()) == 0
 
         call go#coverlay#Coverlay()
-        Expect len(go#coverlay#matches()) == 3
+        Expect len(go#coverlay#matches()) == 5
         call go#coverlay#Clearlay()
         Expect len(go#coverlay#matches()) == 0
     end
@@ -71,6 +71,8 @@ end
 describe 'go#coverlay#genmatch'
     it 'generate mark pattern from cover data'
         let d = {'file': 'f',"startline": "1", "startcol": "2", "endline": "3", "endcol": "4", "numstmt": "5", "cnt": "6"}
-        Expect go#coverlay#genmatch(d) == {'group': 'covered', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+'}
+        Expect go#coverlay#genmatch(d) == {'group': 'covered', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+', "priority": 11}
+        let d = {'file': 'f',"startline": "1", "startcol": "2", "endline": "3", "endcol": "4", "numstmt": "5", "cnt": "0"}
+        Expect go#coverlay#genmatch(d) == {'group': 'uncover', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+', "priority": 10}
     end
 end

From 1262cfafe84a58ee3c0d79ec3c0e7f98a6ba71ed Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Mon, 1 Dec 2014 20:50:48 +0900
Subject: [PATCH 08/19] ignore close parentesis

---
 autoload/go/coverlay.vim | 9 +++------
 t/test.vim               | 4 ++--
 2 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index aaab9770cc..98c30a189d 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -33,17 +33,14 @@ function! go#coverlay#parsegocoverline(line)
 endfunction
 
 function! go#coverlay#genmatch(cov)
-    "TODO: handle colovs
-    "let pat1 = '\%>' . startline . 'l\%' . startcol . 'c'
-    "let pat2 = '\%<' . endline . 'l\%' . endcol . 'c'
     let pat1 = '\%>' . a:cov.startline . 'l'
     let pat2 = '\%<' . a:cov.endline . 'l'
-    let pat3 = '\|\%' . a:cov.startline . 'l\_^\s\+\|\%' . a:cov.endline . 'l\_^\s\+'
+    let pat3 = '\|\%' . a:cov.startline . 'l\_^\s\+\|\%' . a:cov.endline . 'l\_^\s\+\(\}$\)\@!'
     let color = 'covered'
-    let prio = 11
+    let prio = 6
     if a:cov.cnt == 0
         let color = 'uncover'
-        let prio = 10
+        let prio = 5
     endif
     return {'group': color, 'pattern': pat1 . '\_^\s\+' . pat2 . pat3, 'priority': prio}
 endfunction
diff --git a/t/test.vim b/t/test.vim
index bc91ab39e7..c8a2867196 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -71,8 +71,8 @@ end
 describe 'go#coverlay#genmatch'
     it 'generate mark pattern from cover data'
         let d = {'file': 'f',"startline": "1", "startcol": "2", "endline": "3", "endcol": "4", "numstmt": "5", "cnt": "6"}
-        Expect go#coverlay#genmatch(d) == {'group': 'covered', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+', "priority": 11}
+        Expect go#coverlay#genmatch(d) == {'group': 'covered', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+\(\}$\)\@!', "priority": 6}
         let d = {'file': 'f',"startline": "1", "startcol": "2", "endline": "3", "endcol": "4", "numstmt": "5", "cnt": "0"}
-        Expect go#coverlay#genmatch(d) == {'group': 'uncover', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+', "priority": 10}
+        Expect go#coverlay#genmatch(d) == {'group': 'uncover', "pattern": '\%>1l\_^\s\+\%<3l\|\%1l\_^\s\+\|\%3l\_^\s\+\(\}$\)\@!', "priority": 5}
     end
 end

From dac0c656b4759a2c8a7e4d8229c43c103f3db7b9 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sat, 6 Dec 2014 01:39:50 +0900
Subject: [PATCH 09/19] add a screen shot

---
 README.md | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/README.md b/README.md
index 1fe92da6f4..bb05d0c8af 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,8 @@
 # vim-go-coverlay
 Go (golang) code coverage overlays support for Vim.
 
+![ss-vim-go-coverlay](https://cloud.githubusercontent.com/assets/3804806/5319001/81a3b89a-7ce8-11e4-9fbd-2f0fd00ce1c7.gif)
+
 ## Install
 For Vundle add these lines to your vimrc:
 

From f3cc9803ab40d6b48b6434fa7266caf596745abf Mon Sep 17 00:00:00 2001
From: Anup Chenthamarakshan <anupc@dropbox.com>
Date: Thu, 28 May 2015 08:40:31 -0700
Subject: [PATCH 10/19] Make it easier to run coverlay as a BufWritePost
 command. => Clear coverlay before rerunning. => If tests pass, show coverlay
 immediately. => If tests fail, show window with list of failures,    but
 don't switch to line showing failure. Useful for TDD.

---
 autoload/go/coverlay.vim | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 98c30a189d..f1382f65e4 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -67,13 +67,13 @@ function! go#coverlay#overlay(file)
 endfunction
 
 function! go#coverlay#Coverlay(...)
+    call go#coverlay#Clearlay()
     let l:tmpname=tempname()
 
-    let command = "go test -coverprofile=".l:tmpname
+    let out = go#cmd#Test(1, 0, "-coverprofile=".l:tmpname)
 
-    let out = go#tool#ExecuteInDir(command)
     if v:shell_error
-        call go#tool#ShowErrors(out)
+        return
     else
         " clear previous quick fix window
         call setqflist([])

From 7685a2b326ec996a3e3d871b39b13afda2b03937 Mon Sep 17 00:00:00 2001
From: Anup Chenthamarakshan <anupc@dropbox.com>
Date: Wed, 3 Jun 2015 08:33:21 -0700
Subject: [PATCH 11/19] Ignore coverage info if no tests exist.

---
 autoload/go/coverlay.vim | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index f1382f65e4..bdb39f3d95 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -53,6 +53,9 @@ function! go#coverlay#overlay(file)
     highlight covered term=bold ctermbg=green guibg=green
     highlight uncover term=bold ctermbg=red guibg=red
 
+    if !filereadable(a:file)
+        return
+    endif
     let lines = readfile(a:file)
     let mode = lines[0]
     for line in lines[1:]

From 70c8cf8985979e3c473481fad0bbb37569703a73 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sun, 21 Jun 2015 15:13:53 +0900
Subject: [PATCH 12/19] code cleanup

---
 README.md                              |  5 ++++
 VimFlavor                              |  2 +-
 autoload/go/coverlay.vim               | 30 ++++++++---------------
 t/fixtures/src/failtest/sample.go      | 12 +++++++++
 t/fixtures/src/failtest/sample_test.go |  8 ++++++
 t/test.vim                             | 34 ++++++++++++++++++++++++++
 6 files changed, 70 insertions(+), 21 deletions(-)
 create mode 100644 t/fixtures/src/failtest/sample.go
 create mode 100644 t/fixtures/src/failtest/sample_test.go

diff --git a/README.md b/README.md
index bb05d0c8af..52d99d85f1 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,8 @@ For example, add these lines to your vimrc:
 ```vim
 au FileType go nmap <leader>c <Plug>(go-coverlay) "test coverage then overlay covered lines
 au FileType go nmap <leader>C <Plug>(go-clearlay) "clear overlay
+
+au BufWritePost *.go call go#coverlay#Coverlay() "run test and cover on file save
 ```
 
 ## Thanks and Credits
@@ -31,4 +33,7 @@ au FileType go nmap <leader>C <Plug>(go-clearlay) "clear overlay
  * http://vim-jp.org/vimdoc-ja/options.html#%27highlight%27
  * http://www.vim.org/scripts/script.php?script_id=1238
 * Thanks for parser: https://godoc.org/golang.org/x/tools/cover#ParseProfiles
+* Thanks for contributors
+ * https://github.com/t-yuki/vim-go-coverlay
+ * https://github.com/anupcshan/vim-go-coverlay
 
diff --git a/VimFlavor b/VimFlavor
index 038b3194ed..b8a2e742ed 100644
--- a/VimFlavor
+++ b/VimFlavor
@@ -1 +1 @@
-flavor 't-yuki/vim-go', '~> 0.0.1'
+flavor 'fatih/vim-go', '~> 1.0'
diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index bdb39f3d95..88b0bb2da2 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -2,6 +2,12 @@ if !exists("g:go_gopath")
     let g:go_gopath = $GOPATH
 endif
 
+function! go#coverlay#oninitbuf()
+    if !exists("b:go_coverlay_matches")
+        let b:go_coverlay_matches = []
+    endif
+endfunction
+
 function! go#coverlay#isopenedon(file, bufnr)
     if a:file[0] == "_"
         if bufnr(a:file[1:]) == a:bufnr
@@ -46,9 +52,7 @@ function! go#coverlay#genmatch(cov)
 endfunction
 
 function! go#coverlay#overlay(file)
-    if !exists("b:go_coverlay_matches")
-        let b:go_coverlay_matches = []
-    endif
+    call go#coverlay#oninitbuf()
 
     highlight covered term=bold ctermbg=green guibg=green
     highlight uncover term=bold ctermbg=red guibg=red
@@ -75,29 +79,14 @@ function! go#coverlay#Coverlay(...)
 
     let out = go#cmd#Test(1, 0, "-coverprofile=".l:tmpname)
 
-    if v:shell_error
-        return
-    else
-        " clear previous quick fix window
-        call setqflist([])
+    if !v:shell_error
         call go#coverlay#overlay(l:tmpname)
     endif
-
-    let errors = getqflist()
-    if !empty(errors)
-        cwindow
-        if g:go_jump_to_error
-            cc 1 "jump to first error if there is any
-        endif
-    endif
-
     call delete(l:tmpname)
 endfunction
 
 function! go#coverlay#Clearlay(...)
-    if !exists("b:go_coverlay_matches")
-        return
-    endif
+    call go#coverlay#oninitbuf()
     for id in b:go_coverlay_matches
         call matchdelete(id)
     endfor
@@ -105,6 +94,7 @@ function! go#coverlay#Clearlay(...)
 endfunction
 
 function! go#coverlay#matches()
+    call go#coverlay#oninitbuf()
     return b:go_coverlay_matches
 endfunction
 
diff --git a/t/fixtures/src/failtest/sample.go b/t/fixtures/src/failtest/sample.go
new file mode 100644
index 0000000000..5859e92624
--- /dev/null
+++ b/t/fixtures/src/failtest/sample.go
@@ -0,0 +1,12 @@
+// set gopath before
+//go:generate go test --coverprofile=sample.out
+package pkg
+
+func Sample() int {
+	if false {
+		return 0
+	} else if false {
+		return 0
+	}
+	return 1
+}
diff --git a/t/fixtures/src/failtest/sample_test.go b/t/fixtures/src/failtest/sample_test.go
new file mode 100644
index 0000000000..7f1ade0d73
--- /dev/null
+++ b/t/fixtures/src/failtest/sample_test.go
@@ -0,0 +1,8 @@
+package pkg
+
+import "testing"
+
+func TestSample(t *testing.T) {
+	Sample()
+	t.Fatal("itwillfail")
+}
diff --git a/t/test.vim b/t/test.vim
index c8a2867196..348ec26a79 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -1,3 +1,5 @@
+" to execute, `rake test` on parent dir
+
 describe 'go#coverlay#Coverlay'
     before
         new
@@ -26,6 +28,38 @@ describe 'go#coverlay#Coverlay'
         Expect len(go#coverlay#matches()) == 5
         call go#coverlay#Clearlay()
         Expect len(go#coverlay#matches()) == 0
+
+        call go#coverlay#Coverlay()
+        Expect len(go#coverlay#matches()) == 5
+        call go#coverlay#Coverlay()
+        Expect len(go#coverlay#matches()) == 5
+        call go#coverlay#Clearlay()
+        Expect len(go#coverlay#matches()) == 0
+    end
+end
+
+describe 'go#coverlay#Coverlay fail'
+    before
+        new
+	let g:curdir = expand('<sfile>:p:h') . '/'
+	let g:srcpath = 't/fixtures/src/'
+	let g:sample = 'failtest/sample.go'
+	let g:sampleabs = g:curdir . g:srcpath . 'failtest/sample.go'
+	let g:go_gopath = g:curdir . 't/fixtures'
+	execute "badd " . g:srcpath . g:sample
+	execute "buffer " . bufnr("$")
+    end
+    after
+	execute "bprev"
+	execute "bdelete " . g:srcpath . g:sample
+	call setqflist([])
+	cclose
+    end
+
+    it 'does nothing if test fail'
+	call go#coverlay#Coverlay()
+	Expect len(go#coverlay#matches()) == 0
+	Expect len(getqflist()) == 1
     end
 end
 

From 4d9e43c88cac510d40d96d20bef69b81fa324d82 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Mon, 22 Jun 2015 02:00:28 +0900
Subject: [PATCH 13/19] coverlay all buffers at once

---
 autoload/go/coverlay.vim | 67 ++++++++++++++++++++++++++++++++++------
 t/test.vim               | 35 +++++++++++++++++++++
 2 files changed, 92 insertions(+), 10 deletions(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 88b0bb2da2..0b3f89762c 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -2,10 +2,45 @@ if !exists("g:go_gopath")
     let g:go_gopath = $GOPATH
 endif
 
-function! go#coverlay#oninitbuf()
+augroup plugin-go-coverlay
+    autocmd!
+    autocmd BufEnter,BufWinEnter,BufFilePost * call go#coverlay#draw()
+    autocmd BufWinLeave * call go#coverlay#clear()
+augroup END
+
+function! go#coverlay#draw()
+    call go#coverlay#hook()
+    call go#coverlay#clear()
+    for m in b:go_coverlay_matches
+        let id = matchadd(m.group, m.pattern, m.priority)
+        call add(b:go_coverlay_match_ids, id)
+    endfor
+endfunction
+
+function! go#coverlay#hook()
+    "TODO: can we initialize buf local vars more smartly?
     if !exists("b:go_coverlay_matches")
         let b:go_coverlay_matches = []
     endif
+    if !exists("b:go_coverlay_match_ids")
+        let b:go_coverlay_match_ids = []
+    endif
+endfunction
+
+"findbufnr look for the number of buffer that opens `file`,
+" as it is displayed by the ":ls" command.
+"If the buffer doesn't exist, -1 is returned.
+function! go#coverlay#findbufnr(file)
+    if a:file[0] == "_"
+        return bufnr(a:file[1:])
+    endif
+    for path in split(g:go_gopath, ':')
+        let nr = bufnr(path . '/src/' . a:file)
+        if nr != -1
+            return nr
+        endif
+    endfor
+    return -1
 endfunction
 
 function! go#coverlay#isopenedon(file, bufnr)
@@ -52,7 +87,7 @@ function! go#coverlay#genmatch(cov)
 endfunction
 
 function! go#coverlay#overlay(file)
-    call go#coverlay#oninitbuf()
+    call go#coverlay#hook()
 
     highlight covered term=bold ctermbg=green guibg=green
     highlight uncover term=bold ctermbg=red guibg=red
@@ -64,19 +99,26 @@ function! go#coverlay#overlay(file)
     let mode = lines[0]
     for line in lines[1:]
         let c = go#coverlay#parsegocoverline(line)
-        if !go#coverlay#isopenedon(c.file, bufnr("%"))
+        let nr = go#coverlay#findbufnr(c.file)
+        if nr == -1
+            "should we records cov data
+            " even if it is not opened currently?
             continue
         endif
         let m = go#coverlay#genmatch(c)
-        let id = matchadd(m.group, m.pattern, m.priority)
-        call add(b:go_coverlay_matches, id)
-   endfor
+        let matches = get(getbufvar(nr, ""), "go_coverlay_matches", [])
+        call add(matches, m)
+        call setbufvar(nr, "go_coverlay_matches", matches)
+    endfor
+    "TODO: can we draw other window for split windows mode?
+    call go#coverlay#draw()
 endfunction
 
 function! go#coverlay#Coverlay(...)
     call go#coverlay#Clearlay()
     let l:tmpname=tempname()
 
+    "TODO: add -coverpkg options based on current buf list
     let out = go#cmd#Test(1, 0, "-coverprofile=".l:tmpname)
 
     if !v:shell_error
@@ -86,15 +128,20 @@ function! go#coverlay#Coverlay(...)
 endfunction
 
 function! go#coverlay#Clearlay(...)
-    call go#coverlay#oninitbuf()
-    for id in b:go_coverlay_matches
+    call go#coverlay#hook()
+    call go#coverlay#clear()
+    let b:go_coverlay_matches = []
+endfunction
+
+function! go#coverlay#clear(...)
+    for id in b:go_coverlay_match_ids
         call matchdelete(id)
     endfor
-    let b:go_coverlay_matches = []
+    let b:go_coverlay_match_ids = []
 endfunction
 
 function! go#coverlay#matches()
-    call go#coverlay#oninitbuf()
+    call go#coverlay#hook()
     return b:go_coverlay_matches
 endfunction
 
diff --git a/t/test.vim b/t/test.vim
index 348ec26a79..088596bf51 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -63,6 +63,39 @@ describe 'go#coverlay#Coverlay fail'
     end
 end
 
+describe 'go#coverlay#findbufnr'
+    before
+        new
+	let g:curdir = expand('<sfile>:p:h') . '/'
+	let g:srcpath = 't/fixtures/src/'
+	let g:sample = 'pkg1/sample.go'
+	let g:sample2 = 'pkg2/sample.go'
+	let g:sampleabs = g:curdir . g:srcpath . 'pkg1/sample.go'
+	let g:sampleabs2 = g:curdir . g:srcpath . 'pkg2/sample.go'
+	let g:go_gopath = g:curdir . 't/fixtures'
+	execute "badd " . g:srcpath . g:sample
+	execute "badd " . g:srcpath . g:sample2
+    end
+    after
+	execute "bdelete " . g:srcpath . g:sample2
+	execute "bdelete " . g:srcpath . g:sample
+        close!
+    end
+
+    it 'returns BUFNR if FILE is opened at BUFNR'
+        Expect go#coverlay#findbufnr('_' . g:sampleabs) == bufnr(g:sampleabs)
+        Expect go#coverlay#findbufnr(g:sample) == bufnr(g:sampleabs)
+
+        Expect go#coverlay#findbufnr('_' . g:sampleabs2) == bufnr(g:sampleabs2)
+        Expect go#coverlay#findbufnr(g:sample2) == bufnr(g:sampleabs2)
+    end
+
+    it 'returns -1 if FILE is not exists'
+        Expect go#coverlay#findbufnr('pkg1/NOTEXISTS.go') == -1
+        Expect go#coverlay#findbufnr('_' . g:curdir . g:srcpath . 'pkg1/NOTEXISTS.go') == -1
+    end
+end
+
 describe 'go#coverlay#isopenedon'
     before
         new
@@ -94,6 +127,8 @@ describe 'go#coverlay#isopenedon'
     end
 end
 
+
+
 describe 'go#coverlay#parsegocoverline'
     it 'parses a go cover output line and returns as dict'
         let d = {'file': 'f',"startline": "1", "startcol": "2", "endline": "3", "endcol": "4", "numstmt": "5", "cnt": "6"}

From fabeb9dcb33aae3d35b5499d551c2fd96c1d9741 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sat, 16 Jan 2016 20:20:30 +0900
Subject: [PATCH 14/19] update vim-flavor

---
 Gemfile   | 2 +-
 Rakefile  | 2 +-
 VimFlavor | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/Gemfile b/Gemfile
index a87f4e1a2d..d562d4327e 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,3 +1,3 @@
 source 'https://rubygems.org'
 
-gem 'vim-flavor', '~> 1.1'
+gem 'vim-flavor', '~> 2.2.1'
diff --git a/Rakefile b/Rakefile
index 3026d06c93..64346ff0aa 100644
--- a/Rakefile
+++ b/Rakefile
@@ -6,7 +6,7 @@ task :dump do
   sh 'vim --version'
 end
 
-# Firstly, `bundle install`
+# Firstly, `bundle install; bundle install --deployment`
 # Then, `rake test`
 task :test do
   sh 'bundle exec vim-flavor test'
diff --git a/VimFlavor b/VimFlavor
index b8a2e742ed..a70269f428 100644
--- a/VimFlavor
+++ b/VimFlavor
@@ -1 +1 @@
-flavor 'fatih/vim-go', '~> 1.0'
+flavor 'fatih/vim-go', '~> 1.3'

From 674268f5aae0cbdfca40014e22634e79722d5833 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sun, 17 Jan 2016 00:37:58 +0900
Subject: [PATCH 15/19] add experimental support for neovim async test cover.
 catch up latest vim-go behaivior

---
 autoload/go/coverlay.vim | 34 ++++++++++++++++++++++++++++++----
 ftplugin/go/coverlay.vim |  6 +++---
 t/test.vim               | 14 +++++++-------
 3 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/autoload/go/coverlay.vim b/autoload/go/coverlay.vim
index 0b3f89762c..17e5ab4121 100644
--- a/autoload/go/coverlay.vim
+++ b/autoload/go/coverlay.vim
@@ -114,20 +114,46 @@ function! go#coverlay#overlay(file)
     call go#coverlay#draw()
 endfunction
 
-function! go#coverlay#Coverlay(...)
+let s:coverlay_handler_id = ''
+let s:coverlay_handler_jobs = {}
+
+function! s:coverlay_handler(job, exit_status, data)
+    if !has_key(s:coverlay_handler_jobs, a:job.id)
+        return
+    endif
+    let l:tmpname = s:coverlay_handler_jobs[a:job.id]
+    if a:exit_status == 0
+        call go#coverlay#overlay(l:tmpname)
+    endif
+
+    call delete(l:tmpname)
+    unlet s:coverlay_handler_jobs[a:job.id]
+endfunction
+
+function! go#coverlay#Coverlay(bang, ...)
     call go#coverlay#Clearlay()
     let l:tmpname=tempname()
+    let args = [a:bang, 0, "-coverprofile", l:tmpname]
 
+    if a:0
+        call extend(args, a:000)
+    endif
     "TODO: add -coverpkg options based on current buf list
-    let out = go#cmd#Test(1, 0, "-coverprofile=".l:tmpname)
-
+    let id = call('go#cmd#Test', args)
+    if has('nvim')
+        if s:coverlay_handler_id == ''
+            let s:coverlay_handler_id = go#jobcontrol#AddHandler(function('s:coverlay_handler'))
+        endif
+        let s:coverlay_handler_jobs[id] = l:tmpname
+        return
+    endif
     if !v:shell_error
         call go#coverlay#overlay(l:tmpname)
     endif
     call delete(l:tmpname)
 endfunction
 
-function! go#coverlay#Clearlay(...)
+function! go#coverlay#Clearlay()
     call go#coverlay#hook()
     call go#coverlay#clear()
     let b:go_coverlay_matches = []
diff --git a/ftplugin/go/coverlay.vim b/ftplugin/go/coverlay.vim
index cda0a16157..40613dc084 100644
--- a/ftplugin/go/coverlay.vim
+++ b/ftplugin/go/coverlay.vim
@@ -4,11 +4,11 @@ endif
 let b:did_ftplugin_go_coverlay = 1
 
 " Some handy plug mappings
-nnoremap <silent> <Plug>(go-coverlay) :<C-u>call go#coverlay#Coverlay('')<CR>
-nnoremap <silent> <Plug>(go-clearlay) :<C-u>call go#coverlay#Clearlay('')<CR>
+nnoremap <silent> <Plug>(go-coverlay) :<C-u>call go#coverlay#Coverlay(!g:go_jump_to_error)<CR>
+nnoremap <silent> <Plug>(go-clearlay) :<C-u>call go#coverlay#Clearlay()<CR>
 
 " coverlay
-command! -nargs=* GoCoverlay call go#coverlay#Coverlay(<f-args>)
+command! -nargs=* -bang GoCoverlay go#coverlay#Coverlay(<bang>0, <f-args>)
 command! -nargs=* GoClearlay call go#coverlay#Clearlay(<f-args>)
 
 " vim:ts=4:sw=4:et
diff --git a/t/test.vim b/t/test.vim
index 088596bf51..86ba6c790a 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -19,19 +19,19 @@ describe 'go#coverlay#Coverlay'
     end
 
     it 'puts match to the list'
-        call go#coverlay#Coverlay()
+        call go#coverlay#Coverlay(0)
         Expect len(go#coverlay#matches()) == 5
         call go#coverlay#Clearlay()
         Expect len(go#coverlay#matches()) == 0
 
-        call go#coverlay#Coverlay()
+        call go#coverlay#Coverlay(0)
         Expect len(go#coverlay#matches()) == 5
         call go#coverlay#Clearlay()
         Expect len(go#coverlay#matches()) == 0
 
-        call go#coverlay#Coverlay()
+        call go#coverlay#Coverlay(0)
         Expect len(go#coverlay#matches()) == 5
-        call go#coverlay#Coverlay()
+        call go#coverlay#Coverlay(0)
         Expect len(go#coverlay#matches()) == 5
         call go#coverlay#Clearlay()
         Expect len(go#coverlay#matches()) == 0
@@ -44,6 +44,7 @@ describe 'go#coverlay#Coverlay fail'
 	let g:curdir = expand('<sfile>:p:h') . '/'
 	let g:srcpath = 't/fixtures/src/'
 	let g:sample = 'failtest/sample.go'
+	let g:sampletest = 'failtest/sample_test.go'
 	let g:sampleabs = g:curdir . g:srcpath . 'failtest/sample.go'
 	let g:go_gopath = g:curdir . 't/fixtures'
 	execute "badd " . g:srcpath . g:sample
@@ -51,13 +52,12 @@ describe 'go#coverlay#Coverlay fail'
     end
     after
 	execute "bprev"
+	execute "bdelete " . g:srcpath . g:sampletest
 	execute "bdelete " . g:srcpath . g:sample
-	call setqflist([])
-	cclose
     end
 
     it 'does nothing if test fail'
-	call go#coverlay#Coverlay()
+	call go#coverlay#Coverlay(0)
 	Expect len(go#coverlay#matches()) == 0
 	Expect len(getqflist()) == 1
     end

From 0ba4cf2af7b250a452d08f3791113a8ddef6e05e Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Fri, 1 Apr 2016 00:07:02 +0900
Subject: [PATCH 16/19] add tests

---
 t/fixtures/src/buildfail/sample.go          | 13 ++++++
 t/fixtures/src/buildfail/sample_test.go     |  7 ++++
 t/fixtures/src/buildtestfail/sample.go      |  7 ++++
 t/fixtures/src/buildtestfail/sample_test.go | 15 +++++++
 t/fixtures/src/parsefail/sample.go          | 14 +++++++
 t/fixtures/src/parsefail/sample_test.go     |  7 ++++
 t/test.vim                                  | 46 +++++++++++++++++++++
 7 files changed, 109 insertions(+)
 create mode 100644 t/fixtures/src/buildfail/sample.go
 create mode 100644 t/fixtures/src/buildfail/sample_test.go
 create mode 100644 t/fixtures/src/buildtestfail/sample.go
 create mode 100644 t/fixtures/src/buildtestfail/sample_test.go
 create mode 100644 t/fixtures/src/parsefail/sample.go
 create mode 100644 t/fixtures/src/parsefail/sample_test.go

diff --git a/t/fixtures/src/buildfail/sample.go b/t/fixtures/src/buildfail/sample.go
new file mode 100644
index 0000000000..45fc006afd
--- /dev/null
+++ b/t/fixtures/src/buildfail/sample.go
@@ -0,0 +1,13 @@
+// set gopath before
+//go:generate go test --coverprofile=sample.out
+// go1.5.3 example output:
+//   GOPATH=`pwd`/fixtures go test --coverprofile=log.out buildfail
+//   # buildfail
+//   /tmp/go-build264733986/buildfail/_test/_obj_test/sample.go:7: undefined: IT_SHOULD_BE_BUILD_FAILED
+//   /tmp/go-build264733986/buildfail/_test/_obj_test/sample.go:8: missing return at end of function
+//   FAIL    buildfail [build failed]
+package pkg
+
+func Sample() int {
+	IT_SHOULD_BE_BUILD_FAILED
+}
diff --git a/t/fixtures/src/buildfail/sample_test.go b/t/fixtures/src/buildfail/sample_test.go
new file mode 100644
index 0000000000..2356d3ed09
--- /dev/null
+++ b/t/fixtures/src/buildfail/sample_test.go
@@ -0,0 +1,7 @@
+package pkg
+
+import "testing"
+
+func TestSample(t *testing.T) {
+	Sample()
+}
diff --git a/t/fixtures/src/buildtestfail/sample.go b/t/fixtures/src/buildtestfail/sample.go
new file mode 100644
index 0000000000..792f12349b
--- /dev/null
+++ b/t/fixtures/src/buildtestfail/sample.go
@@ -0,0 +1,7 @@
+// set gopath before
+//go:generate go test --coverprofile=sample.out
+package pkg
+
+func Sample() int {
+	return 1
+}
diff --git a/t/fixtures/src/buildtestfail/sample_test.go b/t/fixtures/src/buildtestfail/sample_test.go
new file mode 100644
index 0000000000..17c4d4636e
--- /dev/null
+++ b/t/fixtures/src/buildtestfail/sample_test.go
@@ -0,0 +1,15 @@
+// go1.5.3 example output:
+//   GOPATH=`pwd`/fixtures go test --coverprofile=log.out buildtestfail
+//   # buildtestfail
+//   fixtures/src/buildtestfail/sample_test.go:14: undefined: IT_SHOULD_BE_BUILD_FAILED
+//   FAIL    buildtestfail [build failed]
+//   echo $?
+//   2
+
+package pkg
+
+import "testing"
+
+func TestSample(t *testing.T) {
+	IT_SHOULD_BE_BUILD_FAILED
+}
diff --git a/t/fixtures/src/parsefail/sample.go b/t/fixtures/src/parsefail/sample.go
new file mode 100644
index 0000000000..e9f3faa953
--- /dev/null
+++ b/t/fixtures/src/parsefail/sample.go
@@ -0,0 +1,14 @@
+// set gopath before
+//go:generate go test --coverprofile=sample.out
+// go1.5.3 example output:
+//   GOPATH=`pwd`/fixtures go test --coverprofile=log.out parsefail
+//   # cover parsefail
+//   2016/01/17 23:59:08 cover: /home/sey/vimfiles/_vim/bundle/vim-go-coverlay/t/fixtures/src/parsefail/sample.go: /home/sey/vimfiles/_vim/bundle/vim-go-coverlay/t/fixtures/src/parsefail/sample.go:10:1: expected declaration, found 'IDENT' PARSEFAIL
+//   FAIL    parsefail [build failed]
+//   echo $?
+//   2
+package pkg
+
+PARSEFAIL Sample() int {
+	return 0
+}
diff --git a/t/fixtures/src/parsefail/sample_test.go b/t/fixtures/src/parsefail/sample_test.go
new file mode 100644
index 0000000000..2356d3ed09
--- /dev/null
+++ b/t/fixtures/src/parsefail/sample_test.go
@@ -0,0 +1,7 @@
+package pkg
+
+import "testing"
+
+func TestSample(t *testing.T) {
+	Sample()
+}
diff --git a/t/test.vim b/t/test.vim
index 86ba6c790a..8bd13e1d20 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -63,6 +63,52 @@ describe 'go#coverlay#Coverlay fail'
     end
 end
 
+describe 'go#coverlay#Coverlay build fail'
+    before
+        new
+	let g:curdir = expand('<sfile>:p:h') . '/'
+	let g:srcpath = 't/fixtures/src/'
+	let g:sample = 'buildfail/sample.go'
+	let g:sampleabs = g:curdir . g:srcpath . 'buildfail/sample.go'
+	let g:go_gopath = g:curdir . 't/fixtures'
+	execute "badd " . g:srcpath . g:sample
+	execute "buffer " . bufnr("$")
+    end
+    after
+	execute "bprev"
+	execute "bdelete " . g:srcpath . g:sample
+    end
+
+    it 'does nothing if test fail'
+	call go#coverlay#Coverlay(0)
+	Expect len(go#coverlay#matches()) == 0
+	Expect len(getqflist()) == 1
+    end
+end
+
+describe 'go#coverlay#Coverlay build test fail'
+    before
+        new
+	let g:curdir = expand('<sfile>:p:h') . '/'
+	let g:srcpath = 't/fixtures/src/'
+	let g:sample = 'buildtestfail/sample.go'
+	let g:sampleabs = g:curdir . g:srcpath . 'buildtestfail/sample.go'
+	let g:go_gopath = g:curdir . 't/fixtures'
+	execute "badd " . g:srcpath . g:sample
+	execute "buffer " . bufnr("$")
+    end
+    after
+	execute "bprev"
+	execute "bdelete " . g:srcpath . g:sample
+    end
+
+    it 'does nothing if test fail'
+	call go#coverlay#Coverlay(0)
+	Expect len(go#coverlay#matches()) == 0
+	Expect len(getqflist()) == 1
+    end
+end
+
 describe 'go#coverlay#findbufnr'
     before
         new

From bd794960eb4419fdcd5f6508c05a41cb9051351b Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Fri, 1 Apr 2016 00:08:04 +0900
Subject: [PATCH 17/19] fix command mappings. thanks @svanharmelen

---
 ftplugin/go/coverlay.vim | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ftplugin/go/coverlay.vim b/ftplugin/go/coverlay.vim
index 40613dc084..d6507802b5 100644
--- a/ftplugin/go/coverlay.vim
+++ b/ftplugin/go/coverlay.vim
@@ -8,7 +8,7 @@ nnoremap <silent> <Plug>(go-coverlay) :<C-u>call go#coverlay#Coverlay(!g:go_jump
 nnoremap <silent> <Plug>(go-clearlay) :<C-u>call go#coverlay#Clearlay()<CR>
 
 " coverlay
-command! -nargs=* -bang GoCoverlay go#coverlay#Coverlay(<bang>0, <f-args>)
+command! -nargs=* -bang GoCoverlay call go#coverlay#Coverlay(<bang>0, <f-args>)
 command! -nargs=* GoClearlay call go#coverlay#Clearlay(<f-args>)
 
 " vim:ts=4:sw=4:et

From 5518f61637e860efe1ae268900d7078dac3c3ab2 Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sun, 3 Apr 2016 21:50:34 +0900
Subject: [PATCH 18/19] fix tests

---
 VimFlavor  | 2 +-
 t/test.vim | 2 --
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/VimFlavor b/VimFlavor
index a70269f428..a5cacbd9da 100644
--- a/VimFlavor
+++ b/VimFlavor
@@ -1 +1 @@
-flavor 'fatih/vim-go', '~> 1.3'
+flavor 'fatih/vim-go', '~> 1.5'
diff --git a/t/test.vim b/t/test.vim
index 8bd13e1d20..a8732a0dca 100644
--- a/t/test.vim
+++ b/t/test.vim
@@ -82,7 +82,6 @@ describe 'go#coverlay#Coverlay build fail'
     it 'does nothing if test fail'
 	call go#coverlay#Coverlay(0)
 	Expect len(go#coverlay#matches()) == 0
-	Expect len(getqflist()) == 1
     end
 end
 
@@ -105,7 +104,6 @@ describe 'go#coverlay#Coverlay build test fail'
     it 'does nothing if test fail'
 	call go#coverlay#Coverlay(0)
 	Expect len(go#coverlay#matches()) == 0
-	Expect len(getqflist()) == 1
     end
 end
 

From 2934945177d955c397222b7e40470c21f5e14a1e Mon Sep 17 00:00:00 2001
From: Yukinari Toyota <xxseyxx@gmail.com>
Date: Sun, 3 Apr 2016 21:55:46 +0900
Subject: [PATCH 19/19] rename t/test.vim to t/coverlay.vim for merge

---
 t/{test.vim => coverlay.vim} | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename t/{test.vim => coverlay.vim} (100%)

diff --git a/t/test.vim b/t/coverlay.vim
similarity index 100%
rename from t/test.vim
rename to t/coverlay.vim