A splice: the smallest run of sibling items/statements the selected lines cover is laid out alone (Printer::print_at with the written leading whitespace as the base) and replaces its own bytes; the parent is not re-laid. Whole-file verify_equivalence and convergence still gate the result; verify.rs is unchanged. A file that does not parse is formatted when the error lies outside the target. Descent stops only on a unit's first line; the closing end line alone names its unit. CRLF kept, including after a trailing // comment. Module-level targets defer to format_range, which the CLI's --lines still uses. 36 tests in tests/selection.rs; docs/features/selection-formatting. Co-Authored-By: Claude Fable 5.2 <[email protected]>
235 lines
8.5 KiB
Rust
235 lines
8.5 KiB
Rust
//! The sibling lists a selection can land in, and the units they hold.
|
|
//!
|
|
//! A *scope* is a sibling list exactly as `fence.rs` defines one — an item
|
|
//! list (file, module, package, generate region, generate `begin`…`end`,
|
|
//! class) or a statement list (`begin`…`end`, `fork`…`join`, a function or
|
|
//! task body). A *unit* is one item or statement of such a list. The match
|
|
//! arms below mirror `fence::scan_nested` / `scan_nested_stmt` /
|
|
//! `scan_gen_block`: a single-statement body or a block-less generate item is
|
|
//! not a scope, so it is walked *through* to the scopes it reaches. An arm
|
|
//! missing here only makes a selection coarser, never wrong.
|
|
|
|
use crate::fence;
|
|
use crate::format::Run;
|
|
use vlog_parser::ast::{
|
|
GenBlock, GenElse, GenIf, Item, ItemKind, Meta, SourceFile, Stmt, StmtKind,
|
|
};
|
|
use vlog_parser::span::TextRange;
|
|
use vlog_parser::token::Comment;
|
|
|
|
/// One sibling list. Items carry the AST depth `item_seq` lays them out at.
|
|
#[derive(Clone, Copy)]
|
|
pub(super) enum Scope<'t> {
|
|
Items(&'t [Item], &'t [Comment], usize),
|
|
Stmts(&'t [Stmt], &'t [Comment]),
|
|
}
|
|
|
|
/// One member of a scope.
|
|
#[derive(Clone, Copy)]
|
|
pub(super) enum Unit<'t> {
|
|
Item(&'t Item, usize),
|
|
Stmt(&'t Stmt),
|
|
}
|
|
|
|
impl<'t> Scope<'t> {
|
|
/// The scope a whole file is, or `None` for a fragment whose elements
|
|
/// are not items or statements (enum members, cover items).
|
|
pub(super) fn root(file: &'t SourceFile) -> Option<Scope<'t>> {
|
|
match &file.fragment {
|
|
None => Some(Scope::Items(&file.items, &file.dangling, 0)),
|
|
Some(vlog_parser::ast::Fragment::Statements(stmts)) => {
|
|
Some(Scope::Stmts(stmts, &file.dangling))
|
|
}
|
|
Some(_) => None,
|
|
}
|
|
}
|
|
|
|
pub(super) fn len(&self) -> usize {
|
|
match self {
|
|
Scope::Items(items, ..) => items.len(),
|
|
Scope::Stmts(stmts, _) => stmts.len(),
|
|
}
|
|
}
|
|
|
|
pub(super) fn unit(&self, i: usize) -> Option<Unit<'t>> {
|
|
match *self {
|
|
Scope::Items(items, _, depth) => items.get(i).map(|item| Unit::Item(item, depth)),
|
|
Scope::Stmts(stmts, _) => stmts.get(i).map(Unit::Stmt),
|
|
}
|
|
}
|
|
|
|
pub(super) fn dangling(&self) -> &'t [Comment] {
|
|
match *self {
|
|
Scope::Items(_, dangling, _) | Scope::Stmts(_, dangling) => dangling,
|
|
}
|
|
}
|
|
|
|
/// The units `start..start + count` as something the printers take.
|
|
pub(super) fn run(&self, start: usize, count: usize) -> Option<Run<'t>> {
|
|
let end = start.checked_add(count)?;
|
|
match *self {
|
|
Scope::Items(items, _, depth) => items.get(start..end).map(|s| Run::Items(s, depth)),
|
|
Scope::Stmts(stmts, _) => stmts.get(start..end).map(Run::Stmts),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'t> Unit<'t> {
|
|
fn meta(&self) -> &'t Meta {
|
|
match self {
|
|
Unit::Item(item, _) => &item.meta,
|
|
Unit::Stmt(stmt) => &stmt.meta,
|
|
}
|
|
}
|
|
|
|
pub(super) fn span(&self) -> TextRange {
|
|
match self {
|
|
Unit::Item(item, _) => item.span,
|
|
Unit::Stmt(stmt) => stmt.span,
|
|
}
|
|
}
|
|
|
|
/// The unit's bytes: its first leading comment or attribute through the
|
|
/// comments written after it on the same line.
|
|
pub(super) fn extent(&self, src: &str) -> (usize, usize) {
|
|
let meta = self.meta();
|
|
let span = self.span();
|
|
let mut start = span.start;
|
|
if let Some(c) = meta.leading.first() {
|
|
start = start.min(c.span.start);
|
|
}
|
|
if let Some(a) = meta.attrs.first() {
|
|
start = start.min(a.span.start);
|
|
}
|
|
// In a CRLF file a trailing `//` comment's line ends `\r\n`, and the
|
|
// scan above stops at the `\n`. The `\r` is the line ending, not the
|
|
// unit: the layout does not write it, so the splice must not eat it.
|
|
let mut end = fence::with_trailing_comments(src, span.end as usize);
|
|
while end > span.end as usize && src.as_bytes().get(end - 1) == Some(&b'\r') {
|
|
end -= 1;
|
|
}
|
|
(start as usize, end)
|
|
}
|
|
|
|
/// Whether the unit is a construct `format_range` splits (a module or a
|
|
/// package), which a selection defers to it.
|
|
pub(super) fn is_container(&self) -> bool {
|
|
matches!(
|
|
self,
|
|
Unit::Item(Item { kind: ItemKind::Module(_) | ItemKind::Package { .. }, .. }, _)
|
|
)
|
|
}
|
|
|
|
/// Whether the unit is a compiler directive (`` `ifdef `` and kin),
|
|
/// which is often written at column 0 whatever the code around it does.
|
|
pub(super) fn is_directive(&self) -> bool {
|
|
match self {
|
|
Unit::Item(item, _) => matches!(item.kind, ItemKind::Directive(_)),
|
|
Unit::Stmt(stmt) => matches!(stmt.kind, StmtKind::Directive(_)),
|
|
}
|
|
}
|
|
|
|
/// Whether the unit is a node error recovery made of unparsable text.
|
|
pub(super) fn is_error(&self) -> bool {
|
|
match self {
|
|
Unit::Item(item, _) => matches!(item.kind, ItemKind::Error(_)),
|
|
Unit::Stmt(stmt) => matches!(stmt.kind, StmtKind::Error(_)),
|
|
}
|
|
}
|
|
|
|
/// The scopes directly nested in this unit, in source order.
|
|
pub(super) fn children(&self) -> Vec<Scope<'t>> {
|
|
let mut out = Vec::new();
|
|
match *self {
|
|
Unit::Item(item, depth) => item_scopes(item, depth, &mut out),
|
|
Unit::Stmt(stmt) => stmt_scopes(stmt, &mut out),
|
|
}
|
|
out
|
|
}
|
|
}
|
|
|
|
/// The scopes nested in one item at `depth`, at the depth `item_kind` lays
|
|
/// their items out at.
|
|
fn item_scopes<'t>(item: &'t Item, depth: usize, out: &mut Vec<Scope<'t>>) {
|
|
match &item.kind {
|
|
ItemKind::Module(m) => out.push(Scope::Items(&m.items, &m.dangling, depth + 1)),
|
|
ItemKind::Package { items, dangling, .. }
|
|
| ItemKind::GenerateRegion { items, dangling } => {
|
|
out.push(Scope::Items(items, dangling, depth + 1))
|
|
}
|
|
ItemKind::GenBlock(b) => gen_block_scopes(b, depth, out),
|
|
ItemKind::GenIf(g) => gen_if_scopes(g, depth, out),
|
|
ItemKind::GenFor(g) => gen_block_scopes(&g.body, depth, out),
|
|
ItemKind::GenCase(g) => {
|
|
for i in &g.items {
|
|
gen_block_scopes(&i.body, depth + 1, out);
|
|
}
|
|
}
|
|
ItemKind::Class(c) => out.push(Scope::Items(&c.items, &c.dangling, depth + 1)),
|
|
ItemKind::Function(f) => out.push(Scope::Stmts(&f.stmts, &f.dangling)),
|
|
ItemKind::Task(t) => out.push(Scope::Stmts(&t.stmts, &t.dangling)),
|
|
ItemKind::Always { stmt, .. } | ItemKind::Initial(stmt) | ItemKind::Final(stmt) => {
|
|
stmt_scopes(stmt, out)
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn gen_if_scopes<'t>(g: &'t GenIf, depth: usize, out: &mut Vec<Scope<'t>>) {
|
|
gen_block_scopes(&g.then, depth, out);
|
|
match g.els.as_deref() {
|
|
Some(GenElse::If(_, inner)) => gen_if_scopes(inner, depth, out),
|
|
Some(GenElse::Block(b)) => gen_block_scopes(b, depth, out),
|
|
None => {}
|
|
}
|
|
}
|
|
|
|
/// A generate `begin`…`end` is a scope one level in; a block-less body's
|
|
/// one item is laid out one level in too, and walked through.
|
|
fn gen_block_scopes<'t>(b: &'t GenBlock, depth: usize, out: &mut Vec<Scope<'t>>) {
|
|
if b.is_block {
|
|
out.push(Scope::Items(&b.items, &b.dangling, depth + 1));
|
|
} else {
|
|
for item in &b.items {
|
|
item_scopes(item, depth + 1, out);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The statement lists one statement holds, walking through every
|
|
/// single-statement body.
|
|
fn stmt_scopes<'t>(stmt: &'t Stmt, out: &mut Vec<Scope<'t>>) {
|
|
match &stmt.kind {
|
|
StmtKind::Block { stmts, dangling, .. } | StmtKind::ForkJoin { stmts, dangling, .. } => {
|
|
out.push(Scope::Stmts(stmts, dangling))
|
|
}
|
|
StmtKind::If { then, els, .. } => {
|
|
stmt_scopes(then, out);
|
|
if let Some(e) = els {
|
|
stmt_scopes(e, out);
|
|
}
|
|
}
|
|
StmtKind::Assert { then, els, .. } => {
|
|
for s in then.iter().chain(els.iter()) {
|
|
stmt_scopes(s, out);
|
|
}
|
|
}
|
|
StmtKind::Case { items, .. } | StmtKind::RandCase { items, .. } => {
|
|
for i in items {
|
|
stmt_scopes(&i.stmt, out);
|
|
}
|
|
}
|
|
StmtKind::For { body, .. }
|
|
| StmtKind::While { body, .. }
|
|
| StmtKind::DoWhile { body, .. }
|
|
| StmtKind::Repeat { body, .. }
|
|
| StmtKind::Forever { body }
|
|
| StmtKind::Foreach { body, .. }
|
|
| StmtKind::Labeled { stmt: body, .. } => stmt_scopes(body, out),
|
|
StmtKind::Timing { stmt: Some(inner), .. } | StmtKind::Wait { stmt: Some(inner), .. } => {
|
|
stmt_scopes(inner, out)
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|