I decided to finally mess around with plugins and I made one that does this. It feels really ugly though. I don't have any Python experience so this is just what I pieced together while quickly skipping through the Python docs. I was wondering if anyone with more experienced could answer a few questions about it for me.
- Code: Select all
import sublime, sublimeplugin
class insertNumbers(sublimeplugin.TextCommand):
def run(self, view, args):
self.view = view;
window = view.window()
window.showInputPanel('Enter a starting number and step.', '1 1', self._onDone, None, None)
def _onDone(self, input):
input = input.split(' ')
for region in self.view.sel():
if region.empty():
self.view.insert(region.a, input[0])
else:
self.view.replace(region, input[0])
input[0] = int(input[0]) + int(input[1])
input[0] = str(input[0])
The first issue I had was passing integers as arguments to commands. I'm assuming commands can only take strings for some reason? That then led to the really ugly typecasting I ended up having to do. Is there a better way of doing what I did?
(I hope there is :s)Secondly, is there a better way to pass reference to the view the command was called on? I just stored it in the class and that seemed to work, but I was thinking there might be a better way of doing this?
If there's anything else that can be done cleaner please say so. My first time ever messing with Python and Sublime plugins so I'd like to start right. Thanks.
Oh and if anyone wants to mess with this plugin, bind
insertNumbers to a key, it will open an input box at the bottom of the screen. The first number is the number to start with, and the second number is the step. For example,
1 1 will result in
1, 2, 3, 4..,
1 2 will result in
1, 3, 5, 7.., etc..