-
Notifications
You must be signed in to change notification settings - Fork 8
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
Suggest language when one isn't found #116
Conversation
src/commands/BeautifyCommand.ts
Outdated
const langs = unibeautify.findLanguages({ name: language }); | ||
if (langs.length === 0) { | ||
const bestMatchLanguage = getAllLanguages().find(lang => { | ||
return lang.toLowerCase() === language.toLowerCase(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be OK if you also wanted to add https://www.npmjs.com/package/fast-levenshtein
Something like:
const languages = getAllLanguages();
const distances: number[] = languages.map(lang =>
levenshtein.get(lang.toLowerCase(), language.toLowerCase())
);
const bestDistance: number = Math.min(...distances);
const bestMatchLanguages = getAllLanguages().filter((lang, index) =>
distances[index] === bestDistance
);
// ...
const error = new Error(
`Language '${language}' was not found. Did you mean '${bestMatchLanguages.join(" or ")}'?`
);
The above only gets the best. It could also be modified to support an arbitrary threshold:
- distances[index] === bestDistance
+ const distanceThreshold = 2;
+ distances[index] <= (bestDistance + distanceThreshold)
So C
would also show C++
and visa versa 😉 .
src/commands/BeautifyCommand.ts
Outdated
return this.handleError(error, 1); | ||
} | ||
} | ||
return Promise.resolve({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be return Promise.resolve();
and the method return type is : Promise<void>
Co-authored-by: Glavin Wiechert <[email protected]>
src/commands/BeautifyCommand.ts
Outdated
const error = new Error( | ||
`Language '${language}' was not found. Did you mean '${bestMatchLanguage}'?` | ||
`Language '${language}' was not found. Did you mean:\n\n${bestMatchLanguages.map(lang => `- ${lang}`).join("\n")}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably limit the number of matches returned, just in case.
const limit = 3;
bestMatchLanguages.slice(0, limit).map(lang => `- ${lang}`).join("\n")
src/commands/BeautifyCommand.ts
Outdated
if (bestMatchLanguages.length > 0) { | ||
const limit = 3; | ||
const error = new Error( | ||
`Language '${language}' was not found. Did you mean:\n\n${bestMatchLanguages |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I like the double \n
here.
I just noticed the snapshot at https://github.com/Unibeautify/unibeautify-cli/pull/116/files#diff-79f3cbac345f885b9f1a9467fa09ae86R55
`Language '${language}' was not found. Did you mean:\n\n${bestMatchLanguages | |
`Language '${language}' was not found. Did you mean:\n${bestMatchLanguages |
src/commands/BeautifyCommand.ts
Outdated
const error = new Error( | ||
`Language '${language}' was not found. Did you mean:\n\n${bestMatchLanguages | ||
.slice(0, limit) | ||
.map(lang => `- ${lang}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I like to do is indent list items to the console.
.map(lang => `- ${lang}`) | |
.map(lang => ` - ${lang}`) |
@stevenzeck I think this is getting really close. Awesome work! I'm really excited to merge this 😃 |
Closes #115
-l
is not found