Skip to content

feat: add support for tagged documents with transparent rendering#12

Draft
Enter-tainer wants to merge 1 commit intomainfrom
mgt/tag
Draft

feat: add support for tagged documents with transparent rendering#12
Enter-tainer wants to merge 1 commit intomainfrom
mgt/tag

Conversation

@Enter-tainer
Copy link

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 a powerful new feature that allows associating arbitrary u32 tags with parts of a document. These tags are designed to be transparent during standard rendering, meaning they do not alter the visual output. However, they provide hooks for custom Render implementations to react to these tags, enabling advanced use cases like syntax highlighting or semantic analysis during the rendering process. Additionally, a utility for debugging these tagged documents has been included, allowing developers to inspect the tag structure without affecting the normal debug representation.

Highlights

  • New Doc::Tagged Variant: Introduced a new Tagged variant to the Doc enum, allowing documents to be associated with a u32 identifier.
  • Tagging API: Added tagged method to DocAllocator and tag method to DocBuilder for easily creating and applying transparent tags to documents.
  • Render Trait Callbacks: Extended the Render trait with on_tag_enter and on_tag_exit methods, enabling custom renderers to react to tagged document boundaries.
  • Rendering Engine Updates: Modified the core rendering logic in src/render/fit.rs to correctly process Doc::Tagged variants and invoke the new Render trait callbacks.
  • Debug Tagging Utility: Added a new module src/debug_tags.rs with debug_with_tag_ranges to extract tag information from the debug representation of documents, which keeps tags transparent in standard debug output.

🧠 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
  • src/alloc.rs
    • Added a new tagged method to the DocAllocator trait for allocating documents with an attached transparent tag.
  • src/builder.rs
    • Implemented a tag method on DocBuilder to attach a transparent u32 tag to an existing document.
  • src/debug_tags.rs
    • Added a new module containing DebugTagRange struct and debug_with_tag_ranges function to render debug IR text and collect output ranges for tagged text leaves.
  • src/doc.rs
    • Introduced the Tagged(u32, T) variant to the Doc enum to represent documents with an associated tag.
    • Updated the fmt::Debug implementation for Doc to ensure Tagged documents are transparent in debug output.
  • src/lib.rs
    • Exported the new debug_tags module and its public items, DebugTagRange and debug_with_tag_ranges.
  • src/render.rs
    • Extended the Render trait with on_tag_enter and on_tag_exit methods, which are called when entering and exiting a tagged document respectively.
  • src/render/fit.rs
    • Refactored the internal Cmd struct to use a new CmdKind enum, allowing it to represent both document nodes and tag exit events.
    • Modified the rendering loop to handle Doc::Tagged by calling on_tag_enter and pushing a TagExit command onto the stack.
    • Updated various document processing branches to correctly handle the new CmdKind enum.
  • tests/tagged.rs
    • Added a new test file to verify the functionality of tagged documents.
    • Included tests to confirm that tagged documents render identically to untagged documents.
    • Added tests to ensure that tagged documents maintain transparency in debug output.
    • Implemented tests to verify that on_tag_enter and on_tag_exit callbacks are emitted correctly and in a balanced manner during rendering.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces support for tagged documents, which is a great feature for enabling more advanced tooling on top of the pretty-printer. The implementation is well-structured, with changes to the core Doc enum, the rendering engine, and the addition of helper methods. The inclusion of tests for the new functionality is also very good. I've found one bug in the new debug_with_tag_ranges utility function that could lead to incorrect results in some cases. I've also suggested a small refactoring in the rendering logic to improve readability. Overall, this is a solid contribution.

Comment on lines +15 to +38
pub fn debug_with_tag_ranges<'a, T>(doc: &Doc<'a, T>) -> (String, Vec<DebugTagRange>)
where
T: DocPtr<'a> + fmt::Debug,
{
let text = format!("{doc:#?}");
let mut tagged_tokens = Vec::new();
collect_tagged_tokens(doc, None, &mut tagged_tokens);

let mut cursor = 0;
let mut ranges = Vec::new();
for (tag, token) in tagged_tokens {
if token.is_empty() {
continue;
}
if let Some(found) = text[cursor..].find(&token) {
let start = cursor + found;
let end = start + token.len();
ranges.push(DebugTagRange { tag, start, end });
cursor = end;
}
}

(text, ranges)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The logic in debug_with_tag_ranges for finding character ranges of tagged documents is not robust. It relies on a string search (find) within the complete debug output, which can produce incorrect ranges if the same text appears in both tagged and untagged contexts.

For example, for a document like arena.text("a") + arena.text("a").tag(1), the debug output will contain two instances of "a". The search for the tagged "a" might incorrectly match the first, untagged instance, leading to a wrong range.

To fix this, you could consider a more robust approach, such as having collect_tagged_tokens also account for untagged text segments to ensure the search is performed on the correct parts of the string, or by modifying the debug printing logic to directly output range information.

Comment on lines +101 to +107
let CmdKind::Doc(doc) = cmd.kind else {
let CmdKind::TagExit(id) = cmd.kind else {
unreachable!();
};
out.on_tag_exit(id)?;
break;
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The nested let-else construct here for handling CmdKind is functionally correct but can be a bit hard to follow. Using a match statement would make the intent clearer and is a more idiomatic way to handle different enum variants in Rust. This would improve code readability and maintainability.

Suggested change
let CmdKind::Doc(doc) = cmd.kind else {
let CmdKind::TagExit(id) = cmd.kind else {
unreachable!();
};
out.on_tag_exit(id)?;
break;
};
let doc = match cmd.kind {
CmdKind::Doc(doc) => doc,
CmdKind::TagExit(id) => {
out.on_tag_exit(id)?;
break;
}
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant