Skip to content

Commit

Permalink
govim: make schedule more useful and fix TestDriver (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
myitcv committed Apr 16, 2019
1 parent dda9e76 commit f0f9bbd
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ env:
global:
- GO111MODULE=on
matrix:
- GO_VERSION=go1.12.3 VIM_VERSION=v8.1.1137 VIM_COMMAND="vim"
- GO_VERSION=go1.12.3 VIM_VERSION=v8.1.1137 VIM_COMMAND="xvfb-run -a gvim -f"
- GO_VERSION=go1.12.3 VIM_VERSION=v8.1.1158 VIM_COMMAND="vim"
- GO_VERSION=go1.12.3 VIM_VERSION=v8.1.1158 VIM_COMMAND="xvfb-run -a gvim -f"
- GO_VERSION=go1.12.3 VIM_VERSION=v8.1.1176 VIM_COMMAND="vim"
- GO_VERSION=go1.12.3 VIM_VERSION=v8.1.1176 VIM_COMMAND="xvfb-run -a gvim -f"

Expand Down
3 changes: 2 additions & 1 deletion cmd/govim/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestScripts(t *testing.T) {
var waitLock sync.Mutex
var waitList []func() error

td, err := ioutil.TempDir("", "gobin-gopls-installdir*")
td, err := ioutil.TempDir("", "gobin-gopls-installdir")
if err != nil {
t.Fatalf("failed to create temp install directory for gopls: %v", err)
}
Expand Down Expand Up @@ -64,6 +64,7 @@ func TestScripts(t *testing.T) {
if err != nil {
t.Fatalf("failed to create new driver: %v", err)
}
td.Log = os.Stderr
if *fLogGovim {
tf, err := ioutil.TempFile("", "govim_test_script_govim_log*")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion event_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (e eventQueueInst) Scheduled() Govim {
return e
}

func (e eventQueueInst) Schedule(f func(Govim) error) {
func (e eventQueueInst) Schedule(f func(Govim) error) chan struct{} {
panic(fmt.Errorf("attempt to schedule work on the event queue from the event queue itself"))
}

Expand Down
13 changes: 9 additions & 4 deletions govim.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type Govim interface {
Scheduled() Govim

// Schedule schdules f to run in the event queue
Schedule(func(Govim) error)
Schedule(func(Govim) error) chan struct{}
}

type govimImpl struct {
Expand All @@ -132,7 +132,6 @@ type govimImpl struct {
loaded chan bool

currViewport Viewport
viewportLock sync.Mutex
onViewportChangeSubs []*OnViewportChangeSub
onViewportChangeSubsLock sync.Mutex

Expand Down Expand Up @@ -182,10 +181,16 @@ func (g *govimImpl) Scheduled() Govim {
}
}

func (g *govimImpl) Schedule(f func(Govim) error) {
func (g *govimImpl) Schedule(f func(Govim) error) chan struct{} {
done := make(chan struct{})
g.eventQueue.Add(func() error {
return f(g)
defer func() {
close(done)
g.flushEvents <- struct{}{}
}()
return f(g.Scheduled())
})
return done
}

func (g *govimImpl) load() error {
Expand Down
1 change: 1 addition & 0 deletions plugin/govim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function s:define(channel, msg)
let s:timer = timer_start(100, function('s:updateViewport'), {'repeat': -1})
au CursorMoved,CursorMovedI,BufWinEnter * call s:updateViewport(0)
let s:govim_status = "loaded"
call s:updateViewport(0)
for F in s:loadStatusCallbacks
call call(F, [s:govim_status])
endfor
Expand Down
29 changes: 22 additions & 7 deletions testdriver/testdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,36 @@ func (d *TestDriver) listenDriver() error {
if len(args) == 2 {
force = args[1].(string)
}
add(d.govim.ChannelRedraw(force == "force"))
<-d.govim.Schedule(func(g govim.Govim) error {
add(g.ChannelRedraw(force == "force"))
return nil
})
case "ex":
expr := args[1].(string)
add(d.govim.ChannelEx(expr))
<-d.govim.Schedule(func(g govim.Govim) error {
add(g.ChannelEx(expr))
return nil
})
case "normal":
expr := args[1].(string)
add(d.govim.ChannelNormal(expr))
<-d.govim.Schedule(func(g govim.Govim) error {
add(g.ChannelNormal(expr))
return nil
})
case "expr":
expr := args[1].(string)
resp, err := d.govim.ChannelExpr(expr)
add(err, resp)
<-d.govim.Schedule(func(g govim.Govim) error {
resp, err := g.ChannelExpr(expr)
add(err, resp)
return nil
})
case "call":
fn := args[1].(string)
resp, err := d.govim.ChannelCall(fn, args[2:]...)
add(err, resp)
<-d.govim.Schedule(func(g govim.Govim) error {
resp, err := g.ChannelCall(fn, args[2:]...)
add(err, resp)
return nil
})
default:
panic(fmt.Errorf("don't yet know how to handle %v", cmd))
}
Expand Down
8 changes: 1 addition & 7 deletions viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ type OnViewportChangeSub struct {
func (g *govimImpl) onViewportChange(args ...json.RawMessage) (interface{}, error) {
var r Viewport
g.decodeJSON(args[0], &r)
g.viewportLock.Lock()
g.currViewport = r
r = r.dup()
g.viewportLock.Unlock()

g.Logf("Viewport changed: %v", pretty.Sprint(r))

Expand Down Expand Up @@ -96,11 +94,7 @@ type WinInfo struct {

// Viewport returns the active Vim viewport
func (g *govimImpl) Viewport() Viewport {
var res Viewport
g.viewportLock.Lock()
res = g.currViewport.dup()
g.viewportLock.Unlock()
return res
return g.currViewport.dup()
}

func (v Viewport) dup() Viewport {
Expand Down

0 comments on commit f0f9bbd

Please sign in to comment.