|
| 1 | +//! Alphabetically sorts and deduplicates `use` clauses inside a design unit's context clause using |
| 2 | +//! the [`Rewriter`](vhdl_syntax::syntax::rewrite) API. |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 | +// You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | +// |
| 7 | +// Copyright (c) 2026, Lukas Scheller lukasscheller@icloud.com |
| 8 | +use std::collections::HashMap; |
| 9 | +use vhdl_syntax::parser; |
| 10 | +use vhdl_syntax::syntax::node::{SyntaxElement, SyntaxNode}; |
| 11 | +use vhdl_syntax::syntax::rewrite::RewriteAction; |
| 12 | +use vhdl_syntax::syntax::AstNode; |
| 13 | +use vhdl_syntax::syntax::{ContextItemSyntax, UseClauseContextItemSyntax}; |
| 14 | + |
| 15 | +fn main() { |
| 16 | + // Out-of-order imports with one duplicate (`std_logic_1164` appears twice). |
| 17 | + let vhdl = "\ |
| 18 | +library ieee; |
| 19 | +use ieee.math_real.all; |
| 20 | +use ieee.std_logic_1164.all; |
| 21 | +use ieee.numeric_std.all; |
| 22 | +use ieee.std_logic_1164.all; |
| 23 | +
|
| 24 | +entity foo is |
| 25 | +end foo; |
| 26 | +"; |
| 27 | + let (file, diagnostics) = parser::parse(vhdl); |
| 28 | + assert!(diagnostics.is_empty()); |
| 29 | + |
| 30 | + // 1) Collect every `use`-clause context item from each design unit, in document order. |
| 31 | + let originals: Vec<UseClauseContextItemSyntax> = file |
| 32 | + .design_units() |
| 33 | + .filter_map(|du| du.context_clause()) |
| 34 | + .flat_map(|cc| cc.context_items().collect::<Vec<_>>()) |
| 35 | + .filter_map(|ci| match ci { |
| 36 | + ContextItemSyntax::UseClauseContextItem(u) => Some(u), |
| 37 | + _ => None, |
| 38 | + }) |
| 39 | + .collect(); |
| 40 | + |
| 41 | + // 2) Build the sorted, deduplicated target list. |
| 42 | + let mut sorted: Vec<UseClauseContextItemSyntax> = originals.clone(); |
| 43 | + sorted.sort_by_key(sort_key); |
| 44 | + sorted.dedup_by_key(|u| sort_key(u)); |
| 45 | + |
| 46 | + // 3) Map each original slot (by its offset in the source) to either the next |
| 47 | + // sorted replacement or `None` (= remove this slot, it was a duplicate). |
| 48 | + let mut plan: HashMap<usize, Option<SyntaxNode>> = HashMap::new(); |
| 49 | + let mut replacements = sorted.into_iter().map(|u| u.raw()); |
| 50 | + for orig in &originals { |
| 51 | + plan.insert(orig.raw().offset(), replacements.next()); |
| 52 | + } |
| 53 | + |
| 54 | + // 4) Single rewrite pass: every visited UseClauseContextItem is either swapped to |
| 55 | + // its sorted replacement (`Change`) or dropped entirely (`Remove`). |
| 56 | + let new_file = file.raw().rewrite(|el| match el { |
| 57 | + SyntaxElement::Node(n) => match plan.get(&n.offset()) { |
| 58 | + Some(Some(replacement)) => { |
| 59 | + RewriteAction::Change(SyntaxElement::Node(replacement.clone())) |
| 60 | + } |
| 61 | + Some(None) => RewriteAction::Remove, |
| 62 | + None => RewriteAction::Leave, |
| 63 | + }, |
| 64 | + SyntaxElement::Token(_) => RewriteAction::Leave, |
| 65 | + }); |
| 66 | + |
| 67 | + assert_eq!( |
| 68 | + format!("{}", new_file), |
| 69 | + "\ |
| 70 | +library ieee; |
| 71 | +use ieee.math_real.all; |
| 72 | +use ieee.numeric_std.all; |
| 73 | +use ieee.std_logic_1164.all; |
| 74 | +
|
| 75 | +entity foo is |
| 76 | +end foo; |
| 77 | +" |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +/// Alphabetical sort key — just the displayed text without surrounding whitespace. |
| 82 | +/// Good enough for this example; a real tool would compare the parsed name segments. |
| 83 | +fn sort_key(item: &UseClauseContextItemSyntax) -> String { |
| 84 | + item.raw().to_string().trim().to_lowercase() |
| 85 | +} |
0 commit comments