How to properly update caret position when modifying TMP input field with Rich Text ?

I am working on a game where players have to type code to interact with the environment. I wanted to implement syntax highlighting as they are typing. However, when I insert new rich text tags into the input filed, the caret position takes one blink in the wrong position before updating to the correct one. This is really annoying for someone typing since letters will go in the wrong place.

I have tried some of the solutions suggested here:
https://forum.unity.com/threads/cant-change-caret-position.431105/

But with no success. Here is my code:

 void ChangeColors(IEnumerable<IToken> original_token_list, string input_text)
    {
        string output = input_text;
        //HashSet<IToken> unique_tokens = new HashSet<IToken>(original_token_list);
        List<string> token_text = new List<string>();
        foreach (IToken t in original_token_list)
        {
            if (keywords.Contains(t.Type))
            {
                token_text.Add("<color=#F67599>");
                token_text.Add(t.Text);
                token_text.Add("</color>");
                input.GetComponent<TMP_InputField>().caretPosition = caretPos + 1;
            }
            else if (parens.Contains(t.Type))
            {
                token_text.Add("<color=#9EC651>");
                token_text.Add(t.Text);
                token_text.Add("</color>");
                input.GetComponent<TMP_InputField>().caretPosition = caretPos + 1;
            }
            else if (brackets.Contains(t.Type))
            {
                token_text.Add("<color=#6FE1CD>");
                token_text.Add(t.Text);
                token_text.Add("</color>");
                input.GetComponent<TMP_InputField>().caretPosition = caretPos + 1;
            }

            else if (t.Text != "<EOF>")
            {
                token_text.Add(t.Text);
            }
        }
        output = "";
        foreach (string s in token_text)
        {
            output += s;
        }

        //input.GetComponent<InputFieldWithHideableCaret>().HideCaret();
        //CaretHidden = true;
        input.GetComponent<InputFieldWithHideableCaret>().text = output;
        //ChangeCaretPos();
        Invoke("ChangeCaretPos", 0.05f);
    }

and :

 void ChangeCaretPos()
    {
        input.GetComponent<InputFieldWithHideableCaret>().caretPosition = caretPos + 1;
    }

InputFieldWithHideableCaret is the same as TMP_InputField but with extra classes:

public class InputFieldWithHideableCaret : TMP_InputField
{
    // using same name conventions as the InputField for consistency
    private float m_CachedCaretBlinkRate;

    /// <summary>
    /// Caches BlinkRate, sets it to 0, hides the caret indefinitely.
    /// </summary>
    public void HideCaret()
    {
        m_CachedCaretBlinkRate = caretBlinkRate;
        caretBlinkRate = 0f;
        m_CaretVisible = false;
    }

    /// <summary>
    /// Sets the caretBlinkRate back to original value, existing logic will do the rest.
    /// </summary>
    public void UnhideCaret()
    {
        caretBlinkRate = m_CachedCaretBlinkRate;
    }
}

Did anyone ever find a solution to this?

Thanks!

I’ve resolve this simple calling a StarCoroutine function and inside add the carretPosition update.
I think the major problem is the lap time to update the input field.

Sneak peak of my code:

private void checkInputDialog(string arg0)
{
    if (Input.GetKeyDown(KeyCode.Space))
   {
        DescriptionTMP.SetTextWithoutNotify(CheckSpellWord(arg0)); /*DescriptionTMP: TMPro.TMP_InputField */
        StartCoroutine(SetCaretPosition());
    }
}

private IEnumerator SetCaretPosition()
{
    yield return 0;
    DescriptionTMP.caretPosition = DescriptionTMP.text.Length + 1;
}