فهرست منبع

feat: more advanced comment handling

Jonathan Kelley 3 سال پیش
والد
کامیت
150d824561
4فایلهای تغییر یافته به همراه94 افزوده شده و 55 حذف شده
  1. 43 38
      packages/autofmt/src/buffer.rs
  2. 23 17
      packages/autofmt/src/element.rs
  3. 10 0
      packages/autofmt/tests/fil.rs
  4. 18 0
      packages/autofmt/tests/samples/comments.rsx

+ 43 - 38
packages/autofmt/src/buffer.rs

@@ -83,12 +83,53 @@ impl Buffer {
     pub fn write_body_indented(&mut self, children: &[BodyNode]) -> Result {
         self.indent += 1;
 
-        // let mut had_comments = false;
-        // let mut comments = Vec::new();
+        let mut comments = Vec::new();
 
         for child in children {
             // Exprs handle their own indenting/line breaks
             if !matches!(child, BodyNode::RawExpr(_)) {
+                // collect all comments upwards
+                let start = child.span().start();
+                let line_start = start.line;
+
+                // make sure the comments are actually relevant to this element.
+                let this_line = self.src[line_start - 1].as_str();
+
+                let beginning = if this_line.len() > start.column {
+                    this_line[..start.column].trim()
+                } else {
+                    ""
+                };
+
+                if beginning.is_empty() {
+                    for (id, line) in self.src[..line_start - 1].iter().enumerate().rev() {
+                        if line.trim().starts_with("//") || line.is_empty() {
+                            comments.push(id);
+                        } else {
+                            break;
+                        }
+                    }
+                }
+
+                if comments.len() == 1 && self.src[comments[0]].is_empty() {
+                    comments.pop();
+                }
+
+                let mut last_was_empty = false;
+                for comment_line in comments.drain(..).rev() {
+                    let line = &self.src[comment_line];
+                    if line.is_empty() {
+                        if !last_was_empty {
+                            self.new_line()?;
+                        }
+                        last_was_empty = true;
+                    } else {
+                        last_was_empty = false;
+                        self.tabbed_line()?;
+                        write!(self.buf, "{}", self.src[comment_line].trim())?;
+                    }
+                }
+
                 self.tabbed_line()?;
             }
 
@@ -152,39 +193,3 @@ impl SpanLength for Span {
         self.end().line - self.start().line
     }
 }
-
-// collect all comments upwards
-// let start = child.span().start().line;
-
-// for (id, line) in self.src[..start - 1].iter().enumerate().rev() {
-//     if line.trim().starts_with("//") {
-//         // if line.trim().starts_with("//") || line.is_empty() {
-//         if line.is_empty() && id == 0 {
-//             break;
-//         }
-//         comments.push(id);
-//     } else {
-//         break;
-//     }
-// }
-
-// if comments.len() == 1 && self.src[comments[0]].is_empty() {
-//     comments.pop();
-// }
-
-// // let mut last_line_was_empty = false;
-// had_comments = !comments.is_empty();
-// for comment_line in comments.drain(..).rev() {
-//     let line = &self.src[comment_line];
-//     if line.is_empty() {
-//         continue;
-//         // if last_line_was_empty {
-//         //     continue;
-//         // } else {
-//         //     last_line_was_empty = true;
-//         // }
-//     }
-
-//     self.tabbed_line()?;
-//     write!(self.buf, "{}", self.src[comment_line].trim())?;
-// }

+ 23 - 17
packages/autofmt/src/element.rs

@@ -228,15 +228,29 @@ impl Buffer {
             return Some(0);
         }
 
-        // for child in children {
-        //     'line: for line in self.src[..child.span().start().line - 1].iter().rev() {
-        //         match (line.trim().starts_with("//"), line.is_empty()) {
-        //             (true, _) => return None,
-        //             (_, true) => continue 'line,
-        //             _ => break 'line,
-        //         }
-        //     }
-        // }
+        for child in children {
+            let start = child.span().start();
+            let line_start = start.line;
+
+            // make sure the comments are actually relevant to this element.
+            let this_line = self.src[line_start - 1].as_str();
+
+            let beginning = if this_line.len() > start.column {
+                this_line[..start.column].trim()
+            } else {
+                ""
+            };
+
+            if beginning.is_empty() {
+                'line: for line in self.src[..child.span().start().line - 1].iter().rev() {
+                    match (line.trim().starts_with("//"), line.is_empty()) {
+                        (true, _) => return None,
+                        (_, true) => continue 'line,
+                        _ => break 'line,
+                    }
+                }
+            }
+        }
 
         match children {
             [BodyNode::Text(ref text)] => Some(text.value().len()),
@@ -249,14 +263,6 @@ impl Buffer {
                     self.is_short_children(&comp.children)
                         .map(|child_len| child_len + attr_len)
                 }
-                // let is_short_child = self.is_short_children(&comp.children);
-
-                // match (is_short_child, is_short_attrs) {
-                //     (Some(child_len), Some(attrs_len)) => Some(child_len + attrs_len),
-                //     (Some(child_len), None) => Some(child_len),
-                //     (None, Some(attrs_len)) => Some(attrs_len),
-                //     (None, None) => None,
-                // }
             }
             [BodyNode::RawExpr(ref _expr)] => {
                 // TODO: let rawexprs to be inlined

+ 10 - 0
packages/autofmt/tests/fil.rs

@@ -20,3 +20,13 @@ fn already_formatted_file_properly() {
 
     println!("{}", out);
 }
+
+#[test]
+fn comment_case_work() {
+    let src = include_str!("./samples/comments.rsx");
+
+    let formatted = dioxus_autofmt::fmt_file(src);
+    let out = dioxus_autofmt::apply_formats(src, formatted);
+
+    println!("{}", out);
+}

+ 18 - 0
packages/autofmt/tests/samples/comments.rsx

@@ -0,0 +1,18 @@
+rsx! {
+    div {
+        // The title
+        "hello"
+
+        // The title
+        "hello"
+
+        // The title
+        "hello"
+
+        // The title
+        "hello"
+
+        // body
+        div { "text" }
+    }
+}