buffer.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. use std::{
  2. collections::{HashMap, VecDeque},
  3. fmt::{Result, Write},
  4. };
  5. use dioxus_rsx::{BodyNode, ElementAttr, ElementAttrNamed};
  6. use proc_macro2::{LineColumn, Span};
  7. use syn::{spanned::Spanned, Expr};
  8. #[derive(Default, Debug)]
  9. pub struct Buffer {
  10. pub src: Vec<String>,
  11. pub cached_formats: HashMap<Location, String>,
  12. pub buf: String,
  13. pub indent: usize,
  14. pub comments: VecDeque<usize>,
  15. }
  16. #[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
  17. pub struct Location {
  18. pub line: usize,
  19. pub col: usize,
  20. }
  21. impl Location {
  22. pub fn new(start: LineColumn) -> Self {
  23. Self {
  24. line: start.line,
  25. col: start.column,
  26. }
  27. }
  28. }
  29. impl Buffer {
  30. // Create a new line and tab it to the current tab level
  31. pub fn tabbed_line(&mut self) -> Result {
  32. self.new_line()?;
  33. self.tab()
  34. }
  35. // Create a new line and tab it to the current tab level
  36. pub fn indented_tabbed_line(&mut self) -> Result {
  37. self.new_line()?;
  38. self.indented_tab()
  39. }
  40. pub fn tab(&mut self) -> Result {
  41. self.write_tabs(self.indent)
  42. }
  43. pub fn indented_tab(&mut self) -> Result {
  44. self.write_tabs(self.indent + 1)
  45. }
  46. pub fn write_tabs(&mut self, num: usize) -> std::fmt::Result {
  47. for _ in 0..num {
  48. write!(self.buf, " ")?
  49. }
  50. Ok(())
  51. }
  52. pub fn new_line(&mut self) -> Result {
  53. writeln!(self.buf)
  54. }
  55. // Expects to be written directly into place
  56. pub fn write_ident(&mut self, node: &BodyNode) -> Result {
  57. match node {
  58. BodyNode::Element(el) => self.write_element(el),
  59. BodyNode::Component(component) => self.write_component(component),
  60. BodyNode::Text(text) => self.write_text(text),
  61. BodyNode::RawExpr(exp) => self.write_raw_expr(exp),
  62. }
  63. }
  64. pub fn write_text(&mut self, text: &syn::LitStr) -> Result {
  65. write!(self.buf, "\"{}\"", text.value())
  66. }
  67. pub fn consume(self) -> Option<String> {
  68. Some(self.buf)
  69. }
  70. pub fn write_comments(&mut self, child: Span) -> Result {
  71. // collect all comments upwards
  72. let start = child.start();
  73. let line_start = start.line - 1;
  74. for (id, line) in self.src[..line_start].iter().enumerate().rev() {
  75. if line.trim().starts_with("//") || line.is_empty() {
  76. if id != 0 {
  77. self.comments.push_front(id);
  78. }
  79. } else {
  80. break;
  81. }
  82. }
  83. let mut last_was_empty = false;
  84. while let Some(comment_line) = self.comments.pop_front() {
  85. let line = &self.src[comment_line];
  86. if line.is_empty() {
  87. if !last_was_empty {
  88. self.new_line()?;
  89. }
  90. last_was_empty = true;
  91. } else {
  92. last_was_empty = false;
  93. self.tabbed_line()?;
  94. write!(self.buf, "{}", self.src[comment_line].trim())?;
  95. }
  96. }
  97. Ok(())
  98. }
  99. // Push out the indent level and write each component, line by line
  100. pub fn write_body_indented(&mut self, children: &[BodyNode]) -> Result {
  101. self.indent += 1;
  102. self.write_body_no_indent(children)?;
  103. self.indent -= 1;
  104. Ok(())
  105. }
  106. pub fn write_body_no_indent(&mut self, children: &[BodyNode]) -> Result {
  107. let last_child = children.len();
  108. for (idx, child) in children.iter().enumerate() {
  109. match child {
  110. // check if the expr is a short
  111. BodyNode::RawExpr(_) => {
  112. self.tabbed_line()?;
  113. self.write_ident(child)?;
  114. if idx != last_child - 1 {
  115. write!(self.buf, ",")?;
  116. }
  117. }
  118. _ => {
  119. if self.current_span_is_primary(child.span()) {
  120. self.write_comments(child.span())?;
  121. }
  122. self.tabbed_line()?;
  123. self.write_ident(child)?;
  124. }
  125. }
  126. }
  127. Ok(())
  128. }
  129. pub(crate) fn is_short_attrs(&mut self, attributes: &[ElementAttrNamed]) -> usize {
  130. let mut total = 0;
  131. for attr in attributes {
  132. if self.current_span_is_primary(attr.attr.flart()) {
  133. 'line: for line in self.src[..attr.attr.flart().start().line - 1].iter().rev() {
  134. match (line.trim().starts_with("//"), line.is_empty()) {
  135. (true, _) => return 100000,
  136. (_, true) => continue 'line,
  137. _ => break 'line,
  138. }
  139. }
  140. }
  141. total += match &attr.attr {
  142. ElementAttr::AttrText { value, name } => {
  143. value.value().len() + name.span().line_length() + 3
  144. }
  145. ElementAttr::AttrExpression { name, value } => {
  146. value.span().line_length() + name.span().line_length() + 3
  147. }
  148. ElementAttr::CustomAttrText { value, name } => {
  149. value.value().len() + name.value().len() + 3
  150. }
  151. ElementAttr::CustomAttrExpression { name, value } => {
  152. name.value().len() + value.span().line_length() + 3
  153. }
  154. ElementAttr::EventTokens { tokens, name } => {
  155. let location = Location::new(tokens.span().start());
  156. let len = if let std::collections::hash_map::Entry::Vacant(e) =
  157. self.cached_formats.entry(location)
  158. {
  159. let formatted = prettyplease::unparse_expr(tokens);
  160. let len = if formatted.contains('\n') {
  161. 10000
  162. } else {
  163. formatted.len()
  164. };
  165. e.insert(formatted);
  166. len
  167. } else {
  168. self.cached_formats[&location].len()
  169. };
  170. len + name.span().line_length() + 3
  171. }
  172. };
  173. }
  174. total
  175. }
  176. pub fn retrieve_formatted_expr(&mut self, expr: &Expr) -> &str {
  177. self.cached_formats
  178. .entry(Location::new(expr.span().start()))
  179. .or_insert_with(|| prettyplease::unparse_expr(expr))
  180. .as_str()
  181. }
  182. }
  183. trait SpanLength {
  184. fn line_length(&self) -> usize;
  185. }
  186. impl SpanLength for Span {
  187. fn line_length(&self) -> usize {
  188. self.end().line - self.start().line
  189. }
  190. }