1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
# Programmer: Daniel Pozmanter
# E-mail: drpython@bluebottle.com
# Note: You must reply to the verification e-mail to get through.
#
# Copyright 2003-2005 Daniel Pozmanter
#
# Distributed under the terms of the GPL (GNU Public License)
#
# DrPython is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#Functions for getting information about blocks
import re
reNotEmpty = re.compile('\S')
def CheckIfBlockStart(lines, targetline):
LineIndent = GetLineIndentation(lines, targetline)
if LineIndent == -1:
LineIndent = FindValidLine(lines, targetline)
if LineIndent == -1:
return False
#try till the end
l = len(lines)
li = 1
result = -1
while result == -1:
if targetline + li >= l:
break
result = GetLineIndentation(lines, targetline+li)
li += 1
if result > LineIndent:
return li - 1
return False
def FindValidLine(lines, targetline):
#try till the end
l = len(lines)
li = 1
result = -1
while result == -1:
if targetline + li >= l:
break
result = GetLineIndentation(lines, targetline+li)
if result == -1:
if targetline - li <= 0:
break
result = GetLineIndentation(lines, targetline-li)
li += 1
return result
def RestructureExplicitLineJoining(lines):
editedlines = [lines[0]]
x = 1
l = len(lines)
while x < l:
lline = len(lines[x-1])
if lline > 0:
if lines[x-1][lline - 1] == '\\':
editedlines.append(' ')
else:
editedlines.append(lines[x])
else:
editedlines.append(lines[x])
x += 1
return editedlines
def RestructureImplicitLineJoining(lines):
editedlines = []
x = 0
l = len(lines)
while x < l:
p1 = _get_paren_spread(lines[x], '[', ']')
p2 = _get_paren_spread(lines[x], '(', ')')
p3 = _get_paren_spread(lines[x], '{', '}')
end = 0
if p1:
end = _find_paren_end_line(lines, x+1, p1, '[', ']')
if end == -1:
editedlines.extend(lines[x:])
break
if p2:
tend = _find_paren_end_line(lines, x+1, p2, '(', ')')
if tend == -1:
editedlines.extend(lines[x:])
break
if tend > end:
end = tend
if p3:
tend = _find_paren_end_line(lines, x+1, p3, '{', '}')
if tend == -1:
editedlines.extend(lines[x:])
break
if tend > end:
end = tend
editedlines.append(lines[x])
if end > 0:
newlines, x = _make_lines_whitespace(lines, x+1, end)
editedlines.extend(newlines)
else:
x += 1
return editedlines
def RemoveComments(lines):
lines = removeStringQuotedWith(lines, "'''")
lines = removeStringQuotedWith(lines, '"""')
lines = removeStringQuotedWith(lines, "'")
lines = removeStringQuotedWith(lines, '"')
editedlines = []
for line in lines:
c = line.find('#')
if c > -1:
editedlines.append(line[:c] + ' ')
else:
editedlines.append(line)
return editedlines
def removeQuoteFromSingleLine(line, start, target):
ft2 = line[start:].find(target)
s = reNotEmpty.search(line).start()
#Only remove the string if it is not the first bit of the line.
if (ft2 > -1) and (start > s):
ft2 += start
return line[:start] + ' ' + line[:ft2]
return None
def removeStringQuotedWith(lines, target):
editedlines = []
x = 0
l = len(lines)
while x < l:
ft = lines[x].find(target)
if ft > -1:
sameline = removeQuoteFromSingleLine(lines[x], ft, target)
if sameline is not None:
editedlines.append(sameline)
else:
editedlines.append(lines[x][:ft])
start = x
x += 1
end = -1
while (x < l):
ft = lines[x].find(target)
if ft > -1:
end = x
break
x += 1
if end == -1:
return editedlines
else:
newlines, x = _make_lines_whitespace(lines, start, end)
editedlines.extend(newlines)
else:
editedlines.append(lines[x])
x += 1
return editedlines
def spacefill(length):
result = ''
for x in range(length):
result += ' '
return result
def _get_paren_spread(text, openp, closep):
opencount = text.count(openp)
if opencount > 0:
closecount = text.count(closep)
if opencount > closecount:
return opencount - closecount
return 0
def _find_paren_end_line(lines, pos, spread, openp, closep):
x = pos
l = len(lines)
count = 0
while x < l:
count += lines[x].count(closep)
spread += lines[x].count(openp)
if count >= spread:
return x
x += 1
return -1
def _make_lines_whitespace(lines, startline, endline):
newlines = []
x = startline
while x <= endline:
newlines.append(reNotEmpty.sub(' ', lines[x]))
x += 1
return newlines, x
def GetLines(Document):
text = Document.GetText()
eol = Document.GetEndOfLineCharacter()
lines = text.split(eol)
lines = RestructureExplicitLineJoining(lines)
lines = RemoveComments(lines)
lines = RestructureImplicitLineJoining(lines)
return lines
def GetBlockStart(lines, targetline):
LineIndent = GetLineIndentation(lines, targetline)
if LineIndent == -1:
LineIndent = FindValidLine(lines, targetline)
if LineIndent == -1:
return -1
x = targetline - 1
while x > 0:
i = GetLineIndentation(lines, x)
if i != -1:
if i < LineIndent:
return x
x -= 1
return 0
def GetBlockEnd(lines, targetline):
l = len(lines)
LineIndent = GetLineIndentation(lines, targetline)
if LineIndent == -1:
LineIndent = FindValidLine(lines, targetline)
if LineIndent == -1:
return -1
x = targetline + 1
last = targetline
while x < l:
i = GetLineIndentation(lines, x)
if i != -1:
if i >= LineIndent:
last = x
if i < LineIndent:
return last
x += 1
if last != x:
return last
return len(lines) - 1
def GetKeyWordStart(lines, targetline, kword, kwordlength):
start = GetBlockStart(lines, targetline)
if kwordlength == 0:
return start
while lines[start].strip()[:kwordlength] != kword:
start = GetBlockStart(lines, start)
if start == 0:
break
return start
def GetKeyWordEnd(lines, targetline, kword, kwordlength):
if (kwordlength == 0) or (lines[targetline].strip()[:kwordlength] == kword):
startofblock = CheckIfBlockStart(lines, targetline)
if startofblock:
targetline += startofblock
else:
targetline = GetKeyWordStart(lines, targetline, kword, kwordlength) + 1
end = GetBlockEnd(lines, targetline)
return end
def GetLineIndentation(lines, current):
result = reNotEmpty.search(lines[current])
if result is None:
return -1
return result.start()
def GoToBlockEnd(Document, kword=''):
lines = GetLines(Document)
kwordl = len(kword)
l = GetKeyWordEnd(lines, Document.GetCurrentLine(), kword, kwordl)
i = GetLineIndentation(lines, l)
if i < 0:
i = 0
Document.EnsureVisible(l)
Document.GotoLine(l)
p = Document.PositionFromLine(l) + i
Document.GotoPos(p)
Document.SetSTCFocus(True)
def GoToBlockStart(Document, kword=''):
lines = GetLines(Document)
kwordl = len(kword)
l = GetKeyWordStart(lines, Document.GetCurrentLine(), kword, kwordl)
i = GetLineIndentation(lines, l)
if i < 0:
i = 0
Document.EnsureVisible(l)
Document.GotoLine(l)
p = Document.PositionFromLine(l) + i
Document.GotoPos(p)
Document.SetSTCFocus(True)
|