Basic autocomplete box

This commit is contained in:
Zachary Yedidia 2020-08-15 20:41:54 -04:00
parent 132630a9a5
commit 724cedd37b
2 changed files with 43 additions and 39 deletions

View File

@ -18,6 +18,7 @@ type BufWindow struct {
// Buffer being shown in this window
Buf *buffer.Buffer
completeBox buffer.Loc
active bool
@ -583,6 +584,13 @@ func (w *BufWindow) displayBuffer() {
screen.SetContent(w.X+vloc.X, w.Y+vloc.Y, r, combc, style)
if w.Buf.HasSuggestions && len(w.Buf.Completions) > 0 {
compl := w.Buf.Completions[0].Edits[0].Start
if bloc.X == compl.X && bloc.Y == compl.Y {
w.completeBox = buffer.Loc{w.X + vloc.X, w.Y + vloc.Y}
}
}
if showcursor {
for _, c := range cursors {
if c.X == bloc.X && c.Y == bloc.Y && !c.HasSelection() {
@ -742,9 +750,43 @@ func (w *BufWindow) displayScrollBar() {
}
}
func (w *BufWindow) displayCompleteBox() {
if !w.Buf.HasSuggestions || w.Buf.NumCursors() > 1 {
return
}
width := 0
for _, comp := range w.Buf.Completions {
charcount := util.CharacterCountInString(comp.Label)
if charcount > width {
width = charcount
}
}
width++
for i, comp := range w.Buf.Completions {
label := comp.Label
for j := 0; j < width; j++ {
r := ' '
var combc []rune
var size int
if len(label) > 0 {
r, combc, size = util.DecodeCharacterInString(label)
label = label[size:]
}
st := config.DefStyle.Reverse(true)
if i == w.Buf.CurCompletion {
st = st.Reverse(false)
}
screen.SetContent(w.completeBox.X+j, w.completeBox.Y+i+1, r, combc, st)
}
}
}
// Display displays the buffer and the statusline
func (w *BufWindow) Display() {
w.displayStatusLine()
w.displayScrollBar()
w.displayBuffer()
w.displayCompleteBox()
}

View File

@ -98,44 +98,6 @@ func (s *StatusLine) Display() {
// We'll draw the line at the lowest line in the window
y := s.win.Height + s.win.Y - 1
b := s.win.Buf
// autocomplete suggestions (for the buffer, not for the infowindow)
if b.HasSuggestions && len(b.Completions) > 1 {
statusLineStyle := config.DefStyle.Reverse(true)
if style, ok := config.Colorscheme["statusline"]; ok {
statusLineStyle = style
}
keymenuOffset := 0
if config.GetGlobalOption("keymenu").(bool) {
keymenuOffset = len(keydisplay)
}
x := 0
for j, sug := range b.Completions {
style := statusLineStyle
if b.CurCompletion == j {
style = style.Reverse(true)
}
for _, r := range sug.Label {
screen.SetContent(x, y-keymenuOffset, r, nil, style)
x++
if x >= s.win.Width {
return
}
}
screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
x++
if x >= s.win.Width {
return
}
}
for x < s.win.Width {
screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
x++
}
return
}
formatter := func(match []byte) []byte {
name := match[2 : len(match)-1]
if bytes.HasPrefix(name, []byte("opt")) {