How to make LLM update code in the code editor seamlessly

So I have a project I’m working on and it will require an LLM to update existing code and write it back to the editor,where I’m having issues is finding a good way to update anywhere within the code without Messing up unchanged ones, I know converting the source code to AST can help when doing function/class wide updates it has easy as transversing the nodes and identifying which function or classes require change.

But for use cases where the updates are limited to few lines of code or for code that are just scripts with no classes and functions,I can’t really come up with a good solution to update the code with messing up other lines that aren’t affected, I’ve tried searching on Google to see similar implementations or articles on it but all of them only talk about how to parse and transverse AST.