Skip to main content

splice_function_signature

Function splice_function_signature 

Source
pub fn splice_function_signature(
    source: &str,
    sig_range: Range<usize>,
    new_sig: &str,
) -> String
Expand description

Splice a logical signature string into source at sig_range.

High-level rewrite APIs must use this (not a raw before + new + after on the full FunctionSpan::signature_range). The raw range ends at the body start and includes trailing whitespace (e.g. the space in -> i32 ). Callers and LLMs pass signatures without that trailing space; a naive splice produces -> i32{.

Behavior (#1503):

  1. Replace only the trimmed portion of the old signature so the original gap before the body (space or newline) is kept.
  2. If there is no gap and the remainder starts with {, insert a conventional space (does not insert before ;).
  3. Trailing whitespace on new_sig is stripped.
use patchloom::ast::rewrite::{find_function_span, splice_function_signature};
use patchloom::ast::Language;

let source = "fn add(a: i32) -> i32 {\n    a\n}\n";
let span = find_function_span(source, "add", Language::Rust).unwrap();
let out = splice_function_signature(
    source,
    span.signature_range,
    "fn add(a: i32, b: i32) -> i32", // no trailing space
);
assert!(out.contains("-> i32 {"), "got: {out}");
assert!(!out.contains("i32{"), "got: {out}");