buffer.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. use std::{
  2. collections::{HashMap, VecDeque},
  3. fmt::{Result, Write},
  4. };
  5. use dioxus_rsx::{BodyNode, ElementAttr, ElementAttrNamed, IfmtInput};
  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::DynamicText(text) => self.write_text(text),
  61. BodyNode::RawExpr(exp) => self.write_raw_expr(exp),
  62. }
  63. }
  64. pub fn write_text(&mut self, text: &IfmtInput) -> Result {
  65. write!(self.buf, "\"{}\"", text.source.as_ref().unwrap().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. for child in children {
  108. // Exprs handle their own indenting/line breaks
  109. if !matches!(child, BodyNode::RawExpr(_)) {
  110. if self.current_span_is_primary(child.span()) {
  111. self.write_comments(child.span())?;
  112. }
  113. self.tabbed_line()?;
  114. }
  115. self.write_ident(child)?;
  116. }
  117. Ok(())
  118. }
  119. pub(crate) fn is_short_attrs(&mut self, attributes: &[ElementAttrNamed]) -> usize {
  120. let mut total = 0;
  121. for attr in attributes {
  122. if self.current_span_is_primary(attr.attr.flart()) {
  123. 'line: for line in self.src[..attr.attr.flart().start().line - 1].iter().rev() {
  124. match (line.trim().starts_with("//"), line.is_empty()) {
  125. (true, _) => return 100000,
  126. (_, true) => continue 'line,
  127. _ => break 'line,
  128. }
  129. }
  130. }
  131. total += match &attr.attr {
  132. ElementAttr::AttrText { value, name } => {
  133. value.source.as_ref().unwrap().value().len() + name.span().line_length() + 3
  134. }
  135. ElementAttr::AttrExpression { name, value } => {
  136. value.span().line_length() + name.span().line_length() + 3
  137. }
  138. ElementAttr::CustomAttrText { value, name } => {
  139. value.source.as_ref().unwrap().value().len() + name.value().len() + 3
  140. }
  141. ElementAttr::CustomAttrExpression { name, value } => {
  142. name.value().len() + value.span().line_length() + 3
  143. }
  144. ElementAttr::EventTokens { tokens, name } => {
  145. let location = Location::new(tokens.span().start());
  146. let len = if let std::collections::hash_map::Entry::Vacant(e) =
  147. self.cached_formats.entry(location)
  148. {
  149. let formatted = prettyplease::unparse_expr(tokens);
  150. let len = if formatted.contains('\n') {
  151. 10000
  152. } else {
  153. formatted.len()
  154. };
  155. e.insert(formatted);
  156. len
  157. } else {
  158. self.cached_formats[&location].len()
  159. };
  160. len + name.span().line_length() + 3
  161. }
  162. };
  163. }
  164. total
  165. }
  166. pub fn retrieve_formatted_expr(&mut self, expr: &Expr) -> &str {
  167. self.cached_formats
  168. .entry(Location::new(expr.span().start()))
  169. .or_insert_with(|| prettyplease::unparse_expr(expr))
  170. .as_str()
  171. }
  172. }
  173. trait SpanLength {
  174. fn line_length(&self) -> usize;
  175. }
  176. impl SpanLength for Span {
  177. fn line_length(&self) -> usize {
  178. self.end().line - self.start().line
  179. }
  180. }