Skip to content

Commit

Permalink
Merge pull request #48 from dzhou121/feature/ext-cmdline
Browse files Browse the repository at this point in the history
Feature ext-cmdline
  • Loading branch information
dzhou121 authored Jul 3, 2017
2 parents da8464f + c3edbe3 commit cba647d
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 55 deletions.
235 changes: 235 additions & 0 deletions cmdline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package gonvim

import (
"fmt"
"strings"
)

// CmdContent is the content of the cmdline
type CmdContent struct {
indent int
firstc string
prompt string
content string
}

// Cmdline is the cmdline
type Cmdline struct {
pos int
content *CmdContent
preContent *CmdContent
function []*CmdContent
inFunction bool
rawItems []interface{}
wildmenuShown bool
top int
}

func initCmdline() *Cmdline {
return &Cmdline{
content: &CmdContent{},
rawItems: make([]interface{}, 0),
}
}

func (c *CmdContent) getText() string {
indentStr := ""
for i := 0; i < c.indent; i++ {
indentStr += " "
}
return fmt.Sprintf("%s%s", indentStr, c.content)
}

func (c *Cmdline) getText(ch string) string {
indentStr := ""
for i := 0; i < c.content.indent; i++ {
indentStr += " "
}
return fmt.Sprintf("%s%s%s", c.content.firstc, indentStr, c.content.content[:c.pos]+ch+c.content.content[c.pos:])
}

func (c *Cmdline) show(args []interface{}) {
arg := args[0].([]interface{})
content := arg[0].([]interface{})[0].([]interface{})[1].(string)
pos := reflectToInt(arg[1])
firstc := arg[2].(string)
prompt := arg[3].(string)
indent := reflectToInt(arg[4])
// level := reflectToInt(arg[5])
// fmt.Println("cmdline show", content, pos, firstc, prompt, indent, level)

c.pos = pos
c.content.firstc = firstc
c.content.content = content
c.content.indent = indent
c.content.prompt = prompt
text := c.getText("")
palette := editor.palette
palette.setPattern(text)
c.cursorMove()
if !c.wildmenuShown {
c.showAddition()
palette.scrollCol.Hide()
}
palette.refresh()
}

func (c *Cmdline) showAddition() {
lines := append(c.getPromptLines(), c.getFunctionLines()...)
palette := editor.palette
for i, resultItem := range palette.resultItems {
if i >= len(lines) || i >= palette.showTotal {
resultItem.hide()
continue
}
resultItem.setItem(lines[i], "", []int{})
resultItem.setSelected(false)
resultItem.show()
}
}

func (c *Cmdline) getPromptLines() []string {
result := []string{}
if c.content.prompt == "" {
return result
}

lines := strings.Split(c.content.prompt, "\n")
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
result = append(result, line)
}
return result
}

func (c *Cmdline) getFunctionLines() []string {
result := []string{}
if !c.inFunction {
return result
}

for _, content := range c.function {
result = append(result, content.getText())
}
return result
}

func (c *Cmdline) cursorMove() {
editor.palette.cursorMove(c.pos + len(c.content.firstc) + c.content.indent)
}

func (c *Cmdline) hide(args []interface{}) {
palette := editor.palette
palette.hide()
if c.inFunction {
c.function = append(c.function, c.content)
}
c.preContent = c.content
c.content = &CmdContent{}
}

func (c *Cmdline) functionShow() {
c.inFunction = true
c.function = []*CmdContent{c.preContent}
}

func (c *Cmdline) functionHide() {
c.inFunction = false
}

func (c *Cmdline) changePos(args []interface{}) {
args = args[0].([]interface{})
pos := reflectToInt(args[0])
// level := reflectToInt(args[1])
// fmt.Println("change pos", pos, level)
c.pos = pos
c.cursorMove()
}

func (c *Cmdline) putChar(args []interface{}) {
args = args[0].([]interface{})
ch := args[0].(string)
// shift := reflectToInt(args[1])
// level := reflectToInt(args[2])
// fmt.Println("putChar", ch, shift, level)
text := c.getText(ch)
palette := editor.palette
palette.setPattern(text)
}

func (c *Cmdline) wildmenuShow(args []interface{}) {
c.wildmenuShown = true
args = args[0].([]interface{})
c.rawItems = args[0].([]interface{})
palette := editor.palette
c.top = 0
for i := 0; i < palette.showTotal; i++ {
resultItem := palette.resultItems[i]
if i >= len(c.rawItems) {
resultItem.hide()
continue
}
text := c.rawItems[i].(string)
resultItem.setItem(text, "", []int{})
resultItem.show()
resultItem.setSelected(false)
}

total := len(c.rawItems)
if total > palette.showTotal {
height := int(float64(palette.showTotal) / float64(total) * float64(palette.itemHeight*palette.showTotal))
if height == 0 {
height = 1
}
palette.scrollBar.SetFixedHeight(height)
palette.scrollBarPos = 0
palette.scrollBar.Move2(0, palette.scrollBarPos)
palette.scrollCol.Show()
} else {
palette.scrollCol.Hide()
}
}

func (c *Cmdline) wildmenuSelect(args []interface{}) {
selected := reflectToInt(args[0].([]interface{})[0])
// fmt.Println("selected is", selected)
showTotal := editor.palette.showTotal
if selected == -1 && c.top > 0 {
c.wildmenuScroll(-c.top)
}
if selected-c.top >= showTotal {
c.wildmenuScroll(selected - c.top - showTotal + 1)
}
if selected >= 0 && selected-c.top < 0 {
c.wildmenuScroll(-1)
}
palette := editor.palette
for i := 0; i < palette.showTotal; i++ {
item := palette.resultItems[i]
item.setSelected(selected == i+c.top)
}
}

func (c *Cmdline) wildmenuScroll(n int) {
c.top += n
palette := editor.palette
for i := 0; i < palette.showTotal; i++ {
resultItem := palette.resultItems[i]
if i >= len(c.rawItems) {
resultItem.hide()
continue
}
text := c.rawItems[i+c.top].(string)
resultItem.setItem(text, "", []int{})
resultItem.show()
resultItem.setSelected(false)
}
palette.scrollBarPos = int((float64(c.top) / float64(len(c.rawItems))) * float64(palette.itemHeight*palette.showTotal))
palette.scrollBar.Move2(0, palette.scrollBarPos)
}

func (c *Cmdline) wildmenuHide() {
c.wildmenuShown = false
}
57 changes: 57 additions & 0 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Editor struct {
signature *Signature
popup *PopupMenu
finder *Finder
cmdline *Cmdline
palette *Palette
tabline *Tabline
statusline *Statusline
Expand Down Expand Up @@ -167,6 +168,24 @@ func (e *Editor) handleRedraw(updates [][]interface{}) {
editor.popup.selectItem(args)
case "tabline_update":
editor.tabline.update(args)
case "cmdline_show":
editor.cmdline.show(args)
case "cmdline_pos":
editor.cmdline.changePos(args)
case "cmdline_char":
editor.cmdline.putChar(args)
case "cmdline_hide":
editor.cmdline.hide(args)
case "cmdline_function_show":
editor.cmdline.functionShow()
case "cmdline_function_hide":
editor.cmdline.functionHide()
case "wildmenu_show":
editor.cmdline.wildmenuShow(args)
case "wildmenu_select":
editor.cmdline.wildmenuSelect(args)
case "wildmenu_hide":
editor.cmdline.wildmenuHide()
case "busy_start":
case "busy_stop":
default:
Expand Down Expand Up @@ -349,6 +368,7 @@ func InitEditorNew() {
close: make(chan bool),
popup: popup,
finder: finder,
cmdline: initCmdline(),
palette: palette,
loc: loc,
signature: signature,
Expand Down Expand Up @@ -386,10 +406,47 @@ func InitEditorNew() {

screen.updateSize()

apiInfo, err := editor.nvim.APIInfo()
if err != nil {
fmt.Println("nvim get API info error", err)
app.Quit()
return
}

o := make(map[string]interface{})
o["rgb"] = true
o["ext_popupmenu"] = true
o["ext_tabline"] = true
for _, item := range apiInfo {
i, ok := item.(map[string]interface{})
if !ok {
continue
}
for k, v := range i {
if k != "ui_events" {
continue
}
events, ok := v.([]interface{})
if !ok {
continue
}
for _, event := range events {
function, ok := event.(map[string]interface{})
if !ok {
continue
}
name, ok := function["name"]
if !ok {
continue
}
if name == "wildmenu_show" {
o["ext_wildmenu"] = true
} else if name == "cmdline_show" {
o["ext_cmdline"] = true
}
}
}
}
err = editor.nvim.AttachUI(editor.cols, editor.rows, o)
if err != nil {
fmt.Println("nvim attach UI error", err)
Expand Down
7 changes: 1 addition & 6 deletions finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ func (f *Finder) showResult(args []interface{}) {
// } else {
// f.scrollCol.Hide()
// }
palette.resultWidget.Hide()
palette.resultWidget.Show()

if total > palette.showTotal {
height := int(float64(palette.showTotal) / float64(total) * float64(palette.itemHeight*palette.showTotal))
Expand All @@ -152,10 +150,7 @@ func (f *Finder) showResult(args []interface{}) {
palette.scrollCol.Hide()
}

palette.hide()
palette.show()
palette.hide()
palette.show()
palette.refresh()
}

func formatText(text string, matchIndex []int, path bool) string {
Expand Down
2 changes: 1 addition & 1 deletion font.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func initFontNew(family string, size int, lineSpace int) *Font {

func (f *Font) change(family string, size int) {
f.fontNew.SetFamily(family)
f.fontNew.SetPixelSize(size)
f.fontNew.SetPointSize(size)
f.fontMetrics = gui.NewQFontMetricsF(f.fontNew)
width, height, truewidth, ascent := fontSizeNew(f.fontNew)
f.width = width
Expand Down
9 changes: 4 additions & 5 deletions locpopup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ type Locpopup struct {

func initLocpopup() *Locpopup {
widget := widgets.NewQWidget(nil, 0)
widget.SetContentsMargins(4, 4, 4, 4)
widget.SetContentsMargins(8, 8, 8, 8)
layout := widgets.NewQHBoxLayout()
layout.SetContentsMargins(0, 0, 0, 0)
layout.SetSpacing(4)
widget.SetLayout(layout)
widget.SetStyleSheet("color: rgba(14, 17, 18, 1); background-color: rgba(212, 215, 214, 1);")

widget.SetStyleSheet(".QWidget { border: 1px solid #000; } * {color: rgba(205, 211, 222, 1); background-color: rgba(24, 29, 34, 1);}")
typeLabel := widgets.NewQLabel(nil, 0)
typeLabel.SetContentsMargins(4, 1, 4, 1)

Expand Down Expand Up @@ -68,10 +67,10 @@ func (l *Locpopup) updateLocpopup() {
l.contentLabel.SetText(l.contentText)
if l.typeText == "E" {
l.typeLabel.SetText("Error")
l.typeLabel.SetStyleSheet("background-color: rgba(204, 62, 68, 1); color: rgba(212, 215, 214, 1);")
l.typeLabel.SetStyleSheet("background-color: rgba(204, 62, 68, 1);")
} else if l.typeText == "W" {
l.typeLabel.SetText("Warning")
l.typeLabel.SetStyleSheet("background-color: rgba(203, 203, 65, 1); color: rgba(212, 215, 214, 1);")
l.typeLabel.SetStyleSheet("background-color: rgba(203, 203, 65, 1);")
}
l.widget.Hide()
l.widget.Show()
Expand Down
Loading

0 comments on commit cba647d

Please sign in to comment.