diff options
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcl/json/didyoumean.go')
-rw-r--r-- | vendor/github.com/hashicorp/hcl2/hcl/json/didyoumean.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcl/json/didyoumean.go b/vendor/github.com/hashicorp/hcl2/hcl/json/didyoumean.go new file mode 100644 index 0000000..fbdd8bf --- /dev/null +++ b/vendor/github.com/hashicorp/hcl2/hcl/json/didyoumean.go | |||
@@ -0,0 +1,33 @@ | |||
1 | package json | ||
2 | |||
3 | import ( | ||
4 | "github.com/agext/levenshtein" | ||
5 | ) | ||
6 | |||
7 | var keywords = []string{"false", "true", "null"} | ||
8 | |||
9 | // keywordSuggestion tries to find a valid JSON keyword that is close to the | ||
10 | // given string and returns it if found. If no keyword is close enough, returns | ||
11 | // the empty string. | ||
12 | func keywordSuggestion(given string) string { | ||
13 | return nameSuggestion(given, keywords) | ||
14 | } | ||
15 | |||
16 | // nameSuggestion tries to find a name from the given slice of suggested names | ||
17 | // that is close to the given name and returns it if found. If no suggestion | ||
18 | // is close enough, returns the empty string. | ||
19 | // | ||
20 | // The suggestions are tried in order, so earlier suggestions take precedence | ||
21 | // if the given string is similar to two or more suggestions. | ||
22 | // | ||
23 | // This function is intended to be used with a relatively-small number of | ||
24 | // suggestions. It's not optimized for hundreds or thousands of them. | ||
25 | func nameSuggestion(given string, suggestions []string) string { | ||
26 | for _, suggestion := range suggestions { | ||
27 | dist := levenshtein.Distance(given, suggestion, nil) | ||
28 | if dist < 3 { // threshold determined experimentally | ||
29 | return suggestion | ||
30 | } | ||
31 | } | ||
32 | return "" | ||
33 | } | ||