From 188b579b224568fe4e0765341609ffd41b8dd7ed Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 16 Aug 2020 17:20:17 -0400 Subject: [PATCH] Show detail and doc --- cmd/micro/micro.go | 1 + internal/action/actions.go | 22 ++++++++++++++++--- internal/buffer/autocomplete.go | 23 ++++++++++++++++++- internal/display/bufwindow.go | 39 +++++++++++++++++++++++---------- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 7afc8a6d..6aecf586 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -413,6 +413,7 @@ func DoEvent() { action.MainTab().Display() action.InfoBar.Display() screen.Screen.Show() + action.InfoBar.Message("") // Check for new events select { diff --git a/internal/action/actions.go b/internal/action/actions.go index 5e8f235e..bf6c3064 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -668,7 +668,7 @@ func (h *BufPane) Autocomplete() bool { // if there is an existing completion, always cycle it if b.HasSuggestions { - b.CycleAutocomplete(true) + h.cycleAutocomplete(true) return true } @@ -682,8 +682,12 @@ func (h *BufPane) Autocomplete() bool { // don't autocomplete if cursor is on alpha numeric character (middle of a word) return false } + ret := true if !b.Autocomplete(buffer.LSPComplete) { - return b.Autocomplete(buffer.BufferComplete) + ret = b.Autocomplete(buffer.BufferComplete) + } + if ret { + h.displayCompletionDoc() } return true } @@ -695,12 +699,24 @@ func (h *BufPane) CycleAutocompleteBack() bool { } if h.Buf.HasSuggestions { - h.Buf.CycleAutocomplete(false) + h.cycleAutocomplete(false) return true } return false } +func (h *BufPane) cycleAutocomplete(forward bool) { + h.Buf.CycleAutocomplete(forward) + h.displayCompletionDoc() +} + +func (h *BufPane) displayCompletionDoc() { + c := h.Buf.CurCompletion + if c >= 0 && c < len(h.Buf.Completions) { + InfoBar.Message(h.Buf.Completions[c].Doc) + } +} + // InsertTab inserts a tab or spaces func (h *BufPane) InsertTab() bool { b := h.Buf diff --git a/internal/buffer/autocomplete.go b/internal/buffer/autocomplete.go index c8dcb8a7..679ebd29 100644 --- a/internal/buffer/autocomplete.go +++ b/internal/buffer/autocomplete.go @@ -9,6 +9,7 @@ import ( "github.com/zyedidia/micro/v2/internal/lsp" "github.com/zyedidia/micro/v2/internal/util" + "go.lsp.dev/protocol" ) // A Completer is a function that takes a buffer and returns info @@ -24,7 +25,7 @@ type Completion struct { Edits []Delta Label string CommitChars []rune - Kind int + Kind string Filter string Detail string Doc string @@ -235,6 +236,8 @@ func LSPComplete(b *Buffer) []Completion { completions[i] = Completion{ Label: item.Label, Detail: item.Detail, + Kind: toKindStr(item.Kind), + Doc: getDoc(item.Documentation), } if item.TextEdit != nil && len(item.TextEdit.NewText) > 0 { @@ -288,3 +291,21 @@ func ConvertCompletions(completions, suggestions []string, c *Cursor) []Completi } return comp } + +func toKindStr(k protocol.CompletionItemKind) string { + s := k.String() + return strings.ToLower(string(s[0])) +} + +// returns documentation from a string | MarkupContent item +func getDoc(documentation interface{}) string { + var doc string + switch s := documentation.(type) { + case string: + doc = s + case protocol.MarkupContent: + doc = s.Value + } + + return strings.Split(doc, "\n")[0] +} diff --git a/internal/display/bufwindow.go b/internal/display/bufwindow.go index d398b17c..e7d18b0c 100644 --- a/internal/display/bufwindow.go +++ b/internal/display/bufwindow.go @@ -755,32 +755,49 @@ func (w *BufWindow) displayCompleteBox() { return } - width := 0 + labelw := 0 + detailw := 0 + kindw := 0 for _, comp := range w.Buf.Completions { charcount := util.CharacterCountInString(comp.Label) - if charcount > width { - width = charcount + if charcount > labelw { + labelw = charcount + } + charcount = util.CharacterCountInString(comp.Detail) + if charcount > detailw { + detailw = charcount + } + charcount = util.CharacterCountInString(comp.Kind) + if charcount > kindw { + kindw = charcount } } - width++ + labelw++ + kindw++ - for i, comp := range w.Buf.Completions { - label := comp.Label + display := func(s string, width, x, y int, cur bool) { 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:] + if len(s) > 0 { + r, combc, size = util.DecodeCharacterInString(s) + s = s[size:] } st := config.DefStyle.Reverse(true) - if i == w.Buf.CurCompletion { + if cur { st = st.Reverse(false) } - screen.SetContent(w.completeBox.X+j, w.completeBox.Y+i+1, r, combc, st) + screen.SetContent(w.completeBox.X+x+j, w.completeBox.Y+y, r, combc, st) } } + + for i, comp := range w.Buf.Completions { + cur := i == w.Buf.CurCompletion + display(comp.Label+" ", labelw, 0, i+1, cur) + display(comp.Kind+" ", kindw, labelw, i+1, cur) + display(comp.Detail, detailw, labelw+kindw, i+1, cur) + } } // Display displays the buffer and the statusline