1  #!/usr/bin/env python
 2  #coding: utf8
 3  #################################### IMPORTS ###################################
 4  
 5  # Standard Libs
 6  import re
 7  
 8  # Sublime Libs
 9  import sublime
10  import sublimeplugin
11  
12  ################################## BASE CLASS ##################################
13  
14  class TabTriggeredBase(sublimeplugin.TextCommand):
15      def run(self, view, args):
16          snippet = self.create_snippet(args)
17  
18          if snippet:
19              view.runCommand('insertInlineSnippet', [snippet])
20          else:
21              view.runCommand('insertAndDecodeCharacters', [' '.join(args)+'\t'] )
22  
23  class SnippetsAsYouTypeBase(sublimeplugin.TextCommand):
24      history = {}
25  
26      def init(self, view, args):
27          self.view = view
28          self.args = args
29  
30      def insert(self, abbr):
31          view = self.view
32  
33          def inner_insert():
34              snippet = self.create_snippet(abbr, self.args) or ''
35              view.runCommand('insertInlineSnippet', [snippet])
36  
37          if not self.just_ran:
38              self.erase()
39              sublime.setTimeout(inner_insert, 0)
40          else:
41              inner_insert()
42              self.just_ran = False
43  
44      def erase(self):
45          # called by insert and as onCancel
46          sublime.setTimeout(lambda: self.view.runCommand('undo'), 0)
47  
48      def run(self, view, args):
49          self.just_ran = True
50  
51          self.init(view, args)
52          args = tuple(args)
53  
54          last_entry    =  self.history.get(args, '')
55          self.insert(last_entry)
56  
57          def done(abbr):
58              self.history[args] = abbr
59  
60          view.window().showInputPanel (
61              self.input_message, last_entry, done, self.insert, self.erase )
62          
63  ################################################################################