瀏覽代碼

fix: handle comments around attributes better

Jonathan Kelley 3 年之前
父節點
當前提交
2634755620
共有 3 個文件被更改,包括 31 次插入8 次删除
  1. 9 3
      packages/autofmt/src/buffer.rs
  2. 8 5
      packages/autofmt/src/element.rs
  3. 14 0
      packages/autofmt/tests/samples/attrs.rsx

+ 9 - 3
packages/autofmt/src/buffer.rs

@@ -136,8 +136,14 @@ impl Buffer {
         let mut total = 0;
         let mut total = 0;
 
 
         for attr in attributes {
         for attr in attributes {
-            if self.current_element_has_comments(attr.span()) {
-                return 100000;
+            if self.current_span_is_primary(attr.attr.start()) {
+                'line: for line in self.src[..attr.attr.start().start().line - 1].iter().rev() {
+                    match (line.trim().starts_with("//"), line.is_empty()) {
+                        (true, _) => return 100000,
+                        (_, true) => continue 'line,
+                        _ => break 'line,
+                    }
+                }
             }
             }
 
 
             total += match &attr.attr {
             total += match &attr.attr {
@@ -182,7 +188,7 @@ impl Buffer {
     pub fn retrieve_formatted_expr(&mut self, expr: &Expr) -> &str {
     pub fn retrieve_formatted_expr(&mut self, expr: &Expr) -> &str {
         self.cached_formats
         self.cached_formats
             .entry(Location::new(expr.span().start()))
             .entry(Location::new(expr.span().start()))
-            .or_insert(prettyplease::unparse_expr(expr))
+            .or_insert_with(|| prettyplease::unparse_expr(expr))
             .as_str()
             .as_str()
     }
     }
 }
 }

+ 8 - 5
packages/autofmt/src/element.rs

@@ -222,12 +222,13 @@ impl Buffer {
         Ok(())
         Ok(())
     }
     }
 
 
-    pub fn current_element_has_comments(&self, location: Span) -> bool {
+    // make sure the comments are actually relevant to this element.
+    // test by making sure this element is the primary element on this line
+    pub fn current_span_is_primary(&self, location: Span) -> bool {
         let start = location.start();
         let start = location.start();
-        let line_start = start.line;
+        let line_start = start.line - 1;
 
 
-        // make sure the comments are actually relevant to this element.
-        let this_line = self.src[line_start - 1].as_str();
+        let this_line = self.src[line_start].as_str();
 
 
         let beginning = if this_line.len() > start.column {
         let beginning = if this_line.len() > start.column {
             this_line[..start.column].trim()
             this_line[..start.column].trim()
@@ -235,6 +236,8 @@ impl Buffer {
             ""
             ""
         };
         };
 
 
+        // dbg!(beginning);
+
         beginning.is_empty()
         beginning.is_empty()
     }
     }
 
 
@@ -251,7 +254,7 @@ impl Buffer {
         }
         }
 
 
         for child in children {
         for child in children {
-            if self.current_element_has_comments(child.span()) {
+            if self.current_span_is_primary(child.span()) {
                 'line: for line in self.src[..child.span().start().line - 1].iter().rev() {
                 'line: for line in self.src[..child.span().start().line - 1].iter().rev() {
                     match (line.trim().starts_with("//"), line.is_empty()) {
                     match (line.trim().starts_with("//"), line.is_empty()) {
                         (true, _) => return None,
                         (true, _) => return None,

+ 14 - 0
packages/autofmt/tests/samples/attrs.rsx

@@ -18,4 +18,18 @@ rsx! {
         // This here
         // This here
         "hi"
         "hi"
     }
     }
+
+
+    // Comment head
+    div {
+        class: "asd",
+        "Jon"
+    }
+
+    // Comment head
+    div {
+        // Collapse
+        class: "asd",
+        "Jon"
+    }
 }
 }