diff options
Diffstat (limited to 'tests/integration/lsp_tests.rs')
-rw-r--r-- | tests/integration/lsp_tests.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index 84f334992..47fefeafe 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -12698,3 +12698,87 @@ fn lsp_ts_code_fix_any_param() { panic!("failed to find 'Infer parameter types from usage' fix in fixes: {fixes:#?}"); } + +#[test] +fn lsp_semantic_token_caching() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir().path(); + + let mut client: LspClient = context + .new_lsp_command() + .collect_perf() + .set_root_dir(temp_dir.clone()) + .build(); + client.initialize_default(); + + let a = source_file( + temp_dir.join("a.ts"), + r#" + export const a = 1; + export const b = 2; + export const bar = () => "bar"; + function foo(fun: (number, number, number) => number, c: number) { + const double = (x) => x * 2; + return fun(double(a), b, c); + }"#, + ); + + client.did_open_file(&a); + + // requesting a range won't cache the tokens, so this will + // be computed + let res = client.write_request( + "textDocument/semanticTokens/range", + json!({ + "textDocument": a.identifier(), + "range": { + "start": a.range_of("const bar").start, + "end": a.range_of("}").end, + } + }), + ); + + assert_eq!( + client + .perf() + .measure_count("tsc.request.getEncodedSemanticClassifications"), + 1, + ); + + // requesting for the full doc should compute and cache the tokens + let _full = client.write_request( + "textDocument/semanticTokens/full", + json!({ + "textDocument": a.identifier(), + }), + ); + + assert_eq!( + client + .perf() + .measure_count("tsc.request.getEncodedSemanticClassifications"), + 2, + ); + + // use the cached tokens + let res_cached = client.write_request( + "textDocument/semanticTokens/range", + json!({ + "textDocument": a.identifier(), + "range": { + "start": a.range_of("const bar").start, + "end": a.range_of("}").end, + } + }), + ); + + // make sure we actually used the cache + assert_eq!( + client + .perf() + .measure_count("tsc.request.getEncodedSemanticClassifications"), + 2, + ); + + assert_eq!(res, res_cached); +} |