25dadf4de540877692a8fd3a37d2581bbfdde76f
[blender-staging.git] / release / scripts / textplugin_templates.py
1 #!BPY
2 """
3 Name: 'Template Completion | Tab'
4 Blender: 246
5 Group: 'TextPlugin'
6 Shortcut: 'Tab'
7 Tooltip: 'Completes templates based on the text preceding the cursor'
8 """
9
10 # Only run if we have the required modules
11 try:
12         import bpy
13         from BPyTextPlugin import *
14         from Blender import Text
15 except ImportError:
16         OK = False
17 else:
18         OK = True
19
20 templates = {
21         'ie':
22                 'if ${1:cond}:\n'
23                 '\t${2}\n'
24                 'else:\n'
25                 '\t${3}\n',
26         'iei':
27                 'if ${1:cond}:\n'
28                 '\t${2}\n'
29                 'elif:\n'
30                 '\t${3}\n'
31                 'else:\n'
32                 '\t${4}\n',
33         'def':
34                 'def ${1:name}(${2:params}):\n'
35                 '\t"""(${2}) - ${3:comment}"""\n'
36                 '\t${4}',
37         'cls':
38                 'class ${1:name}(${2:parent}):\n'
39                 '\t"""${3:docs}"""\n'
40                 '\t\n'
41                 '\tdef __init__(self, ${4:params}):\n'
42                 '\t\t"""Creates a new ${1}"""\n'
43                 '\t\t${5}'
44 }
45
46 def main():
47         txt = bpy.data.texts.active
48         if not txt:
49                 return
50         
51         row, c = txt.getCursorPos()
52         line = txt.asLines(row, row+1)[0]
53         indent=0
54         while indent<c and (line[indent]==' ' or line[indent]=='\t'):
55                 indent += 1
56         
57         # Check we are in a normal context
58         if get_context(txt) != CTX_NORMAL:
59                 return
60         
61         targets = get_targets(line, c-1);
62         if len(targets) != 1: return
63         
64         color = (0, 192, 32)
65         
66         for trigger, template in templates.items():
67                 if trigger != targets[0]: continue
68                 inserts = {}
69                 txt.delete(-len(trigger)-1)
70                 y, x = txt.getCursorPos()
71                 first = None
72                 
73                 # Insert template text and parse for insertion points
74                 count = len(template); i = 0
75                 while i < count:
76                         if i<count-1 and template[i]=='$' and template[i+1]=='{':
77                                 i += 2
78                                 e = template.find('}', i)
79                                 item = template[i:e].split(':')
80                                 if len(item)<2: item.append('')
81                                 if not inserts.has_key(item[0]):
82                                         inserts[item[0]] = (item[1], [(x, y)])
83                                 else:
84                                         inserts[item[0]][1].append((x, y))
85                                         item[1] = inserts[item[0]][0]
86                                 if not first: first = (item[1], x, y)
87                                 txt.insert(item[1])
88                                 x += len(item[1])
89                                 i = e
90                         else:
91                                 txt.insert(template[i])
92                                 if template[i] == '\n':
93                                         txt.insert(line[:indent])
94                                         y += 1
95                                         x = indent
96                                 else:
97                                         x += 1
98                         i += 1
99                 
100                 # Insert markers at insertion points
101                 for id, (text, points) in inserts.items():
102                         for x, y in points:
103                                 txt.setCursorPos(y, x)
104                                 txt.setSelectPos(y, x+len(text))
105                                 txt.markSelection(hash(text)+int(id), color, Text.TMARK_TEMP | Text.TMARK_EDITALL)
106                 if first:
107                         text, x, y = first
108                         txt.setCursorPos(y, x)
109                         txt.setSelectPos(y, x+len(text))
110                 break
111                 
112
113 # Check we are running as a script and not imported as a module
114 if __name__ == "__main__" and OK:
115         main()