]>
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,
22 pos
=wxDefaultPosition
, size
=wxDefaultSize
, style
=0):
23 wxEditor
.__init
__(self
, parent
, id, pos
, size
, style
)
25 wxNamedColour('black'),
26 wxNamedColour('blue'),
28 wxNamedColour('darkgreen'),
29 wxNamedColour('brown')
32 # ------------------------------------------------------------------
34 def OnUpdateHighlight(self
, line
= -1):
36 t
= self
.text
[line
].text
39 toks
= Tokenizer(t
).tokens()
40 for type, string
, begin
, end
in toks
:
42 syn
.append((begin
, 1))
44 elif type == "COMMENT":
45 syn
.append((begin
, 2))
46 elif type == "STRING":
47 syn
.append((begin
, 3))
49 elif type == "NUMBER":
50 syn
.append((begin
, 4))
54 syn
.append((begin
, 4))
58 self
.text
[line
].syntax
= syn
60 # ------------------------------------------------------------------
62 def OnUpdateSyntax(self
, line
= -1):
65 tx, syn, m = self.text[line]
67 for i in range(0,len(tx)):
73 t = Tokenizer(t).line()
76 self.text[line] = t, syn, m
78 self
.OnUpdateHighlight(line
)
80 # ------------------------------------------------------------------
82 def OnTabulator(self
, event
):
86 t
= self
.GetTextLine(self
.cy
)
88 indent
= self
.GetIndent(t
)
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
)
98 # ------------------------------------------------------------------
100 def FindQuote(self
, lineno
, quote_type
='"""', direction
=1):
101 """find line containing the matching quote"""
103 while (l
< len(self
.text
)-1) and (l
>= 0):
104 if find(self
.text
[l
].text
, quote_type
) >=0: return l
108 def FindNextLine(self
, lineno
, direction
=1):
109 """get the next line of code (skipping comment lines and empty lines)"""
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
118 l
= self
.GetLine(self
.cy
)
127 if qpos
>=0: qtype
='"""'
130 if qpos
>=0: qtype
="'''"
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
137 while l
<= closing_quote
:
138 self
.text
[l
].visible
=self
.text
[l
].visible
+1
141 else: # try normal fold on leading whitespace
142 lim
= self
.GetIndent(t
)
143 lnext
=self
.FindNextLine(l
)
145 and (self
.GetIndent(self
.text
[lnext
].text
) >lim
):
148 l
=self
.FindNextLine(l
)
150 and (self
.GetIndent(self
.text
[l
].text
) >lim
):
151 l
=self
.FindNextLine(l
)
155 for line
in self
.text
[lstart
:l
]:
156 line
.visible
=line
.visible
+1
160 lim
= line
.visible
+ 1
161 line
.editable
= not line
.editable
165 while (l
< (len(self
.text
) -1)) and (line
.visible
>=lim
):
166 line
.visible
= line
.visible
- 1
173 self
.cy
= len(self
.lines
) - 1
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)
180 t
= self
.GetTextLine(self
.cy
)
181 # indent-based folding
182 indent
=self
.GetIndent(t
)
183 if indent
<prev_indent
:
186 # triple-quote folding
188 if qpos
>=0: qtype
='"""'
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
198 self
.cy
=self
.FindNextLine(self
.cy
, -1)
199 if self
.cy
==None: self
.cy
=0
201 # ------------------------------------------------------------------
206 # ------------------------------------------------------------------