Editing
The script module is an editable object. You can change the entire source code or just a part of it whenever the user modifies corresponding fragments of the code.
Due to the incremental nature of script analysis, source code edits are efficient operations. The analyzer does not rebuild the entire inner semantic representation of the code with every edit. Instead, it patches its inner structures according to the changes whenever the API user queries corresponding semantic features (e.g., when you query code diagnostics).
To edit the code, you should obtain the script module's write guard.
let module = ScriptModule::new(Package::meta(), "let x = 10;");
module.rename("Example Module");
let handle = TriggerHandle::new();
let mut write_guard = module.write(&handle, 1).unwrap();
// An absolute source code character range.
//
// Alternatively, you can use a line-column range:
// `Position::new(1, 5)..Position::new(1, 6)`.
//
// The `..` range specifies the entire text range:
// `write_guard.edit(.., "let new_variable_name = 10;").unwrap();`
write_guard.edit(4..5, "new_variable_name").unwrap();
let module_text = write_guard.text();
println!("{module_text}");
Prints:
╭──╢ module [‹doctest›.‹Example Module›] ╟──────────────────────────────────╮
1 │ let new_variable_name = 10; │
╰───────────────────────────────────────────────────────────────────────────╯