newedit/core: Highlighter.LateUpdates also deliver styled.Text.

Although the result is not used, it makes Highlighter.LateUpdates and
Prompt.LateUpdates identical.
This commit is contained in:
Qi Xiao 2018-12-08 14:08:05 +00:00
parent 28cbb3112e
commit eb7d704110
3 changed files with 19 additions and 31 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/elves/elvish/edit/tty"
"github.com/elves/elvish/newedit/types"
"github.com/elves/elvish/styled"
"github.com/elves/elvish/sys"
)
@ -198,20 +199,16 @@ func (ed *Editor) ReadCode() (string, error) {
}()
}
// Relay late updates from prompt, rprompt and highlighter.
stopRelayLateUpdates := make(chan struct{})
defer close(stopRelayLateUpdates)
// Relay late prompt/rprompt updates.
relayLateUpdates := func(p Prompt) {
if p == nil {
return
}
relayLateUpdates := func(ch <-chan styled.Text) {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-p.LateUpdates():
case <-ch:
ed.loop.Input(lateUpdate{})
case <-stopRelayLateUpdates:
return
@ -219,23 +216,14 @@ func (ed *Editor) ReadCode() (string, error) {
}
}()
}
relayLateUpdates(ed.Prompt)
relayLateUpdates(ed.RPrompt)
// Relay highlighter late updates.
if ed.Prompt != nil {
relayLateUpdates(ed.Prompt.LateUpdates())
}
if ed.RPrompt != nil {
relayLateUpdates(ed.RPrompt.LateUpdates())
}
if ed.Highlighter != nil {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ed.Highlighter.LateUpdates():
ed.loop.Input(lateUpdate{})
case <-stopRelayLateUpdates:
return
}
}
}()
relayLateUpdates(ed.Highlighter.LateUpdates())
}
// Trigger an initial prompt update.

View File

@ -7,8 +7,8 @@ import "github.com/elves/elvish/styled"
type Highlighter interface {
// Get returns the highlighted code and any static errors.
Get(code string) (styled.Text, []error)
// LateUpdates returns a channel for notifying late updates.
LateUpdates() <-chan struct{}
// LateUpdates returns a channel for delivering late updates.
LateUpdates() <-chan styled.Text
}
func highlighterGet(hl Highlighter, code string) (styled.Text, []error) {
@ -21,13 +21,13 @@ func highlighterGet(hl Highlighter, code string) (styled.Text, []error) {
// A Highlighter implementation useful for testing.
type fakeHighlighter struct {
get func(code string) (styled.Text, []error)
lateUpdates chan struct{}
lateUpdates chan styled.Text
}
func (hl fakeHighlighter) Get(code string) (styled.Text, []error) {
return hl.get(code)
}
func (hl fakeHighlighter) LateUpdates() <-chan struct{} {
func (hl fakeHighlighter) LateUpdates() <-chan styled.Text {
return hl.lateUpdates
}

View File

@ -12,7 +12,7 @@ const latesBufferSize = 128
type Highlighter struct {
dep Dep
state state
lates chan struct{}
lates chan styled.Text
}
type state struct {
@ -23,7 +23,7 @@ type state struct {
}
func NewHighlighter(dep Dep) *Highlighter {
return &Highlighter{dep, state{}, make(chan struct{}, latesBufferSize)}
return &Highlighter{dep, state{}, make(chan styled.Text, latesBufferSize)}
}
// Get returns the highlighted code and static errors found in the code.
@ -39,12 +39,12 @@ func (hl *Highlighter) Get(code string) (styled.Text, []error) {
hl.state.Lock()
hl.state.styledCode = styledCode
hl.state.Unlock()
hl.lates <- struct{}{}
hl.lates <- styledCode
}
return highlight(code, hl.dep, lateCb)
}
// LateUpdates returns a channel for notifying late updates.
func (hl *Highlighter) LateUpdates() <-chan struct{} {
func (hl *Highlighter) LateUpdates() <-chan styled.Text {
return hl.lates
}