Merge pull request #359 from boombuler/params

Params
This commit is contained in:
Zachary Yedidia
2016-09-14 17:14:01 -04:00
committed by GitHub
3 changed files with 49 additions and 19 deletions

View File

@@ -18,12 +18,14 @@ var pluginCompletions []func(string) []string
func FileComplete(input string) (string, []string) { func FileComplete(input string) (string, []string) {
var sep string = string(os.PathSeparator) var sep string = string(os.PathSeparator)
dirs := strings.Split(input, sep) dirs := strings.Split(input, sep)
var files []os.FileInfo var files []os.FileInfo
var err error var err error
if len(dirs) > 1 { if len(dirs) > 1 {
home, _ := homedir.Dir() home, _ := homedir.Dir()
directories := strings.Join(dirs[:len(dirs)-1], sep) directories := strings.Join(dirs[:len(dirs)-1], sep) + sep
if strings.HasPrefix(directories, "~") { if strings.HasPrefix(directories, "~") {
directories = strings.Replace(directories, "~", home, 1) directories = strings.Replace(directories, "~", home, 1)
} }
@@ -31,6 +33,7 @@ func FileComplete(input string) (string, []string) {
} else { } else {
files, err = ioutil.ReadDir(".") files, err = ioutil.ReadDir(".")
} }
var suggestions []string var suggestions []string
if err != nil { if err != nil {
return "", suggestions return "", suggestions

View File

@@ -230,42 +230,64 @@ func FuncName(i interface{}) string {
// The returned slice contains at least one string // The returned slice contains at least one string
func SplitCommandArgs(input string) []string { func SplitCommandArgs(input string) []string {
var result []string var result []string
var curQuote *bytes.Buffer = nil
curArg := new(bytes.Buffer) curArg := new(bytes.Buffer)
inQuote := false
escape := false escape := false
appendResult := func() { finishQuote := func() {
str := curArg.String() if curQuote == nil {
inQuote = false return
escape = false }
if strings.HasPrefix(str, `"`) && strings.HasSuffix(str, `"`) { str := curQuote.String()
if unquoted, err := strconv.Unquote(str); err == nil { if unquoted, err := strconv.Unquote(str); err == nil {
str = unquoted str = unquoted
} }
curArg.WriteString(str)
curQuote = nil
} }
appendResult := func() {
finishQuote()
escape = false
str := curArg.String()
result = append(result, str) result = append(result, str)
curArg.Reset() curArg.Reset()
} }
for _, r := range input { for _, r := range input {
if r == ' ' && !inQuote { if r == ' ' && curQuote == nil {
appendResult() appendResult()
} else { } else {
curArg.WriteRune(r) runeHandled := false
appendRuneToBuff := func() {
if r == '"' && !inQuote { if curQuote != nil {
inQuote = true curQuote.WriteRune(r)
} else { } else {
if inQuote && !escape { curArg.WriteRune(r)
if r == '"' {
inQuote = false
} }
if r == '\\' { runeHandled = true
}
if r == '"' && curQuote == nil {
curQuote = new(bytes.Buffer)
appendRuneToBuff()
} else {
if curQuote != nil && !escape {
if r == '"' {
appendRuneToBuff()
finishQuote()
} else if r == '\\' {
appendRuneToBuff()
escape = true escape = true
continue continue
} }
} }
} }
if !runeHandled {
appendRuneToBuff()
}
} }
escape = false escape = false

View File

@@ -100,9 +100,14 @@ func TestJoinAndSplitCommandArgs(t *testing.T) {
Query string Query string
Wanted []string Wanted []string
}{ }{
{`"hallo""Welt"`, []string{`"hallo""Welt"`}}, {`"hallo""Welt"`, []string{`halloWelt`}},
{`"hallo" "Welt"`, []string{`hallo`, `Welt`}},
{`\"`, []string{`\"`}}, {`\"`, []string{`\"`}},
{`"foo`, []string{`"foo`}},
{`"foo"`, []string{`foo`}},
{`"\"`, []string{`"\"`}}, {`"\"`, []string{`"\"`}},
{`"C:\\"foo.txt`, []string{`C:\foo.txt`}},
{`"\n"new"\n"line`, []string{"\nnew\nline"}},
} }
for i, test := range splitTests { for i, test := range splitTests {