]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/editor/py_editor.py
1 # (C)opyright by Dirk Holtwick, 1999
2 # ----------------------------------
4 # http://www.spirito.de/pyde
9 from tokenizer
import *
12 This module will be loaded by the main
13 window. It implements some methods that
14 are typical for Python sources.
17 class wxPyEditor(wxEditor
):
19 # ------------------------------------------------------------------
21 def __init__(self
, parent
, id=-1):
22 wxEditor
.__init
__(self
, parent
, id)
24 wxNamedColour('black'),
25 wxNamedColour('blue'),
27 wxNamedColour('darkgreen'),
28 wxNamedColour('brown')
31 # ------------------------------------------------------------------
33 def OnUpdateHighlight(self
, line
= -1):
35 t
= self
.text
[line
].text
38 toks
= Tokenizer(t
).tokens()
39 for type, string
, begin
, end
in toks
:
41 syn
.append((begin
, 1))
43 elif type == "COMMENT":
44 syn
.append((begin
, 2))
45 elif type == "STRING":
46 syn
.append((begin
, 3))
48 elif type == "NUMBER":
49 syn
.append((begin
, 4))
53 syn
.append((begin
, 4))
57 self
.text
[line
].syntax
= syn
59 # ------------------------------------------------------------------
61 def OnUpdateSyntax(self
, line
= -1):
64 tx, syn, m = self.text[line]
66 for i in range(0,len(tx)):
72 t = Tokenizer(t).line()
75 self.text[line] = t, syn, m
77 self
.OnUpdateHighlight(line
)
79 # ------------------------------------------------------------------
81 def OnTabulator(self
, event
):
85 t
= self
.GetTextLine(self
.cy
)
87 indent
= self
.GetIndent(t
)
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
)
97 # ------------------------------------------------------------------
99 def FindQuote(self
, lineno
, quote_type
='"""', direction
=1):
100 """find line containing the matching quote"""
102 while (l
< len(self
.text
)-1) and (l
>= 0):
103 if find(self
.text
[l
].text
, quote_type
) >=0: return l
107 def FindNextLine(self
, lineno
, direction
=1):
108 """get the next line of code (skipping comment lines and empty lines)"""
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
117 l
= self
.GetLine(self
.cy
)
126 if qpos
>=0: qtype
='"""'
129 if qpos
>=0: qtype
="'''"
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
136 while l
<= closing_quote
:
137 self
.text
[l
].visible
=self
.text
[l
].visible
+1
140 else: # try normal fold on leading whitespace
141 lim
= self
.GetIndent(t
)
142 lnext
=self
.FindNextLine(l
)
144 and (self
.GetIndent(self
.text
[lnext
].text
) >lim
):
147 l
=self
.FindNextLine(l
)
149 and (self
.GetIndent(self
.text
[l
].text
) >lim
):
150 l
=self
.FindNextLine(l
)
154 for line
in self
.text
[lstart
:l
]:
155 line
.visible
=line
.visible
+1
159 lim
= line
.visible
+ 1
160 line
.editable
= not line
.editable
164 while (l
< (len(self
.text
) -1)) and (line
.visible
>=lim
):
165 line
.visible
= line
.visible
- 1
172 self
.cy
= len(self
.lines
) - 1
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)
179 t
= self
.GetTextLine(self
.cy
)
180 # indent-based folding
181 indent
=self
.GetIndent(t
)
182 if indent
<prev_indent
:
185 # triple-quote folding
187 if qpos
>=0: qtype
='"""'
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
197 self
.cy
=self
.FindNextLine(self
.cy
, -1)
198 if self
.cy
==None: self
.cy
=0
200 # ------------------------------------------------------------------
205 # ------------------------------------------------------------------