How can I make a line visible in the text editor ? (jump to / scroll to / highlight)

I’m creating an add-on that works a bit like a TOC and when someone clicks on a section, I’d like to scroll / jump and highlight a line of text (assuming the text editor is visible)

Assuming I have the line number to jump to, is that something doable ?

I’m asking because my initial tests for jumping where not that successful and I couldn’t see an API to make a given line visible in the text editor (i.e. scroll to it to make sure it’s visible)

Thanks for any leads

I believe you’re looking for bpy.ops.text.cursor_set (in particular, the y value)

Thanks, I managed to achieve it, however it was a bit more involved then what the API hints at. Especially since the context is wrong. I’m writing my answer here, maybe someone can let me know if there is a better way. Note that the get_script method returns the text object pointed at by my add-on inside the View3D. It is necessary to set that override when using the text editor, so you’ll have to somehow give it the right text object. Also note that the I found on google that someone was saying that set cursor was finicky and a jump/move combo was better. Not sure if it’s true. I just know that it works.

        for window in
            screen = window.screen

            for area in screen.areas:
                if area.type == 'TEXT_EDITOR':
                    for region in area.regions:
                        if region.type == "WINDOW":
                            override = context.copy()
                            override['window'] = window
                            override['screen'] = screen
                            override['area'] =  area
                            override['region'] = region
                            override['edit_text'] = context.scene.fountain.get_script()
                            bpy.ops.text.jump(override, line=context.scene.fountain.script_line)
                            bpy.ops.text.move(override, type='LINE_BEGIN')

PS. I’ll publish my fountain add-on soon in case someone is curious about the reference to fountain in this script.