From e536800327dd280515ff9fef305c51cd6ff073f9 Mon Sep 17 00:00:00 2001 From: moculus Date: Wed, 22 Aug 2007 16:32:32 +0000 Subject: [PATCH] teh awesome --HG-- branch : pmacs2 --- code_examples/Reporting2.pm | 9 +++ mode/perl.py | 157 ++++++++++++++++++------------------ 2 files changed, 89 insertions(+), 77 deletions(-) diff --git a/code_examples/Reporting2.pm b/code_examples/Reporting2.pm index 836b7a4..fcce789 100644 --- a/code_examples/Reporting2.pm +++ b/code_examples/Reporting2.pm @@ -13,6 +13,15 @@ drop table foog; select cast(plunk as timestamp) from blarg join plarg using(id_what) where x = 3; EOT +# gwiejgwe gwe gwe gwejig weig weig wegji weg weig wegi wegjiwe gjweig weig +# wejig wejgi wejgiwe jgiwe gjiwej gwei gweig jweig jweig wig wejgiewj ge giwej +# gweijg weigj weigjwe giwej gwe +# 1. gewj gweig weigweigewiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiii iiiigewigweigwi +# iweigiwigigewigewgweigi +# 2. gweii XXXXX a e gwejgiwe jiaw jhw +# gwe gjiwegij wegiwe jgiwe giwej gweigj wiegjwei gjweig weigj weig jwegi +# wejgiwe jgiweg jweigewgj we gwee e e e ee e + my $foo = { #@@:string:mode.sql.Sql 'drop table ', diff --git a/mode/perl.py b/mode/perl.py index 8895b33..2bd26c8 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -529,95 +529,98 @@ class PerlHashCleanup(Method): class PerlWrapLine(Method): '''Wrap Comments and POD''' - margin = 80 - comment_re = re.compile('(#+)( *)(.*)') + # enumerations for line types + LT_COMMENT = 1 + LT_POD = 2 + + margin = 80 + comment_re = re.compile('( *)(#+)( *)(.*)') + def _is_newline(self, t): return t.name == 'eol' def _is_space(self, t): return t.name == 'null' and regex.space.match(t.string) def _detect_line_type(self, w, y): - c = w.logical_cursor() - highlighter = w.buffer.highlights[w.mode.name()] + h = w.buffer.highlights[w.mode.name()] ltype = None - for t in highlighter.tokens[c.y]: - if self._is_space(t): + for t in h.tokens[y]: + fqname = t.fqname() + if t.name == 'null' or t.name == 'eol': pass - elif t.name == 'comment': - if ltype: - return None - else: - ltype = 'comment' - elif t.name == 'eol': - return ltype + elif fqname.startswith('comment'): + if ltype and ltype != 'comment': + ltype = None + break + ltype = self.LT_COMMENT + elif fqname.startswith('pod'): + if ltype and ltype != 'pod': + ltype = None + break + ltype = self.LT_POD else: - return None + ltype = None + break + return ltype + + def _fix_comments(self, c, w): + h = w.buffer.highlights[w.mode.name()] + y1 = c.y + y2 = c.y + while y2 < len(w.buffer.lines) - 1: + if self._detect_line_type(w, y2 + 1): + y2 += 1 + else: + break + + lines = w.buffer.lines[y1:y2 + 1] + m = self.comment_re.match(lines[0]) + assert m + prepend = m.group(1) + m.group(2) + rmargin = self.margin - len(prepend) + dpad = m.group(3) + + segments = [] + for line in lines: + m = self.comment_re.match(line) + assert m + pad, data = m.group(3), m.group(4) + if segments and pad == dpad and segments[-1][0] == dpad and segments[-1][1]: + data = segments.pop(-1)[1] + ' ' + data + i = 0 + while len(pad) + len(data[i:]) > rmargin: + while data[i] == ' ': + i += 1 + j = rmargin - len(pad) + while j >= 0 and data[i + j] != ' ': + j -= 1 + if j < 0: + j = rmargin - len(pad) + segments.append([pad, data[i:i + j]]) + i += j + if data: + while data[i] == ' ': + i += 1 + segments.append([pad, data[i:]]) + else: + segments.append(['', '']) + + lines2 = [prepend + x[0] + x[1] for x in segments] + p1 = Point(0, y1) + p2 = Point(len(w.buffer.lines[y2]), y2) + w.buffer.delete(p1, p2) + w.buffer.insert_lines(p1, lines2) + w.set_error("wrapped comment lines %d-%d" % (y1 + 1, y2 + 1)) + + def _fix_pod(self, c, w): + w.set_error("pod wrapping not yet supported") def _execute(self, w, **vargs): c = w.logical_cursor() ltype = self._detect_line_type(w, c.y) - if ltype == 'comment': - return self._fix_comments(c, w) - elif ltype == 'pod': - return self._fix_pod(c, w) + if ltype == self.LT_COMMENT: + self._fix_comments(c, w) + elif ltype == self.LT_POD: + self._fix_pod(c, w) else: w.set_error("did not detect comment or pod lines") - return - def _fix_comments(self, c, w): - w.set_error("comment!") - def _fix_pod(self, c, w): - pass -#class PerlWrapLine(Method): -# '''Wrap lines, comments, POD''' -# margin = 80 -# comment_re = re.compile('^( *)(#+)( *)([^ ].*)$') -# def _execute(self, w, **vargs): -# pcursor = w.physical_cursor() -# r = w.get_region(pcursor) -# if r is None: -# return -# -# t = r[4] -# if t == 'pod': -# assert False, 'POD: %s' % repr(r) -# elif t == 'comment': -# self._wrap_comment(w) -# else: -# return -# -# def _wrap_comment(self, w): -# l = w.logical_cursor() -# m = self.comment_re.match(w.buffer.lines[l.y]) -# if not m: -# assert False, 'no match oh geez' -# -# pad = m.group(1) + m.group(2) + m.group(3) -# data = m.group(4) + ' ' -# -# start = l.y -# end = l.y + 1 -# -# while end < len(w.buffer.lines): -# m = self.comment_re.match(w.buffer.lines[end]) -# if m: -# data += m.group(4) + ' ' -# end += 1 -# else: -# break -# -# words = [word for word in data.split() if word] -# -# lines = [pad] -# for word in words: -# if len(lines[-1]) == len(pad): -# lines[-1] += word -# elif len(lines[-1]) + 1 + len(word) <= self.margin: -# lines[-1] += ' ' + word -# else: -# lines.append(pad + word) -# -# # remove the old text and add the new -# start_p = Point(0, start) -# end_p = Point(len(w.buffer.lines[end-1]), end-1) -# w.kill(start_p, end_p) -# w.insert(start_p, '\n'.join(lines))