Skip to content

Commit 3b733ba

Browse files
committed
optimize: reuse template compilation result for typescript
1 parent ca28e89 commit 3b733ba

File tree

12 files changed

+368
-56
lines changed

12 files changed

+368
-56
lines changed

Cargo.lock

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ compact_str = "0.8.0"
1616
cssparser = "0.34.0"
1717
cssparser-color = "0.2.0"
1818
futures = "0.3.31"
19-
glass-easel-template-compiler = "0.15.2"
19+
glass-easel-template-compiler = "0.15.3"
2020
itertools = "0.13.0"
2121
log = "0.4.22"
2222
lsp-server = "0.7.7"
@@ -29,3 +29,6 @@ swc_ecma_lexer = "23.0.0"
2929
tokio = { version = "1.41.0", features = ["fs", "macros", "rt", "sync", "time"] }
3030
tokio-stream = { version = "0.1.16", features = ["fs"] }
3131
toml = "0.8.19"
32+
33+
[patch.crates-io]
34+
glass-easel-template-compiler = { path = "/Users/lastleaf/work/wxweb/glass-easel/glass-easel-template-compiler" }

pnpm-lock.yaml

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context/project.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ use std::{
66

77
use futures::StreamExt;
88
use glass_easel_template_compiler::{
9-
parse::{ParseError, ParseErrorKind, ParseErrorLevel, Template},
10-
TmplGroup,
9+
parse::{ParseError, ParseErrorKind, ParseErrorLevel, Template}, TmplConvertedExpr, TmplGroup
1110
};
1211
use lsp_types::{Diagnostic, DiagnosticSeverity, Position, Range};
1312
use tokio::sync::Mutex as AsyncMutex;
1413

1514
use super::{FileLang, ServerContextOptions};
16-
use crate::wxss::{self, StyleSheet};
15+
use crate::wxss::{self, Location, StyleSheet};
1716

1817
#[derive(Debug)]
1918
pub(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);

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod reference;
1515
mod semantic;
1616
mod symbol;
1717
mod utils;
18+
mod wxml_ts;
1819
mod wxml_utils;
1920
mod wxs;
2021
mod wxss;
@@ -100,6 +101,7 @@ async fn handle_request(
100101
($name:expr, $f:path) => {
101102
if method.as_str() == $name {
102103
let params = serde_json::from_value(params).map_err(|err| {
104+
log::error!("Invalid params: {:?}", err);
103105
anyhow::Error::from(err).context(format!("Invalid params on {:?}", method))
104106
})?;
105107
let ret = $f(ctx, params).await?;
@@ -115,6 +117,10 @@ async fn handle_request(
115117

116118
// handlers for each method
117119
handler!("shutdown", cleanup);
120+
handler!("glassEaselAnalyzer/tmplConvertedExprRelease", wxml_ts::tmpl_converted_expr_release);
121+
handler!("glassEaselAnalyzer/tmplConvertedExprCode", wxml_ts::tmpl_converted_expr_code);
122+
handler!("glassEaselAnalyzer/tmplConvertedExprGetSourceLocation", wxml_ts::tmpl_converted_expr_get_source_location);
123+
handler!("glassEaselAnalyzer/tmplConvertedExprGetTokenAtSourcePosition", wxml_ts::tmpl_converted_expr_get_token_at_source_position);
118124
handler!("textDocument/foldingRange", folding::folding_range);
119125
handler!("textDocument/semanticTokens/full", semantic::tokens_full);
120126
handler!("textDocument/semanticTokens/range", semantic::tokens_range);

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub(crate) fn location_to_lsp_range(loc: &Location) -> lsp_types::Range {
9797
}
9898
}
9999

100-
pub(crate) fn _lsp_range_to_location(loc: &lsp_types::Range) -> Location {
100+
pub(crate) fn lsp_range_to_location(loc: &lsp_types::Range) -> Location {
101101
let start = Position {
102102
line: loc.start.line,
103103
utf16_col: loc.start.character,

0 commit comments

Comments
 (0)