@@ -6,14 +6,13 @@ use std::{
66
77use futures:: StreamExt ;
88use glass_easel_template_compiler:: {
9- parse:: { ParseError , ParseErrorKind , ParseErrorLevel , Template } ,
10- TmplGroup ,
9+ parse:: { ParseError , ParseErrorKind , ParseErrorLevel , Template } , TmplConvertedExpr , TmplGroup
1110} ;
1211use lsp_types:: { Diagnostic , DiagnosticSeverity , Position , Range } ;
1312use tokio:: sync:: Mutex as AsyncMutex ;
1413
1514use super :: { FileLang , ServerContextOptions } ;
16- use crate :: wxss:: { self , StyleSheet } ;
15+ use crate :: wxss:: { self , Location , StyleSheet } ;
1716
1817#[ derive( Debug ) ]
1918pub ( crate ) struct FileContentMetadata {
@@ -110,6 +109,7 @@ pub(crate) struct Project {
110109 file_contents : HashMap < PathBuf , FileContentMetadata > ,
111110 json_config_map : HashMap < PathBuf , JsonConfig > ,
112111 template_group : TmplGroup ,
112+ cached_wxml_converted_expr : HashMap < String , TmplConvertedExpr > ,
113113 style_sheet_map : HashMap < PathBuf , StyleSheet > ,
114114 enable_other_ss : bool ,
115115}
@@ -121,6 +121,7 @@ impl Default for Project {
121121 file_contents : HashMap :: new ( ) ,
122122 json_config_map : HashMap :: new ( ) ,
123123 template_group : TmplGroup :: new ( ) ,
124+ cached_wxml_converted_expr : HashMap :: new ( ) ,
124125 style_sheet_map : HashMap :: new ( ) ,
125126 enable_other_ss : false ,
126127 }
@@ -186,6 +187,7 @@ impl Project {
186187 file_contents : HashMap :: new ( ) ,
187188 json_config_map : HashMap :: new ( ) ,
188189 template_group : TmplGroup :: new ( ) ,
190+ cached_wxml_converted_expr : HashMap :: new ( ) ,
189191 style_sheet_map : HashMap :: new ( ) ,
190192 enable_other_ss : options. enable_other_ss ,
191193 }
@@ -521,6 +523,7 @@ impl Project {
521523 fn cleanup_wxml ( & mut self , abs_path : & Path ) -> anyhow:: Result < ( ) > {
522524 let tmpl_path = self . unix_rel_path_or_fallback ( & abs_path) ;
523525 self . template_group . remove_tmpl ( & tmpl_path) ;
526+ self . cached_wxml_converted_expr . remove ( & tmpl_path) ;
524527 self . file_contents . remove ( abs_path) ;
525528 Ok ( ( ) )
526529 }
@@ -557,6 +560,37 @@ impl Project {
557560 self . template_group . list_template_trees ( )
558561 }
559562
563+ pub ( crate ) fn wxml_converted_expr_release ( & mut self , abs_path : & Path ) -> bool {
564+ let tmpl_path = self . unix_rel_path_or_fallback ( & abs_path) ;
565+ self . cached_wxml_converted_expr . remove ( & tmpl_path) . is_some ( )
566+ }
567+
568+ pub ( crate ) fn wxml_converted_expr_code ( & mut self , abs_path : & Path , ts_env : & str ) -> anyhow:: Result < String > {
569+ let tmpl_path = self . unix_rel_path_or_fallback ( & abs_path) ;
570+ let expr = self . template_group . get_tmpl_converted_expr ( & tmpl_path, ts_env) ?;
571+ let code = expr. code ( ) . to_string ( ) ;
572+ self . cached_wxml_converted_expr . insert ( tmpl_path, expr) ;
573+ Ok ( code)
574+ }
575+
576+ pub ( crate ) fn wxml_converted_expr_get_source_location ( & self , abs_path : & Path , loc : Location ) -> Option < Location > {
577+ let tmpl_path = self . unix_rel_path_or_fallback ( & abs_path) ;
578+ self . cached_wxml_converted_expr
579+ . get ( & tmpl_path)
580+ . and_then ( |x| x. get_source_location ( loc) )
581+ }
582+
583+ pub ( crate ) fn wxml_converted_expr_get_token_at_source_position (
584+ & self ,
585+ abs_path : & Path ,
586+ pos : crate :: wxss:: Position ,
587+ ) -> Option < ( Location , crate :: wxss:: Position ) > {
588+ let tmpl_path = self . unix_rel_path_or_fallback ( & abs_path) ;
589+ self . cached_wxml_converted_expr
590+ . get ( & tmpl_path)
591+ . and_then ( |x| x. get_token_at_source_position ( pos) )
592+ }
593+
560594 pub ( crate ) fn for_each_json_config ( & self , mut f : impl FnMut ( & Path , & JsonConfig ) ) {
561595 for ( p, json_config) in self . json_config_map . iter ( ) {
562596 f ( p, json_config) ;
0 commit comments