Sublime Forum

Rid of viewport blinking

#1

I am writing a plugin for C++ formatting by Uncrustify, that is what I written:

[code]
import sublime, sublime_plugin, subprocess, traceback

class FirmSelection():
def init(self, view):
self.view = view
def enter(self):
self.old_selection = ]
for region in self.view.sel():
self.old_selection.append(region)
def exit(self, type, value, traceback):
self.view.sel().clear()
for region in self.old_selection:
self.view.sel().add(region)

class UncrustifyCommand(sublime_plugin.TextCommand):
def run(self, edit):
old_viewport_position = self.view.viewport_position()
x = old_viewport_position[0]
y = old_viewport_position[1]
with FirmSelection(self.view):
try:
region = sublime.Region(0, self.view.size())
content = self.view.substr(region)
command = “uncrustify”, “-q”, “-l”, “CPP”, “-c”, “D:/uncrustify/default.cfg”]
popopenObject = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = popopenObject.communicate(input=content)[0]
returnCode = popopenObject.poll()
if (returnCode != 0):
stderr = popopenObject.communicate()[1]
raise Exception(("Non-zero return code (%d): " + stderr) % returnCode)
# e = self.view.begin_edit()
print(self.view)
self.view.replace(edit, region, stdout)
# self.view.end_edit(e)
print(self.view)
except (OSError, ValueError, subprocess.CalledProcessError, Exception) as e:
err = ‘%s: Uncrustify was unable to executed (%s)’ % (name, traceback.print_exc())
sublime.error_message(err)
self.result = False
# edit.active_view().set_viewport_position((0.0, 1339.0))
print(self.view)
self.view.set_viewport_position((0.0, 0.1339))
self.view.set_viewport_position((x, y))
print(old_viewport_position)
print(x)
print(y)[/code]

How u can see I need a weird thing with set_viewport_position, only in this way it brings me the position of viewport in a right place, more details about this problem u can see here (stackoverflow.com/questions/1505 … 5_15053679).
Ok, now it works somehow. But it is obvious that there are unpleasant blinkings of viewport because of moving. Can somebody explain me how to fix all this mess with viewport.

0 Likes