From 4fc91dcc03a9210dd6d3248fc6dcc32d9882d69d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 21 Nov 2022 11:05:36 +0530 Subject: [PATCH] There should be no trailing space when completing directories for a file match based on patterns --- tools/cli/completion.go | 2 +- tools/cli/files.go | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tools/cli/completion.go b/tools/cli/completion.go index 9dc269b96..260c213f3 100644 --- a/tools/cli/completion.go +++ b/tools/cli/completion.go @@ -132,7 +132,7 @@ func (self *Completions) AddMatchGroup(title string) *MatchGroup { return &ans } -type CompletionFunc func(completions *Completions, word string, arg_num int) +type CompletionFunc = func(completions *Completions, word string, arg_num int) func NamesCompleter(title string, names ...string) CompletionFunc { return func(completions *Completions, word string, arg_num int) { diff --git a/tools/cli/files.go b/tools/cli/files.go index b2b8ef18b..d7a0ca063 100644 --- a/tools/cli/files.go +++ b/tools/cli/files.go @@ -244,9 +244,25 @@ func make_completer(title string, relative_to relative_to, patterns []string, f return func(completions *Completions, word string, arg_num int) { q := f(word, cwd, lpats) if len(q) > 0 { + dirs, files := make([]string, 0, len(q)), make([]string, 0, len(q)) + for _, x := range q { + if strings.HasSuffix(x, "/") { + dirs = append(dirs, x) + } else { + files = append(files, x) + } + } + if len(dirs) > 0 { + mg := completions.AddMatchGroup("Directories") + mg.IsFiles = true + mg.NoTrailingSpace = true + for _, c := range dirs { + mg.AddMatch(c) + } + } mg := completions.AddMatchGroup(title) mg.IsFiles = true - for _, c := range q { + for _, c := range files { mg.AddMatch(c) } }