Interesting behavior from editor.getFirstVisibleLine()
-
As discussed in a separate thread I have a script which jumps to a particular line. I decided to rework it a bit so that it scrolls the window to center the just-jumped-to line vertically in the window.
After some experimentation, I have found some odd behavior with editor.getsFirstVisibleLine().
For small line numbers, say 200, this script works correctly:
editor.gotoLine(200) print(editor.getFirstVisibleLine() )
It jumps to line 200 and prints the correct first visible line (first line visible at the top). However, as the line numbers get larger it doesn’t work correctly.
editor.gotoLine(1700) print(editor.getFirstVisibleLine() )
jumps to line 1700, but it displays the first visible line as 1688 which is actually in the middle of the displayed lines. At 3000 it displays the first visible line as 3003, below the jumped to line.
editor.gotoLine(3000) print(editor.getFirstVisibleLine() )
What am I missing?
-
Forgot an important detail. It’s probably obvious, but this is using the python script plugin.
-
What am I missing?
Not sure. It gives me exactly what I expect:
Do you have long lines that are wrapped? Maybe that influences things.
Do you have any plugins that have created annotations? like
-
@peterjones said in Interesting behavior from editor.getFirstVisibleLine():
Do you have long lines that are wrapped? Maybe that influences things.
Survey says, yes it does:
this is a normal line this is a long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long line
repeated a couple hundred times, with word wrap turned on.
>>> editor.gotoLine(100); print(editor.getFirstVisibleLine()) 113
even though the line number for the first full line is 77, which means you expect it to report 76 (or maybe 75, because the line that was visible was implied number 76, which 0-base is 75).
>>> editor.gotoLine(100); print(editor.getFirstVisibleLine()); print(editor.docLineFromVisible(editor.getFirstVisibleLine())) 113 75
Yep, the
docLineFromVisible
will change to the real line number (minus 1, of course) -
“Do you have long lines that are wrapped? Maybe that influences things.”
That is exactly what I was missing. Explains why the error increased as the line number went up.
Thanks!
-
So a cause has been established, but one would think you are calling the
getFirstVisibleLine()
function because you need it to provide accurate info.Do we know of any workarounds in order to get good data?
-
Do we know of any workarounds in order to get good data?
Yes, like I showed above,
editor.docLineFromVisible(editor.getFirstVisibleLine())
, gives the actual document line number as derived from the display line number. Hence, the correct 75 that got printed on the second line in the final example box.Addenda:
As implied in the
editor
section of the PythonScript docs when searching for “display line”, the visible line’s number is influenced by hidden lines and line wrapping. The “document line” is the real underlying line number for the line in the file. So there are a few conversion helpers:visibleFromDocLine
converts the “document line” into the “display line”;docLineFromVisible
converts the “display line” into “document line”; andwrapCount
gives the number of “display lines” necessary to render the given “document line”. (All the other mentions of “display line” have to do with where HOME and END and their related commands end up.)Similarly, if you want to set the first visible line (to scroll) based on an actual document line number, it would be
editor.setFirstVisibleLine(editor.visibleFromDocLine(actualLineNumber))