]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/editor/py_editor.py
Added wxEditor from Dirc Holtwic
[wxWidgets.git] / utils / wxPython / lib / editor / py_editor.py
1 # (C)opyright by Dirk Holtwick, 1999
2 # ----------------------------------
3 # holtwick@spirito.de
4 # http://www.spirito.de/pyde
5
6 from editor import *
7 from string import *
8 from keyword import *
9 from tokenizer import *
10
11 """
12 This module will be loaded by the main
13 window. It implements some methods that
14 are typical for Python sources.
15 """
16
17 class wxPyEditor(wxEditor):
18
19 # ------------------------------------------------------------------
20
21 def __init__(self, parent, id=-1):
22 wxEditor.__init__(self, parent, id)
23 self.SetFontTab([
24 wxNamedColour('black'),
25 wxNamedColour('blue'),
26 wxNamedColour('red'),
27 wxNamedColour('darkgreen'),
28 wxNamedColour('brown')
29 ])
30
31 # ------------------------------------------------------------------
32
33 def OnUpdateHighlight(self, line = -1):
34 if line>=0:
35 t = self.text[line].text
36 syn = []
37
38 toks = Tokenizer(t).tokens()
39 for type, string, begin, end in toks:
40 if type == "KEY":
41 syn.append((begin, 1))
42 syn.append((end, 0))
43 elif type == "COMMENT":
44 syn.append((begin, 2))
45 elif type == "STRING":
46 syn.append((begin, 3))
47 syn.append((end, 0))
48 elif type == "NUMBER":
49 syn.append((begin, 4))
50 syn.append((end, 0))
51 elif type == "NAME":
52 if string=="self":
53 syn.append((begin, 4))
54 syn.append((end, 0))
55 else:
56 pass
57 self.text[line].syntax = syn
58
59 # ------------------------------------------------------------------
60
61 def OnUpdateSyntax(self, line = -1):
62 if line>=0:
63 """
64 tx, syn, m = self.text[line]
65 pre = 0
66 for i in range(0,len(tx)):
67 if tx[i] != " ":
68 pre = i
69 break
70 t = tx[pre:]
71
72 t = Tokenizer(t).line()
73
74 t = tx[:pre] + t
75 self.text[line] = t, syn, m
76 """
77 self.OnUpdateHighlight(line)
78
79 # ------------------------------------------------------------------
80
81 def OnTabulator(self, event):
82 add = +1
83 if event.ShiftDown():
84 add = -1
85 t = self.GetTextLine(self.cy)
86 if strip(t):
87 indent = self.GetIndent(t)
88 # print indent
89 t = t[indent:]
90 tabs = indent / self.tabsize
91 # for i in range(0,tabs+add):
92 t = (" " * 4 * (tabs+add)) + t
93 self.SetTextLine(self.cy, t)
94 elif add>0:
95 self.InsertText(" ")
96
97 # ------------------------------------------------------------------
98
99 def FindQuote(self, lineno, quote_type='"""', direction=1):
100 """find line containing the matching quote"""
101 l =lineno +direction
102 while (l < len(self.text)-1) and (l >= 0):
103 if find(self.text[l].text, quote_type) >=0: return l
104 l =l +direction
105 return None
106
107 def FindNextLine(self, lineno, direction=1):
108 """get the next line of code (skipping comment lines and empty lines)"""
109 l =lineno +direction
110 while (l < len(self.text)-1) and (l >= 0):
111 str =lstrip(self.text[l].text)
112 if (len(str) >0) and (str[0] !="#"): return l
113 l =l +direction
114 return None
115
116 def Fold(self):
117 l = self.GetLine(self.cy)
118 line = self.text[l]
119 t = line.text
120
121 # fold ...
122 if line.editable:
123
124 # 3*quotes
125 qpos =find(t, '"""')
126 if qpos >=0: qtype ='"""'
127 else:
128 qpos =find(t, "'''")
129 if qpos >=0: qtype ="'''"
130
131 if (qpos >=0) and (find(t[qpos+3:], qtype) <0):
132 closing_quote =self.FindQuote(l, qtype)
133 if closing_quote !=None:
134 line.editable = not line.editable
135 l =l +1
136 while l <= closing_quote:
137 self.text[l].visible =self.text[l].visible +1
138 l =l +1
139
140 else: # try normal fold on leading whitespace
141 lim = self.GetIndent(t)
142 lnext =self.FindNextLine(l)
143 if (lnext !=None) \
144 and (self.GetIndent(self.text[lnext].text) >lim):
145 line.editable =FALSE
146 lstart =l +1
147 l =self.FindNextLine(l)
148 while (l !=None) \
149 and (self.GetIndent(self.text[l].text) >lim):
150 l =self.FindNextLine(l)
151 if l ==None:
152 # fold till the end
153 l =len(self.text)
154 for line in self.text[lstart:l]:
155 line.visible =line.visible +1
156
157 # ... or unfold
158 else:
159 lim = line.visible + 1
160 line.editable = not line.editable
161
162 l = l + 1
163 line = self.text[l]
164 while (l < (len(self.text) -1)) and (line.visible>=lim):
165 line.visible = line.visible - 1
166 l = l + 1
167 line = self.text[l]
168
169 def FoldAll(self):
170 self.CalcLines()
171 self.cx = 0
172 self.cy = len(self.lines) - 1
173 prev_indent =0
174 # following loop is exited in two cases:
175 # when self.cy becomes 0 (topmost level is not folded by FoldAll)
176 # or when FindNextLine() returns None (all remaining lines till
177 # the beginning of the text are empty or comments)
178 while self.cy:
179 t = self.GetTextLine(self.cy)
180 # indent-based folding
181 indent =self.GetIndent(t)
182 if indent <prev_indent:
183 self.Fold()
184 prev_indent =indent
185 # triple-quote folding
186 qpos =find(t, '"""')
187 if qpos >=0: qtype ='"""'
188 else:
189 qpos =find(t, "'''")
190 if qpos >=0: qtype ="'''"
191 if (qpos >=0) and (find(t[qpos+3:], qtype) <0):
192 closing_quote =self.FindQuote(self.cy, qtype, -1)
193 if closing_quote !=None:
194 # XXX potential bug: unmatched triple quotes
195 self.cy =closing_quote
196 self.Fold()
197 self.cy =self.FindNextLine(self.cy, -1)
198 if self.cy ==None: self.cy =0
199
200 # ------------------------------------------------------------------
201
202 def OnFold(self):
203 self.Fold()
204
205 # ------------------------------------------------------------------
206
207 def OnInit(self):
208 #self.FoldAll()
209 pass
210