From 693b24bf65ac2cd2bdcaab00b6e5cb899fd83250 Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:04:04 +0800 Subject: [PATCH] fix(editor): keep the class brace in place when Rearrange Code moves the last method (#7921) Rearrange Code moves TypeScript and JavaScript methods as whole lines. When a class's closing brace, or a block comment running on below, shared a line with the last method and that method moved up, the brace moved with it and the methods after it ended up outside the class. Such a class is now left alone; when the last method stays last, the class is rearranged as before. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- crates/ide-language/src/syntax/rearrange.rs | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/ide-language/src/syntax/rearrange.rs b/crates/ide-language/src/syntax/rearrange.rs index 8a8e378cab..87c2cd28af 100644 --- a/crates/ide-language/src/syntax/rearrange.rs +++ b/crates/ide-language/src/syntax/rearrange.rs @@ -428,6 +428,15 @@ fn script_class_change(body: Node, text: &str) -> Option { return None; } let ranges: Vec> = blocks.into_iter().map(|b| b.3).collect(); + // Whole lines move: what shares the last method's last line (the closing brace, a comment + // running on below it) stays at the end, so the last method must too. + let end = ranges.last()?.end; + if order.last() != Some(&(ranges.len() - 1)) + && (end >= body.end_byte() + || body.children(&mut body.walk()).any(|n| n.start_byte() < end && n.end_byte() > end)) + { + return None; + } let mut gaps = Vec::new(); for pair in ranges.windows(2) { // Only blank lines between methods; anything else stays with the class. @@ -650,6 +659,11 @@ mod tests { "class A {\n // #region helpers\n b() {}\n // #endregion\n constructor() {}\n}\n", // Two members on one line. "class A {\n b() {} constructor() {}\n}\n", + // The closing brace on the last method's line would move with it. + "class A {\n b() {}\n constructor() {} }\n", + "const A = class {\n b() {}\n constructor() {} };", + // So would the first line of a comment running on below it. + "class A {\n b() {}\n constructor() {}; /* note\n */\n}\n", // A syntax error anywhere in the file. "class A {\n b() {}\n constructor() {}\n}\nlet = ;\n", ] { @@ -665,6 +679,23 @@ mod tests { assert_eq!(script("javascript", text).as_deref(), Some(expected)); } + #[test] + fn a_last_method_sharing_its_line_stays_last() { + let text = "class A {\n b() {}\n static of() {}\n c() {} }"; + assert_eq!(script("typescript", text).as_deref(), Some("class A {\n static of() {}\n b() {}\n c() {} }")); + let text = "class A {\n b() {}\n static of() {}\n c() {} /* note\n */\n}\n"; + let expected = "class A {\n static of() {}\n b() {}\n c() {} /* note\n */\n}\n"; + assert_eq!(script("javascript", text).as_deref(), Some(expected)); + // Below the last method's line: an empty class element, or a space only JavaScript knows. + let text = "class A {\n b() {}\n constructor() {}\n ;\n}\n"; + assert_eq!(script("typescript", text).as_deref(), Some("class A {\n constructor() {}\n b() {}\n ;\n}\n")); + let text = "class A {\n b() {}\n constructor() {}\n \u{feff}}\n"; + assert_eq!( + script("javascript", text).as_deref(), + Some("class A {\n constructor() {}\n b() {}\n \u{feff}}\n") + ); + } + #[test] fn static_and_instance_methods_with_one_name_stay_separate() { let text = "class A {\n of() {}\n static of() {}\n}\n";