makefile-lossless 0.3.32

Lossless Parser for Makefiles
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use super::makefile::MakefileItem;
use crate::lossless::{remove_with_preceding_comments, Error, ErrorInfo, Include, ParseError};
use crate::SyntaxKind::{EXPR, IDENTIFIER};
use rowan::ast::AstNode;
use rowan::{GreenNodeBuilder, SyntaxNode};

impl Include {
    /// Get the raw path of the include directive
    pub fn path(&self) -> Option<String> {
        self.syntax()
            .children()
            .find(|it| it.kind() == EXPR)
            .map(|it| it.text().to_string().trim().to_string())
    }

    /// Get the text range of the path portion of the include directive.
    ///
    /// # Example
    /// ```
    /// use makefile_lossless::Makefile;
    /// let makefile: Makefile = "include config.mk\n".parse().unwrap();
    /// let inc = makefile.includes().next().unwrap();
    /// let range = inc.path_range().unwrap();
    /// assert_eq!(&makefile.to_string()[std::ops::Range::from(range)], "config.mk");
    /// ```
    pub fn path_range(&self) -> Option<rowan::TextRange> {
        self.syntax()
            .children()
            .find(|it| it.kind() == EXPR)
            .map(|it| it.text_range())
    }

    /// Check if this is an optional include (-include or sinclude)
    pub fn is_optional(&self) -> bool {
        let text = self.syntax().text();
        text.to_string().starts_with("-include") || text.to_string().starts_with("sinclude")
    }

    /// Get the parent item of this include directive, if any
    ///
    /// Returns `Some(MakefileItem)` if this include has a parent that is a MakefileItem
    /// (e.g., a Conditional), or `None` if the parent is the root Makefile node.
    ///
    /// # Example
    /// ```
    /// use makefile_lossless::Makefile;
    ///
    /// let makefile: Makefile = r#"ifdef DEBUG
    /// include debug.mk
    /// endif
    /// "#.parse().unwrap();
    /// let cond = makefile.conditionals().next().unwrap();
    /// let inc = cond.if_items().next().unwrap();
    /// // Include's parent is the conditional
    /// assert!(matches!(inc, makefile_lossless::MakefileItem::Include(_)));
    /// ```
    pub fn parent(&self) -> Option<MakefileItem> {
        self.syntax().parent().and_then(MakefileItem::cast)
    }

    /// Remove this include directive from the makefile
    ///
    /// This will also remove any preceding comments.
    ///
    /// # Example
    /// ```
    /// use makefile_lossless::Makefile;
    /// let mut makefile: Makefile = "include config.mk\nVAR = value\n".parse().unwrap();
    /// let mut inc = makefile.includes().next().unwrap();
    /// inc.remove().unwrap();
    /// assert_eq!(makefile.includes().count(), 0);
    /// ```
    pub fn remove(&mut self) -> Result<(), Error> {
        let Some(parent) = self.syntax().parent() else {
            return Err(Error::Parse(ParseError {
                errors: vec![ErrorInfo {
                    message: "Cannot remove include: no parent node".to_string(),
                    line: 1,
                    context: "include_remove".to_string(),
                }],
            }));
        };

        remove_with_preceding_comments(self.syntax(), &parent);
        Ok(())
    }

    /// Set the path of this include directive
    ///
    /// # Example
    /// ```
    /// use makefile_lossless::Makefile;
    /// let mut makefile: Makefile = "include old.mk\n".parse().unwrap();
    /// let mut inc = makefile.includes().next().unwrap();
    /// inc.set_path("new.mk");
    /// assert_eq!(inc.path(), Some("new.mk".to_string()));
    /// assert_eq!(makefile.to_string(), "include new.mk\n");
    /// ```
    pub fn set_path(&mut self, new_path: &str) {
        // Find the EXPR node containing the path
        let expr_index = self
            .syntax()
            .children()
            .find(|it| it.kind() == EXPR)
            .map(|it| it.index());

        if let Some(expr_idx) = expr_index {
            // Build a new EXPR node with the new path
            let mut builder = GreenNodeBuilder::new();
            builder.start_node(EXPR.into());
            builder.token(IDENTIFIER.into(), new_path);
            builder.finish_node();

            let new_expr = SyntaxNode::new_root_mut(builder.finish());

            // Replace the old EXPR with the new one
            self.syntax()
                .splice_children(expr_idx..expr_idx + 1, vec![new_expr.into()]);
        }
    }

    /// Make this include optional (change "include" to "-include")
    ///
    /// If the include is already optional, this has no effect.
    ///
    /// # Example
    /// ```
    /// use makefile_lossless::Makefile;
    /// let mut makefile: Makefile = "include config.mk\n".parse().unwrap();
    /// let mut inc = makefile.includes().next().unwrap();
    /// inc.set_optional(true);
    /// assert!(inc.is_optional());
    /// assert_eq!(makefile.to_string(), "-include config.mk\n");
    /// ```
    pub fn set_optional(&mut self, optional: bool) {
        use crate::SyntaxKind::INCLUDE;

        // Find the first IDENTIFIER token (which is the include keyword)
        let keyword_token = self.syntax().children_with_tokens().find(|it| {
            it.as_token()
                .map(|t| t.kind() == IDENTIFIER)
                .unwrap_or(false)
        });

        if let Some(token_element) = keyword_token {
            let token = token_element.as_token().unwrap();
            let current_text = token.text();

            let new_keyword = if optional {
                // Make it optional
                if current_text == "include" {
                    "-include"
                } else if current_text == "sinclude" || current_text == "-include" {
                    // Already optional, no change needed
                    return;
                } else {
                    // Shouldn't happen, but handle gracefully
                    return;
                }
            } else {
                // Make it non-optional
                if current_text == "-include" || current_text == "sinclude" {
                    "include"
                } else if current_text == "include" {
                    // Already non-optional, no change needed
                    return;
                } else {
                    // Shouldn't happen, but handle gracefully
                    return;
                }
            };

            // Rebuild the entire INCLUDE node, replacing just the keyword token
            let mut builder = GreenNodeBuilder::new();
            builder.start_node(INCLUDE.into());

            for child in self.syntax().children_with_tokens() {
                match child {
                    rowan::NodeOrToken::Token(tok)
                        if tok.kind() == IDENTIFIER && tok.text() == current_text =>
                    {
                        // Replace the include keyword
                        builder.token(IDENTIFIER.into(), new_keyword);
                    }
                    rowan::NodeOrToken::Token(tok) => {
                        // Copy other tokens as-is
                        builder.token(tok.kind().into(), tok.text());
                    }
                    rowan::NodeOrToken::Node(node) => {
                        // For nodes (like EXPR), rebuild them
                        builder.start_node(node.kind().into());
                        for node_child in node.children_with_tokens() {
                            if let rowan::NodeOrToken::Token(tok) = node_child {
                                builder.token(tok.kind().into(), tok.text());
                            }
                        }
                        builder.finish_node();
                    }
                }
            }

            builder.finish_node();
            let new_include = SyntaxNode::new_root_mut(builder.finish());

            // Replace the old INCLUDE node with the new one
            let index = self.syntax().index();
            if let Some(parent) = self.syntax().parent() {
                parent.splice_children(index..index + 1, vec![new_include.clone().into()]);

                // Update self to point to the new node
                *self = Include::cast(
                    parent
                        .children_with_tokens()
                        .nth(index)
                        .and_then(|it| it.into_node())
                        .unwrap(),
                )
                .unwrap();
            }
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::lossless::Makefile;

    #[test]
    fn test_include_parent() {
        let makefile: Makefile = "include common.mk\n".parse().unwrap();

        let inc = makefile.includes().next().unwrap();
        let parent = inc.parent();
        // Parent is ROOT node which doesn't cast to MakefileItem
        assert!(parent.is_none());
    }

    #[test]
    fn test_add_include() {
        let mut makefile = Makefile::new();
        makefile.add_include("config.mk");

        let includes: Vec<_> = makefile.includes().collect();
        assert_eq!(includes.len(), 1);
        assert_eq!(includes[0].path(), Some("config.mk".to_string()));

        let files: Vec<_> = makefile.included_files().collect();
        assert_eq!(files, vec!["config.mk"]);

        // Check the generated text
        assert_eq!(makefile.to_string(), "include config.mk\n");
    }

    #[test]
    fn test_add_include_to_existing() {
        let mut makefile: Makefile = "VAR = value\nrule:\n\tcommand\n".parse().unwrap();
        makefile.add_include("config.mk");

        // Include should be added at the beginning
        let files: Vec<_> = makefile.included_files().collect();
        assert_eq!(files, vec!["config.mk"]);

        // Check that the include comes first
        let text = makefile.to_string();
        assert!(text.starts_with("include config.mk\n"));
        assert!(text.contains("VAR = value"));
    }

    #[test]
    fn test_insert_include() {
        let mut makefile: Makefile = "VAR = value\nrule:\n\tcommand\n".parse().unwrap();
        makefile.insert_include(1, "config.mk").unwrap();

        let items: Vec<_> = makefile.items().collect();
        assert_eq!(items.len(), 3);

        // Check the middle item is the include
        let files: Vec<_> = makefile.included_files().collect();
        assert_eq!(files, vec!["config.mk"]);
    }

    #[test]
    fn test_insert_include_at_beginning() {
        let mut makefile: Makefile = "VAR = value\n".parse().unwrap();
        makefile.insert_include(0, "config.mk").unwrap();

        let text = makefile.to_string();
        assert!(text.starts_with("include config.mk\n"));
    }

    #[test]
    fn test_insert_include_at_end() {
        let mut makefile: Makefile = "VAR = value\n".parse().unwrap();
        let item_count = makefile.items().count();
        makefile.insert_include(item_count, "config.mk").unwrap();

        let text = makefile.to_string();
        assert!(text.ends_with("include config.mk\n"));
    }

    #[test]
    fn test_insert_include_out_of_bounds() {
        let mut makefile: Makefile = "VAR = value\n".parse().unwrap();
        let result = makefile.insert_include(100, "config.mk");
        assert!(result.is_err());
    }

    #[test]
    fn test_insert_include_after() {
        let mut makefile: Makefile = "VAR1 = value1\nVAR2 = value2\n".parse().unwrap();
        let first_var = makefile.items().next().unwrap();
        makefile
            .insert_include_after(&first_var, "config.mk")
            .unwrap();

        let files: Vec<_> = makefile.included_files().collect();
        assert_eq!(files, vec!["config.mk"]);

        // Check that the include is after VAR1
        let text = makefile.to_string();
        let var1_pos = text.find("VAR1").unwrap();
        let include_pos = text.find("include config.mk").unwrap();
        assert!(include_pos > var1_pos);
    }

    #[test]
    fn test_insert_include_after_with_rule() {
        let mut makefile: Makefile = "rule1:\n\tcommand1\nrule2:\n\tcommand2\n".parse().unwrap();
        let first_rule_item = makefile.items().next().unwrap();
        makefile
            .insert_include_after(&first_rule_item, "config.mk")
            .unwrap();

        let text = makefile.to_string();
        let rule1_pos = text.find("rule1:").unwrap();
        let include_pos = text.find("include config.mk").unwrap();
        let rule2_pos = text.find("rule2:").unwrap();

        // Include should be between rule1 and rule2
        assert!(include_pos > rule1_pos);
        assert!(include_pos < rule2_pos);
    }

    #[test]
    fn test_include_remove() {
        let makefile: Makefile = "include config.mk\nVAR = value\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.remove().unwrap();

        assert_eq!(makefile.includes().count(), 0);
        assert_eq!(makefile.to_string(), "VAR = value\n");
    }

    #[test]
    fn test_include_remove_multiple() {
        let makefile: Makefile = "include first.mk\ninclude second.mk\nVAR = value\n"
            .parse()
            .unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.remove().unwrap();

        assert_eq!(makefile.includes().count(), 1);
        let remaining = makefile.includes().next().unwrap();
        assert_eq!(remaining.path(), Some("second.mk".to_string()));
    }

    #[test]
    fn test_include_set_path() {
        let makefile: Makefile = "include old.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.set_path("new.mk");

        assert_eq!(inc.path(), Some("new.mk".to_string()));
        assert_eq!(makefile.to_string(), "include new.mk\n");
    }

    #[test]
    fn test_include_set_path_preserves_optional() {
        let makefile: Makefile = "-include old.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.set_path("new.mk");

        assert_eq!(inc.path(), Some("new.mk".to_string()));
        assert!(inc.is_optional());
        assert_eq!(makefile.to_string(), "-include new.mk\n");
    }

    #[test]
    fn test_include_set_optional_true() {
        let makefile: Makefile = "include config.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.set_optional(true);

        assert!(inc.is_optional());
        assert_eq!(makefile.to_string(), "-include config.mk\n");
    }

    #[test]
    fn test_include_set_optional_false() {
        let makefile: Makefile = "-include config.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.set_optional(false);

        assert!(!inc.is_optional());
        assert_eq!(makefile.to_string(), "include config.mk\n");
    }

    #[test]
    fn test_include_set_optional_from_sinclude() {
        let makefile: Makefile = "sinclude config.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.set_optional(false);

        assert!(!inc.is_optional());
        assert_eq!(makefile.to_string(), "include config.mk\n");
    }

    #[test]
    fn test_include_set_optional_already_optional() {
        let makefile: Makefile = "-include config.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.set_optional(true);

        // Should remain unchanged
        assert!(inc.is_optional());
        assert_eq!(makefile.to_string(), "-include config.mk\n");
    }

    #[test]
    fn test_include_set_optional_already_non_optional() {
        let makefile: Makefile = "include config.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.set_optional(false);

        // Should remain unchanged
        assert!(!inc.is_optional());
        assert_eq!(makefile.to_string(), "include config.mk\n");
    }

    #[test]
    fn test_include_combined_operations() {
        let makefile: Makefile = "include old.mk\nVAR = value\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();

        // Change path and make optional
        inc.set_path("new.mk");
        inc.set_optional(true);

        assert_eq!(inc.path(), Some("new.mk".to_string()));
        assert!(inc.is_optional());
        assert_eq!(makefile.to_string(), "-include new.mk\nVAR = value\n");
    }

    #[test]
    fn test_include_path_range() {
        let makefile: Makefile = "include config.mk\n".parse().unwrap();
        let inc = makefile.includes().next().unwrap();
        let range = inc.path_range().unwrap();
        assert_eq!(
            &makefile.to_string()[std::ops::Range::from(range)],
            "config.mk"
        );
    }

    #[test]
    fn test_include_path_range_optional() {
        let makefile: Makefile = "-include optional.mk\n".parse().unwrap();
        let inc = makefile.includes().next().unwrap();
        let range = inc.path_range().unwrap();
        assert_eq!(
            &makefile.to_string()[std::ops::Range::from(range)],
            "optional.mk"
        );
    }

    #[test]
    fn test_include_path_range_sinclude() {
        let makefile: Makefile = "sinclude silent.mk\n".parse().unwrap();
        let inc = makefile.includes().next().unwrap();
        let range = inc.path_range().unwrap();
        assert_eq!(
            &makefile.to_string()[std::ops::Range::from(range)],
            "silent.mk"
        );
    }

    #[test]
    fn test_include_with_comment() {
        let makefile: Makefile = "# Comment\ninclude config.mk\n".parse().unwrap();
        let mut inc = makefile.includes().next().unwrap();
        inc.remove().unwrap();

        // Comment should also be removed
        assert_eq!(makefile.includes().count(), 0);
        assert!(!makefile.to_string().contains("# Comment"));
    }
}