Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Interesting behavior from editor.getFirstVisibleLine()

    Plugin Development
    3
    7
    149
    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.
    • Michael Brock
      Michael Brock last edited by

      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?

      Michael Brock PeterJones 2 Replies Last reply Reply Quote 0
      • Michael Brock
        Michael Brock @Michael Brock last edited by

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

        1 Reply Last reply Reply Quote 0
        • PeterJones
          PeterJones @Michael Brock last edited by

          @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

          PeterJones 1 Reply Last reply Reply Quote 1
          • PeterJones
            PeterJones @PeterJones last edited by PeterJones

            @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)

            Michael Brock 1 Reply Last reply Reply Quote 3
            • Michael Brock
              Michael Brock @PeterJones last edited by

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

              Alan Kilborn 1 Reply Last reply Reply Quote 1
              • Alan Kilborn
                Alan Kilborn @Michael Brock last edited by

                @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?

                PeterJones 1 Reply Last reply Reply Quote 0
                • PeterJones
                  PeterJones @Alan Kilborn last edited by PeterJones

                  @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
                  • First post
                    Last post
                  Copyright © 2014 NodeBB Forums | Contributors