8000 feat(cli): prompt for misspelled parameter names by johnstcn · Pull Request #10350 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(cli): prompt for misspelled parameter names #10350

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

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add max distance limit
  • Loading branch information
johnstcn committed Nov 6, 2023
commit 06fa1c3186b9ef60d715368944a4d4911d5c3f52
15 changes: 11 additions & 4 deletions cli/cliutil/levenshtein/levenshtein.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import (
// If no matches are found, an empty slice is returned.
func Matches(needle string, maxDistance int, haystack ...string) (matches []string) {
for _, hay := range haystack {
if d, err := Distance(needle, hay); err == nil && d <= maxDistance {
if d, err := Distance(needle, hay, maxDistance); err == nil && d <= maxDistance {
matches = append(matches, hay)
}
}

return matches
}

var ErrMaxDist error = xerrors.New("levenshtein: maxDist exceeded")

// Distance returns the edit distance between a and b using the
// Wagner-Fischer algorithm.
// A and B must be less than 255 characters long.
func Distance(a, b string) (int, error) {
// maxDist is the maximum distance to consider.
// A value of -1 for maxDist means no maximum.
func Distance(a, b string, maxDist int) (int, error) {
if len(a) > 255 {
return 0, xerrors.Errorf("levenshtein: a must be less than 255 characters long")
}
Expand Down Expand Up @@ -54,7 +58,7 @@ func Distance(a, b string) (int, error) {

// Target prefixes
for j = 1; j < n; j++ {
d[0][j] = j
d[0][j] = j // nolint:gosec // this cannot overflow
}

// Compute the distance
Expand All @@ -71,12 +75,15 @@ func Distance(a, b string) (int, error) {
d[i+1][j]+1, // insertion
d[i][j]+subCost, // substitution
)
// check maxDist on the diagonal
if maxDist > -1 && i == j && d[i+1][j+1] > uint8(maxDist) {
return int(d[i+1][j+1]), ErrMaxDist
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not against us having our own implementation, but there's also: https://github.com/agnivade/levenshtein (in this case, the performance optimizations are negligible, though.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also considering https://pkg.go.dev/github.com/junegunn/fzf@v0.0.0-20231029150554-1cfa3ee4c7c1/src/algo but didn't want to necessarily add a dep just for this.


return int(d[m][n]), nil
}

func min[T constraints.Ordered](ts ...T) T {
if len(ts) == 0 {
panic("min: no arguments")
Expand Down
27 changes: 24 additions & 3 deletions cli/cliutil/levenshtein/levenshtein_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,63 +104,84 @@ func Test_Levenshtein_Distance(t *testing.T) {
Name string
A string
B string
MaxDist int
Expected int
Error string
}{
{
Name: "empty",
A: "",
B: "",
MaxDist: -1,
Expected: 0,
},
{
Name: "a empty",
A: "",
B: "foo",
MaxDist: -1,
Expected: 3,
},
{
Name: "b empty",
A: "foo",
B: "",
MaxDist: -1,
Expected: 3,
},
{
Name: "a is b",
A: "foo",
B: "foo",
MaxDist: -1,
Expected: 0,
},
{
Name: "one addition",
A: "foo",
B: "fooo",
MaxDist: -1,
Expected: 1,
},
{
Name: "one deletion",
A: "fooo",
B: "foo",
MaxDist: -1,
Expected: 1,
},
{
Name: "one substitution",
A: "foo",
B: "fou",
MaxDist: -1,
Expected: 1,
},
{
Name: "different strings entirely",
A: "foo",
B: "bar",
MaxDist: -1,
Expected: 3,
},
{
Name: "different strings, max distance 2",
A: "foo",
B: "bar",
MaxDist: 2,
Error: levenshtein.ErrMaxDist.Error(),
},
} {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
t.Parallel()
actual, err := levenshtein.Distance(tt.A, tt.B)
require.NoError(t, err)
require.Equal(t, tt.Expected, actual)
actual, err := levenshtein.Distance(tt.A, tt.B, tt.MaxDist)
if tt.Error == "" {
require.NoError(t, err)
require.Equal(t, tt.Expected, actual)
} else {
require.EqualError(t, err, tt.Error)
}
})
}
}
0