Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable use of carapace completer as a library #655

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ad387ff
Merge upstream master
maxlandon Dec 20, 2022
8fc83ce
Enable using carapace completer as a library
maxlandon Dec 26, 2022
53d7461
Fix bugs from previous commits/merges
maxlandon Dec 30, 2022
d2b5b0c
Merge branch 'master' of github.com:rsteube/carapace into library
maxlandon Jan 5, 2023
8e704bc
Fixes after merge
maxlandon Jan 5, 2023
8ae1b48
Merge branch 'master' of github.com:rsteube/carapace
maxlandon Jan 5, 2023
58dda9b
For some reason fish action doubles the fmt import...
maxlandon Jan 5, 2023
382407e
Make UniqueList() to keep individual suffix matchers
maxlandon Jan 6, 2023
3bc923c
Merge branch 'master' of github.com:rsteube/carapace
maxlandon Apr 16, 2023
fc9b8c3
Merge branch 'master' into library
maxlandon Apr 16, 2023
a5e6f95
Only use namespace multipart of flags when they need it.
maxlandon May 10, 2023
c37cf28
Merge branch 'master' of github.com:rsteube/carapace
maxlandon May 15, 2023
47d0a68
Recommit correct configuration file load
maxlandon May 15, 2023
b5307f7
Merge branch 'master' into library
maxlandon May 16, 2023
6362679
Add help completion
maxlandon May 17, 2023
feaf11d
Refactor values in internal/shell
maxlandon May 27, 2023
1ea9c3a
Add support for Windows absolute path + fixes to relative paths
maxlandon May 28, 2023
f11fb22
Merge branch 'powershell-path' into library
maxlandon May 28, 2023
867bc04
Merge branch 'fixes' into library
maxlandon May 28, 2023
e8d757e
Remove backslash replacer, not needed
maxlandon Jun 2, 2023
080d3bd
Merge branch 'master' into library
maxlandon Aug 8, 2023
a30f518
Merge branch 'master' into library
maxlandon Aug 16, 2023
656cf21
Merge upstream master into library
maxlandon Nov 29, 2023
fa41dc4
Merge branch 'master' into library
maxlandon Nov 29, 2023
4ce9864
Merge upstream fixes
maxlandon Nov 29, 2023
c05a3a8
Fix missing master stuff
maxlandon Nov 29, 2023
45d01ed
Remove old code
maxlandon Nov 29, 2023
356c9d5
Remove old code again
maxlandon Nov 29, 2023
0b978c5
Fix test typo in strings
maxlandon Nov 29, 2023
41ee6d5
Remove unused function
maxlandon Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ func (a Action) Filter(values ...string) Action {
})
}

// FilterArgs filters Context.Args
// FilterArgs filters Context.Args.
func (a Action) FilterArgs() Action {
return ActionCallback(func(c Context) Action {
return a.Filter(c.Args...)
})
}

// FilterArgs filters Context.Parts
// FilterArgs filters Context.Parts.
func (a Action) FilterParts() Action {
return ActionCallback(func(c Context) Action {
return a.Filter(c.Parts...)
Expand Down Expand Up @@ -142,7 +142,7 @@ func (a Action) MultiParts(dividers ...string) Action {
})
}

// MultiPartsP is like MultiParts but with placeholders
// MultiPartsP is like MultiParts but with placeholders.
func (a Action) MultiPartsP(delimiter string, pattern string, f func(placeholder string, matches map[string]string) Action) Action {
return ActionCallback(func(c Context) Action {
invoked := a.Invoke(c)
Expand Down
2 changes: 1 addition & 1 deletion compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (d compDirective) ToA(values ...string) Action {
var action Action
switch {
case d.matches(cobra.ShellCompDirectiveError):
return ActionMessage("an error occured")
return ActionMessage("an error occurred")
case d.matches(cobra.ShellCompDirectiveFilterDirs):
switch len(values) {
case 0:
Expand Down
29 changes: 29 additions & 0 deletions complete.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
package carapace

import (
"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/internal/config"
"github.com/rsteube/carapace/internal/shell/bash"
"github.com/rsteube/carapace/internal/shell/library"
"github.com/rsteube/carapace/internal/shell/nushell"
"github.com/rsteube/carapace/pkg/ps"
"github.com/spf13/cobra"
)

// Complete can be used by Go programs wishing to produce completions for
// themselves, without passing through shell snippets/output or export formats.
//
// The `onFinalize` function parameter, if non nil, will be called after having
// generated the completions from the given command/tree. This function is generally
// used to reset the command tree, which is needed when the Go program is a shell itself.
// Also, and before calling `onFinalize` if not nil, the completion storage is cleared.
func Complete(cmd *cobra.Command, args []string, onFinalize func()) (common.RawValues, common.Meta) {
// Generate the completion as normally done for an external system shell
initHelpCompletion(cmd)
action, context := traverse(cmd, args[2:])

if err := config.Load(); err != nil {
action = ActionMessage("failed to load config: " + err.Error())
}

if onFinalize != nil {
storage = make(_storage)

onFinalize()
}

invoked := action.Invoke(context)

return library.ActionRawValues(context.Value, invoked.meta, invoked.rawValues)
}

func complete(cmd *cobra.Command, args []string) (string, error) {
switch len(args) {
case 0:
Expand Down
1 change: 0 additions & 1 deletion example/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ func init() {
return carapace.ActionExecute(cmd)
}),
)

}
2 changes: 1 addition & 1 deletion example/cmd/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCompat(t *testing.T) {
)

s.Run("compat", "--error", "").
Expect(carapace.ActionMessage("an error occured").
Expect(carapace.ActionMessage("an error occurred").
Usage("ShellCompDirectiveError"))

s.Run("compat", "--nospace", "").
Expand Down
2 changes: 0 additions & 2 deletions example/cmd/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestCache(t *testing.T) {
s.Run("modifier", "--cache", "").
ExpectNot(cached.
Usage("Cache()"))

})
}

Expand Down Expand Up @@ -239,7 +238,6 @@ func TestFilterParts(t *testing.T) {
NoSpace(',').
Prefix("one,three,").
Usage("FilterParts()"))

})
}

Expand Down
2 changes: 1 addition & 1 deletion example/cmd/multiparts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/rsteube/carapace/pkg/style"
)

// TODO rename
// TODO rename.
func TestMultiparts(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Files(
Expand Down
1 change: 0 additions & 1 deletion example/cmd/special.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ func init() {
carapace.Gen(specialCmd).PositionalCompletion(
carapace.ActionValues(`p1 & < > ' " { } $ # | ? ( ) ; [ ] * \ `+"`", "positional1"),
)

}
42 changes: 42 additions & 0 deletions internal/shell/library/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package library

import (
"strings"

"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/pkg/style"
)

var sanitizer = strings.NewReplacer(
"\n", ``,
"\r", ``,
"\t", ``,
)

var quoter = strings.NewReplacer(
// `\`, `\\`,
` `, `\ `,
)

// ActionRawValues formats values for carapace if used as library.
func ActionRawValues(_ string, meta common.Meta, values common.RawValues) (common.RawValues, common.Meta) {
sorted := make(common.RawValues, 0)

values.EachTag(func(_ string, values common.RawValues) {
for index, val := range values {
val.Value = sanitizer.Replace(val.Value)
val.Value = quoter.Replace(val.Value)
if !meta.Nospace.Matches(val.Value) {
val.Value += " "
}
if val.Style != "" {
val.Style = style.SGR(val.Style)
}
values[index] = val
}

sorted = append(sorted, values...)
})

return sorted, meta
}
2 changes: 1 addition & 1 deletion internal/shell/nushell/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nushell
import (
"strings"

"github.com/rsteube/carapace-shlex"
shlex "github.com/rsteube/carapace-shlex"
)

// Patch uses the lexer to parse and patch given arguments which
Expand Down
3 changes: 1 addition & 2 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCheck(t *testing.T) {
}
}

// BenchmarkStorage tests for concurrent map read/write
// BenchmarkStorage tests for concurrent map read/write.
func BenchmarkStorage(b *testing.B) {
cmd := &cobra.Command{}
cmd2 := &cobra.Command{}
Expand All @@ -98,5 +98,4 @@ func BenchmarkStorage(b *testing.B) {
storage.getPositional(cmd2, 0)
}
})

}
Loading