AstUtils
loreline.AstUtils (Class)
Static utility functions for programmatic AST formatting transformations.
Usable as static extensions on AstNode via using loreline.AstUtils.
Static Members
Add double quotes to all unquoted string literals in the AST.
Remove double quotes from string literals that don't strictly need them.
Convert all blocks to brace style.
Convert all blocks to plain/indent style.
Convert &&/|| operators to and/or word form.
Convert and/or operators to &&/|| symbol form.
Convert all if conditions to use parentheses.
Convert all if conditions to plain style (no parentheses).
Re-indent function body raw code strings.
Insert localization key hash comments directly into source text.
Returns the modified source content. Does NOT use the Printer,
so all existing content (comments, formatting, test blocks) is preserved.
When includeImports is false and node is a Script, imported
scripts are skipped. Their byte offsets refer to other files'
contents and would land at wrong positions in content. Use this
for per-file rewrites that walk imports externally.
When reservedIds is provided, it's used (and mutated) as the
shared existing-IDs set across calls. Useful for project-wide
coordination: pass the same map to every per-file call so
file B's generation avoids every ID already in file A (both
pre-existing AND just-generated). Existing IDs found in this
file are added to the map; newly-generated IDs are added too.
Remove all #hash localization keys from source content, using AST positions.
Extract translations from a parsed translation file.
Returns a map of localization key -> NStringLiteral.
Looks for hash comments (#key) on text/dialogue nodes.
Collects all translatable entries with #id from a source script, in source order.
Returns pairs of {id, str} for NTextStatement, NDialogueStatement, and NChoiceOption.
Returns true iff node contains at least one translatable string
(NTextStatement / NDialogueStatement / NChoiceOption text) that has
no #id hash comment. Used by tooling to gate "add tags?" prompts
without doing any AST or content mutation. Stops at the first
untagged occurrence.
Lex-only scan of content for every hash-comment identifier
(#xxxx). Cheap compared to a full parse. Runs the lexer, walks
tokens, pulls out every CommentHash payload. No AST is built, no
imports are resolved.
Use for project-wide reserved-IDs registries: scanning every
.lor file in a project to collect every hash ID currently in
use, so a subsequent insertLocalizationKeys call can be passed
the union as its reservedIds and avoid generating any of them.
Fills + returns out; allocates a fresh map when out is null.
| Name |
Type |
Default |
content |
String |
|
out |
Map |
(optional) |
Generates a translation file from a source script.
Each entry has: #id // original-text-as-in-source (with quotes preserved),
followed by translated text or original as placeholder.
Helper: find the first hash comment on a node and return its content (the localization key).
Returns null if no hash comment is found.
Private Members
Check if a source line contains a hash comment (# followed by word chars, not preceded by ).
Transform a Raw text part from unquoted to quoted context.
- Remove unquoted-only escapes: = -> =, { -> {, \X -> X (for X not in {n,t,r,,<})
- Add quoted-only escapes: " -> "
- Keep shared escapes: \n, \t, \r, \, <, $$
Transform a Raw text part from quoted to unquoted context.
- Remove quoted-only escapes: " -> "
- Keep shared escapes: \n, \t, \r, \, <, $$
Checks if removing quotes from this text would be safe (re-parseable as unquoted).
Conservative: rejects if any condition makes the text ambiguous as unquoted.
Re-indent code by replacing leading old-indent units with new-indent units.
Find the last line (1-based) occupied by a string literal, accounting for multiline raw parts.
Generate a random base36 ID of the given length, guaranteed to not be a valid hex string.
| Name |
Parameters |
:expose |
- |