Home Download Buy Blog Forum Support

First plugin feedback? Repository

First plugin feedback? Repository

Postby reagle on Tue Jun 24, 2014 3:59 pm

I really love WriteMonkey's repository feature which moves text to a repository (actually, the end of the text file which is hidden in the normal view). I wanted something similar, but to move it to a different text file with an extension '.repo_md' that is also in my list of header extensions so it can easily be opened with alt+o. Any suggestions for improvement?

Code: Select all

from os.path import abspath, basename, dirname, exists, splitext
import sublime, sublime_plugin
import time

# https://www.sublimetext.com/docs/3/api_reference.html

class MoveToRepoCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel(): 
            if not region.empty(): 
                selection = self.view.substr(region) 
                date = time.strftime("%Y-%m-%d %H:%M UTC", time.localtime())
                chunk = '\n<!-- moved from main text: %s -->\n\n%s\n\n' % (
                    date, selection)
                fn = abspath(self.view.file_name())
                path = dirname(fn)
                fn_base, fn_ext = splitext(basename(fn))
                fn_repo = path + '/' + fn_base + '.repo_md'
                print('fn_repo = %s' % fn_repo)
                if exists(fn_repo):
                    with open(fn_repo, 'r') as repo:
                        repo_content = repo.read()
                    repo_content = ''
                with open(fn_repo, 'w') as repo:
                    repo.write(chunk + repo_content)
                self.view.replace(edit, region, '')

Posts: 4
Joined: Tue Jun 24, 2014 3:19 pm

Re: First plugin feedback? Repository

Postby FichteFoll on Fri Jun 27, 2014 1:46 pm

Yes. Don't ever use hardcoded slashes for filepaths, use `os.path.join`. Other than that I didn't really analyze the plugin's functionality.
Posts: 953
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests