feat: implement IR mapping functionality and integrate with formatter#473
feat: implement IR mapping functionality and integrate with formatter#473Enter-tainer wants to merge 1 commit intomasterfrom
Conversation
Summary of ChangesHello @Enter-tainer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces comprehensive Intermediate Representation (IR) mapping capabilities to the formatter. This new functionality allows for precise tracking of how segments of the original source code correspond to parts of the generated pretty-printed IR. The changes span the core formatting logic, the WebAssembly interface, and the playground frontend, enabling users to visualize these mappings, which is crucial for debugging and understanding the formatter's behavior. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request successfully implements IR mapping functionality, allowing for precise source-to-IR synchronization in the playground. The instrumentation of PrettyPrinter and the integration with prettyless's tagging system are well-executed. The remapping logic for indentation changes is clever and handles multi-byte characters correctly. However, there are a few critical and medium-level issues to address: a local path dependency in Cargo.toml that will break builds for others, a potential bug in line-ending handling for \r\n in the offset mapping logic, and an opportunity to optimize the joining of IR mappings by avoiding an unnecessary HashMap allocation.
| [patch.crates-io] | ||
| # tinymist-world = { git = "https://github.com/Myriad-Dreamin/tinymist", rev = "0f9f4644567236bbfcd1b21baaa4b8bdf224ecb3" } | ||
| # prettyless = { git = "https://github.com/typstyle-rs/prettyless", rev = "9afa5bdece2dbf14c54ade8bef46cd0828cb952b" } | ||
| prettyless = { path = "../prettyless" } |
There was a problem hiding this comment.
| let src_by_id: HashMap<u32, (&usize, &usize)> = src_atoms | ||
| .iter() | ||
| .map(|s| (s.atom_id, (&s.src_start, &s.src_end))) | ||
| .collect(); | ||
|
|
||
| let mut mapping: Vec<SpanMapping> = out_atoms | ||
| .iter() | ||
| .filter_map(|out| { | ||
| let (src_start, src_end) = src_by_id.get(&out.atom_id)?; | ||
| Some(SpanMapping { | ||
| src_start: **src_start, | ||
| src_end: **src_end, | ||
| out_start: out.out_start, | ||
| out_end: out.out_end, | ||
| }) | ||
| }) | ||
| .collect(); |
There was a problem hiding this comment.
The HashMap here is unnecessary and adds overhead. Since IrAtomRecorder allocates atom_ids sequentially starting from 0 and pushes them into src_atoms, the atom_id is guaranteed to be the index of the atom in the src_atoms slice. You can directly look up the source atom by index, avoiding the map construction and lookups.
let mut mapping: Vec<SpanMapping> = out_atoms
.iter()
.filter_map(|out| {
let src = src_atoms.get(out.atom_id as usize)?;
Some(SpanMapping {
src_start: src.src_start,
src_end: src.src_end,
out_start: out.out_start,
out_end: out.out_end,
})
})
.collect();| for line in text.lines() { | ||
| if !first { | ||
| // `lines()` splits on `\n`; preserve newline 1:1. | ||
| map[old_pos] = new_pos; | ||
| old_pos += 1; | ||
| new_pos += 1; | ||
| } |
There was a problem hiding this comment.
The build_indent_offset_map function assumes that line separators are always 1 byte long (i.e., \n). However, str::lines() also handles \r\n (2 bytes). If the input text contains \r\n, old_pos will drift and the mapping for subsequent lines will be incorrect. While the internal IR likely uses \n, it's safer to handle line endings robustly or explicitly normalize the input to ensure the mapping remains accurate across different environments.
Summary
Changes
Checklist
Before submitting, please ensure you've done the following:
Testing
Additional Notes