-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdynamicsnippets.py
71 lines (54 loc) · 2.24 KB
/
dynamicsnippets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
#coding: utf8
#################################### IMPORTS ###################################
# Standard Libs
import re
# Sublime Libs
import sublime
import sublime_plugin
################################## BASE CLASS ##################################
class CommandsAsYouTypeBase(sublime_plugin.TextCommand):
history = {}
filter_input = lambda s, i: i
grammar = None
def run_command(self, view, value):
if '\n' in value:
for sel in view.sel():
trailing = sublime.Region(sel.end(), view.line(sel).end())
if view.substr(trailing).isspace():
view.erase(self.edit, trailing)
view.run_command('insert_snippet', { 'contents': value })
def insert(self, abbr):
view = self.view
if not abbr and self.erase:
self.undo()
self.erase = False
return
def inner_insert():
self.edit = edit = view.begin_edit()
cmd_input = self.filter_input(abbr) or ''
self.erase = self.run_command(view, cmd_input) is not False
view.end_edit(edit)
self.undo()
sublime.set_timeout(inner_insert, 0)
def undo(self):
if self.erase:
sublime.set_timeout(lambda: self.view.run_command('undo'), 0)
def run(self, edit, **args):
self.erase = False
panel = self.view.window().show_input_panel (
self.input_message, self.default_input, None, self.insert, self.undo )
panel.sel().clear()
panel.sel().add(sublime.Region(0, panel.size()))
if self.grammar:
panel.set_syntax_file(self.grammar)
setting = panel.settings().set
# For some reason we can't set these in the
# `ZenCoding/ZenCoding.sublime-settings`, only in
# `User/ZenCoding.sublime-settings`, only in
setting('line_numbers', False)
setting('gutter', False)
setting('auto_complete', False)
setting('tab_completion', False)
setting('auto_id_class', True)
################################################################################