hint: show the matching usage template for multi-form commands
A parse error in `add index …` showed the `add column` usage: `add` and `drop` are multi-form commands, and both the ambient hint and the submit-time usage block picked the first-listed form unconditionally. New `grammar::usage_key_for_input` disambiguates by the form word after the entry keyword — `column` / `index` / `table` / `relationship`, or the leading digit of `add 1:n …`. The ambient hint now shows that one form; `render_usage_block` shows the committed form's usage and falls back to the whole family only for a bare `add` / `drop` with no form chosen.
This commit is contained in:
+23
-2
@@ -341,8 +341,10 @@ pub fn ambient_hint(
|
||||
}
|
||||
} else {
|
||||
let _ = position;
|
||||
let usage = crate::dsl::grammar::usage_keys_for_input(input)
|
||||
.and_then(|(_, keys)| keys.first().copied())
|
||||
// The form the user has committed to drives the
|
||||
// usage template — `add index …` shows the
|
||||
// `add index` usage, not the first `add` form.
|
||||
let usage = crate::dsl::grammar::usage_key_for_input(input)
|
||||
.map(|key| crate::friendly::translate(key, &[]));
|
||||
Some(AmbientHint::Prose(match usage {
|
||||
Some(u) => crate::t!(
|
||||
@@ -876,6 +878,25 @@ mod tests {
|
||||
assert_eq!(cs, vec!["data".to_string(), "table".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ambient_hint_usage_matches_the_add_form_typed() {
|
||||
// A trailing-junk error after `add index …` must show
|
||||
// the `add index` usage — `add` is a multi-form
|
||||
// command and the hint used to always show the first
|
||||
// form (`add column`).
|
||||
let input = "add index on Customers (barg):";
|
||||
let h = prose(input, input.len()).expect("prose hint");
|
||||
assert!(h.contains("usage:"), "got {h:?}");
|
||||
assert!(
|
||||
h.contains("add index"),
|
||||
"should show the `add index` usage, got {h:?}",
|
||||
);
|
||||
assert!(
|
||||
!h.contains("add column"),
|
||||
"should not show the `add column` usage, got {h:?}",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ambient_hint_for_definite_error_includes_usage_template() {
|
||||
let h = prose("insert into T extra", 19).expect("prose hint");
|
||||
|
||||
Reference in New Issue
Block a user