From 8ad459755d73ac683fb2e21e51a979a3aa26105f Mon Sep 17 00:00:00 2001 From: Felix Angell Date: Sat, 12 May 2018 20:43:26 +0100 Subject: [PATCH] fixed some focus issues; change focus behaviour is now mostly sound! in addition, the command palette shows all of the open buffers and you can switch to a buffer using the command palette. --- gui/buffer_pane.go | 5 +++ gui/palette.go | 86 +++++++++++++++++++++++++++++++++++++++++----- gui/view.go | 44 ++++++++++-------------- 3 files changed, 101 insertions(+), 34 deletions(-) diff --git a/gui/buffer_pane.go b/gui/buffer_pane.go index 14146d1..bf1940c 100644 --- a/gui/buffer_pane.go +++ b/gui/buffer_pane.go @@ -34,6 +34,11 @@ func NewBufferPane(buff *Buffer) *BufferPane { var lastWidth int +func (b *BufferPane) SetFocus(focus bool) { + b.Buff.SetFocus(focus) + b.BaseComponent.SetFocus(focus) +} + func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) { conf := b.Buff.cfg.Theme.Palette diff --git a/gui/palette.go b/gui/palette.go index efda8ac..a3ceb74 100644 --- a/gui/palette.go +++ b/gui/palette.go @@ -26,10 +26,17 @@ type CommandPalette struct { conf *cfg.TomlConfig parent *View + pathToIndex map[string]int + suggestionIndex int recentSuggestions *[]suggestion } +func (p *CommandPalette) SetFocus(focus bool) { + p.buff.SetFocus(focus) + p.BaseComponent.SetFocus(focus) +} + var suggestionBoxHeight, suggestionBoxWidth = 48, 0 type suggestion struct { @@ -123,22 +130,42 @@ func NewCommandPalette(conf cfg.TomlConfig, view *View) *CommandPalette { } func (b *CommandPalette) processCommand() { - tokenizedLine := strings.Split(b.buff.contents[0].String(), " ") - command := tokenizedLine[0] + input := b.buff.contents[0].String() + input = strings.TrimSpace(input) + tokenizedLine := strings.Split(input, " ") - log.Println("command palette: ", tokenizedLine) + // command + if strings.Compare(tokenizedLine[0], "!") == 0 { + tokenizedLine := strings.Split(input, " ") + log.Println("COMMAND TING '", input, "', ", tokenizedLine) + command := tokenizedLine[0] - action, exists := actions[command] - if !exists { + log.Println("command palette: ", tokenizedLine) + + action, exists := actions[command] + if !exists { + return + } + + action.proc(b.parent, tokenizedLine[1:]) return } - action.proc(b.parent, tokenizedLine[1:]) + if index, ok := b.pathToIndex[input]; ok { + b.parent.setFocusTo(index) + } } -func (b *CommandPalette) calculateSuggestions() { - tokenizedLine := strings.Split(b.buff.contents[0].String(), " ") - command := tokenizedLine[0] +func (b *CommandPalette) calculateCommandSuggestions() { + input := b.buff.contents[0].String() + input = strings.TrimSpace(input) + + tokenizedLine := strings.Split(input, " ") + if strings.Compare(tokenizedLine[0], "!") == 0 { + return + } + + command := tokenizedLine[1] if command == "" { b.recentSuggestions = nil @@ -159,6 +186,47 @@ func (b *CommandPalette) calculateSuggestions() { b.recentSuggestions = &suggestions } +func (b *CommandPalette) calculateSuggestions() { + input := b.buff.contents[0].String() + input = strings.TrimSpace(input) + + if len(input) == 0 { + return + } + + if input[0] == '!' { + b.calculateCommandSuggestions() + return + } + + // fill it with the currently open files! + + openFiles := make([]string, len(b.parent.buffers)) + + b.pathToIndex = map[string]int{} + + for i, pane := range b.parent.buffers { + path := pane.Buff.filePath + openFiles[i] = path + b.pathToIndex[path] = i + } + + ranks := fuzzy.RankFind(input, openFiles) + suggestions := []suggestion{} + for _, r := range ranks { + pane := b.parent.buffers[r.Index] + if pane != nil { + sugg := suggestion{ + b, + pane.Buff.filePath, + } + suggestions = append(suggestions, sugg) + } + } + + b.recentSuggestions = &suggestions +} + func (b *CommandPalette) scrollSuggestion(dir int) { if b.recentSuggestions != nil { b.suggestionIndex += dir diff --git a/gui/view.go b/gui/view.go index c352c44..f10eaf2 100644 --- a/gui/view.go +++ b/gui/view.go @@ -40,13 +40,6 @@ func (n *View) hidePalette() { p.clearInput() p.SetFocus(false) - // set focus to the buffer - // that invoked the cmd palette - if p.parentBuff != nil { - p.parentBuff.SetFocus(true) - n.focusedBuff = p.parentBuff.index - } - // remove focus from palette p.buff.SetFocus(false) } @@ -55,9 +48,6 @@ func (n *View) focusPalette(buff *Buffer) { p := n.commandPalette p.SetFocus(true) - // focus the command palette - p.buff.SetFocus(true) - // remove focus from the buffer // that invoked the command palette p.parentBuff = buff @@ -66,7 +56,7 @@ func (n *View) focusPalette(buff *Buffer) { func (n *View) UnfocusBuffers() { // clear focus from buffers for _, buffPane := range n.buffers { - buffPane.Buff.SetFocus(false) + buffPane.SetFocus(false) } } @@ -108,6 +98,15 @@ func (n *View) removeBuffer(index int) { n.ChangeFocus(dir) } +func (n *View) setFocusTo(index int) { + log.Println("Moving focus from ", n.focusedBuff, " to ", index) + + n.UnfocusBuffers() + n.focusedBuff = index + buff := n.getCurrentBuffPane() + buff.SetFocus(true) +} + // FIXME func (n *View) ChangeFocus(dir int) { // we cant change focus if there are no @@ -116,27 +115,22 @@ func (n *View) ChangeFocus(dir int) { return } - prevBuff := n.buffers[n.focusedBuff] + newFocus := n.focusedBuff if dir == -1 { - n.focusedBuff-- + newFocus-- } else if dir == 1 { - n.focusedBuff++ + newFocus++ } - if n.focusedBuff < 0 { - n.focusedBuff = len(n.buffers) - 1 - } else if n.focusedBuff >= len(n.buffers) { - n.focusedBuff = 0 + if newFocus < 0 { + newFocus = len(n.buffers) - 1 + } else if newFocus >= len(n.buffers) { + newFocus = 0 } - if prevBuff != nil { - prevBuff.Buff.SetFocus(false) - } - - if buffPane := n.getCurrentBuffPane(); buffPane != nil { - buffPane.Buff.SetFocus(true) - } + n.UnfocusBuffers() + n.setFocusTo(newFocus) } func (n *View) getCurrentBuffPane() *BufferPane {