Sublime Forum

show_at_center not scrolling to text_point

#1

Hi all, it’s stupid again.

I have the following code. it is pretty much the same code taken from goto_line.py except for the replace_view command.

The goto variable is an int the plugin computes depending on the content of the view.

self.view.run_command("replace_view", {"newcontent" : content} )

pt = self.view.text_point(goto, 0)
self.view.sel().clear()
self.view.sel().add(sublime.Region(pt))

sublime.active_window().active_view().show_at_center(pt)

Sometimes the view scrolls to the point. Sometimes it doesn’t. It seems it is working randomly, and I cannot figure out why.

The plugin imports and exports text to / from the view and I have flags determining the goto variable.

The only thing I can think of that may be happening is that there is some sort of sublime caching issue or race condition, where potentially the goto line code somehow beats the replacement of content.

I’m all out of ideas, and I got more than a bazzillion print statements… i give up… any thoughts?

Thanks for any and all help :slightly_smiling:

0 Likes

#2

you could try creating another command to center the view, in the hope that, when running multiple commands, ST runs them in order and waits for the first one to complete before executing the second one.
alternatively, you could include the centering code in your ReplaceViewCommand plugin code, by passing an extra argument for the line to go to.

0 Likes

#3

Thanks for the ideas. :slight_smile:

I forgot to mention, for you or any others that might have an ‘aha’ moment. The cursor goes to the correct line… if you scroll down manually when it bugs out, you see the correct line is highlighted. So it is sort-of working… its just that the view is not scrolling to that line… or I should say… randomly scrolling to the line —whenever it feels like it. lol

I will try the separate command. Though, that’s another file… and I like to keep things tidy. Arg. It’s nearly 4am. Ugh. I will try tomorrow.

Thanks again for the ideas, and if you or anyone else has other ideas please let me know.

0 Likes

#4

this may be a silly question, but is there a reason why you are using sublime.active_window().active_view() rather than self.view? It seems unlikely that it would make a difference, but just a thought…

0 Likes

#5

Hahaha,

The reason was at 4 in the morning you start trying anything and everything. I did have it origionaly as self.view… but you know how it goes: desperate times call for desperate lines of code. :slightly_smiling:

1 Like

#6

Right. So I threw together a new command like you suggested last night. And still no dice. Now, it seems to be just going straight to the last line of the file… line 567 instead of the flag which prints out as 220.

class GotoSectionCommand(sublime_plugin.TextCommand):

def run(self, edit, goto):
	print('running go to section', goto)

	pt = self.view.text_point(goto, 0)
	self.view.sel().clear()
	self.view.sel().add(sublime.Region(pt))

	self.view.show_at_center(pt)
0 Likes

#7

Right. I figured out the pattern of failure. Bust still don’t have a solution

I dice up a text file into sections.
I import / export these sections to / from disk.

If I import / export different sections one after another. It works as expected.
If I import / export the same section one after another. It does NOT work.

Basically if I put my import / export flag on the same line and try to import / export after I just have… it fails

_import
section-section-section-section-section-section-section-section-section-section


text to import / export


endsection-endsection-endsection-endsection-endsection-endsection-endsection


I have tried to add this to the go to command after show_at_center but still no dice

self.view.sel().subtract(sublime.Region(pt))
self.view.sel().clear()
0 Likes

#8

I think I found the solution.

Basically, from the pattern of failure it seemed to me something wonky was going on behind the scenes. Something with how Sublimes internals work. The problem it seems was in my replace_view command —which was one of the first ‘hello world’ commands I wrote when I first started trying to learn Sublime.

OLD VERSION
class ReplaceViewCommand(sublime_plugin.TextCommand):

def run(self, edit, newcontent):
	# Erase the old content
	oldcontent = sublime.Region(0, self.view.size())
	self.view.erase(edit, oldcontent)

	# Replace with new content
	self.view.insert(edit, 0, newcontent)

NEW VERSION

self.view.replace(edit, oldcontent, newcontent)

Somewhere there is a difference behind the scenes between erasing everything and inserting new…
and —view.replace. Wish I’d seen view.replace back when I wrote that first command, but hell… I was still learning… and still am…

Hope this is the solution… and hope this helps some poor bastard one day.
:slight_smile:

0 Likes