Python gotoLine() Not going to line after Open()



  • I have a file that has a list of files and linenumbers. I want to click on a line and hit an Fkey which will load the file and goto the line.
    My code looks like this;

    print("Opening "+os.path.normpath(c)+"...")
    notepad.open(c)
    print("Going to line "+parms[2]+"...")
    editor.gotoLine(int(parms[2]))
    

    My Console output looks like this;

    Opening excpmon.cbp...
    Going to line 136...
    

    It loads the file fine but it doesn’t goto the line. What have i done wrong?
    Thanks in advance for any help



  • @Anthony-Blinco

    Could it be that there are notifications or threads involved
    which result in executing gotoLine before the document has been loaded?

    870050d9-6451-40aa-b38d-89a4abd0828c-image.png



  • @Anthony-Blinco said in Python gotoLine() Not going to line after Open():

    It loads the file fine but it doesn’t goto the line

    I changed your code a bit, to:

    p = r'd:\blah.txt'
    print("Opening "+ p + "...")
    notepad.open(p)
    line = 12
    print("Going to line " + str(line) + "...")
    editor.gotoLine(line)
    

    and, running it, it didn’t have a problem opening the file and moving to the correct line.



  • @Alan-Kilborn I’m not sure what the difference (between my code and yours) is there
    It is very weird. If I turn word wrap off it works, if word wrap is on it doesn’t work (For my primary file, not the loaded file).
    I do have pre-processor python code that runs when a file is loaded.
    Is there anyway to wait until there is no other processing and then goto the line?



  • @Anthony-Blinco said in Python gotoLine() Not going to line after Open():

    If I turn word wrap off it works, if word wrap is on it doesn’t work (For my primary file, not the loaded file).

    I’m not sure what to think about that…

    I do have pre-processor python code that runs when a file is loaded.

    I’m not sure what “pre-processor python code” exactly means…

    Is there anyway to wait until there is no other processing and then goto the line?

    You could certainly do some kind of “sleep”.
    You might start with time.sleep() for experimentation purposes.
    A threading timer is a bit more elegant if the basic sleep works.
    I’m not convinced you should need it, but only you have your exact situation.



  • @Anthony-Blinco said in Python gotoLine() Not going to line after Open():

    It is very weird. If I turn word wrap off it works, if word wrap is on it doesn’t work (For my primary file, not the loaded file).

    A quote from the Scintilla documentation

    Wrapping is not performed immediately there is a change but is delayed until the display is redrawn. This delay improves performance by allowing a set of changes to be performed and then wrapped and displayed once. Because of this, some operations may not occur as expected. If a file is read and the scroll position moved to a particular line in the text, such as occurs when a container tries to restore a previous editing session, then the scroll position will have been determined before wrapping so an unexpected range of text will be displayed. To scroll to the position correctly, delay the scroll until the wrapping has been performed by waiting for an initial SCN_PAINTED notification.



  • @Anthony-Blinco said in Python gotoLine() Not going to line after Open():

    if word wrap is on it doesn’t work (For my primary file, not the loaded file).

    So what didn’t register with me is that “word wrap” is a global setting, it affects all files. So if you have one “wrapped” you have them all “wrapped”. So saying something like “word wrap is on…for my primary file not the loaded file” isn’t really a valid statement.

    So yes, I think in light of that plus @dail 's input, a lot more is starting to make sense now as to what is happening for you. :-)



  • Hm, wrapping has an impact on scripts, learned something new today.



  • Awesome advice as usual. A 10ms delay did it.
    I will investigate the SCN_PAINTED notification to implement it properly
    Thanks and very much appreciated


Log in to reply