Sublime Forum

Updating plugin code to ST3

#1

I have a plugin I copy/pasted from the net which adds a timestamp to tpl files on save, works on ST2 but not on ST3, the issue is due to begin_edit()
and would like to request for help on how to convert it so that it works on ST3 or a link to some examples on using the new begin_edit().

import sublime, sublime_plugin, datetime

class TimestampListener (sublime_plugin.EventListener):
    def on_pre_save (self, view):
        fileType = view.file_name()-4:]
        if (not fileType == ".tpl"):
            return

        edit = view.begin_edit()

        # Check for existing timestamp to replace
        stamp = view.find_all("last modified:\\s*(201[0-9]-\\d+-\\d+\\s+\\d+:\\d+:\\d+(\\.\\d+)]\\s+\\+[0-9]{4}?)", 0)
        for region in reversed(stamp):
            view.replace(edit, region, "last modified:")

        # Generate timestamp
        timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S +0800')

        # Locate timestamp placeholder
        newStamp = view.find_all("last modified:", 0)
        for region in reversed(newStamp):
            view.replace(edit, region, "last modified: {0}".format(timestamp))

        view.end_edit (edit)

TIA

0 Likes

#2

You need to extract all the part between begin_edit and end_edit and put that into a TextCommand that you call with a run_command.

Untested code for you to start:

class UpdateTimestampCommand(sublime_plugin.TextCommand):

    def run(self, edit, user_input=None):
        view = edit.active_view()
        # Check for existing timestamp to replace
        stamp = view.find_all("last modified:\\s*(201[0-9]-\\d+-\\d+\\s+\\d+:\\d+:\\d+(\\.\\d+)]\\s+\\+[0-9]{4}?)", 0)
        for region in reversed(stamp):
            view.replace(edit, region, "last modified:")

        # Generate timestamp
        timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S +0800')

        # Locate timestamp placeholder
        newStamp = view.find_all("last modified:", 0)
        for region in reversed(newStamp):
            view.replace(edit, region, "last modified: {0}".format(timestamp))
0 Likes

#3

[quote=“Clams”]You need to extract all the part between begin_edit and end_edit and put that into a TextCommand that you call with a run_command.

Untested code for you to start:
<-- code -->
[/quote]

Thanks for the starting point mate, was able to make it work after a couple of hours of trial and error and checking out examples. :blush:
Gist and code if anyone’s interested - http://goo.gl/Rrfn8W

import sublime, sublime_plugin, datetime

class InsertTimestampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # Check for existing timestamp to replace
        stamp = self.view.find_all("last modified:\\s*(201[0-9]-\\d+-\\d+\\s+\\d+:\\d+:\\d+(\\.\\d+)]\\s+\\+[0-9]{4}?)", 0)
        for region in reversed(stamp):
            self.view.replace(edit, region, "last modified:")

        # Generate timestamp
        timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S +0800')

        # Locate timestamp placeholder
        newStamp = self.view.find_all("last modified:", 0)
        for region in reversed(newStamp):
            self.view.replace(edit, region, "last modified: {0}".format(timestamp))


class TimestampListener(sublime_plugin.EventListener):
    def on_pre_save (self, view):
        fileType = self.view.file_name()-4:]
        if (fileType == ".tpl"):
            view.run_command("insert_timestamp")
0 Likes