From fc5cde943443b0ea700d7e9f9178463e08d39f36 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 29 Oct 2024 12:35:34 -0600 Subject: [PATCH] Fix quotes in Rust (#19914) Release Notes: - (preview only) Fixed quote-autoclose in Rust --- crates/editor/src/editor.rs | 30 +++++++++++++-------------- crates/languages/src/rust/config.toml | 6 +++--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 81ee6a01de..40cdf27f2e 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -3244,9 +3244,21 @@ impl Editor { } if enabled && pair.start.ends_with(text.as_ref()) { - bracket_pair = Some(pair.clone()); - is_bracket_pair_start = true; - break; + let prefix_len = pair.start.len() - text.len(); + let preceding_text_matches_prefix = prefix_len == 0 + || (selection.start.column >= (prefix_len as u32) + && snapshot.contains_str_at( + Point::new( + selection.start.row, + selection.start.column - (prefix_len as u32), + ), + &pair.start[..prefix_len], + )); + if preceding_text_matches_prefix { + bracket_pair = Some(pair.clone()); + is_bracket_pair_start = true; + break; + } } if pair.end.as_str() == text.as_ref() { bracket_pair = Some(pair.clone()); @@ -3263,8 +3275,6 @@ impl Editor { self.use_auto_surround && snapshot_settings.use_auto_surround; if selection.is_empty() { if is_bracket_pair_start { - let prefix_len = bracket_pair.start.len() - text.len(); - // If the inserted text is a suffix of an opening bracket and the // selection is preceded by the rest of the opening bracket, then // insert the closing bracket. @@ -3272,15 +3282,6 @@ impl Editor { .chars_at(selection.start) .next() .map_or(true, |c| scope.should_autoclose_before(c)); - let preceding_text_matches_prefix = prefix_len == 0 - || (selection.start.column >= (prefix_len as u32) - && snapshot.contains_str_at( - Point::new( - selection.start.row, - selection.start.column - (prefix_len as u32), - ), - &bracket_pair.start[..prefix_len], - )); let is_closing_quote = if bracket_pair.end == bracket_pair.start && bracket_pair.start.len() == 1 @@ -3299,7 +3300,6 @@ impl Editor { if autoclose && bracket_pair.close && following_text_allows_autoclose - && preceding_text_matches_prefix && !is_closing_quote { let anchor = snapshot.anchor_before(selection.end); diff --git a/crates/languages/src/rust/config.toml b/crates/languages/src/rust/config.toml index 81b9c1e2d9..96207904f5 100644 --- a/crates/languages/src/rust/config.toml +++ b/crates/languages/src/rust/config.toml @@ -5,9 +5,9 @@ line_comments = ["// ", "/// ", "//! "] autoclose_before = ";:.,=}])>" brackets = [ { start = "{", end = "}", close = true, newline = true }, - { start = "r#\"", end = "\"#", close = true, newline = true }, - { start = "r##\"", end = "\"##", close = true, newline = true }, - { start = "r###\"", end = "\"###", close = true, newline = true }, + { start = "r#\"", end = "\"#", close = true, newline = true, not_in = ["string", "comment"] }, + { start = "r##\"", end = "\"##", close = true, newline = true, not_in = ["string", "comment"] }, + { start = "r###\"", end = "\"###", close = true, newline = true, not_in = ["string", "comment"] }, { start = "[", end = "]", close = true, newline = true }, { start = "(", end = ")", close = true, newline = true }, { start = "<", end = ">", close = false, newline = true, not_in = ["string", "comment"] },