Hi,
I am trying to enhance php-tidy plugin which formats php code when you press a key combo. I would like it to format the code on save and then scroll to the line that was previously focused(now it jumps to the beginning of the file). I added all the necessary code to achieve that, the only thing that does not work is to scroll to a certain line.
import sublime, sublime_plugin, re, os
class EventDump(sublime_plugin.EventListener):
def on_pre_save(self, view):
view.run_command('format')
class FormatCommand(sublime_plugin.TextCommand):
def run(self, edit):
FILE = self.view.file_name()
settings = sublime.load_settings('PhpTidy.sublime-settings')
supported_filetypes = settings.get('filetypes') or '.php', '.module', '.inc']
(row,col) = self.view.rowcol(self.view.sel()[0].begin())
print('PhpTidy: invoked on file: %s' % (FILE))
if os.path.splitext(FILE)[1] in supported_filetypes:
print('PhpTidy: Ok, this seems to be PHP')
# set tidy type
tidy_type = settings.get('tidytype') or 'wp'
if tidy_type == 'wp':
tidy_file = 'wp-phptidy.php'
else:
tidy_file = 'phptidy.php'
# path to plugin - <sublime dir>/Packages/PhpTidy
pluginpath = sublime.packages_path() + '/PhpTidy'
scriptpath = pluginpath + '/' + tidy_file
# path to temp file
tmpfile = '/tmp/phptidy-sublime-buffer.php'
phppath = '/usr/bin/php'
# set different paths for php and temp file on windows
if sublime.platform() == 'windows':
tmpfile = pluginpath + '/phptidy-sublime-buffer.php'
phppath = 'php.exe'
retval = os.system( '%s -v' % ( phppath ) )
print('PhpTidy: calling php.exe -v returned: %s' % (retval))
if not ((retval == 0) or (retval == 1)):
sublime.error_message('PhpTidy cannot find %s. Make sure it is available in your PATH. (Error %s)' % (phppath,retval))
return
# set script and check if it exists
if not os.path.exists( scriptpath ):
sublime.error_message('PhpTidy cannot find the script at %s.' % (scriptpath))
return
# get current buffer
bufferLength = sublime.Region(0, self.view.size())
bufferContent = self.view.substr(bufferLength).encode('utf-8')
# write tmpfile
fileHandle = open ( tmpfile, 'wb' )
fileHandle.write ( bufferContent )
fileHandle.close()
print('PhpTidy: buffer written to tmpfile: %s' % (tmpfile))
# call phptidy on tmpfile
scriptpath = pluginpath + '/' + tidy_file
print('PhpTidy: calling script: %s "%s" replace "%s"' % ( phppath, scriptpath, tmpfile ) )
retval = os.system( '%s "%s" replace "%s"' % ( phppath, scriptpath, tmpfile ) )
if not ((retval == 0) or (retval == 1)):
print('PhpTidy: script returned: %s' % (retval))
if retval == 32512:
sublime.error_message('PhpTidy cannot find the script at %s.' % (scriptpath))
return
else:
sublime.error_message('There was an error calling the script at %s. Return value: %s' % (scriptpath,retval))
# read tmpfile and delete
fileHandle = open ( tmpfile, 'r' )
newContent = fileHandle.read()
fileHandle.close()
os.remove( tmpfile )
print('PhpTidy: tmpfile was processed and removed')
# remove hidden tmp file generated by phptidy.php
if os.path.exists('/tmp/.phptidy-sublime-buffer.php.phptidybak~'):
os.remove( '/tmp/.phptidy-sublime-buffer.php.phptidybak~' )
# write new content back to buffer
self.view.replace(edit, bufferLength, fixup(newContent))
self.goto_line(row)
def fixup(self, string):
return re.sub(r'\r\n|\r', '\n', string)
def goto_line(self, line):
line = int(line)
# Negative line numbers count from the end of the buffer
if line < 0:
lines, _ = self.view.rowcol(self.view.size())
line = lines + line + 1
pt = self.view.text_point(line, 0)
self.view.sel().clear()
self.view.sel().add(sublime.Region(pt))
self.view.show(pt)
**class EventDump(sublime_plugin.EventListener):
def on_pre_save(self, view):
view.run_command(‘format’)
(row,col) = self.view.rowcol(self.view.sel()[0].begin())
self.goto_line(row)
def goto_line(self, line):
line = int(line)
# Negative line numbers count from the end of the buffer
if line < 0:
lines, _ = self.view.rowcol(self.view.size())
line = lines + line + 1
pt = self.view.text_point(line, 0)
self.view.sel().clear()
self.view.sel().add(sublime.Region(pt))
self.view.show(pt)**
The code in bold is the code that I added. goto_line function is a copy and paste from the default goto_line plugin. Basically what this plugin does is, check for the validity of the file being formatted, saves its contents in a tmp file, creates a php process to format the temp file, reads the tmp file again now containing the formatted code and puts it into the buffer. The problem is goto_line function, it behaves strangely.When you put as a parameter the row number it does not scroll to the right row, most of the times it scrolls to the 0th row. When you give it a row parameter with a certain offset like row + 20 it works. Any ideas why it does that?