"Widgetizing" a token?

Hello there,

I’m working on some basic home-made language mode and everything works fine. Now I am trying to add some assistance to the users by detecting a specific text token, let’s name it “start calculation”, and replace it with a more graphical element. Clicking on this element brings up multiple tools to work inside that calculation but also to work on that calculation definition itself. To define the calculation itself, I made the token so it can hold some optional definitions.

Here’s a more concrete example:

I start a calculation using “=(” which turns into a token. I could also use “=[someRandomOptions](”.

Now what I’m doing is listen on change events, find the wanted token(s) and use markText() on them to replace with my button.

My most important question, is how I’d go from here to insert the “[randomOption]” string in the marked token using my helper functions?

And as a bonus question, am I using the tools planned for it or more hacking my way through? What would be the best way to go about?