Skip to content

Commit

Permalink
[cli-ptb] Add "did you mean" for misspelled commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian committed Mar 5, 2024
1 parent debcc9d commit 8442db6
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 5 deletions.
17 changes: 17 additions & 0 deletions crates/sui/src/client_ptb/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ pub const KEYWORDS: &[&str] = &[
ADDRESS, BOOL, VECTOR, SOME, NONE, GAS, U8, U16, U32, U64, U128, U256,
];

pub const COMMANDS: &[&str] = &[
TRANSFER_OBJECTS,
SPLIT_COINS,
MERGE_COINS,
MAKE_MOVE_VEC,
MOVE_CALL,
PUBLISH,
UPGRADE,
ASSIGN,
PREVIEW,
WARN_SHADOWS,
GAS_BUDGET,
SUMMARY,
GAS_COIN,
JSON,
];

pub fn is_keyword(s: &str) -> bool {
KEYWORDS.contains(&s)
}
Expand Down
6 changes: 4 additions & 2 deletions crates/sui/src/client_ptb/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ pub fn to_ordinal_contraction(num: usize) -> String {
format!("{}{}", num, suffix)
}

fn find_did_you_means<'a>(
pub(crate) fn find_did_you_means<'a>(
needle: &str,
haystack: impl IntoIterator<Item = &'a str>,
) -> Vec<&'a str> {
Expand All @@ -1055,7 +1055,9 @@ fn find_did_you_means<'a>(
results
}

fn display_did_you_mean(possibles: Vec<&str>) -> Option<String> {
pub(crate) fn display_did_you_mean<S: AsRef<str> + std::fmt::Display>(
possibles: Vec<S>,
) -> Option<String> {
if possibles.is_empty() {
return None;
}
Expand Down
23 changes: 20 additions & 3 deletions crates/sui/src/client_ptb/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ use move_command_line_common::{
};
use sui_types::{base_types::ObjectID, Identifier};

use crate::{client_ptb::ast::all_keywords, err, error, sp};
use crate::{
client_ptb::{
ast::{all_keywords, COMMANDS},
builder::{display_did_you_mean, find_did_you_means},
},
err, error, sp,
};

use super::{
ast::{self as A, is_keyword, Argument, ModuleAccess, ParsedPTBCommand, ParsedProgram},
Expand Down Expand Up @@ -135,8 +141,19 @@ impl<'a, I: Iterator<Item = &'a str>> ProgramParser<'a, I> {
Ok(cap.span.wrap(ParsedPTBCommand::Upgrade(src, cap)))
}),

L(T::Command, _) => {
let err = err!(sp, "Unknown {lexeme}");
L(T::Command, s) => {
let possibles = find_did_you_means(s, COMMANDS.iter().copied())
.into_iter()
.map(|s| format!("--{s}"))
.collect();
let err = if let Some(suggestion) = display_did_you_mean(possibles) {
err!(
sp => help: { "{suggestion}" },
"Unknown {lexeme}",
)
} else {
err!(sp, "Unknown {lexeme}")
};
self.state.errors.push(err);
self.fast_forward_to_next_command();
}
Expand Down
6 changes: 6 additions & 0 deletions crates/sui/tests/ptb_files/parsing/invalid_commands.ptb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--move-cal
--assing
--assig
--splitcoins
--move-vec
--transfer-object
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
source: crates/sui/tests/ptb_files_tests.rs
expression: "results.join(\"\\n\")"
---
=== ERRORS AFTER PARSING INPUT COMMANDS ===
× Error when processing PTB
╭─[1:1]
1--move-cal
· ─────┬────
· ╰── Unknown command '--move-cal'
2--assing
╰────
help: Did you mean '--move-call'?

× Error when processing PTB
╭─[2:1]
1--move-cal
2--assing
· ────┬───
· ╰── Unknown command '--assing'
3--assig
╰────
help: Did you mean '--assign'?

× Error when processing PTB
╭─[3:1]
2--assing
3--assig
· ───┬───
· ╰── Unknown command '--assig'
4--splitcoins
╰────
help: Did you mean '--assign'?

× Error when processing PTB
╭─[4:1]
3--assig
4--splitcoins
· ──────┬─────
· ╰── Unknown command '--splitcoins'
5--move-vec
╰────
help: Did you mean '--split-coins'?

× Error when processing PTB
╭─[5:1]
4--splitcoins
5--move-vec
· ─────┬────
· ╰── Unknown command '--move-vec'
6--transfer-object
╰────
help: Did you mean '--move-call'?

× Error when processing PTB
╭─[6:1]
5--move-vec
6--transfer-object
· ────────┬────────
· ╰── Unknown command '--transfer-object'
╰────
help: Did you mean '--transfer-objects'?

× Error when processing PTB
╭─[6:18]
5--move-vec
6--transfer-object
· ▲
· ╰── Gas budget not set.
╰────
help: Use --gas-budget <u64> to set a gas budget

0 comments on commit 8442db6

Please sign in to comment.