Home Download Buy Blog Forum Support

First plugin ever

First plugin ever

Postby davidjdriver on Thu Jan 10, 2013 5:36 pm

Could you help me with my first plugin? I have a file that is delimited by tilde's "~". I would like to insert a new line after the tilde character. I would also like to insert a new line before the string of characters "INS*". I gave it a first stab, I had written some python years ago, by following the ROT13 example on the website. This is what I cam up with:

Code: Select all
import sublime, sublime_plugin

class X12Command(sublime_plugin.TextCommand):
   def run(self, view, args):
      for region in view.sel():
         if not region.empty():
            s = view.substr(region)
            s = s.replace('~', '~\n')
            s = s.replace('INS*', '\nINS*')
            view.replace(region, s)


When I run it in the console I don't get any output:

>>> view.run_command('X12')

But there are no errors. The text in the editor window does not update and there is no other feedback. I tried dropping a print statement in the run method but it didn't write to the command window like I thought it would.

Any help is appreciated. Thanks!
davidjdriver
 
Posts: 5
Joined: Thu Jan 10, 2013 5:27 pm

Re: First plugin ever

Postby adzenith on Thu Jan 10, 2013 11:35 pm

Have you selected the whole document before running the command?
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: First plugin ever

Postby davidjdriver on Fri Jan 11, 2013 12:50 am

I just tried that and it didn't seem to change anything.
davidjdriver
 
Posts: 5
Joined: Thu Jan 10, 2013 5:27 pm

Re: First plugin ever

Postby skuroda on Fri Jan 11, 2013 2:45 am

Couple of things, try the following "view.run_command("x12")". Also, I think your the arguments for view.replace is wrong. According to the API reference it should be "view.replace(edit, region, string)"
skuroda
 
Posts: 689
Joined: Fri Oct 05, 2012 8:11 am

Re: First plugin ever

Postby agibsonsw on Fri Jan 11, 2013 11:49 am

skuroda wrote:Couple of things, try the following "view.run_command("x12")". Also, I think your the arguments for view.replace is wrong. According to the API reference it should be "view.replace(edit, region, string)"


Yes, the default plugin is:

Code: Select all
import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")


which supplies edit, and view is available as self.view.

You may possible need 'x_12' to run your command.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: First plugin ever

Postby davidjdriver on Tue Jan 15, 2013 4:28 pm

I am sorry that it took me so long to get back to this. I was off work Friday and swamped Monday. I cracked the plugin back open this morning and compared it to the one in the template. Evidently the source code I based my plugin on was from an older API. With your suggestions and using the template I was able to get it to work.

Code: Select all
import sublime, sublime_plugin

class X12Command(sublime_plugin.TextCommand):
   def run(self, edit):
      for region in self.view.sel():
         if not region.empty():
            s = self.view.substr(region)
            s = s.replace('~', '~\n')
            s = s.replace('INS*', '\nINS*')
            self.view.replace(edit, region, s)


And this Run command:
Code: Select all
>>> view.run_command('x12')


And it worked. Now all I have to do is get the command registered and I am in business.

I appreciate all of your help.

Thanks!
davidjdriver
 
Posts: 5
Joined: Thu Jan 10, 2013 5:27 pm

Re: First plugin ever

Postby davidjdriver on Tue Jan 15, 2013 4:31 pm

One more question though. Is there a way I can make this run against all text in the file and not just the selected text?
davidjdriver
 
Posts: 5
Joined: Thu Jan 10, 2013 5:27 pm

Re: First plugin ever

Postby FichteFoll on Sat Jan 19, 2013 4:12 pm

Code: Select all
reg = sublime.Region(0, self.view.size())
text = self.view.substr(reg)
# ...
self.view.replace(edit, reg, text)
FichteFoll
 
Posts: 408
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Re: First plugin ever

Postby davidjdriver on Tue Jan 22, 2013 11:08 pm

Works like a charm. Thank you very much!
davidjdriver
 
Posts: 5
Joined: Thu Jan 10, 2013 5:27 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests