klyn.code.SourceTokenCursor.renameIdentifier
method
public static renameIdentifier(tokenizer as Tokenizer, source as String, oldName as String, newName as String) as String
Description

Safely renames all occurrences of an identifier in executable code without modifying comments, documentation blocks, or string/character/regex literals.

Parameters
ParameterDescription
tokenizerLanguage tokenizer to use.
sourceOriginal source code text.
oldNameIdentifier name to replace.
newNameReplacement identifier name.
Returns

Updated source text with matching identifier tokens replaced.

Example
import klyn.code
import klyn.code.klyn

code = "value = \"value\"\nreturn value # value"
updated = SourceTokenCursor.renameIdentifier(KlynTokenizer(), code, "value", "result")
assert updated == "result = \"value\"\nreturn result # value"