From 724cedd37b4f889e072cc9a08b47a257d33d4be8 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 15 Aug 2020 20:41:54 -0400 Subject: [PATCH] Basic autocomplete box --- internal/display/bufwindow.go | 44 +++++++++++++++++++++++++++++++++- internal/display/statusline.go | 38 ----------------------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/internal/display/bufwindow.go b/internal/display/bufwindow.go index 9c61140d..d398b17c 100644 --- a/internal/display/bufwindow.go +++ b/internal/display/bufwindow.go @@ -17,7 +17,8 @@ type BufWindow struct { *View // Buffer being shown in this window - Buf *buffer.Buffer + 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() } diff --git a/internal/display/statusline.go b/internal/display/statusline.go index daec8125..b5b235bb 100644 --- a/internal/display/statusline.go +++ b/internal/display/statusline.go @@ -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")) {