| 1 | //! Parse wrapper type following rust-analyzer's pattern for thread-safe storage in Salsa. |
| 2 | |
| 3 | use craterust-analyzer cargo deb822-lossless 0.5.18 crate/::losslessrust-analyzer cargo deb822-lossless 0.5.18 lossless/::{Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#, ParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/ParseError#, PositionedParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#}; |
| 4 | use rowanrust-analyzer cargo rowan 0.16.1 crate/::astrust-analyzer cargo rowan 0.16.1 ast/::AstNoderust-analyzer cargo rowan 0.16.1 ast/AstNode#; |
| 5 | use rowanrust-analyzer cargo rowan 0.16.1 crate/::{GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#}; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#; |
| 7 | |
| 8 | /// The result of parsing: a syntax tree and a collection of errors. |
| 9 | /// |
| 10 | /// This type is designed to be stored in Salsa databases as it contains |
| 11 | /// the thread-safe `GreenNode` instead of the non-thread-safe `SyntaxNode`. |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 13 | pub struct Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#<T> { |
| 14 | greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green.: GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, |
| 15 | errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 16 | positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#positioned_errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PositionedParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#>, |
| 17 | _tyrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#_ty.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<T>, |
| 18 | } |
| 19 | |
| 20 | impl<T> Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#<T> { |
| 21 | /// Create a new Parse result from a GreenNode and errors |
| 22 | pub fn newrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]new().(green: GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, errors: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Selfrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`] { |
| 23 | Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse# { |
| 24 | greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green., |
| 25 | errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#errors., |
| 26 | positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#positioned_errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 27 | _tyrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#_ty.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Create a new Parse result from a GreenNode, errors, and positioned errors |
| 32 | pub fn new_with_positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]new_with_positioned_errors().( |
| 33 | green: GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, |
| 34 | errors: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 35 | positioned_errors: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PositionedParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#>, |
| 36 | ) -> Selfrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`] { |
| 37 | Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse# { |
| 38 | greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green., |
| 39 | errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#errors., |
| 40 | positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#positioned_errors., |
| 41 | _tyrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#_ty.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Get the green node (thread-safe representation) |
| 46 | pub fn greenrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]green().(&self) -> &GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode# { |
| 47 | &self.greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green. |
| 48 | } |
| 49 | |
| 50 | /// Get the syntax errors |
| 51 | pub fn errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]errors().(&self) -> &[Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#] { |
| 52 | &self.errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#errors. |
| 53 | } |
| 54 | |
| 55 | /// Get parse errors with position information |
| 56 | pub fn positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]positioned_errors().(&self) -> &[PositionedParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#] { |
| 57 | &self.positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#positioned_errors. |
| 58 | } |
| 59 | |
| 60 | /// Get parse errors as strings (for backward compatibility if needed) |
| 61 | pub fn error_messagesrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]error_messages().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 62 | self.positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#positioned_errors. |
| 63 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 64 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|e| e.messagerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#message..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().()) |
| 65 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 66 | } |
| 67 | |
| 68 | /// Check if there are any errors |
| 69 | pub fn okrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]ok().(&self) -> bool { |
| 70 | self.errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#errors..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() |
| 71 | } |
| 72 | |
| 73 | /// Convert to a Result, returning the tree if there are no errors |
| 74 | pub fn to_resultrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]to_result().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, ParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/ParseError#> |
| 75 | where |
| 76 | T: AstNoderust-analyzer cargo rowan 0.16.1 ast/AstNode#<Languagerust-analyzer cargo rowan 0.16.1 ast/AstNode#Language# = craterust-analyzer cargo deb822-lossless 0.5.18 crate/::losslessrust-analyzer cargo deb822-lossless 0.5.18 lossless/::Langrust-analyzer cargo deb822-lossless 0.5.18 lossless/Lang#>, |
| 77 | { |
| 78 | if self.errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#errors..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 79 | let node = SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#::new_root_mutrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]new_root_mut().(self.greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green.); |
| 80 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(T::castrust-analyzer cargo rowan 0.16.1 ast/AstNode#cast().(node).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("root node has wrong type")) |
| 81 | } else { |
| 82 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/ParseError#(self.errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#errors.)) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Get the parsed syntax tree |
| 87 | /// |
| 88 | /// Returns the syntax tree even if there are parse errors. |
| 89 | /// Use `errors()` or `positioned_errors()` to check for errors if needed. |
| 90 | /// This allows for error-resilient tooling that can work with partial/invalid input. |
| 91 | pub fn treerust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]tree().(&self) -> T |
| 92 | where |
| 93 | T: AstNoderust-analyzer cargo rowan 0.16.1 ast/AstNode#<Languagerust-analyzer cargo rowan 0.16.1 ast/AstNode#Language# = craterust-analyzer cargo deb822-lossless 0.5.18 crate/::losslessrust-analyzer cargo deb822-lossless 0.5.18 lossless/::Langrust-analyzer cargo deb822-lossless 0.5.18 lossless/Lang#>, |
| 94 | { |
| 95 | let node = SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#::new_root_mutrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]new_root_mut().(self.greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green..clonerust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNode][Clone]clone().()); |
| 96 | T::castrust-analyzer cargo rowan 0.16.1 ast/AstNode#cast().(node).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("root node has wrong type") |
| 97 | } |
| 98 | |
| 99 | /// Get the syntax node |
| 100 | pub fn syntax_noderust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]syntax_node().(&self) -> SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#<craterust-analyzer cargo deb822-lossless 0.5.18 crate/::losslessrust-analyzer cargo deb822-lossless 0.5.18 lossless/::Langrust-analyzer cargo deb822-lossless 0.5.18 lossless/Lang#> { |
| 101 | SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#::new_rootrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]new_root().(self.greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green..clonerust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNode][Clone]clone().()) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Implement Send + Sync since GreenNode is thread-safe |
| 106 | unsafe impl<T> Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# for Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#<T> {} |
| 107 | unsafe impl<T> Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# for Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#<T> {} |
| 108 | |
| 109 | impl Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#<Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#> { |
| 110 | /// Parse deb822 text, returning a Parse result |
| 111 | pub fn parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(text: &str) -> Selfrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`] { |
| 112 | let parsed = craterust-analyzer cargo deb822-lossless 0.5.18 crate/::losslessrust-analyzer cargo deb822-lossless 0.5.18 lossless/::parserust-analyzer cargo deb822-lossless 0.5.18 lossless/parse().(text); |
| 113 | Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#::new_with_positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]new_with_positioned_errors().( |
| 114 | parsed.green_noderust-analyzer cargo deb822-lossless 0.5.18 lossless/Parse#green_node., |
| 115 | parsed.errorsrust-analyzer cargo deb822-lossless 0.5.18 lossless/Parse#errors., |
| 116 | parsed.positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 lossless/Parse#positioned_errors., |
| 117 | ) |
| 118 | } |
| 119 | |
| 120 | /// Incrementally reparse after a text edit. |
| 121 | /// |
| 122 | /// Given the new full text and the range that was edited (in the *new* text |
| 123 | /// coordinates after the edit has been applied), this tries to reuse |
| 124 | /// unchanged paragraphs from the previous parse and only reparse the |
| 125 | /// affected region. |
| 126 | /// |
| 127 | /// Falls back to a full reparse if the edit spans the entire file or if |
| 128 | /// incremental reparsing is not beneficial. |
| 129 | pub fn reparserust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]reparse().(&self, new_text: &str, edit: rowanrust-analyzer cargo rowan 0.16.1 crate/::TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#) -> Selfrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`] { |
| 130 | use rowanrust-analyzer cargo rowan 0.16.1 crate/::TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#; |
| 131 | |
| 132 | let root = &self.greenrust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#green.; |
| 133 | |
| 134 | // Collect children with their text ranges |
| 135 | let mut children: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<( |
| 136 | rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#<&rowanrust-analyzer cargo rowan 0.16.1 crate/::GreenNodeDatarust-analyzer cargo rowan 0.16.1 green/node/GreenNodeData#, &rowanrust-analyzer cargo rowan 0.16.1 crate/::GreenTokenDatarust-analyzer cargo rowan 0.16.1 green/token/GreenTokenData#>, |
| 137 | TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#, |
| 138 | TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#, |
| 139 | )> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 140 | let mut offset = TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#::fromrust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`From<u32>`]from().(0); |
| 141 | for child in root.childrenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]children().() { |
| 142 | let len = match &child { |
| 143 | rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#::Noderust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#Node#(n) => n.text_lenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]text_len().(), |
| 144 | rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#::Tokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#Token#(t) => t.text_lenrust-analyzer cargo rowan 0.16.1 green/token/impl#[GreenTokenData]text_len().(), |
| 145 | }; |
| 146 | children.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((child, offset, offset rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add().+rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add(). rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add().len)); |
| 147 | offset rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`AddAssign<A>`]add_assign().+=rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`AddAssign<A>`]add_assign(). rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`AddAssign<A>`]add_assign().len; |
| 148 | } |
| 149 | |
| 150 | let old_len = offset; |
| 151 | |
| 152 | // If there are very few children, just do a full reparse |
| 153 | if children.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().2 { |
| 154 | return Selfrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(new_text); |
| 155 | } |
| 156 | |
| 157 | // Find the range of children affected by the edit. |
| 158 | // The edit range is in terms of the *new* text. We need to figure out |
| 159 | // which old children overlap. The edit replaces old text |
| 160 | // [edit.start .. edit.start + old_len - new_len + edit.len()] with |
| 161 | // new text [edit.start .. edit.end]. |
| 162 | let new_len = TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#::ofrust-analyzer cargo text-size 1.1.1 size/impl#[TextSize]of().(new_text); |
| 163 | let len_delta: i64 = i64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[i64][`From<u32>`]from().(u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(new_len)) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().i64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[i64][`From<u32>`]from().(u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(old_len)); |
| 164 | |
| 165 | // In old-text coordinates, the edit covered: |
| 166 | let old_edit_start = edit.startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().(); |
| 167 | let old_edit_end = TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#::fromrust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`From<u32>`]from().((i64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[i64][`From<u32>`]from().(u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(edit.endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().())) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().len_delta) as u32); |
| 168 | |
| 169 | // Find first and last affected child indices. |
| 170 | // Use >= for the first child to catch inserts at child boundaries. |
| 171 | let first_affected = children |
| 172 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 173 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|(_, _, end)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().old_edit_start); |
| 174 | let last_affected = children |
| 175 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 176 | .rpositionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]rposition().(|(_, start, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().old_edit_end); |
| 177 | |
| 178 | let (first_affected, last_affected) = match (first_affected, last_affected) { |
| 179 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(f), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(l)) => (f, l), |
| 180 | _ => return Selfrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(new_text), |
| 181 | }; |
| 182 | |
| 183 | // Expand to paragraph boundaries: find the text region in the *new* |
| 184 | // text that covers all affected children. We use the old children |
| 185 | // offsets for the prefix (unchanged) and derive the suffix. |
| 186 | let reparse_start = children[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().first_affected]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..1; |
| 187 | let reparse_old_end = children[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().last_affected]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..2; |
| 188 | |
| 189 | // In new-text coordinates, the end of the affected region is shifted |
| 190 | // by the length delta. |
| 191 | let reparse_new_end = |
| 192 | TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#::fromrust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`From<u32>`]from().((i64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[i64][`From<u32>`]from().(u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_old_end)) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Add<Self>`]add().len_delta) as u32); |
| 193 | |
| 194 | // Bounds check |
| 195 | if u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_start) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(new_len) |
| 196 | || u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_new_end) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(new_len) |
| 197 | { |
| 198 | return Selfrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(new_text); |
| 199 | } |
| 200 | |
| 201 | let reparse_slice = &new_text[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().usize::fromrust-analyzer cargo text-size 1.1.1 size/impl#[usize][`From<TextSize>`]from().(reparse_start)..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#usize::fromrust-analyzer cargo text-size 1.1.1 size/impl#[usize][`From<TextSize>`]from().(reparse_new_end)]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 202 | |
| 203 | // Parse just the affected region |
| 204 | let reparsed = craterust-analyzer cargo deb822-lossless 0.5.18 crate/::losslessrust-analyzer cargo deb822-lossless 0.5.18 lossless/::parserust-analyzer cargo deb822-lossless 0.5.18 lossless/parse().(reparse_slice); |
| 205 | let reparsed_root = reparsed.green_noderust-analyzer cargo deb822-lossless 0.5.18 lossless/Parse#green_node.; |
| 206 | |
| 207 | // Build the new root by splicing: prefix children + reparsed children + suffix children |
| 208 | let to_owned = |c: &rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#<&rowanrust-analyzer cargo rowan 0.16.1 crate/::GreenNodeDatarust-analyzer cargo rowan 0.16.1 green/node/GreenNodeData#, &rowanrust-analyzer cargo rowan 0.16.1 crate/::GreenTokenDatarust-analyzer cargo rowan 0.16.1 green/token/GreenTokenData#>| -> rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#<GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#, rowanrust-analyzer cargo rowan 0.16.1 crate/::GreenTokenrust-analyzer cargo rowan 0.16.1 green/token/GreenToken#> { |
| 209 | match c { |
| 210 | rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#::Noderust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#Node#(n) => rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#::Noderust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#Node#((*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().n).to_ownedrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData][ToOwned]to_owned().()), |
| 211 | rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#::Tokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#Token#(t) => rowanrust-analyzer cargo rowan 0.16.1 crate/::NodeOrTokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#::Tokenrust-analyzer cargo rowan 0.16.1 utility_types/NodeOrToken#Token#((*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().t).to_ownedrust-analyzer cargo rowan 0.16.1 green/token/impl#[GreenTokenData][ToOwned]to_owned().()), |
| 212 | } |
| 213 | }; |
| 214 | let mut new_root_children = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 215 | for (c, _, _) in &children[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#first_affected]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). { |
| 216 | new_root_children.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(to_owned(c)); |
| 217 | } |
| 218 | for c in reparsed_root.childrenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]children().() { |
| 219 | new_root_children.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(c.to_ownedrust-analyzer cargo rowan 0.16.1 green/element/impl#[`NodeOrToken<&GreenNodeData, &GreenTokenData>`]to_owned().()); |
| 220 | } |
| 221 | for (c, _, _) in &children[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().last_affected rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). { |
| 222 | new_root_children.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(to_owned(c)); |
| 223 | } |
| 224 | |
| 225 | let new_green = GreenNoderust-analyzer cargo rowan 0.16.1 green/node/GreenNode#::newrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNode]new().( |
| 226 | rowanrust-analyzer cargo rowan 0.16.1 crate/::SyntaxKindrust-analyzer cargo rowan 0.16.1 green/SyntaxKind#(craterust-analyzer cargo deb822-lossless 0.5.18 crate/::lexrust-analyzer cargo deb822-lossless 0.5.18 lex/::SyntaxKindrust-analyzer cargo deb822-lossless 0.5.18 lex/SyntaxKind#::ROOTrust-analyzer cargo deb822-lossless 0.5.18 lex/SyntaxKind#ROOT# as u16), |
| 227 | new_root_children, |
| 228 | ); |
| 229 | |
| 230 | // Collect errors: errors from unchanged prefix/suffix are tricky to |
| 231 | // preserve with correct offsets, so we re-collect from scratch. |
| 232 | // For a full solution we'd need to offset-shift the old errors, but |
| 233 | // since most files are error-free, a full error re-scan is acceptable. |
| 234 | // We use the reparsed errors (offset-shifted) plus any errors from |
| 235 | // children outside the reparsed region. |
| 236 | // For simplicity, combine positioned errors from the reparse |
| 237 | // (shifted to absolute positions) with a note that prefix/suffix |
| 238 | // errors are dropped. TODO: preserve prefix/suffix errors with shifted offsets. |
| 239 | let positioned_errors: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = reparsed |
| 240 | .positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 lossless/Parse#positioned_errors. |
| 241 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 242 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|e| PositionedParseErrorrust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError# { |
| 243 | messagerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#message.: e.messagerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#message..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), |
| 244 | rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range.: rowanrust-analyzer cargo rowan 0.16.1 crate/::TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().( |
| 245 | e.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().() rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add().+rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add(). rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add().reparse_start, |
| 246 | e.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().() rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add().+rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add(). rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Add<Self>`]add().reparse_start, |
| 247 | ), |
| 248 | coderust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#code.: e.coderust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#code..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Clone]clone().(), |
| 249 | }) |
| 250 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 251 | let errors: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = positioned_errors |
| 252 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 253 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|e| e.messagerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#message..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().()) |
| 254 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 255 | |
| 256 | Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#::new_with_positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]new_with_positioned_errors().(new_green, errors, positioned_errors) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | #[cfg(test)] |
| 261 | mod testsrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/ { |
| 262 | use superrust-analyzer cargo deb822-lossless 0.5.18 parse/::*; |
| 263 | |
| 264 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 265 | fn test_positioned_errors_apirust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_positioned_errors_api().() { |
| 266 | let input = "Invalid field without colon\nBroken: field: extra colon\n"; |
| 267 | let parsed = Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#::<Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#>::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(input); |
| 268 | |
| 269 | // Should have positioned errors |
| 270 | let positioned_errors = parsed.positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]positioned_errors().(); |
| 271 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().positioned_errors.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().()); |
| 272 | |
| 273 | // Should still have string errors for backward compatibility |
| 274 | let string_errors = parsed.errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]errors().(); |
| 275 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().string_errors.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().()); |
| 276 | |
| 277 | // Should be able to get error messages |
| 278 | let error_messages = parsed.error_messagesrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]error_messages().(); |
| 279 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(error_messages.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), positioned_errors.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 280 | |
| 281 | for (i, positioned_error) in positioned_errors.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 282 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(positioned_error.messagerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#message., error_messages[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().i]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 283 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(positioned_error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().() <= positioned_error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().()); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 288 | fn test_positioned_errors_examplerust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_positioned_errors_example().() { |
| 289 | // Example from the requirements document |
| 290 | let input = "Invalid: field\nBroken field without colon"; |
| 291 | let parsed = Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#::<Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#>::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(input); |
| 292 | |
| 293 | let positioned_errors = parsed.positioned_errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]positioned_errors().(); |
| 294 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().positioned_errors.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().()); |
| 295 | |
| 296 | // Example usage like in a language server |
| 297 | for error in positioned_errors { |
| 298 | let start_offset: u32 = error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 299 | let end_offset: u32 = error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 300 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 301 | "Error at {:?} ({}..{}): {}", |
| 302 | error.rangerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#range., start_offset, end_offset, error.messagerust-analyzer cargo deb822-lossless 0.5.18 lossless/PositionedParseError#message. |
| 303 | ); |
| 304 | |
| 305 | // Verify we can extract the problematic text |
| 306 | if end_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() as u32 { |
| 307 | let error_text = &input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start_offset as usize..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end_offset as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 308 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().error_text.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().()); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /// Helper: parse old text, apply edit to get new text, reparse incrementally, |
| 314 | /// and verify the result matches a full parse of the new text. |
| 315 | fn assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().( |
| 316 | old_text: &str, |
| 317 | edit_start: u32, |
| 318 | edit_end: u32, |
| 319 | replacement: &str, |
| 320 | ) { |
| 321 | let parsed = Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#::<Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#>::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(old_text); |
| 322 | let mut new_text = old_text.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 323 | new_text.replace_rangerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]replace_range().(edit_start as usize..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#edit_end as usize, replacement); |
| 324 | |
| 325 | // The edit range in new-text coordinates |
| 326 | let new_edit_end = edit_start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().replacement.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() as u32; |
| 327 | let edit_range = rowanrust-analyzer cargo rowan 0.16.1 crate/::TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().( |
| 328 | rowanrust-analyzer cargo rowan 0.16.1 crate/::TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#::fromrust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`From<u32>`]from().(edit_start), |
| 329 | rowanrust-analyzer cargo rowan 0.16.1 crate/::TextSizerust-analyzer cargo text-size 1.1.1 size/TextSize#::fromrust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`From<u32>`]from().(new_edit_end), |
| 330 | ); |
| 331 | |
| 332 | let incremental = parsed.reparserust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]reparse().(&new_text, edit_range); |
| 333 | let full = Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#::<Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#>::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(&new_text); |
| 334 | |
| 335 | // The trees must produce identical text |
| 336 | let inc_tree = incremental.treerust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]tree().(); |
| 337 | let full_tree = full.treerust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]tree().(); |
| 338 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 339 | inc_tree.syntaxrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822][AstNode]syntax().().textrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]text().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 340 | full_tree.syntaxrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822][AstNode]syntax().().textrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]text().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 341 | "tree text mismatch for edit [{edit_start}..{edit_end}] -> {replacement:?}" |
| 342 | ); |
| 343 | |
| 344 | // Green nodes should be structurally equal |
| 345 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 346 | incremental.greenrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]green().(), |
| 347 | full.greenrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]green().(), |
| 348 | "green node mismatch for edit [{edit_start}..{edit_end}] -> {replacement:?}" |
| 349 | ); |
| 350 | } |
| 351 | |
| 352 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 353 | fn test_reparse_edit_within_first_paragraphrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_reparse_edit_within_first_paragraph().() { |
| 354 | let old = "Source: foo\nMaintainer: Alice\n\nPackage: bar\nArchitecture: all\n"; |
| 355 | // Change "foo" to "baz" (bytes 8..11) |
| 356 | assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().(old, 8, 11, "baz"); |
| 357 | } |
| 358 | |
| 359 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 360 | fn test_reparse_edit_within_second_paragraphrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_reparse_edit_within_second_paragraph().() { |
| 361 | let old = "Source: foo\nMaintainer: Alice\n\nPackage: bar\nArchitecture: all\n"; |
| 362 | // Change "bar" to "qux" (bytes 38..41) |
| 363 | assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().(old, 38, 41, "qux"); |
| 364 | } |
| 365 | |
| 366 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 367 | fn test_reparse_insert_field_in_paragraphrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_reparse_insert_field_in_paragraph().() { |
| 368 | let old = "Source: foo\n\nPackage: bar\nArchitecture: all\n"; |
| 369 | // Insert "Section: net\n" after "Source: foo\n" (at byte 12) |
| 370 | assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().(old, 12, 12, "Section: net\n"); |
| 371 | } |
| 372 | |
| 373 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 374 | fn test_reparse_delete_fieldrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_reparse_delete_field().() { |
| 375 | let old = "Source: foo\nSection: net\nMaintainer: Alice\n\nPackage: bar\n"; |
| 376 | // Delete "Section: net\n" (bytes 12..25) |
| 377 | assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().(old, 12, 25, ""); |
| 378 | } |
| 379 | |
| 380 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 381 | fn test_reparse_edit_spanning_paragraph_boundaryrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_reparse_edit_spanning_paragraph_boundary().() { |
| 382 | let old = "Source: foo\n\nPackage: bar\n\nPackage: baz\n"; |
| 383 | // Delete the blank line and merge paragraphs (bytes 11..13) |
| 384 | assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().(old, 11, 13, "\n"); |
| 385 | } |
| 386 | |
| 387 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 388 | fn test_reparse_single_paragraphrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_reparse_single_paragraph().() { |
| 389 | // Only one paragraph — should fall back to full reparse |
| 390 | let old = "Source: foo\nMaintainer: Alice\n"; |
| 391 | assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().(old, 8, 11, "bar"); |
| 392 | } |
| 393 | |
| 394 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 395 | fn test_reparse_add_new_paragraphrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_reparse_add_new_paragraph().() { |
| 396 | let old = "Source: foo\n\nPackage: bar\n"; |
| 397 | // Add a new paragraph at the end |
| 398 | assert_incremental_matches_fullrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/assert_incremental_matches_full().(old, 25, 25, "\nPackage: baz\nArchitecture: any\n"); |
| 399 | } |
| 400 | |
| 401 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 402 | fn test_tree_with_errors_does_not_panicrust-analyzer cargo deb822-lossless 0.5.18 parse/tests/test_tree_with_errors_does_not_panic().() { |
| 403 | // Verify that tree() returns a tree even when there are parse errors, |
| 404 | // enabling error-resilient tooling |
| 405 | let input = "Invalid field without colon\nBroken: field: extra colon\n"; |
| 406 | let parsed = Parserust-analyzer cargo deb822-lossless 0.5.18 parse/Parse#::<Deb822rust-analyzer cargo deb822-lossless 0.5.18 lossless/Deb822#>::parse_deb822rust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<Deb822>`]parse_deb822().(input); |
| 407 | |
| 408 | // Verify there are errors |
| 409 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().parsed.errorsrust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]errors().().is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().()); |
| 410 | |
| 411 | // tree() should not panic despite errors |
| 412 | let tree = parsed.treerust-analyzer cargo deb822-lossless 0.5.18 parse/impl#[`Parse<T>`]tree().(); |
| 413 | |
| 414 | // Verify we got a valid syntax tree |
| 415 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(tree.syntaxrust-analyzer cargo deb822-lossless 0.5.18 lossless/impl#[Deb822][AstNode]syntax().().textrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]text().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() == input); |
| 416 | } |
| 417 | } |