Oktay Acikalin 7 years ago • updated 7 years ago 6
I've written center_view_on_line.py. Today I wanted to refactor it 'cause it behaved strangely in some cases. Now I'm trying to come up with an algorithm to center the visible region over the current line or selection. I've got the final row numbers and could translate them into a proper region. If I compare the visible region I get by scrolling there manually and the target region my script calculates they seem to be nearly identical. But the view scrolls somewhere else - often the cursor is at the top of the screen. When querying the visible region again the coords are totally off.

Some vars I work with:
visible_region = (366, 998) # manually centered view
selection = [26, 26] # cursor in line w/o selection
region = [26, 26] # in this case same as selection
visible_region = [16, 36] # first row, last row from above
size = 2099 # self.view.size()
region_size = 0 # diff between region end and begin
visible_size = 20 # diff between last and first row
begin = 16 # calculated first row
end = 36 # calculated last row
target_region = (366, 946) # calculated for self.view.show()
target_region = [16, 36] # translated into first, last row
visible_region = (603, 1351) # region I get after self.view.show()

Any idea?
I can’t remember where I found it, but I have a working plugin for that. I put it on GitHub for you: https://github.com/stiang/CenterSelectionInVisibleArea
Nice - thank you! It does work around the problem I have and does the correction by using scroll_lines.

Some things regarding the code:
* Missing the import sublime_plugin line.
* One should really push it thru PythonTidy ;).
* It does center on the beginning and not the whole selection. We should probably fix that some time...

Nevertheless would it be nice if Jon corrects the wrong movement or at least does explain why that happens. But no hurry, there are more important things to implement or polish for now.
Doh! I’ve pushed a new version which includes the import line and is more tidy :)
You could have used http://www.sublimetext.com/forum/viewtopic.php?f=5&t=1609 which does not add the shebang and encoding line, but.... I'm happy now ;).
Build 2024 introduce the show_at_center command that, I think, is exactly what you want.
That's kind of right and I've already switched to it, but I still would like to know why that happens. Also I would like to have the whole selection centered in the view and not only the end point.
If we would talk about priorities, this issue is not that important to me anymore 'cause at least the important half of what I wanted to have works quite fine.