~ubuntu-branches/ubuntu/raring/wxwidgets2.8/raring

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/syntax/_python.py

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2012-01-07 13:59:25 UTC
  • mfrom: (1.1.9) (5.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20120107135925-2601miy9ullcon9j
Tags: 2.8.12.1-6ubuntu1
* Resync from Debian, changes that were kept:
  - debian/rules: re-enable mediactrl. This allows libwx_gtk2u_media-2.8 to be
    built, as this is required by some applications (LP: #632984)
  - debian/control: Build-dep on libxt-dev for mediactrl.
  - Patches
    + fix-bashism-in-example
* Add conflict on python-wxgtk2.8 (<< 2.8.12.1-6ubuntu1~) to python-wxversion
  to guarantee upgrade ordering when moving from pycentral to dh_python2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
"""
15
15
 
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 $"
19
19
 
20
20
#-----------------------------------------------------------------------------#
21
21
# Imports
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)
83
83
 
84
84
        # Setup
85
85
        self.SetLexer(stc.STC_LEX_PYTHON)
103
103
 
104
104
#-----------------------------------------------------------------------------#
105
105
 
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
112
 
    @return: string
113
111
 
114
112
    """
115
 
    rtxt = u''
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()
121
118
 
122
119
    # Cursor is in the indent area somewhere
123
120
    if inspace:
124
 
        return u"\n" + text
 
121
        estc.AddText(eolch + text.replace(eolch, u""))
 
122
        return
125
123
 
126
124
    # Check if the cursor is in column 0 and just return newline.
127
125
    if not len(text):
128
 
        return u"\n"
129
 
 
130
 
    # Ignore empty lines and backtrace to find the previous line that we can
131
 
    # get the indent position from
132
 
#    while text.isspace():
133
 
#        line -= 1
134
 
#        if line < 0:
135
 
#            return u''
136
 
#        text = stc.GetTextRange(stc.PositionFromLine(line), pos)
137
 
 
138
 
    indent = stc.GetLineIndentation(line)
 
126
        estc.AddText(eolch)
 
127
        return
 
128
 
 
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")}]({[" ]
 
132
        bRecurse = False
 
133
        for idx, val in enumerate(bcount[:3]):
 
134
            if val > bcount[idx+3]:
 
135
                bRecurse = True
 
136
                break
 
137
        if bRecurse:
 
138
            tline = tline - 1
 
139
            if tline < 0:
 
140
                return tmp_text
 
141
            spos = estc.PositionFromLine(tline)
 
142
            tmp_text = estc.GetTextRange(spos, pos)
 
143
            BackTrack(tmp_text, tline)
 
144
        return tmp_text
 
145
    text = BackTrack(text, line)
 
146
    pos = PosOpenBracket(text)
 
147
    if pos > -1:
 
148
        rval = eolch + (pos + 1) * u" "
 
149
        estc.AddText(rval)
 
150
        return
 
151
 
 
152
    indent = estc.GetLineIndentation(line)
139
153
    if ichar == u"\t":
140
 
        tabw = stc.GetTabWidth()
 
154
        tabw = estc.GetTabWidth()
141
155
    else:
142
 
        tabw = stc.GetIndent()
 
156
        tabw = estc.GetIndent()
143
157
 
144
158
    i_space = indent / tabw
145
159
    end_spaces = ((indent - (tabw * i_space)) * u" ")
151
165
                i_space += 1
152
166
        elif tokens[-1].endswith(u"\\"):
153
167
            i_space += 1
 
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)
156
179
 
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):
163
 
            rval = u"\n"
 
186
            rval = eolch
164
187
 
165
 
    return rval
 
188
    # Put text in the buffer
 
189
    estc.AddText(rval)
166
190
 
167
191
#---- End Required Module Functions ----#
168
192
 
174
198
    """
175
199
    return PY_KW[1]
176
200
 
 
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
 
208
 
 
209
    """
 
210
    # Store positions of '(', '[','{', ')', ']', '}'
 
211
    brackets = [[], [], [], [], [], []]
 
212
    quotes = u"'" + u'"'
 
213
    in_string = False
 
214
    for pos, char in enumerate(text):
 
215
        if in_string:
 
216
            in_string = not char == quote
 
217
        else:
 
218
            if char == u'#':
 
219
                break
 
220
            typ = u'([{)]}'.find(char)
 
221
            if typ > -1:
 
222
                brackets[typ].append(pos)
 
223
            else:
 
224
                typ = quotes.find(char)
 
225
                if typ > -1:
 
226
                    in_string = True
 
227
                    quote = quotes[typ]
 
228
    res = -1
 
229
    for typ in range(3):
 
230
        opn, cls = brackets[typ], brackets[typ+3]
 
231
        nopn, ncls = len(opn), len(cls)
 
232
        if nopn > ncls:
 
233
            res = max(res, opn[nopn - ncls - 1])
 
234
    return res
 
235
 
177
236
#---- End Syntax Modules Internal Functions ----#