| 1 | use superrust-analyzer cargo makefile-lossless 0.3.40 ast/::makefilerust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/::MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#; |
| 2 | use craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::{remove_with_preceding_commentsrust-analyzer cargo makefile-lossless 0.3.40 lossless/remove_with_preceding_comments()., Conditionalrust-analyzer cargo makefile-lossless 0.3.40 lossless/Conditional#, Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#, ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#, ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#}; |
| 3 | use craterust-analyzer cargo makefile-lossless 0.3.40 crate/::SyntaxKindrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#::*; |
| 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/::{GreenNodeBuilderrust-analyzer cargo rowan 0.16.1 green/builder/GreenNodeBuilder#, SyntaxNoderust-analyzer cargo rowan 0.16.1 api/SyntaxNode#}; |
| 6 | |
| 7 | /// Split `s` at the first top-level comma. A comma is "top-level" when it |
| 8 | /// is not inside a `$(...)` / `${...}` group. Returns `None` if no such |
| 9 | /// comma exists. |
| 10 | fn split_top_level_commarust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/split_top_level_comma().(s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(&str, &str)> { |
| 11 | let bytes = s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 12 | let mut paren = 0usize; |
| 13 | let mut brace = 0usize; |
| 14 | let mut i = 0; |
| 15 | while i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 16 | match bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). { |
| 17 | b'$' if i 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 cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() => { |
| 18 | let next = bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i 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/index/Index#index().; |
| 19 | if next rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'(' { |
| 20 | paren rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 21 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().2; |
| 22 | continue; |
| 23 | } |
| 24 | if next rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'{' { |
| 25 | brace rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 26 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().2; |
| 27 | continue; |
| 28 | } |
| 29 | // $$ or $X — skip both. |
| 30 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().2; |
| 31 | continue; |
| 32 | } |
| 33 | b'(' => paren rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1, |
| 34 | b')' => paren = paren.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(1), |
| 35 | b'{' => brace rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1, |
| 36 | b'}' => brace = brace.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(1), |
| 37 | b',' if paren rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 && brace rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 => { |
| 38 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((&s[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#i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i 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 core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)); |
| 39 | } |
| 40 | _ => {} |
| 41 | } |
| 42 | i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 43 | } |
| 44 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 45 | } |
| 46 | |
| 47 | /// Extract two quoted strings from `s`, in the form `"a" "b"` or `'a' 'b'`. |
| 48 | /// Returns `None` if fewer than two quoted strings are present. Surrounding |
| 49 | /// quote characters are stripped from each result. |
| 50 | fn quoted_pairrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/quoted_pair().(s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<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#>> { |
| 51 | let mut out = 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().(); |
| 52 | let mut iter = s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(); |
| 53 | while iter.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 54 | // Skip leading whitespace between args. |
| 55 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&c) = iter.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().() { |
| 56 | if c.is_ascii_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_whitespace().() { |
| 57 | iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().(); |
| 58 | } else { |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | let opener = match iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 63 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c @ ('"' | '\'')) => c, |
| 64 | _ => break, |
| 65 | }; |
| 66 | let mut buf = 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().(); |
| 67 | for c in iter.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().() { |
| 68 | if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().opener { |
| 69 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(buf); |
| 70 | buf = 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().(); |
| 71 | break; |
| 72 | } |
| 73 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c); |
| 74 | } |
| 75 | } |
| 76 | if out.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>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().2 { |
| 77 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(out) |
| 78 | } else { |
| 79 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl Conditionalrust-analyzer cargo makefile-lossless 0.3.40 lossless/Conditional# { |
| 84 | /// Get the parent item of this conditional, if any |
| 85 | /// |
| 86 | /// Returns `Some(MakefileItem)` if this conditional has a parent that is a MakefileItem |
| 87 | /// (e.g., another Conditional for nested conditionals), or `None` if the parent is the root Makefile node. |
| 88 | /// |
| 89 | /// # Example |
| 90 | /// ``` |
| 91 | /// use makefile_lossless::Makefile; |
| 92 | /// |
| 93 | /// let makefile: Makefile = r#"ifdef OUTER |
| 94 | /// ifdef INNER |
| 95 | /// VAR = value |
| 96 | /// endif |
| 97 | /// endif |
| 98 | /// "#.parse().unwrap(); |
| 99 | /// |
| 100 | /// let outer = makefile.conditionals().next().unwrap(); |
| 101 | /// let inner = outer.if_items().find_map(|item| { |
| 102 | /// if let makefile_lossless::MakefileItem::Conditional(c) = item { |
| 103 | /// Some(c) |
| 104 | /// } else { |
| 105 | /// None |
| 106 | /// } |
| 107 | /// }).unwrap(); |
| 108 | /// // Inner conditional's parent is the outer conditional |
| 109 | /// assert!(inner.parent().is_some()); |
| 110 | /// ``` |
| 111 | pub fn parentrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]parent().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#> { |
| 112 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().parentrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]parent().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#::castrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[MakefileItem]cast().) |
| 113 | } |
| 114 | |
| 115 | /// Get the type of conditional (ifdef, ifndef, ifeq, ifneq) |
| 116 | pub fn conditional_typerust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]conditional_type().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 117 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 118 | .childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().() |
| 119 | .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|it| it.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_IFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_IF#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). |
| 120 | .children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() |
| 121 | .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|it| it.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().IDENTIFIERrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#IDENTIFIER#) |
| 122 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|it| it.as_tokenrust-analyzer cargo rowan 0.16.1 utility_types/impl#[`NodeOrToken<N, T>`]as_token().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().textrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxToken<L>`]text().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 123 | } |
| 124 | |
| 125 | /// Get the condition expression |
| 126 | pub fn conditionrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]condition().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 127 | let if_node = self |
| 128 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 129 | .childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().() |
| 130 | .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|it| it.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_IFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_IF#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 131 | |
| 132 | // Find the EXPR node which contains the condition |
| 133 | let expr_node = if_node.childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|it| it.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().EXPRrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#EXPR#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 134 | |
| 135 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(expr_node.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().().trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 136 | } |
| 137 | |
| 138 | /// For an `ifeq` / `ifneq` conditional, return the two argument |
| 139 | /// strings (unexpanded). Supports both the `(a,b)` and `"a" "b"` |
| 140 | /// (or `'a' 'b'`) syntaxes. |
| 141 | /// |
| 142 | /// Returns `None` for `ifdef` / `ifndef` (or if the args can't be |
| 143 | /// recovered). |
| 144 | /// |
| 145 | /// # Example |
| 146 | /// ``` |
| 147 | /// use makefile_lossless::Makefile; |
| 148 | /// let mf: Makefile = "ifeq ($(A),$(B))\nX = 1\nendif\n".parse().unwrap(); |
| 149 | /// let c = mf.conditionals().next().unwrap(); |
| 150 | /// assert_eq!(c.ifeq_args(), Some(("$(A)".to_string(), "$(B)".to_string()))); |
| 151 | /// ``` |
| 152 | pub fn ifeq_argsrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]ifeq_args().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)> { |
| 153 | let if_node = self |
| 154 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 155 | .childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().() |
| 156 | .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|it| it.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_IFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_IF#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 157 | // Inside CONDITIONAL_IF there is one EXPR node wrapping the args. |
| 158 | let wrapper = if_node.childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|it| it.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().EXPRrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#EXPR#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 159 | |
| 160 | let text = wrapper.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().(); |
| 161 | let stripped = text.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().(); |
| 162 | // Form 1: parenthesised — `(a, b)`. Split at the top-level comma, |
| 163 | // ignoring commas inside nested `$(...)` / `${...}`. |
| 164 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(inner) = stripped |
| 165 | .strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('(') |
| 166 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| s.trim_endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end().().strip_suffixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_suffix().(')')) |
| 167 | { |
| 168 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((a, b)) = split_top_level_commarust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/split_top_level_comma().(inner) { |
| 169 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((a.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), b.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Form 2: quoted — `"a" "b"` or `'a' 'b'`. |
| 174 | let mut parts = quoted_pairrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/quoted_pair().(&text)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 175 | let b = parts.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]pop().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 176 | let a = parts.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]pop().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 177 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((a, b)) |
| 178 | } |
| 179 | |
| 180 | /// Check if this conditional has an else clause |
| 181 | pub fn has_elserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]has_else().(&self) -> bool { |
| 182 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 183 | .childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().() |
| 184 | .anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|it| it.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ELSErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ELSE#) |
| 185 | } |
| 186 | |
| 187 | /// Get the body content of the if branch |
| 188 | pub fn if_bodyrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]if_body().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 189 | let mut body = 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().(); |
| 190 | let mut in_if_body = false; |
| 191 | |
| 192 | for child in self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() { |
| 193 | if child.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_IFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_IF# { |
| 194 | in_if_body = true; |
| 195 | continue; |
| 196 | } |
| 197 | if child.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ELSErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ELSE# || child.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF# { |
| 198 | break; |
| 199 | } |
| 200 | if in_if_body { |
| 201 | body.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(child.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if body.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() { |
| 206 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 207 | } else { |
| 208 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(body) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// Get the body content of the else branch (if it exists) |
| 213 | pub fn else_bodyrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]else_body().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 214 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.has_elserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]has_else().() { |
| 215 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 216 | } |
| 217 | |
| 218 | let mut body = 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().(); |
| 219 | let mut in_else_body = false; |
| 220 | |
| 221 | for child in self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() { |
| 222 | if child.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ELSErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ELSE# { |
| 223 | in_else_body = true; |
| 224 | continue; |
| 225 | } |
| 226 | if child.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF# { |
| 227 | break; |
| 228 | } |
| 229 | if in_else_body { |
| 230 | body.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(child.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if body.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() { |
| 235 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 236 | } else { |
| 237 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(body) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// Remove this conditional from the makefile |
| 242 | pub fn removerust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]remove().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#> { |
| 243 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent) = self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().parentrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]parent().() else { |
| 244 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#::Parserust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#Parse#(ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError# { |
| 245 | errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#errors.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo# { |
| 246 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message.: "Cannot remove conditional: no parent node".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 247 | linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line.: 1, |
| 248 | contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context.: "conditional_remove".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 249 | }], |
| 250 | })); |
| 251 | }; |
| 252 | |
| 253 | remove_with_preceding_commentsrust-analyzer cargo makefile-lossless 0.3.40 lossless/remove_with_preceding_comments().(self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().(), &parent); |
| 254 | |
| 255 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 256 | } |
| 257 | |
| 258 | /// Remove the conditional directives (ifdef/endif) but keep the body content |
| 259 | /// |
| 260 | /// This "unwraps" the conditional, keeping only the if branch content. |
| 261 | /// Returns an error if the conditional has an else clause. |
| 262 | /// |
| 263 | /// # Example |
| 264 | /// ``` |
| 265 | /// use makefile_lossless::Makefile; |
| 266 | /// let mut makefile: Makefile = r#"ifdef DEBUG |
| 267 | /// VAR = debug |
| 268 | /// endif |
| 269 | /// "#.parse().unwrap(); |
| 270 | /// let mut cond = makefile.conditionals().next().unwrap(); |
| 271 | /// cond.unwrap().unwrap(); |
| 272 | /// // Now makefile contains just "VAR = debug\n" |
| 273 | /// assert!(makefile.to_string().contains("VAR = debug")); |
| 274 | /// assert!(!makefile.to_string().contains("ifdef")); |
| 275 | /// ``` |
| 276 | pub fn unwraprust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]unwrap().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#> { |
| 277 | // Check if there's an else clause |
| 278 | if self.has_elserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]has_else().() { |
| 279 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#::Parserust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#Parse#(ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError# { |
| 280 | errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#errors.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo# { |
| 281 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message.: "Cannot unwrap conditional with else clause".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 282 | linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line.: 1, |
| 283 | contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context.: "conditional_unwrap".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 284 | }], |
| 285 | })); |
| 286 | } |
| 287 | |
| 288 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent) = self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().parentrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]parent().() else { |
| 289 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#::Parserust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#Parse#(ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError# { |
| 290 | errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#errors.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo# { |
| 291 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message.: "Cannot unwrap conditional: no parent node".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 292 | linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line.: 1, |
| 293 | contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context.: "conditional_unwrap".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 294 | }], |
| 295 | })); |
| 296 | }; |
| 297 | |
| 298 | // Collect the body items (everything between CONDITIONAL_IF and CONDITIONAL_ENDIF) |
| 299 | let body_nodes: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = self |
| 300 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 301 | .children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() |
| 302 | .skip_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip_while().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().CONDITIONAL_IFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_IF#) |
| 303 | .skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(1) // Skip CONDITIONAL_IF itself |
| 304 | .take_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take_while().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF#) |
| 305 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 306 | |
| 307 | // Find the position of this conditional in parent |
| 308 | let conditional_index = self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().indexrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]index().(); |
| 309 | |
| 310 | // Replace the entire conditional with just its body items |
| 311 | parent.splice_childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]splice_children().(conditional_index..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#conditional_index 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, body_nodes); |
| 312 | |
| 313 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 314 | } |
| 315 | |
| 316 | /// Get all items (rules, variables, includes, nested conditionals) in the if branch |
| 317 | /// |
| 318 | /// # Example |
| 319 | /// ``` |
| 320 | /// use makefile_lossless::Makefile; |
| 321 | /// let makefile: Makefile = r#"ifdef DEBUG |
| 322 | /// VAR = debug |
| 323 | /// rule: |
| 324 | /// command |
| 325 | /// endif |
| 326 | /// "#.parse().unwrap(); |
| 327 | /// let cond = makefile.conditionals().next().unwrap(); |
| 328 | /// let items: Vec<_> = cond.if_items().collect(); |
| 329 | /// assert_eq!(items.len(), 2); // One variable, one rule |
| 330 | /// ``` |
| 331 | pub fn if_itemsrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]if_items().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#> + '_ { |
| 332 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 333 | .childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().() |
| 334 | .skip_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip_while().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().CONDITIONAL_IFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_IF#) |
| 335 | .skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(1) // Skip the CONDITIONAL_IF itself |
| 336 | .take_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take_while().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().CONDITIONAL_ELSErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ELSE# && n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF#) |
| 337 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#::castrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[MakefileItem]cast().) |
| 338 | } |
| 339 | |
| 340 | /// Get all items (rules, variables, includes, nested conditionals) in the else branch |
| 341 | /// |
| 342 | /// # Example |
| 343 | /// ``` |
| 344 | /// use makefile_lossless::Makefile; |
| 345 | /// let makefile: Makefile = r#"ifdef DEBUG |
| 346 | /// VAR = debug |
| 347 | /// else |
| 348 | /// VAR = release |
| 349 | /// endif |
| 350 | /// "#.parse().unwrap(); |
| 351 | /// let cond = makefile.conditionals().next().unwrap(); |
| 352 | /// let items: Vec<_> = cond.else_items().collect(); |
| 353 | /// assert_eq!(items.len(), 1); // One variable in else branch |
| 354 | /// ``` |
| 355 | pub fn else_itemsrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]else_items().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#> + '_ { |
| 356 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 357 | .childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children().() |
| 358 | .skip_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip_while().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().CONDITIONAL_ELSErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ELSE#) |
| 359 | .skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(1) // Skip the CONDITIONAL_ELSE itself |
| 360 | .take_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take_while().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF#) |
| 361 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#::castrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[MakefileItem]cast().) |
| 362 | } |
| 363 | |
| 364 | /// Add an item to the if branch of the conditional |
| 365 | /// |
| 366 | /// # Example |
| 367 | /// ``` |
| 368 | /// use makefile_lossless::{Makefile, MakefileItem}; |
| 369 | /// let mut makefile: Makefile = "ifdef DEBUG\nendif\n".parse().unwrap(); |
| 370 | /// let mut cond = makefile.conditionals().next().unwrap(); |
| 371 | /// let temp: Makefile = "CFLAGS = -g\n".parse().unwrap(); |
| 372 | /// let var = temp.variable_definitions().next().unwrap(); |
| 373 | /// cond.add_if_item(MakefileItem::Variable(var)); |
| 374 | /// assert!(makefile.to_string().contains("CFLAGS = -g")); |
| 375 | /// ``` |
| 376 | pub fn add_if_itemrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_if_item().(&mut self, item: MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#) { |
| 377 | let item_node = item.syntaxrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[MakefileItem]syntax().().clonerust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`][Clone]clone().(); |
| 378 | |
| 379 | // Find position after CONDITIONAL_IF |
| 380 | let insert_pos = self |
| 381 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 382 | .children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() |
| 383 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#position().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_IFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_IF#) |
| 384 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|p| p 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) |
| 385 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0); |
| 386 | |
| 387 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 388 | .splice_childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]splice_children().(insert_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#insert_pos, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![item_node.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()]); |
| 389 | } |
| 390 | |
| 391 | /// Add an item to the else branch of the conditional |
| 392 | /// |
| 393 | /// If the conditional doesn't have an else branch, this will create one. |
| 394 | /// |
| 395 | /// # Example |
| 396 | /// ``` |
| 397 | /// use makefile_lossless::{Makefile, MakefileItem}; |
| 398 | /// let mut makefile: Makefile = "ifdef DEBUG\nVAR=1\nendif\n".parse().unwrap(); |
| 399 | /// let mut cond = makefile.conditionals().next().unwrap(); |
| 400 | /// let temp: Makefile = "CFLAGS = -O2\n".parse().unwrap(); |
| 401 | /// let var = temp.variable_definitions().next().unwrap(); |
| 402 | /// cond.add_else_item(MakefileItem::Variable(var)); |
| 403 | /// assert!(makefile.to_string().contains("else")); |
| 404 | /// assert!(makefile.to_string().contains("CFLAGS = -O2")); |
| 405 | /// ``` |
| 406 | pub fn add_else_itemrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_else_item().(&mut self, item: MakefileItemrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/MakefileItem#) { |
| 407 | // Ensure there's an else clause |
| 408 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.has_elserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]has_else().() { |
| 409 | self.add_else_clauserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_else_clause().(); |
| 410 | } |
| 411 | |
| 412 | let item_node = item.syntaxrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[MakefileItem]syntax().().clonerust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`][Clone]clone().(); |
| 413 | |
| 414 | // Find position after CONDITIONAL_ELSE |
| 415 | let insert_pos = self |
| 416 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 417 | .children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() |
| 418 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#position().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ELSErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ELSE#) |
| 419 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|p| p 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) |
| 420 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0); |
| 421 | |
| 422 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 423 | .splice_childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]splice_children().(insert_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#insert_pos, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![item_node.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()]); |
| 424 | } |
| 425 | |
| 426 | /// Add a matching `endif` to close this conditional, if it doesn't already |
| 427 | /// have one. |
| 428 | /// |
| 429 | /// Returns `Ok(true)` if a CONDITIONAL_ENDIF was inserted, `Ok(false)` if |
| 430 | /// the conditional was already terminated. Returns an error if the |
| 431 | /// conditional has no recognized opener (e.g. a bare `else`/`endif` that |
| 432 | /// the parser wrapped in a CONDITIONAL node). |
| 433 | /// |
| 434 | /// If the existing body does not end with a newline, one is inserted |
| 435 | /// before the `endif` so it lands on its own line. |
| 436 | /// |
| 437 | /// # Example |
| 438 | /// ``` |
| 439 | /// use makefile_lossless::Makefile; |
| 440 | /// let makefile: Makefile = "ifdef DEBUG\nVAR = 1\n".parse().unwrap(); |
| 441 | /// let mut cond = makefile.conditionals().next().unwrap(); |
| 442 | /// assert!(cond.add_endif().unwrap()); |
| 443 | /// assert_eq!(makefile.code(), "ifdef DEBUG\nVAR = 1\nendif\n"); |
| 444 | /// ``` |
| 445 | pub fn add_endifrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_endif().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#> { |
| 446 | if self.conditional_typerust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]conditional_type().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 447 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#::Parserust-analyzer cargo makefile-lossless 0.3.40 lossless/Error#Parse#(ParseErrorrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError# { |
| 448 | errorsrust-analyzer cargo makefile-lossless 0.3.40 lossless/ParseError#errors.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ErrorInforust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo# { |
| 449 | messagerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#message.: "Cannot add endif to conditional with no opener".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 450 | linerust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#line.: 1, |
| 451 | contextrust-analyzer cargo makefile-lossless 0.3.40 lossless/ErrorInfo#context.: "conditional_add_endif".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 452 | }], |
| 453 | })); |
| 454 | } |
| 455 | if self |
| 456 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 457 | .children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() |
| 458 | .anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|c| c.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF#) |
| 459 | { |
| 460 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false); |
| 461 | } |
| 462 | |
| 463 | // Need a newline before `endif` if the current tail doesn't already end |
| 464 | // with one. |
| 465 | let needs_newline = self |
| 466 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 467 | .last_tokenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]last_token().() |
| 468 | .is_none_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none_or().(|t| t.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxToken<L>`]kind().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().NEWLINErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#NEWLINE#); |
| 469 | |
| 470 | let mut builder = GreenNodeBuilderrust-analyzer cargo rowan 0.16.1 green/builder/GreenNodeBuilder#::newrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]new().(); |
| 471 | builder.start_noderust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]start_node().(CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 472 | if needs_newline { |
| 473 | builder.tokenrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]token().(NEWLINErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#NEWLINE#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "\n"); |
| 474 | } |
| 475 | builder.tokenrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]token().(IDENTIFIERrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#IDENTIFIER#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "endif"); |
| 476 | builder.tokenrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]token().(NEWLINErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#NEWLINE#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "\n"); |
| 477 | builder.finish_noderust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]finish_node().(); |
| 478 | |
| 479 | let endif = 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().(builder.finishrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]finish().()); |
| 480 | let count = self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(); |
| 481 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 482 | .splice_childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]splice_children().(count..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#count, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![endif.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()]); |
| 483 | |
| 484 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true) |
| 485 | } |
| 486 | |
| 487 | /// Add an else clause to the conditional if it doesn't already have one |
| 488 | fn add_else_clauserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_else_clause().(&mut self) { |
| 489 | if self.has_elserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]has_else().() { |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | let mut builder = GreenNodeBuilderrust-analyzer cargo rowan 0.16.1 green/builder/GreenNodeBuilder#::newrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]new().(); |
| 494 | builder.start_noderust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]start_node().(CONDITIONAL_ELSErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ELSE#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 495 | builder.tokenrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]token().(IDENTIFIERrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#IDENTIFIER#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "else"); |
| 496 | builder.tokenrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]token().(NEWLINErust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#NEWLINE#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "\n"); |
| 497 | builder.finish_noderust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]finish_node().(); |
| 498 | |
| 499 | let syntax = 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().(builder.finishrust-analyzer cargo rowan 0.16.1 green/builder/impl#[`GreenNodeBuilder<'_>`]finish().()); |
| 500 | |
| 501 | // Find position before CONDITIONAL_ENDIF |
| 502 | let insert_pos = self |
| 503 | .syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 504 | .children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().() |
| 505 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#position().(|n| n.kindrust-analyzer cargo rowan 0.16.1 api/impl#[`NodeOrToken<SyntaxNode<L>, SyntaxToken<L>>`]kind().() rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().==rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq(). rust-analyzer cargo makefile-lossless 0.3.40 impl#[SyntaxKind][`PartialEq<Self>`]eq().CONDITIONAL_ENDIFrust-analyzer cargo makefile-lossless 0.3.40 SyntaxKind#CONDITIONAL_ENDIF#) |
| 506 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().().children_with_tokensrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]children_with_tokens().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().()); |
| 507 | |
| 508 | self.syntaxrust-analyzer cargo makefile-lossless 0.3.40 lossless/impl#[Conditional][AstNode]syntax().() |
| 509 | .splice_childrenrust-analyzer cargo rowan 0.16.1 api/impl#[`SyntaxNode<L>`]splice_children().(insert_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#insert_pos, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![syntax.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()]); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | #[cfg(test)] |
| 514 | mod testsrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/ { |
| 515 | |
| 516 | use craterust-analyzer cargo makefile-lossless 0.3.40 crate/::losslessrust-analyzer cargo makefile-lossless 0.3.40 lossless/::Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#; |
| 517 | |
| 518 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 519 | fn test_conditional_parentrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/test_conditional_parent().() { |
| 520 | let makefile: Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile# = r#"ifdef DEBUG |
| 521 | VAR = debug |
| 522 | endif |
| 523 | "# |
| 524 | .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() |
| 525 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 526 | |
| 527 | let cond = makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 528 | let parent = cond.parentrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]parent().(); |
| 529 | // Parent is ROOT node which doesn't cast to MakefileItem |
| 530 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(parent.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 531 | } |
| 532 | |
| 533 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 534 | fn test_add_endif_to_unterminatedrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/test_add_endif_to_unterminated().() { |
| 535 | let makefile: Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile# = "ifdef DEBUG\nVAR = 1\n".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 536 | let mut cond = makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 537 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(cond.add_endifrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_endif().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 538 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.coderust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]code().(), "ifdef DEBUG\nVAR = 1\nendif\n"); |
| 539 | } |
| 540 | |
| 541 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 542 | fn test_add_endif_already_terminated_nooprust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/test_add_endif_already_terminated_noop().() { |
| 543 | let makefile: Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile# = "ifdef DEBUG\nVAR = 1\nendif\n".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 544 | let mut cond = makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 545 | 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().cond.add_endifrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_endif().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 546 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.coderust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]code().(), "ifdef DEBUG\nVAR = 1\nendif\n"); |
| 547 | } |
| 548 | |
| 549 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 550 | fn test_add_endif_no_trailing_newlinerust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/test_add_endif_no_trailing_newline().() { |
| 551 | // Source with no final newline produces a parse error, but the tree |
| 552 | // is still usable for mutations. |
| 553 | let parsed = Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#::parserust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]parse().("ifdef DEBUG\nVAR = 1"); |
| 554 | let makefile = parsed.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 555 | let mut cond = makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 556 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(cond.add_endifrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_endif().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 557 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.coderust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]code().(), "ifdef DEBUG\nVAR = 1\nendif\n"); |
| 558 | } |
| 559 | |
| 560 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 561 | fn test_add_endif_with_elserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/test_add_endif_with_else().() { |
| 562 | let makefile: Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile# = "ifdef DEBUG\nA = 1\nelse\nA = 2\n".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 563 | let mut cond = makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 564 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(cond.add_endifrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_endif().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 565 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.coderust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]code().(), "ifdef DEBUG\nA = 1\nelse\nA = 2\nendif\n"); |
| 566 | } |
| 567 | |
| 568 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 569 | fn test_add_endif_rejects_bare_elserust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/test_add_endif_rejects_bare_else().() { |
| 570 | // A bare `else`/`endif` is wrapped in a Conditional node with no |
| 571 | // CONDITIONAL_IF, so conditional_type() is None. We refuse to add |
| 572 | // an `endif` to it — the parser already complains. |
| 573 | let parsed = Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile#::parserust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]parse().("else\nVAR = 1\n"); |
| 574 | let makefile = parsed.treerust-analyzer cargo makefile-lossless 0.3.40 parse/impl#[`Parse<T>`]tree().(); |
| 575 | let mut cond = makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 576 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(cond.conditional_typerust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]conditional_type().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 577 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(cond.add_endifrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_endif().().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 578 | } |
| 579 | |
| 580 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 581 | fn test_add_endif_preserves_existing_bodyrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/tests/test_add_endif_preserves_existing_body().() { |
| 582 | let makefile: Makefilerust-analyzer cargo makefile-lossless 0.3.40 lossless/Makefile# = "ifeq ($(X),y)\nA = 1\nB = 2\n".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 583 | let mut cond = makefile.conditionalsrust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]conditionals().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 584 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(cond.add_endifrust-analyzer cargo makefile-lossless 0.3.40 ast/conditional/impl#[Conditional]add_endif().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 585 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(makefile.coderust-analyzer cargo makefile-lossless 0.3.40 ast/makefile/impl#[Makefile]code().(), "ifeq ($(X),y)\nA = 1\nB = 2\nendif\n"); |
| 586 | } |
| 587 | } |