-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add support for tagged documents with transparent rendering #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Enter-tainer
wants to merge
1
commit into
main
Choose a base branch
from
mgt/tag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| use std::fmt; | ||
|
|
||
| use crate::{Doc, DocPtr, text::Text}; | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub struct DebugTagRange { | ||
| pub tag: u32, | ||
| pub start: usize, | ||
| pub end: usize, | ||
| } | ||
|
|
||
| /// Render debug IR text and collect output ranges for tagged text leaves. | ||
| /// | ||
| /// The returned text is identical to `format!("{doc:#?}")`. | ||
| 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) | ||
| } | ||
|
|
||
| fn collect_tagged_tokens<'a, T>( | ||
| doc: &Doc<'a, T>, | ||
| active_tag: Option<u32>, | ||
| out: &mut Vec<(u32, String)>, | ||
| ) where | ||
| T: DocPtr<'a>, | ||
| { | ||
| match doc { | ||
| Doc::Nil | Doc::Fail | Doc::HardLine | Doc::ExpandParent => {} | ||
| Doc::Text(text) => { | ||
| if let Some(tag) = active_tag { | ||
| out.push((tag, format!("{text:?}"))); | ||
| } | ||
| } | ||
| Doc::TextWithLen(_, inner) => collect_tagged_tokens(inner, active_tag, out), | ||
| Doc::Append(left, right) => { | ||
| collect_tagged_tokens(left, active_tag, out); | ||
| collect_tagged_tokens(right, active_tag, out); | ||
| } | ||
| Doc::LineSuffix(inner) | ||
| | Doc::Nest(_, inner) | ||
| | Doc::DedentToRoot(inner) | ||
| | Doc::Align(inner) | ||
| | Doc::Flatten(inner) => collect_tagged_tokens(inner, active_tag, out), | ||
| Doc::Tagged(id, inner) => collect_tagged_tokens(inner, Some(*id), out), | ||
| Doc::BreakOrFlat(break_doc, flat_doc) => match (&**break_doc, &**flat_doc) { | ||
| (Doc::HardLine, Doc::Text(Text::Borrowed(" "))) => {} | ||
| (Doc::HardLine, Doc::Nil) => {} | ||
| (_, Doc::Nil) => collect_tagged_tokens(break_doc, active_tag, out), | ||
| (Doc::Nil, _) => collect_tagged_tokens(flat_doc, active_tag, out), | ||
| _ => { | ||
| // Debug prints FlatOrBreak(y, x). | ||
| collect_tagged_tokens(flat_doc, active_tag, out); | ||
| collect_tagged_tokens(break_doc, active_tag, out); | ||
| } | ||
| }, | ||
| Doc::Group(inner) => match &**inner { | ||
| Doc::BreakOrFlat(break_doc, flat_doc) | ||
| if matches!( | ||
| (&**break_doc, &**flat_doc), | ||
| (Doc::HardLine, Doc::Text(Text::Borrowed(" "))) | ||
| ) => {} | ||
| Doc::BreakOrFlat(break_doc, flat_doc) | ||
| if matches!((&**break_doc, &**flat_doc), (Doc::HardLine, Doc::Nil)) => {} | ||
| _ => collect_tagged_tokens(inner, active_tag, out), | ||
| }, | ||
| Doc::Union(left, right) | Doc::PartialUnion(left, right) => { | ||
| collect_tagged_tokens(left, active_tag, out); | ||
| collect_tagged_tokens(right, active_tag, out); | ||
| } | ||
| #[cfg(feature = "contextual")] | ||
| Doc::OnColumn(_) | Doc::OnNesting(_) => {} | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::{Arena, DocAllocator}; | ||
|
|
||
| use super::debug_with_tag_ranges; | ||
|
|
||
| #[test] | ||
| fn keeps_debug_output_stable_and_collects_ranges() { | ||
| let arena = Arena::new(); | ||
| let doc = (arena.text("alpha").tag(1) + arena.space() + arena.text("beta").tag(2)) | ||
| .group(); | ||
|
|
||
| let (text, ranges) = debug_with_tag_ranges(&doc); | ||
| assert_eq!(text, format!("{doc:#?}")); | ||
| assert_eq!(ranges.len(), 2); | ||
| assert_eq!(ranges[0].tag, 1); | ||
| assert_eq!(ranges[1].tag, 2); | ||
| assert_eq!(&text[ranges[0].start..ranges[0].end], "\"alpha\""); | ||
| assert_eq!(&text[ranges[1].start..ranges[1].end], "\"beta\""); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic in
debug_with_tag_rangesfor 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_tokensalso 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.