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