| 1 | //! Incremental reparsing support for efficient handling of text edits. |
| 2 | //! |
| 3 | //! Instead of reparsing the entire file after each edit, this module identifies |
| 4 | //! which top-level items are affected and reparses only those, splicing the |
| 5 | //! results back into the existing green tree. |
| 6 | |
| 7 | use craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::{ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#, Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#, PositionedParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#}; |
| 8 | use craterust-analyzer cargo makefile-lossless 0.3.40 crate/::parserust-analyzer cargo makefile-lossless 0.3.40 parse/::Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#; |
| 9 | use rowanrust-analyzer cargo rowan 0.16.1 crate/::TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#; |
| 10 | |
| 11 | /// A text edit applied to the source, as typically received from an LSP. |
| 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 TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit# { |
| 14 | /// The byte range in the old text to replace. |
| 15 | pub rangerust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#range.: TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#, |
| 16 | /// The new text to insert in place of the range. |
| 17 | pub new_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#new_text.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 18 | } |
| 19 | |
| 20 | impl TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit# { |
| 21 | /// Create a new text edit. |
| 22 | pub fn newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(range: TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#, new_text: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit] { |
| 23 | Selfrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit] { rangerust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#range., new_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#new_text. } |
| 24 | } |
| 25 | |
| 26 | /// The length delta introduced by this edit. |
| 27 | fn deltarust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]delta().(&self) -> i64 { |
| 28 | self.new_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#new_text..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() as i64 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().u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(self.rangerust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#range..lenrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]len().()) as i64 |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Apply a text edit to the old source, producing the new source text. |
| 33 | pub fn apply_edit_to_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/apply_edit_to_text().(old_text: &str, edit: &TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 34 | let start: usize = u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(edit.rangerust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#range..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().()) as usize; |
| 35 | let end: usize = u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(edit.rangerust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#range..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().()) as usize; |
| 36 | let mut new = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(old_text.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().().wrapping_add_signedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]wrapping_add_signed().(edit.deltarust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]delta().() as isize)); |
| 37 | new.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&old_text[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#start]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 38 | new.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&edit.new_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#new_text.); |
| 39 | new.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&old_text[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().end..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 40 | new |
| 41 | } |
| 42 | |
| 43 | impl Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#<Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#> { |
| 44 | /// Apply an incremental text edit and reparse only the affected region. |
| 45 | /// |
| 46 | /// This is more efficient than a full reparse for large files, as it reuses |
| 47 | /// the green tree nodes for unaffected top-level items. |
| 48 | /// |
| 49 | /// # Arguments |
| 50 | /// * `old_text` - The full text before the edit |
| 51 | /// * `edit` - The edit to apply |
| 52 | /// |
| 53 | /// # Returns |
| 54 | /// A new `Parse<Makefile>` with the edit applied, and the new full text. |
| 55 | /// |
| 56 | /// # Example |
| 57 | /// ``` |
| 58 | /// use makefile_lossless::{Makefile, Parse, TextEdit, TextRange}; |
| 59 | /// |
| 60 | /// let old_text = "VAR1 = old\nVAR2 = value\n"; |
| 61 | /// let parse = Parse::<Makefile>::parse_makefile(old_text); |
| 62 | /// |
| 63 | /// // Change "old" to "new" in VAR1 |
| 64 | /// let edit = TextEdit::new( |
| 65 | /// TextRange::new(7.into(), 10.into()), |
| 66 | /// "new".to_string(), |
| 67 | /// ); |
| 68 | /// let (new_parse, new_text) = parse.apply_edit(old_text, &edit); |
| 69 | /// assert_eq!(new_text, "VAR1 = new\nVAR2 = value\n"); |
| 70 | /// |
| 71 | /// let makefile: Makefile = new_parse.tree(); |
| 72 | /// let vars: Vec<_> = makefile.variable_definitions().collect(); |
| 73 | /// assert_eq!(vars.len(), 2); |
| 74 | /// assert_eq!(vars[0].raw_value(), Some("new".to_string())); |
| 75 | /// assert_eq!(vars[1].raw_value(), Some("value".to_string())); |
| 76 | /// ``` |
| 77 | pub fn apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(&self, old_text: &str, edit: &TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#) -> (Selfrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`], Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) { |
| 78 | let new_text = apply_edit_to_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/apply_edit_to_text().(old_text, edit); |
| 79 | let delta = edit.deltarust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]delta().(); |
| 80 | |
| 81 | // Walk the ROOT green node's children to find which ones overlap the edit range. |
| 82 | let old_green = self.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]green().(); |
| 83 | let children: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = old_green.childrenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]children().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| c.to_ownedrust-analyzer cargo rowan 0.16.1 green/element/impl#[`NodeOrToken<&GreenNodeData, &GreenTokenData>`]to_owned().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 84 | |
| 85 | // Compute the text range of each direct child of ROOT. |
| 86 | let mut child_ranges: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(children.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 87 | let mut offset = 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().(0); |
| 88 | for child in &children { |
| 89 | let len = match child { |
| 90 | 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().(), |
| 91 | 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().(), |
| 92 | }; |
| 93 | child_ranges.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(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)); |
| 94 | 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; |
| 95 | } |
| 96 | |
| 97 | if children.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 98 | // Empty tree — just do a full parse. |
| 99 | let new_parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(&new_text); |
| 100 | return (new_parse, new_text); |
| 101 | } |
| 102 | |
| 103 | // Find the first and last children that overlap or are adjacent to the edit range. |
| 104 | let first_affected = child_ranges |
| 105 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 106 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|r| r.endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().edit.rangerust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#range..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().()) |
| 107 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(children.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().().saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(1)); |
| 108 | |
| 109 | let last_affected = child_ranges |
| 110 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 111 | .rpositionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]rposition().(|r| r.startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().edit.rangerust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#range..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().()) |
| 112 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(first_affected); |
| 113 | |
| 114 | // The reparse region in the *old* text. |
| 115 | let reparse_start = child_ranges[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()..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().(); |
| 116 | let reparse_end_old = child_ranges[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()..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().(); |
| 117 | |
| 118 | // The corresponding region in the *new* text. |
| 119 | // Everything before first_affected is unchanged, so reparse_start is the same. |
| 120 | // The end shifts by the delta. |
| 121 | let reparse_end_new = |
| 122 | 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().((u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_end_old) as i64 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().delta) as u32); |
| 123 | |
| 124 | let reparse_region = |
| 125 | &new_text[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_start) as usize..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_end_new) as usize]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().; |
| 126 | |
| 127 | // Reparse just the affected region. |
| 128 | let reparsed = craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::parserust-analyzer cargo makefile-lossless 0.3.40 lossless/parse().(reparse_region, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 129 | let reparsed_root = reparsed.green_noderust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#green_node.; |
| 130 | |
| 131 | // Build the new ROOT by splicing: [old children before] + [reparsed children] + [old children after] |
| 132 | let new_root = old_green.splice_childrenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]splice_children().( |
| 133 | first_affected..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#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, |
| 134 | reparsed_root.childrenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]children().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| c.to_ownedrust-analyzer cargo rowan 0.16.1 green/element/impl#[`NodeOrToken<&GreenNodeData, &GreenTokenData>`]to_owned().()), |
| 135 | ); |
| 136 | |
| 137 | // Rebuild errors: keep errors outside the affected region, add reparsed errors with adjusted positions. |
| 138 | let mut new_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().(); |
| 139 | let mut new_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().(); |
| 140 | |
| 141 | // Errors before the affected region (unchanged). |
| 142 | for err in self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]errors().() { |
| 143 | // ErrorInfo uses line numbers, not byte offsets. We need to figure out |
| 144 | // which errors are before the reparse region by line number. |
| 145 | // Since we can't easily map line numbers to byte offsets without the text, |
| 146 | // we use a simpler approach: count newlines up to reparse_start. |
| 147 | let lines_before = old_text[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_start) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 148 | .matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]matches().('\n') |
| 149 | .countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(); |
| 150 | if err.linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line. 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().lines_before { |
| 151 | new_errors.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(err.clonerust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[ErrorInfo][Clone]clone().()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Errors from the reparsed region (adjusted line numbers). |
| 156 | let line_offset = old_text[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_start) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 157 | .matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]matches().('\n') |
| 158 | .countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(); |
| 159 | for err in &reparsed.errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#errors. { |
| 160 | new_errors.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo# { |
| 161 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message.: err.messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), |
| 162 | linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line.: err.linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line. 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().line_offset, |
| 163 | contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context.: err.contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | // Errors after the affected region (adjusted line numbers). |
| 168 | let old_lines_in_region = old_text |
| 169 | [rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_start) as usize..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#u32::fromrust-analyzer cargo text-size 1.1.1 size/impl#[u32][`From<TextSize>`]from().(reparse_end_old) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 170 | .matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]matches().('\n') |
| 171 | .countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(); |
| 172 | let new_lines_in_region = reparse_region.matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]matches().('\n').countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(); |
| 173 | let line_delta = new_lines_in_region as i64 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().old_lines_in_region as i64; |
| 174 | let lines_after_start = line_offset 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().old_lines_in_region; |
| 175 | for err in self.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]errors().() { |
| 176 | if err.linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().lines_after_start { |
| 177 | new_errors.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo# { |
| 178 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message.: err.messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), |
| 179 | linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line.: (err.linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line. as i64 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().line_delta) as usize, |
| 180 | contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context.: err.contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), |
| 181 | }); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Positioned errors before. |
| 186 | for err in self.positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]positioned_errors().() { |
| 187 | if err.rangerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#range..endrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]end().() 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().reparse_start { |
| 188 | new_positioned_errors.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(err.clonerust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[PositionedParseError][Clone]clone().()); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Positioned errors from reparsed region (shifted by reparse_start). |
| 193 | for err in &reparsed.positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/Parse#positioned_errors. { |
| 194 | new_positioned_errors.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(PositionedParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError# { |
| 195 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#message.: err.messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#message..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), |
| 196 | rangerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#range.: TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().( |
| 197 | err.rangerust-analyzer cargo makefile-lossless 0.3.40 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, |
| 198 | err.rangerust-analyzer cargo makefile-lossless 0.3.40 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, |
| 199 | ), |
| 200 | coderust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#code.: err.coderust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#code..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Clone]clone().(), |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | // Positioned errors after (shifted by delta). |
| 205 | for err in self.positioned_errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]positioned_errors().() { |
| 206 | if err.rangerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#range..startrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]start().() 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().reparse_end_old { |
| 207 | let shift = 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().(delta.unsigned_absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]unsigned_abs().() as u32); |
| 208 | let (new_start, new_end) = if delta rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]ge().0 { |
| 209 | (err.rangerust-analyzer cargo makefile-lossless 0.3.40 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().shift, err.rangerust-analyzer cargo makefile-lossless 0.3.40 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().shift) |
| 210 | } else { |
| 211 | (err.rangerust-analyzer cargo makefile-lossless 0.3.40 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][`Sub<Self>`]sub().-rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Sub<Self>`]sub(). rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Sub<Self>`]sub().shift, err.rangerust-analyzer cargo makefile-lossless 0.3.40 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][`Sub<Self>`]sub().-rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Sub<Self>`]sub(). rust-analyzer cargo text-size 1.1.1 size/impl#[TextSize][`Sub<Self>`]sub().shift) |
| 212 | }; |
| 213 | new_positioned_errors.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(PositionedParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError# { |
| 214 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#message.: err.messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#message..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(), |
| 215 | rangerust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#range.: TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(new_start, new_end), |
| 216 | coderust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#code.: err.coderust-analyzer cargo makefile-lossless 0.3.40 lossless/PositionedParseError#code..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Clone]clone().(), |
| 217 | }); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | let new_parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::newrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]new().(new_root, new_errors, new_positioned_errors); |
| 222 | (new_parse, new_text) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | #[cfg(test)] |
| 227 | mod testsrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/ { |
| 228 | use superrust-analyzer cargo makefile-lossless 0.3.40 incremental/::*; |
| 229 | 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#; |
| 230 | |
| 231 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 232 | fn test_apply_edit_to_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_apply_edit_to_text().() { |
| 233 | let old = "hello world"; |
| 234 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(6.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "rust".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 235 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(apply_edit_to_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/apply_edit_to_text().(old, &edit), "hello rust"); |
| 236 | } |
| 237 | |
| 238 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 239 | fn test_apply_edit_to_text_insertrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_apply_edit_to_text_insert().() { |
| 240 | let old = "hello world"; |
| 241 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(5.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 5.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), " beautiful".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 242 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(apply_edit_to_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/apply_edit_to_text().(old, &edit), "hello beautiful world"); |
| 243 | } |
| 244 | |
| 245 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 246 | fn test_apply_edit_to_text_deleterust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_apply_edit_to_text_delete().() { |
| 247 | let old = "hello world"; |
| 248 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(5.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().()); |
| 249 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(apply_edit_to_textrust-analyzer cargo makefile-lossless 0.3.40 incremental/apply_edit_to_text().(old, &edit), "hello"); |
| 250 | } |
| 251 | |
| 252 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 253 | fn test_incremental_change_variable_valuerust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_change_variable_value().() { |
| 254 | let old_text = "VAR1 = old\nVAR2 = value\n"; |
| 255 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 256 | |
| 257 | // Change "old" to "new" |
| 258 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(7.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 10.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "new".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 259 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 260 | |
| 261 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "VAR1 = new\nVAR2 = value\n"); |
| 262 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 263 | let vars: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 264 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 2); |
| 265 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..namerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]name().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("VAR1".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 266 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..raw_valuerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]raw_value().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("new".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 267 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..namerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]name().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("VAR2".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 268 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..raw_valuerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]raw_value().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("value".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 269 | } |
| 270 | |
| 271 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 272 | fn test_incremental_change_rule_commandrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_change_rule_command().() { |
| 273 | let old_text = "all:\n\techo hello\n"; |
| 274 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 275 | |
| 276 | // Change "hello" to "goodbye" |
| 277 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 16.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "goodbye".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 278 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 279 | |
| 280 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "all:\n\techo goodbye\n"); |
| 281 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 282 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.rulesrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]rules().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 1); |
| 283 | } |
| 284 | |
| 285 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 286 | fn test_incremental_insert_new_variablerust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_insert_new_variable().() { |
| 287 | let old_text = "VAR1 = one\nVAR2 = two\n"; |
| 288 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 289 | |
| 290 | // Insert a new variable between the two |
| 291 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().( |
| 292 | TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 293 | "NEW = inserted\n".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 294 | ); |
| 295 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 296 | |
| 297 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "VAR1 = one\nNEW = inserted\nVAR2 = two\n"); |
| 298 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 299 | let vars: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 300 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 3); |
| 301 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..namerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]name().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("VAR1".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 302 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..namerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]name().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("NEW".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 303 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().2]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..namerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]name().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("VAR2".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 304 | } |
| 305 | |
| 306 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 307 | fn test_incremental_delete_variablerust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_delete_variable().() { |
| 308 | let old_text = "VAR1 = one\nVAR2 = two\nVAR3 = three\n"; |
| 309 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 310 | |
| 311 | // Delete VAR2 line |
| 312 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 22.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().()); |
| 313 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 314 | |
| 315 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "VAR1 = one\nVAR3 = three\n"); |
| 316 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 317 | let vars: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 318 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 2); |
| 319 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..namerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]name().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("VAR1".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 320 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..namerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]name().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("VAR3".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 321 | } |
| 322 | |
| 323 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 324 | fn test_incremental_edit_preserves_unaffectedrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_edit_preserves_unaffected().() { |
| 325 | let old_text = "VAR1 = one\nVAR2 = two\nVAR3 = three\n"; |
| 326 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 327 | |
| 328 | // Only change VAR2's value |
| 329 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(18.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 21.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "TWO".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 330 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 331 | |
| 332 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "VAR1 = one\nVAR2 = TWO\nVAR3 = three\n"); |
| 333 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 334 | let vars: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 335 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 3); |
| 336 | |
| 337 | // Verify that unaffected green nodes are structurally identical. |
| 338 | let old_children: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = parse.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]green().().childrenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]children().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| c.to_ownedrust-analyzer cargo rowan 0.16.1 green/element/impl#[`NodeOrToken<&GreenNodeData, &GreenTokenData>`]to_owned().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 339 | let new_children: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = new_parse.greenrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]green().().childrenrust-analyzer cargo rowan 0.16.1 green/node/impl#[GreenNodeData]children().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| c.to_ownedrust-analyzer cargo rowan 0.16.1 green/element/impl#[`NodeOrToken<&GreenNodeData, &GreenTokenData>`]to_owned().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 340 | |
| 341 | // First child (VAR1 node) should be structurally identical. |
| 342 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 343 | old_children[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., new_children[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., |
| 344 | "VAR1 green node should be identical (reused)" |
| 345 | ); |
| 346 | } |
| 347 | |
| 348 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 349 | fn test_incremental_empty_filerust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_empty_file().() { |
| 350 | let old_text = ""; |
| 351 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 352 | |
| 353 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().( |
| 354 | TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(0.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 0.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 355 | "VAR = value\n".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 356 | ); |
| 357 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 358 | |
| 359 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "VAR = value\n"); |
| 360 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 361 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 1); |
| 362 | } |
| 363 | |
| 364 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 365 | fn test_incremental_change_variable_to_rulerust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_change_variable_to_rule().() { |
| 366 | let old_text = "target = value\n"; |
| 367 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 368 | |
| 369 | // Change "= value" to ":" |
| 370 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(7.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 14.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), ":".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 371 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 372 | |
| 373 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "target :\n"); |
| 374 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 375 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 0); |
| 376 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.rulesrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]rules().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 1); |
| 377 | } |
| 378 | |
| 379 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 380 | fn test_incremental_matches_full_reparserust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_matches_full_reparse().() { |
| 381 | let old_text = "VAR1 = one\nall: dep\n\techo $(VAR1)\nVAR2 = two\n"; |
| 382 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 383 | |
| 384 | // Edit inside the rule |
| 385 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(26.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 30.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "VAR2".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 386 | let (incremental, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 387 | let full = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(&new_text); |
| 388 | |
| 389 | // Both should produce the same tree. |
| 390 | let inc_tree = incremental.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 391 | let full_tree: Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile# = full.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 392 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 393 | inc_tree.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Makefile][AstNode]syntax().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 394 | full_tree.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Makefile][AstNode]syntax().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 399 | fn test_incremental_edit_at_endrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_edit_at_end().() { |
| 400 | let old_text = "VAR = value\n"; |
| 401 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 402 | |
| 403 | // Append a new rule at the end |
| 404 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().( |
| 405 | TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(12.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 12.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 406 | "all:\n\techo done\n".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 407 | ); |
| 408 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 409 | |
| 410 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "VAR = value\nall:\n\techo done\n"); |
| 411 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 412 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 1); |
| 413 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.rulesrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]rules().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 1); |
| 414 | } |
| 415 | |
| 416 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 417 | fn test_incremental_positioned_errors_shiftedrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_positioned_errors_shifted().() { |
| 418 | // Verify that positioned errors from the reparsed region get correct offsets |
| 419 | let old_text = "VAR1 = one\nVAR2 = two\n"; |
| 420 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 421 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(parse.okrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]ok().()); |
| 422 | |
| 423 | // Insert text that causes a parse error (indented line not in a rule) |
| 424 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().( |
| 425 | TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 11.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 426 | "\tbad line\n".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 427 | ); |
| 428 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 429 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "VAR1 = one\n\tbad line\nVAR2 = two\n"); |
| 430 | |
| 431 | // Full reparse should produce the same error count |
| 432 | let full = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(&new_text); |
| 433 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_parse.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]errors().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), full.errorsrust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]errors().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 434 | } |
| 435 | |
| 436 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 437 | fn test_incremental_with_includerust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_with_include().() { |
| 438 | let old_text = "include foo.mk\nVAR = value\n"; |
| 439 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 440 | |
| 441 | // Change include path |
| 442 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(8.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 14.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "bar.mk".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 443 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 444 | |
| 445 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "include bar.mk\nVAR = value\n"); |
| 446 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 447 | let includes: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = makefile.includesrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]includes().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 448 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(includes.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 1); |
| 449 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(includes[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..pathrust-analyzer cargo makefile-lossless 0.3.40 ast/include/impl#[Include]path().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("bar.mk".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 450 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 1); |
| 451 | } |
| 452 | |
| 453 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 454 | fn test_incremental_with_conditionalrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_with_conditional().() { |
| 455 | let old_text = "ifdef DEBUG\nCFLAGS = -g\nendif\nVAR = value\n"; |
| 456 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 457 | |
| 458 | // Change variable inside conditional |
| 459 | let edit = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(21.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 23.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "-O2".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 460 | let (new_parse, new_text) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit); |
| 461 | |
| 462 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(new_text, "ifdef DEBUG\nCFLAGS = -O2\nendif\nVAR = value\n"); |
| 463 | let makefile = new_parse.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 464 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 1); |
| 465 | // 2 variables: CFLAGS inside conditional + VAR at top level |
| 466 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(), 2); |
| 467 | } |
| 468 | |
| 469 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 470 | fn test_incremental_multiple_edits_sequentiallyrust-analyzer cargo makefile-lossless 0.3.40 incremental/tests/test_incremental_multiple_edits_sequentially().() { |
| 471 | let old_text = "VAR1 = one\nVAR2 = two\nVAR3 = three\n"; |
| 472 | let parse = Parserust-analyzer cargo makefile-lossless 0.3.40 parse/Parse#::parse_makefilerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<Makefile>`]parse_makefile().(old_text); |
| 473 | |
| 474 | // First edit: change VAR1 |
| 475 | let edit1 = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(7.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 10.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "ONE".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 476 | let (parse2, text2) = parse.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(old_text, &edit1); |
| 477 | |
| 478 | // Second edit: change VAR3 (in the new text) |
| 479 | let edit2 = TextEditrust-analyzer cargo makefile-lossless 0.3.40 incremental/TextEdit#::newrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[TextEdit]new().(TextRangerust-analyzer cargo text-size 1.1.1 range/TextRange#::newrust-analyzer cargo text-size 1.1.1 range/impl#[TextRange]new().(29.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), 34.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), "THREE".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 480 | let (parse3, text3) = parse2.apply_editrust-analyzer cargo makefile-lossless 0.3.40 incremental/impl#[`Parse<Makefile>`]apply_edit().(&text2, &edit2); |
| 481 | |
| 482 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text3, "VAR1 = ONE\nVAR2 = two\nVAR3 = THREE\n"); |
| 483 | let makefile = parse3.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 484 | let vars: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = makefile.variable_definitionsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]variable_definitions().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 485 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..raw_valuerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]raw_value().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("ONE".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 486 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..raw_valuerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]raw_value().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("two".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 487 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(vars[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().2]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..raw_valuerust-analyzer cargo makefile-lossless 0.3.40 ast/variable/impl#[VariableDefinition]raw_value().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("THREE".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 488 | } |
| 489 | } |