oak_handlebars/builder/
mod.rs1use crate::{HandlebarsParser, ast::*, kind::HandlebarsSyntaxKind, language::HandlebarsLanguage};
2use core::range::Range;
3use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, OakError, Parser, RedNode, RedTree, SourceText, TextEdit, source::Source};
4
5#[derive(Clone)]
7pub struct HandlebarsBuilder<'config> {
8 config: &'config HandlebarsLanguage,
9}
10
11impl<'config> HandlebarsBuilder<'config> {
12 pub fn new(config: &'config HandlebarsLanguage) -> Self {
13 Self { config }
14 }
15}
16
17impl<'config> Builder<HandlebarsLanguage> for HandlebarsBuilder<'config> {
18 fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<HandlebarsLanguage>) -> OakDiagnostics<HandlebarsRoot> {
19 let parser = HandlebarsParser::new(self.config);
20
21 let mut parse_cache = oak_core::parser::session::ParseSession::<HandlebarsLanguage>::default();
22 let parse_result = parser.parse(source, edits, &mut parse_cache);
23
24 match parse_result.result {
25 Ok(green_tree) => {
26 let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
27 match self.build_root(green_tree, &source_text) {
28 Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
29 Err(build_error) => {
30 let mut diagnostics = parse_result.diagnostics;
31 diagnostics.push(build_error.clone());
32 OakDiagnostics { result: Err(build_error), diagnostics }
33 }
34 }
35 }
36 Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
37 }
38 }
39}
40
41impl<'config> HandlebarsBuilder<'config> {
42 pub(crate) fn build_root<'a>(&self, green_tree: &'a GreenNode<'a, HandlebarsLanguage>, source: &SourceText) -> Result<HandlebarsRoot, OakError> {
43 let red_root = RedNode::new(green_tree, 0);
44 let mut nodes = Vec::new();
45
46 for child in red_root.children() {
47 if let RedTree::Node(n) = child {
48 nodes.push(self.build_node(n, source)?);
49 }
50 else if let RedTree::Leaf(t) = child {
51 if t.kind == HandlebarsSyntaxKind::Content {
52 nodes.push(TemplateNode::Content(Content { text: text(source, t.span.clone().into()), span: t.span.clone().into() }));
53 }
54 }
55 }
56 Ok(HandlebarsRoot { nodes })
57 }
58
59 fn build_node(&self, node: RedNode<HandlebarsLanguage>, source: &SourceText) -> Result<TemplateNode, OakError> {
60 match node.green.kind {
61 HandlebarsSyntaxKind::Mustache => {
62 Ok(TemplateNode::Mustache(Mustache { expression: Expression::Path(text(source, node.span())), is_unescaped: false, span: node.span() }))
64 }
65 HandlebarsSyntaxKind::Block => Ok(TemplateNode::Block(Block { name: "todo".to_string(), params: Vec::new(), body: Vec::new(), inverse: None, span: node.span() })),
66 HandlebarsSyntaxKind::CommentNode => Ok(TemplateNode::Comment(Comment { text: text(source, node.span()), span: node.span() })),
67 _ => Ok(TemplateNode::Content(Content { text: text(source, node.span()), span: node.span() })),
68 }
69 }
70}
71
72fn text(source: &SourceText, range: Range<usize>) -> String {
73 source.get_text_in(range).to_string()
74}