(no commit message)
[blender-addons-contrib.git] / development_class_viewer.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 bl_info = {
20         'name': "Class Viewer",
21         'author': "Mackraken", "batFinger"
22         'version': (0, 1, 2),
23         'blender': (2, 5, 8),
24         'api': 37702,
25         'location': "Text Editor > Toolbar, Text Editor > Right Click",
26         'warning': "",
27         'description': "List text's classes and definitions",
28         'wiki_url': "https://sites.google.com/site/aleonserra/home/scripts/class-viewer",
29         'category': "Development"}
30
31 import bpy
32
33 #lists all defs, comment or classes
34 #space = current text editor
35 #sort = sort result
36 #tipo = kind of struct to look for
37 #escape = character right next to the class or def name 
38
39 def getfunc(space, sort, tipo="def ", escape= ""):
40         defs = []
41         if space.type=="TEXT_EDITOR":
42                 if space.text!=None:
43                         txt = space.text
44                         
45                         for i, l in enumerate(txt.lines):
46                                 try:
47                                         line = l.body
48                                 except:
49                                         line=""
50                                 
51                                 if line[0:len(tipo)]==tipo:
52                                         end = line.find(escape)
53                                         if end==0:
54                                                 func = line[len(tipo)::]
55                                         else:
56                                                 func = line[len(tipo):end]
57                                         defs.append([func, i+1])
58                         
59                         if sort: defs.sort()
60         
61         return defs
62
63 class TEXT_OT_Jumptoline(bpy.types.Operator):
64         bl_label = "Jump"
65         bl_idname = "text.jumptoline"
66         __doc__ = "Jump to line"
67         line = bpy.props.IntProperty(default=-1)
68         
69         @classmethod
70         def poll(cls, context):
71                 if context.area.spaces[0].type!="TEXT_EDITOR":
72                         return False
73                 else: 
74                         return context.area.spaces[0].text!=None
75         
76         def execute(self, context):
77                 
78                 scn = context.scene
79
80                 if self.line!=-1:
81                         bpy.ops.text.jump(line=self.line)
82
83                 return {'FINISHED'}
84
85
86
87 class CommentsMenu(bpy.types.Menu):
88         bl_idname = "OBJECT_MT_select_comments"
89         bl_label = "Select"
90         
91         
92         
93         @classmethod
94         def poll(cls, context):
95                 if context.area.spaces[0].type!="TEXT_EDITOR":
96                         return False
97                 else: 
98                         return context.area.spaces[0].text!=None
99                 
100         def draw(self, context):
101         
102                 layout = self.layout
103                 space = context.area.spaces[0]
104                 
105                 items = getfunc(space, 1, "### ", "")
106                 
107                 for it in items:
108                         layout.operator("text.jumptoline",text=it[0]).line = it[1]
109
110 class DefsMenu(bpy.types.Menu):
111         bl_idname = "OBJECT_MT_select_defs"
112         bl_label = "Select"
113         
114         tipo = bpy.props.BoolProperty(default=False)
115         
116         @classmethod
117         def poll(cls, context):
118                 if context.area.spaces[0].type!="TEXT_EDITOR":
119                         return False
120                 else: 
121                         return context.area.spaces[0].text!=None
122                 
123         def draw(self, context):
124                 scn = context.scene
125                 layout = self.layout
126                 space = context.area.spaces[0]
127                 tipo = self.tipo
128                 
129                 
130                 items = getfunc(space, 1, "def ", "(")
131                 
132                 for it in items:
133                         layout.operator("text.jumptoline",text=it[0]).line = it[1]
134                                 
135 class ClassesMenu(bpy.types.Menu):
136         bl_idname = "OBJECT_MT_select_classes"
137         bl_label = "Select"
138         
139         
140         
141         @classmethod
142         def poll(cls, context):
143                 if context.area.spaces[0].type!="TEXT_EDITOR":
144                         return False
145                 else: 
146                         return context.area.spaces[0].text!=None
147                 
148         def draw(self, context):
149         
150                 layout = self.layout
151                 space = context.area.spaces[0]
152         
153                 
154                 
155                 items = getfunc(space, 1, "class ", "(")
156                 
157                 for it in items:
158                         layout.operator("text.jumptoline",text=it[0]).line = it[1]
159
160
161                         
162 def GotoComments(self, context): 
163         self.layout.menu("OBJECT_MT_select_comments", text="Comments", icon='PLUGIN')
164         return False
165
166 def GotoDefs(self, context): 
167         self.layout.menu("OBJECT_MT_select_defs", text="Defs", icon='PLUGIN')
168         return False
169
170 def GotoClasses(self, context): 
171         self.layout.menu("OBJECT_MT_select_classes", text="Classes", icon='PLUGIN')
172         return False
173
174
175 classes = [TEXT_OT_Jumptoline, ClassesMenu, DefsMenu, CommentsMenu]
176
177
178 def register():
179         for c in classes:
180                 bpy.utils.register_class(c)
181                 
182         bpy.types.TEXT_MT_toolbox.append(GotoComments)
183         bpy.types.TEXT_MT_toolbox.append(GotoDefs)
184         bpy.types.TEXT_MT_toolbox.append(GotoClasses)
185
186 def unregister():
187         for c in classes:
188                 bpy.utils.unregister_class(c)
189                 
190         bpy.types.TEXT_MT_toolbox.remove(GotoComments)
191         bpy.types.TEXT_MT_toolbox.remove(GotoDefs)
192         bpy.types.TEXT_MT_toolbox.remove(GotoClasses)
193         
194 if __name__ == "__main__":
195         register()