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

feat(repl): Suggestions now start off by default and added a new flag #4891

Merged
merged 3 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ $ which -a pkg-config
/usr/bin/pkg-config
```

To compile and use the REPL, use the following command:
To compile and use the REPL, use the following command to run the repl with suggestions enabled:

```
$ go build ./cmd/flux
$ ./flux repl
$ ./flux --enable-suggestions
```

Alternatively, because the `pkg-config` wrapper may not work in all projects you may not want to add the wrapper `pkg-config` to your `PATH`. In this case you can set `PKG_CONFIG` and Go will use it. Eg, to build and install to ${GOPATH}/bin using `PKG_CONFIG`:
Expand Down
12 changes: 7 additions & 5 deletions cmd/flux/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
)

var flags struct {
ExecScript bool
Trace string
Format string
Features string
ExecScript bool
Trace string
Format string
Features string
EnableSuggestions bool
}

func runE(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -63,7 +64,7 @@ func runE(cmd *cobra.Command, args []string) error {
ctx = feature.Dependency{Flagger: flagger}.Inject(ctx)

if len(args) == 0 {
return replE(ctx)
return replE(ctx, flags.EnableSuggestions)
}
return executeE(ctx, script, flags.Format)
}
Expand Down Expand Up @@ -115,6 +116,7 @@ func main() {
SilenceUsage: true,
}
fluxCmd.Flags().BoolVarP(&flags.ExecScript, "exec", "e", false, "Interpret file argument as a raw flux script")
fluxCmd.Flags().BoolVarP(&flags.EnableSuggestions, "enable-suggestions", "", false, "enable suggestions in the repl")
fluxCmd.Flags().StringVar(&flags.Trace, "trace", "", "Trace query execution")
fluxCmd.Flags().StringVarP(&flags.Format, "format", "", "cli", "Output format one of: cli,csv. Defaults to cli")
fluxCmd.Flag("trace").NoOptDefVal = "jaeger"
Expand Down
4 changes: 2 additions & 2 deletions cmd/flux/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/influxdata/flux/repl"
)

func replE(ctx context.Context) error {
r := repl.New(ctx)
func replE(ctx context.Context, enableSuggestions bool) error {
r := repl.New(ctx, enableSuggestions)
r.Run()
return nil
}
4 changes: 2 additions & 2 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func TestInterpreter_MultiPhaseInterpretation(t *testing.T) {
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
defer deps.Finish()

r := repl.New(ctx)
r := repl.New(ctx, false)
if _, err := r.Eval(prelude); err != nil {
t.Fatalf("unable to evaluate prelude: %s", err)
}
Expand Down Expand Up @@ -772,7 +772,7 @@ func TestInterpreter_MultipleEval(t *testing.T) {
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
defer deps.Finish()

r := repl.New(ctx)
r := repl.New(ctx, false)

if _, err := r.Eval(prelude); err != nil {
t.Fatalf("unable to evaluate prelude: %s", err)
Expand Down
65 changes: 36 additions & 29 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ type REPL struct {

cancelMu sync.Mutex
cancelFunc context.CancelFunc

enableSuggestions bool
}

func New(ctx context.Context) *REPL {
func New(ctx context.Context, enableSuggestions bool) *REPL {
scope := values.NewScope()
importer := runtime.StdLib()
for _, p := range runtime.PreludeList {
Expand All @@ -56,11 +58,12 @@ func New(ctx context.Context) *REPL {
}

return &REPL{
ctx: ctx,
scope: scope,
itrp: interpreter.NewInterpreter(nil, &lang.ExecOptsConfig{}),
analyzer: analyzer,
importer: importer,
ctx: ctx,
scope: scope,
itrp: interpreter.NewInterpreter(nil, &lang.ExecOptsConfig{}),
analyzer: analyzer,
importer: importer,
enableSuggestions: enableSuggestions,
}
}

Expand Down Expand Up @@ -100,35 +103,39 @@ func (r *REPL) clearCancel() {
}

func (r *REPL) completer(d prompt.Document) []prompt.Suggest {
names := make([]string, 0, r.scope.Size())
r.scope.Range(func(k string, v values.Value) {
names = append(names, k)
})
sort.Strings(names)

s := make([]prompt.Suggest, 0, len(names))
for _, n := range names {
if n == "_" || !strings.HasPrefix(n, "_") {
s = append(s, prompt.Suggest{Text: n})
}
}
if d.Text == "" || strings.HasPrefix(d.Text, "@") {
root := "./" + strings.TrimPrefix(d.Text, "@")
fluxFiles, err := getFluxFiles(root)
if err == nil {
for _, fName := range fluxFiles {
s = append(s, prompt.Suggest{Text: "@" + fName})
if r.enableSuggestions {
names := make([]string, 0, r.scope.Size())
r.scope.Range(func(k string, v values.Value) {
names = append(names, k)
})
sort.Strings(names)

s := make([]prompt.Suggest, 0, len(names))
for _, n := range names {
if n == "_" || !strings.HasPrefix(n, "_") {
s = append(s, prompt.Suggest{Text: n})
}
}
dirs, err := getDirs(root)
if err == nil {
for _, fName := range dirs {
s = append(s, prompt.Suggest{Text: "@" + fName + string(os.PathSeparator)})
if d.Text == "" || strings.HasPrefix(d.Text, "@") {
root := "./" + strings.TrimPrefix(d.Text, "@")
fluxFiles, err := getFluxFiles(root)
if err == nil {
for _, fName := range fluxFiles {
s = append(s, prompt.Suggest{Text: "@" + fName})
}
}
dirs, err := getDirs(root)
if err == nil {
for _, fName := range dirs {
s = append(s, prompt.Suggest{Text: "@" + fName + string(os.PathSeparator)})
}
}
}

return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
return nil

return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}

func (r *REPL) Input(t string) (*libflux.FluxError, error) {
Expand Down