• Login
Community
  • Login

Interesting behavior from editor.getFirstVisibleLine()

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
7 Posts 3 Posters 599 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M
    Michael Brock
    last edited by Apr 26, 2022, 5:17 PM

    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?

    M P 2 Replies Last reply Apr 26, 2022, 5:28 PM Reply Quote 0
    • M
      Michael Brock @Michael Brock
      last edited by Apr 26, 2022, 5:28 PM

      Forgot an important detail. It’s probably obvious, but this is using the python script plugin.

      1 Reply Last reply Reply Quote 0
      • P
        PeterJones @Michael Brock
        last edited by Apr 26, 2022, 5:29 PM

        @michael-brock ,

        What am I missing?

        Not sure. It gives me exactly what I expect:
        acf4abf4-95a0-4173-a166-ecd5ec4fa585-image.png

        Do you have long lines that are wrapped? Maybe that influences things.
        Do you have any plugins that have created annotations? like
        3503cdfa-1be5-4252-a155-57eaf7e9af3e-image.png

        P 1 Reply Last reply Apr 26, 2022, 5:35 PM Reply Quote 1
        • P
          PeterJones @PeterJones
          last edited by PeterJones Apr 26, 2022, 5:35 PM Apr 26, 2022, 5:35 PM

          @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
          

          4808b05a-6996-40d9-a3fc-e19018c2c008-image.png

          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)

          M 1 Reply Last reply Apr 26, 2022, 5:41 PM Reply Quote 3
          • M
            Michael Brock @PeterJones
            last edited by Apr 26, 2022, 5:41 PM

            “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!

            A 1 Reply Last reply Apr 26, 2022, 6:18 PM Reply Quote 1
            • A
              Alan Kilborn @Michael Brock
              last edited by Apr 26, 2022, 6:18 PM

              @michael-brock

              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?

              P 1 Reply Last reply Apr 26, 2022, 6:23 PM Reply Quote 0
              • P
                PeterJones @Alan Kilborn
                last edited by PeterJones Apr 26, 2022, 6:35 PM Apr 26, 2022, 6:23 PM

                @alan-kilborn ,

                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”; and wrapCount 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))

                1 Reply Last reply Reply Quote 2
                5 out of 7
                • First post
                  5/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors