16
16
__author__ = "Cody Precord <cprecord@editra.org>"
17
__svnid__ = "$Id: _python.py 64177 2010-04-30 01:44:27Z CJP $"
18
__revision__ = "$Revision: 64177 $"
17
__svnid__ = "$Id: _python.py 66807 2011-01-28 18:29:56Z CJP $"
18
__revision__ = "$Revision: 66807 $"
20
20
#-----------------------------------------------------------------------------#
79
79
class SyntaxData(syndata.SyntaxDataBase):
80
80
"""SyntaxData object for Python"""
81
81
def __init__(self, langid):
82
syndata.SyntaxDataBase.__init__(self, langid)
82
super(SyntaxData, self).__init__(langid)
85
85
self.SetLexer(stc.STC_LEX_PYTHON)
104
104
#-----------------------------------------------------------------------------#
106
def AutoIndenter(stc, pos, ichar):
107
"""Auto indent python code. uses \n the text buffer will
108
handle any eol character formatting.
109
@param stc: EditraStyledTextCtrl
106
def AutoIndenter(estc, pos, ichar):
107
"""Auto indent python code.
108
@param estc: EditraStyledTextCtrl
110
109
@param pos: current carat position
111
110
@param ichar: Indentation character
116
line = stc.GetCurrentLine()
117
spos = stc.PositionFromLine(line)
118
text = stc.GetTextRange(spos, pos)
119
epos = stc.GetLineEndPosition(line)
113
line = estc.GetCurrentLine()
114
spos = estc.PositionFromLine(line)
115
text = estc.GetTextRange(spos, pos)
116
eolch = estc.GetEOLChar()
120
117
inspace = text.isspace()
122
119
# Cursor is in the indent area somewhere
121
estc.AddText(eolch + text.replace(eolch, u""))
126
124
# Check if the cursor is in column 0 and just return newline.
127
125
if not len(text):
130
# Ignore empty lines and backtrace to find the previous line that we can
131
# get the indent position from
132
# while text.isspace():
136
# text = stc.GetTextRange(stc.PositionFromLine(line), pos)
138
indent = stc.GetLineIndentation(line)
129
# In case of open bracket: Indent next to open bracket
130
def BackTrack(tmp_text, tline):
131
bcount = [ tmp_text.count(brac) for brac in u")}]({[" ]
133
for idx, val in enumerate(bcount[:3]):
134
if val > bcount[idx+3]:
141
spos = estc.PositionFromLine(tline)
142
tmp_text = estc.GetTextRange(spos, pos)
143
BackTrack(tmp_text, tline)
145
text = BackTrack(text, line)
146
pos = PosOpenBracket(text)
148
rval = eolch + (pos + 1) * u" "
152
indent = estc.GetLineIndentation(line)
139
153
if ichar == u"\t":
140
tabw = stc.GetTabWidth()
154
tabw = estc.GetTabWidth()
142
tabw = stc.GetIndent()
156
tabw = estc.GetIndent()
144
158
i_space = indent / tabw
145
159
end_spaces = ((indent - (tabw * i_space)) * u" ")
152
166
elif tokens[-1].endswith(u"\\"):
168
elif len(tokens[-1]) and tokens[-1][-1] in u"}])":
169
ptok = tokens[-1][-1]
170
paren_pos = pos - (len(text) - text.rfind(ptok))
171
oparen, cparen = estc.GetBracePair(paren_pos)
172
if cparen >= 0: # Found matching bracket
173
line = estc.LineFromPosition(cparen)
174
indent = estc.GetLineIndentation(line)
175
i_space = indent / tabw
176
end_spaces = ((indent - (tabw * i_space)) * u" ")
154
177
elif tokens[0] in UNINDENT_KW:
155
178
i_space = max(i_space - 1, 0)
157
rval = u"\n" + (ichar * i_space) + end_spaces
180
rval = eolch + (ichar * i_space) + end_spaces
158
181
if inspace and ichar != u"\t":
159
182
rpos = indent - (pos - spos)
160
183
if rpos < len(rval) and rpos > 0:
161
184
rval = rval[:-rpos]
162
185
elif rpos >= len(rval):
188
# Put text in the buffer
167
191
#---- End Required Module Functions ----#
201
def PosOpenBracket(text):
202
"""Returns the position of the right most open bracket in text.
203
Brackets inside strings are ignored. In case of no open bracket
204
the returned value is -1
205
@param text: The line preceding the new line to be indented.
206
@return res: The position of right most open bracket.
207
@note: Used by AutoIndenter
210
# Store positions of '(', '[','{', ')', ']', '}'
211
brackets = [[], [], [], [], [], []]
214
for pos, char in enumerate(text):
216
in_string = not char == quote
220
typ = u'([{)]}'.find(char)
222
brackets[typ].append(pos)
224
typ = quotes.find(char)
230
opn, cls = brackets[typ], brackets[typ+3]
231
nopn, ncls = len(opn), len(cls)
233
res = max(res, opn[nopn - ncls - 1])
177
236
#---- End Syntax Modules Internal Functions ----#