-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Win.linemap, which translates visual line numbers to file line numbers #1181
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is overly complicated and unnecessary. win->view->topline->lineno
gives you the on disk line number of the top line in the current view. Similarly for view->bottomline
(though that might need to be checked with view->lastline
). If you have that and you want the on screen line number for importantline
its some very basic math:
function get_visual_line(win, line)
...
if ... then
return line - win.viewport_lines.start
end
...
end
I'm sure you can fill in the rest.
Now we could have a reasonable discussion about changing the viewport
member so that it was something like:
local line_range = win.viewport.lines
local byte_range = win.viewport.bytes
Yes you are right, but that doesn't really change what I'm suggesting. If you have the first on disk line number and the last on disk line number and the height of the window (in rows) it is trivial to determine if there are any long lines in view. Now there are two cases, the one I gave above, and the long line case. In the second case you will need to iterate over the lines in Why is this better? Because you still have the fast first case and the second case now requires a single loop instead of two and doesn't require you to construct some dynamic array that is only valid for a single operation. |
I should add that I wouldn't be that against your approach if Edit: I'm also not saying we should do this. It would just simplify this particular case. I would have to actually read through the uses of Line to see if it makes sense in the context of the rest of vis. |
Sorry, I guess I forgot that the viewport height and width isn't visible from lua right now, only the height/width of windows. Personally I would be ok if those were exposed separately. If someone were to do the server client thing the window height/width would be deleted but the viewport height/width would still be useful. |
Shoot, that's a good point. Better to do some quick arithmetic rather than have a list that's O(height) to access. This new commit should cover everything: |
Can you keep the UI stuff relegated to your other pull request? Sorry to ping-pong back and forth but its unrelated to this. I will leave a further comment there. As for the It doesn't need to be a separate class, you can just push a table with both ranges when |
Rebased and force-pushed in order to get rid of a commit that's been moved to #1180. Hope the plugin maintainers don't get too upset about the API change. 😛 |
This will break all plugins which currently use Win.viewport.
Applied thanks!
If they do they can get mad at me, There are tons of cool ideas for plugins that could actually be created if the API was a little more expressive. Some things will have to be broken to achieve that. |
Win.linemap
contains a Lua array (1-initiated) with one element for every visual line. Index 1 corresponds to the window's topmost line, and index#linemap
corresponds to the last line in the viewport. The value of each index corresponds to the the value inwin->view->lines->...->lineno
. Combined with PR #1180, linemap can be combined with output from, say, a compiler, to highlight line numbers of important lines whenever they appear in the viewport, without needing to calculate the exact location of the line.For example:
Prior to this change,
get_visual_line
would be a very large Lua function that would need to calculate line breaks and account for multiple windows.