Add more infobar autocomplete

This commit is contained in:
Zachary Yedidia
2019-01-20 19:38:23 -05:00
parent fc7058d47c
commit bc1d6b6f94
3 changed files with 55 additions and 41 deletions

View File

@@ -29,9 +29,9 @@ var commands map[string]Command
func InitCommands() {
commands = map[string]Command{
"set": Command{(*BufPane).SetCmd, nil},
"setlocal": Command{(*BufPane).SetLocalCmd, nil},
"show": Command{(*BufPane).ShowCmd, nil},
"set": Command{(*BufPane).SetCmd, OptionValueComplete},
"setlocal": Command{(*BufPane).SetLocalCmd, OptionValueComplete},
"show": Command{(*BufPane).ShowCmd, OptionComplete},
"showkey": Command{(*BufPane).ShowKeyCmd, nil},
"run": Command{(*BufPane).RunCmd, nil},
"bind": Command{(*BufPane).BindCmd, nil},
@@ -43,7 +43,7 @@ func InitCommands() {
"vsplit": Command{(*BufPane).VSplitCmd, buffer.FileComplete},
"hsplit": Command{(*BufPane).HSplitCmd, buffer.FileComplete},
"tab": Command{(*BufPane).NewTabCmd, buffer.FileComplete},
"help": Command{(*BufPane).HelpCmd, nil},
"help": Command{(*BufPane).HelpCmd, HelpComplete},
"eval": Command{(*BufPane).EvalCmd, nil},
"togglelog": Command{(*BufPane).ToggleLogCmd, nil},
"plugin": Command{(*BufPane).PluginCmd, nil},

View File

@@ -3,29 +3,12 @@ package action
import (
"bytes"
"strings"
"unicode/utf8"
"github.com/zyedidia/micro/cmd/micro/buffer"
"github.com/zyedidia/micro/cmd/micro/config"
"github.com/zyedidia/micro/cmd/micro/util"
)
// Completion represents a type of completion
type Completion int
const (
NoCompletion Completion = iota
FileCompletion
CommandCompletion
HelpCompletion
OptionCompletion
PluginCmdCompletion
PluginNameCompletion
OptionValueCompletion
)
var pluginCompletions []func(string) []string
// This file is meant (for now) for autocompletion in command mode, not
// while coding. This helps micro autocomplete commands and then filenames
// for example with `vsplit filename`.
@@ -33,18 +16,7 @@ var pluginCompletions []func(string) []string
// CommandComplete autocompletes commands
func CommandComplete(b *buffer.Buffer) (string, []string) {
c := b.GetActiveCursor()
l := b.LineBytes(c.Y)
l = util.SliceStart(l, c.X)
args := bytes.Split(l, []byte{' '})
input := string(args[len(args)-1])
argstart := 0
for i, a := range args {
if i == len(args)-1 {
break
}
argstart += utf8.RuneCount(a) + 1
}
input, argstart := buffer.GetArg(b)
var suggestions []string
for cmd := range commands {
@@ -61,7 +33,10 @@ func CommandComplete(b *buffer.Buffer) (string, []string) {
}
// HelpComplete autocompletes help topics
func HelpComplete(input string) (string, []string) {
func HelpComplete(b *buffer.Buffer) (string, []string) {
c := b.GetActiveCursor()
input, argstart := buffer.GetArg(b)
var suggestions []string
for _, file := range config.ListRuntimeFiles(config.RTHelp) {
@@ -73,12 +48,13 @@ func HelpComplete(input string) (string, []string) {
var chosen string
if len(suggestions) == 1 {
chosen = suggestions[0]
chosen = util.SliceEndStr(suggestions[0], c.X-argstart)
}
return chosen, suggestions
}
// ColorschemeComplete tab-completes names of colorschemes.
// This is just a heper value for OptionValueComplete
func ColorschemeComplete(input string) (string, []string) {
var suggestions []string
files := config.ListRuntimeFiles(config.RTColorscheme)
@@ -107,7 +83,10 @@ func contains(s []string, e string) bool {
}
// OptionComplete autocompletes options
func OptionComplete(input string) (string, []string) {
func OptionComplete(b *buffer.Buffer) (string, []string) {
c := b.GetActiveCursor()
input, argstart := buffer.GetArg(b)
var suggestions []string
localSettings := config.DefaultLocalSettings()
for option := range config.GlobalSettings {
@@ -123,13 +102,41 @@ func OptionComplete(input string) (string, []string) {
var chosen string
if len(suggestions) == 1 {
chosen = suggestions[0]
chosen = util.SliceEndStr(suggestions[0], c.X-argstart)
}
return chosen, suggestions
}
// OptionValueComplete completes values for various options
func OptionValueComplete(inputOpt, input string) (string, []string) {
func OptionValueComplete(b *buffer.Buffer) (string, []string) {
c := b.GetActiveCursor()
l := b.LineBytes(c.Y)
l = util.SliceStart(l, c.X)
input, argstart := buffer.GetArg(b)
completeValue := false
args := bytes.Split(l, []byte{' '})
if len(args) >= 2 {
localSettings := config.DefaultLocalSettings()
for option := range config.GlobalSettings {
if option == string(args[len(args)-2]) {
completeValue = true
break
}
}
for option := range localSettings {
if option == string(args[len(args)-2]) {
completeValue = true
break
}
}
}
if !completeValue {
return OptionComplete(b)
}
inputOpt := string(args[len(args)-2])
inputOpt = strings.TrimSpace(inputOpt)
var suggestions []string
localSettings := config.DefaultLocalSettings()
@@ -180,7 +187,7 @@ func OptionValueComplete(inputOpt, input string) (string, []string) {
var chosen string
if len(suggestions) == 1 {
chosen = suggestions[0]
chosen = util.SliceEndStr(suggestions[0], c.X-argstart)
}
return chosen, suggestions
}

View File

@@ -20,8 +20,7 @@ func (b *Buffer) Autocomplete(c Completer) {
}
// FileComplete autocompletes filenames
func FileComplete(b *Buffer) (string, []string) {
func GetArg(b *Buffer) (string, int) {
c := b.GetActiveCursor()
l := b.LineBytes(c.Y)
l = util.SliceStart(l, c.X)
@@ -36,6 +35,14 @@ func FileComplete(b *Buffer) (string, []string) {
argstart += utf8.RuneCount(a) + 1
}
return input, argstart
}
// FileComplete autocompletes filenames
func FileComplete(b *Buffer) (string, []string) {
c := b.GetActiveCursor()
input, argstart := GetArg(b)
sep := string(os.PathSeparator)
dirs := strings.Split(input, sep)