//! IDE consistency: autocomplete, hover, and jump must read the pristine //! synth even after a render ran error recovery. The regression this suite //! pins down: recovery used to mark up the one shared synth file, which broke //! property access (`integral.triple`) and hover until the next clean sync. use crate::{ bindings::TypstJump, tests::{fixtures, harness}, }; /// A page with one undefined math ident (recovery marks it red) and two /// completion targets after it: a plain math ident and a property access. fn recovered_page() -> fixtures::Fixture { fixtures::get("broken", "recovered_page") } fn utf16_offset(text: &str, byte_offset: usize) -> usize { text[..byte_offset].chars().map(char::len_utf16).sum() } /// Completes a prefix and asserts both the label and the `from` offset /// CodeMirror uses to filter the option list. `expected_from` is the byte /// offset the replacement should start at: the start of a plain ident, or the /// start of the field part for property access. fn assert_completion( state: &mut crate::state::TypstState, id: &crate::bindings::TypstFileId, text: &str, prefix: &str, expected_from: usize, label: &str, ) { let start = text.find(prefix).expect("prefix missing from fixture"); let cursor = utf16_offset(text, start + prefix.len()); let expected_from = utf16_offset(text, expected_from); let result = state .autocomplete_at(id, cursor, true) .expect("autocomplete returned nothing after recovery"); let labels = result .completions .iter() .map(|completion| completion.label.clone()) .collect::>(); assert!( labels.iter().any(|candidate| candidate == label), "missing `{label}` for `{prefix}`: {labels:?}", ); assert_eq!( result.offset, expected_from, "completion start offset for `{prefix}`", ); } #[test] fn clean_math_field_completion_works() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; } let text = "$ qu + integral.triple $"; let mut state = harness::state(); let id = harness::page(&mut state, "ide_autocomplete_clean"); let _ = harness::compile(&mut state, &id, text); let integral = text.find("integral").unwrap(); let field = integral + "integral.".len(); assert_completion( &mut state, &id, text, "qu", text.find("qu").unwrap(), "quad", ); assert_completion(&mut state, &id, text, "integral.", field, "triple"); assert_completion(&mut state, &id, text, "integral.tri", field, "triple"); } #[test] fn autocomplete_survives_recovered_render() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; } let page = recovered_page(); let text = &page.source; let mut state = harness::state(); let id = harness::page(&mut state, &page.name); let render = harness::compile(&mut state, &id, text); assert!(render.document.is_some()); assert!( render .diagnostics .iter() .any(|diagnostic| diagnostic.message.contains("unknown variable")), "fixture did not trigger the marking pass: {:#?}", render.diagnostics, ); let integral = text.find("integral").unwrap(); let field = integral + "integral.".len(); assert_completion( &mut state, &id, text, "qu", text.find("qu").unwrap(), "quad", ); assert_completion(&mut state, &id, text, "integral.", field, "triple"); assert_completion(&mut state, &id, text, "integral.tri", field, "triple"); } /// A completion query reads the synth source, and that source is otherwise only /// rebuilt as a side effect of a render. Write mode renders in a microtask, so /// its queries are current. Split and source mode render on a debounce, and /// CodeMirror's own activation delay is shorter than that, so a query arrived /// first and answered for the previous keystroke. The editor syncs the sources /// without rendering, which is what this covers. #[test] fn autocomplete_follows_a_synced_keystroke() { let before = "Before.\n\n#let alpha = 1\n\n#"; let after = "Before.\n\n#let alto = 1\n\n#"; let mut state = harness::state(); let id = harness::page(&mut state, "ide_source_sync"); // A render, which is what leaves the engine with `before`. let render = harness::compile(&mut state, &id, before); assert!(render.document.is_some()); let labels_at = |state: &mut crate::state::TypstState, text: &str| { let cursor = utf16_offset(text, text.len()); state .autocomplete_at(&id, cursor, true) .map(|result| { result .completions .iter() .map(|completion| completion.label.clone()) .collect::>() }) .unwrap_or_default() }; let labels = labels_at(&mut state, before); assert!( labels.iter().any(|label| label == "alpha"), "the rendered text should complete its own binding: {labels:?}", ); // The keystroke, with no render since. The engine's document still describes // `before`, so this is the case that used to answer for the old text. crate::source::sync_source_state(&id, after, "", crate::source::RenderTarget::Svg, &mut state); let labels = labels_at(&mut state, after); assert!( labels.iter().any(|label| label == "alto"), "the synced text did not complete its own binding: {labels:?}", ); assert!( !labels.iter().any(|label| label == "alpha"), "the query answered from the text before the keystroke: {labels:?}", ); } /// Syncing builds sources and nothing else. A render is what discovers a missing /// package or file, and the request protocol belongs to the render, so a sync /// that asked for them would resolve them on every keystroke. #[test] fn syncing_sources_requests_nothing() { let mut state = harness::state(); let id = harness::page(&mut state, "source_sync_requests"); let source = "#import \"@preview/absent:0.1.0\": x\n\n#let f = 1\n"; crate::source::sync_source_state( &id, source, "", crate::source::RenderTarget::Svg, &mut state, ); assert!( state.world.take_requests().is_empty(), "the sync asked for sources the render has not reached yet", ); } /// Field completion traces the expression, and a trace is a recompile of the /// note's source rather than a lookup in the last rendered document. So it reads /// whatever text the sources contain, which before a source sync was the text of the /// last render. Renaming the binding between the two texts decides the list, and /// the field name is a dictionary key so it cannot come from anywhere else. /// /// Every math function is also in the general math list, which is why completing /// `integral.tri` proves nothing on its own: `triple` turns up either way. #[test] fn field_completion_follows_a_synced_keystroke() { let before = "#let d = (oldname: 1)\n\n#d.\n"; let after = "#let d = (newname: 1)\n\n#d.\n"; let mut state = harness::state(); let id = harness::page(&mut state, "ide_field_sync"); let _ = harness::compile(&mut state, &id, before); let fields_at = |state: &mut crate::state::TypstState, text: &str| { let after_dot = text.find("#d.").unwrap() + "#d.".len(); let cursor = utf16_offset(text, after_dot); state .autocomplete_at(&id, cursor, true) .map(|result| { result .completions .iter() .map(|completion| completion.label.clone()) .collect::>() }) .unwrap_or_default() }; let fields = fields_at(&mut state, before); assert!( fields.iter().any(|label| label == "oldname"), "the rendered text should complete its own dictionary: {fields:?}", ); // The keystroke, with no render since. crate::source::sync_source_state(&id, after, "", crate::source::RenderTarget::Svg, &mut state); let fields = fields_at(&mut state, after); assert!( fields.iter().any(|label| label == "newname"), "the synced text did not complete its own dictionary: {fields:?}", ); assert!( !fields.iter().any(|label| label == "oldname"), "the trace recompiled the text before the keystroke: {fields:?}", ); } /// Click-to-source maps a point in the frames the preview is painting, so the /// document it reads has to be the one from that render. The editor's lint source /// compiles the same note for diagnostics, and it used to store its document in /// the same slot: in split mode the linter runs at 400ms against the preview's /// 160ms, so the slot ended up with a document from text nobody was looking /// at, and a click resolved against the wrong layout. #[test] fn a_diagnostics_pass_leaves_the_painted_document_alone() { let mut state = harness::state(); let id = harness::page(&mut state, "diagnostics_document"); let painted = "Before.\n\n= Heading\n\nAfter.\n"; harness::compile_svg(&mut state, &id, painted); let jump_at_paint = state.jump_paged_at(&id, 10.0, 10.0); assert!(jump_at_paint.is_some(), "the painted document is not there"); // What the lint source runs: the same recovery, no frame rendering, and no // document stored. let edited = "Before.\n\n= A much longer heading than the last one\n\nAfter.\n"; let _ = harness::compile(&mut state, &id, edited); assert!( state.jump_paged_at(&id, 10.0, 10.0).is_some(), "the diagnostics pass took the painted document with it", ); } #[test] fn hover_survives_recovered_render() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; } let page = recovered_page(); let text = &page.source; let mut state = harness::state(); let id = harness::page(&mut state, &page.name); let render = harness::compile(&mut state, &id, text); assert!(render.document.is_some()); let cursor = text.find("integral").unwrap() + 2; let tooltip = state.hover(&id, utf16_offset(text, cursor), 1); assert!(tooltip.is_some(), "hover returned nothing after recovery"); } #[test] fn jump_maps_both_sources_to_raw() { let page = recovered_page(); let text = &page.source; let mut state = harness::state(); let id = harness::page(&mut state, &page.name); let _ = harness::compile(&mut state, &id, text); let context = state.source_context_map.get(&id).unwrap(); let render_text = context .render_source(&state.world) .unwrap() .text() .to_string(); let synth_text = context .synth_source(&state.world) .unwrap() .text() .to_string(); let expected = utf16_offset(text, text.find("After").unwrap()); for (file_id, source_text) in [ (context.render_id, render_text), (context.synth_id, synth_text), ] { let position = source_text .find("After") .expect("fixture text missing from source"); let jump = TypstJump::from_mapped( typst_ide::Jump::File(file_id, position), context, &state.world, ); match jump { Some(TypstJump::File { position }) => assert_eq!(position, expected), other => panic!("jump did not map to a raw file position: {other:?}"), } } } #[test] fn jump_maps_repaired_positions_to_raw() { let fixture = fixtures::get("broken", "unclosed_dollar"); let source = &fixture.source; let mut state = harness::state(); let id = harness::page(&mut state, &fixture.name); let _ = harness::compile(&mut state, &id, source); let context = state.source_context_map.get(&id).unwrap(); let render_text = context .render_source(&state.world) .unwrap() .text() .to_string(); let position = render_text .find("After paragraph.") .expect("text missing from render source"); let expected = utf16_offset(source, source.find("After paragraph.").unwrap()); let jump = TypstJump::from_mapped( typst_ide::Jump::File(context.render_id, position), context, &state.world, ); match jump { Some(TypstJump::File { position }) => assert_eq!(position, expected), other => panic!("jump did not map to a raw file position: {other:?}"), } } /// Runs a completion query with the cursor at the end of `marker`, which /// includes the text already typed at the completion site. fn query_result_data( text: &str, marker: &str, data: crate::complete::CompletionData, ) -> Option<(usize, Vec)> { let start = text.find(marker).expect("marker missing from source"); let cursor = utf16_offset(text, start + marker.len()); let mut state = harness::state(); let id = harness::page(&mut state, "query_completion"); state.set_completion_data(data); let _ = harness::compile(&mut state, &id, text); state.autocomplete_at(&id, cursor, true).map(|result| { let completions = result.completions.into_vec(); (result.offset, completions) }) } fn query_result( text: &str, marker: &str, ) -> Option<(usize, Vec)> { query_result_data(text, marker, crate::complete::CompletionData::default()) } fn query_completion_data( text: &str, marker: &str, data: crate::complete::CompletionData, ) -> Option<(usize, Vec<(String, Option)>)> { query_result_data(text, marker, data).map(|(offset, completions)| { ( offset, completions .iter() .map(|completion| (completion.label.clone(), completion.apply.clone())) .collect(), ) }) } fn query_completion(text: &str, marker: &str) -> Option<(usize, Vec<(String, Option)>)> { query_completion_data(text, marker, crate::complete::CompletionData::default()) } fn sample_completions() -> crate::complete::CompletionData { use crate::complete::{CompletionData, CompletionItem}; CompletionData { pages: vec![ CompletionItem { label: String::from("Welcome"), value: String::from("page-welcome"), detail: Some(String::from("pages/welcome.typ")), }, CompletionItem { label: String::from("Meeting notes"), value: String::from("page-notes"), detail: None, }, ], categories: vec![CompletionItem { label: String::from("Work"), value: String::from("cat-work"), detail: None, }], months: vec![CompletionItem { label: String::from("2026-11"), value: String::from("2026-11"), detail: None, }], dates: vec![CompletionItem { label: String::from("2026-11-23"), value: String::from("2026-11-23"), detail: None, }], plugins: vec![CompletionItem { label: String::from("Calendar"), value: String::from("instance-calendar"), detail: Some(String::from("calendar")), }], } } fn labels(options: &[(String, Option)]) -> Vec<&str> { options.iter().map(|(label, _)| label.as_str()).collect() } #[test] fn query_kind_completions_fill_a_closed_string() { let text = "Before.\n\n#typbase.query(\"pa\")"; let (offset, options) = query_completion(text, "#typbase.query(\"pa").expect("completion missing"); let content = utf16_offset(text, text.find("\"pa").unwrap() + 1); assert_eq!(offset, content, "replacement starts inside the quote"); assert_eq!(options.len(), 8, "every query kind is offered"); assert!( options.contains(&(String::from("pages"), Some(String::from("pages")))), "pages completes without doubling the existing closing quote: {options:?}", ); } #[test] fn query_kind_completions_close_an_unfinished_string() { let text = "Before.\n\n#typbase.query(\"pa)"; let (offset, options) = query_completion(text, "#typbase.query(\"pa").expect("completion missing"); let content = utf16_offset(text, text.find("\"pa").unwrap() + 1); assert_eq!(offset, content); assert!( options.contains(&(String::from("pages"), Some(String::from("pages\"")))), "the replacement supplies the missing quote: {options:?}", ); } #[test] fn query_kind_completions_at_an_empty_argument() { let text = "Before.\n\n#typbase.query()"; let marker = "#typbase.query("; let cursor = utf16_offset(text, text.find(marker).unwrap() + marker.len()); let (offset, options) = query_completion(text, marker).expect("completion missing"); assert_eq!(offset, cursor, "an empty slot replaces nothing"); assert!( options.contains(&(String::from("pages"), Some(String::from("\"pages\"")))), "an empty slot gets both quotes: {options:?}", ); } #[test] fn query_filter_completions_follow_the_kind() { let pages = "Before.\n\n#typbase.query(\"pages\", filter: \"by\")"; let (_, pages) = query_completion(pages, "#typbase.query(\"pages\", filter: \"by").expect("completion"); assert_eq!(labels(&pages), ["by-id/", "by-category/"]); let daily = "Before.\n\n#typbase.query(\"daily\", filter: \"by\")"; let (_, daily) = query_completion(daily, "#typbase.query(\"daily\", filter: \"by").expect("completion"); assert_eq!(labels(&daily), ["by-month/"]); let config = "Before.\n\n#typbase.query(\"config\", filter: \"by\")"; let (_, config) = query_completion(config, "#typbase.query(\"config\", filter: \"by") .expect("completion call"); assert!(config.is_empty(), "config takes no filter: {config:?}"); } #[test] fn query_filter_completions_at_an_empty_value() { let text = "Before.\n\n#typbase.query(\"pages\", filter: )"; let (_, options) = query_completion(text, "#typbase.query(\"pages\", filter: ").expect("completion missing"); assert!( options.contains(&(String::from("by-id/"), Some(String::from("\"by-id/${}\"")))), "an empty filter slot gets the quotes and a value placeholder: {options:?}", ); } #[test] fn query_completion_leaves_other_strings_alone() { let text = "Before.\n\n#text(\"pa\")"; let (_, options) = query_completion(text, "#text(\"pa").expect("completion call"); assert!( !labels(&options).contains(&"pages"), "a plain text call must not get query kinds: {options:?}", ); } #[test] fn query_parameter_completion_still_works() { let text = "Before.\n\n#typbase.query(\"pages\", )"; let (_, completions) = query_result(text, "#typbase.query(\"pages\", ").expect("completion missing"); let filter = completions .iter() .find(|completion| completion.label == "filter") .expect("the second slot still offers the filter parameter"); assert!( filter .detail .as_deref() .is_some_and(|detail| detail.contains("Optional filter")), "the parameter detail comes from the stdlib doc comment: {:?}", filter.detail, ); } #[test] fn page_link_completions_offer_pages_and_none() { let text = "Before.\n\n#typbase.page-link()"; let marker = "#typbase.page-link("; let cursor = utf16_offset(text, text.find(marker).unwrap() + marker.len()); let (offset, options) = query_completion_data(text, marker, sample_completions()).expect("completion missing"); assert_eq!(offset, cursor); assert!( options.contains(&( String::from("Welcome"), Some(String::from("\"page-welcome\"")) )), "a page id is inserted as a quoted string: {options:?}", ); assert!( options.contains(&(String::from("none"), Some(String::from("none")))), "the empty target stays unquoted: {options:?}", ); } #[test] fn page_link_completions_replace_a_typed_string() { let text = "Before.\n\n#typbase.page-link(\"Wel\")"; let (offset, options) = query_completion_data(text, "#typbase.page-link(\"Wel", sample_completions()) .expect("completion missing"); let content = utf16_offset(text, text.find("\"Wel").unwrap() + 1); assert_eq!(offset, content, "replacement starts inside the quote"); assert!( options.contains(&(String::from("Welcome"), Some(String::from("page-welcome")))), "the closing quote is kept: {options:?}", ); assert!( !options.iter().any(|(label, _)| label == "none"), "none is a value, not a string: {options:?}", ); } #[test] fn embed_completions_use_host_pages() { let text = "Before.\n\n#typbase.embed(\"Wel\")"; let (_, options) = query_completion_data(text, "#typbase.embed(\"Wel", sample_completions()) .expect("completion missing"); assert!( options.contains(&(String::from("Welcome"), Some(String::from("page-welcome")))), "embed completes page ids: {options:?}", ); } #[test] fn page_link_body_does_not_complete_page_ids() { let text = "Before.\n\n#typbase.page-link(\"page-welcome\", \"Re\")"; let (_, options) = query_completion_data( text, "#typbase.page-link(\"page-welcome\", \"Re", sample_completions(), ) .expect("completion call"); assert!( !options.iter().any(|(label, _)| label == "Welcome"), "body text is not a page id: {options:?}", ); } #[test] fn query_filter_values_use_host_data() { let by_id = "Before.\n\n#typbase.query(\"pages\", filter: \"by-id/\")"; let (offset, options) = query_completion_data( by_id, "#typbase.query(\"pages\", filter: \"by-id/", sample_completions(), ) .expect("completion missing"); let value_start = utf16_offset(by_id, by_id.find("by-id/").unwrap() + "by-id/".len()); assert_eq!(offset, value_start, "the page id replaces after the slash"); assert!( options.contains(&(String::from("Welcome"), Some(String::from("page-welcome")))), "page ids follow by-id: {options:?}", ); let by_category = "Before.\n\n#typbase.query(\"pages\", filter: \"by-category/\")"; let (_, options) = query_completion_data( by_category, "#typbase.query(\"pages\", filter: \"by-category/", sample_completions(), ) .expect("completion missing"); assert!( options.contains(&(String::from("Work"), Some(String::from("cat-work")))), "categories follow by-category: {options:?}", ); let by_month = "Before.\n\n#typbase.query(\"daily\", filter: \"by-month/\")"; let (_, options) = query_completion_data( by_month, "#typbase.query(\"daily\", filter: \"by-month/", sample_completions(), ) .expect("completion missing"); assert!( options.contains(&(String::from("2026-11"), Some(String::from("2026-11")))), "months follow by-month: {options:?}", ); let sections = "Before.\n\n#typbase.query(\"sections\", filter: \"Wel\")"; let (_, options) = query_completion_data( sections, "#typbase.query(\"sections\", filter: \"Wel", sample_completions(), ) .expect("completion missing"); assert!( options.contains(&(String::from("Welcome"), Some(String::from("page-welcome")))), "sections takes a page id: {options:?}", ); let content = "Before.\n\n#typbase.query(\"content\", filter: \"2026\")"; let (_, options) = query_completion_data( content, "#typbase.query(\"content\", filter: \"2026", sample_completions(), ) .expect("completion missing"); assert!( options.contains(&(String::from("2026-11-23"), Some(String::from("2026-11-23")))), "content accepts a daily date: {options:?}", ); let backlinks = "Before.\n\n#typbase.query(\"backlinks\", filter: \"Wel\")"; let (_, options) = query_completion_data( backlinks, "#typbase.query(\"backlinks\", filter: \"Wel", sample_completions(), ) .expect("completion missing"); assert!( options.contains(&(String::from("Welcome"), Some(String::from("page-welcome")))), "backlinks takes a page id: {options:?}", ); let plugin_data = "Before.\n\n#typbase.query(\"plugin-data\", filter: \"Cal\")"; let (_, options) = query_completion_data( plugin_data, "#typbase.query(\"plugin-data\", filter: \"Cal", sample_completions(), ) .expect("completion missing"); assert!( options.contains(&( String::from("Calendar"), Some(String::from("instance-calendar")) )), "plugin-data takes an instance id: {options:?}", ); } #[test] fn query_filter_prefix_reopens_at_the_value() { let text = "Before.\n\n#typbase.query(\"pages\", filter: \"by\")"; let (_, options) = query_completion(text, "#typbase.query(\"pages\", filter: \"by") .expect("completion missing"); assert!( options.contains(&(String::from("by-id/"), Some(String::from("by-id/${}")))), "the prefix leaves a placeholder for the value: {options:?}", ); }