mirror of
https://github.com/vale981/python-docstring-mode
synced 2025-03-04 17:11:41 -05:00
python 3 upgrade
This commit is contained in:
parent
d35d2e0fbe
commit
0d2f783f5a
2 changed files with 87 additions and 23 deletions
|
@ -105,13 +105,17 @@ class RegularParagraph(object):
|
||||||
otherIndent = ""
|
otherIndent = ""
|
||||||
|
|
||||||
def __init__(self, pointTracker, fixedIndent="", hangIndent="",
|
def __init__(self, pointTracker, fixedIndent="", hangIndent="",
|
||||||
followIndent=""):
|
followIndent="", originalIndent=0):
|
||||||
self.words = []
|
self.words = []
|
||||||
self.fixedIndent = fixedIndent
|
self.fixedIndent = fixedIndent
|
||||||
self.hangIndent = hangIndent
|
self.hangIndent = hangIndent
|
||||||
self.followIndent = followIndent
|
self.followIndent = followIndent
|
||||||
self.more = None
|
self.more = None
|
||||||
|
self.prev = None
|
||||||
self.pointTracker = pointTracker
|
self.pointTracker = pointTracker
|
||||||
|
# originalIndent is the width of the indentation of the line this
|
||||||
|
# paragraph originally came from in the input text.
|
||||||
|
self.originalIndent = originalIndent
|
||||||
self._unwrappedLines = 0
|
self._unwrappedLines = 0
|
||||||
self._headingType = None
|
self._headingType = None
|
||||||
self._headingPoints = []
|
self._headingPoints = []
|
||||||
|
@ -140,25 +144,54 @@ class RegularParagraph(object):
|
||||||
def isHeading(self):
|
def isHeading(self):
|
||||||
return bool(self._headingType)
|
return bool(self._headingType)
|
||||||
|
|
||||||
|
def connect(self, more):
|
||||||
|
self.more = more
|
||||||
|
more.prev = self
|
||||||
|
return more
|
||||||
|
|
||||||
|
def islist(self):
|
||||||
|
return self.words and startslist(self.words[0])
|
||||||
|
|
||||||
|
def previousListPeer(self):
|
||||||
|
"""
|
||||||
|
Find a previous paragraph that is also a list element, of the same
|
||||||
|
indentation level if one exists.
|
||||||
|
"""
|
||||||
|
previous = self.prev
|
||||||
|
matched = None
|
||||||
|
while previous:
|
||||||
|
if not previous.words:
|
||||||
|
previous = previous.prev
|
||||||
|
continue
|
||||||
|
if not previous.islist():
|
||||||
|
break
|
||||||
|
if previous.originalIndent <= self.originalIndent:
|
||||||
|
return previous
|
||||||
|
if previous.originalIndent > self.originalIndent:
|
||||||
|
matched = previous
|
||||||
|
previous = previous.prev
|
||||||
|
if matched:
|
||||||
|
return matched
|
||||||
|
|
||||||
def add(self, line):
|
def add(self, line):
|
||||||
clean = self.pointTracker.peek(line)
|
clean = self.pointTracker.peek(line)
|
||||||
stripped = clean.strip()
|
stripped = clean.strip()
|
||||||
|
thisLineIndent = len(clean) - len(clean.lstrip())
|
||||||
|
|
||||||
if stripped:
|
if stripped:
|
||||||
self._unwrappedLines += 1
|
self._unwrappedLines += 1
|
||||||
active = self
|
active = self
|
||||||
firstword = list(self.pointTracker.filterWords(line.split()))[0]
|
firstword = list(self.pointTracker.filterWords(line.split()))[0]
|
||||||
if beginsField(stripped):
|
if beginsField(stripped):
|
||||||
fp = FieldParagraph(pointTracker=self.pointTracker)
|
fp = FieldParagraph(pointTracker=self.pointTracker, originalIndent=thisLineIndent)
|
||||||
fp.words.extend(line.split())
|
fp.words.extend(line.split())
|
||||||
active = self.more = fp
|
active = active.connect(fp)
|
||||||
elif isUnderline(stripped) and self._unwrappedLines == 2:
|
elif isUnderline(stripped) and self._unwrappedLines == 2:
|
||||||
# This paragraph is actually a section heading.
|
# This paragraph is actually a section heading.
|
||||||
active.setIsHeading(stripped[0])
|
active.setIsHeading(stripped[0])
|
||||||
self._headingPoints = self.pointTracker.extractPoints(line)
|
self._headingPoints = self.pointTracker.extractPoints(line)
|
||||||
# FIXME: should respect leading indentation.
|
# FIXME: should respect leading indentation.
|
||||||
active = self.nextRegular()
|
active = active.connect(self.genRegular(originalIndent=thisLineIndent))
|
||||||
elif startslist(firstword):
|
elif startslist(firstword):
|
||||||
# Aesthetically I prefer a 2-space indent here, but the
|
# Aesthetically I prefer a 2-space indent here, but the
|
||||||
# convention in the codebase seems to be 4 spaces.
|
# convention in the codebase seems to be 4 spaces.
|
||||||
|
@ -174,24 +207,31 @@ class RegularParagraph(object):
|
||||||
fixedIndent=fi,
|
fixedIndent=fi,
|
||||||
hangIndent=" " * hangIndent,
|
hangIndent=" " * hangIndent,
|
||||||
followIndent=self.followIndent,
|
followIndent=self.followIndent,
|
||||||
|
originalIndent=thisLineIndent,
|
||||||
)
|
)
|
||||||
fp.words.extend(line.split())
|
fp.words.extend(line.split())
|
||||||
active = self.more = fp
|
fp.prev = self
|
||||||
|
peer = fp.previousListPeer()
|
||||||
|
if peer:
|
||||||
|
if peer.originalIndent >= fp.originalIndent:
|
||||||
|
fp.fixedIndent = peer.fixedIndent
|
||||||
|
else:
|
||||||
|
fp.fixedIndent = peer.fixedIndent + (" " * LIST_INDENT)
|
||||||
|
active = active.connect(fp)
|
||||||
else:
|
else:
|
||||||
self.words.extend(line.split())
|
self.words.extend(line.split())
|
||||||
if stripped.endswith("::"):
|
if stripped.endswith("::"):
|
||||||
active.more = PreFormattedParagraph(
|
active = active.connect(PreFormattedParagraph(
|
||||||
active,
|
active,
|
||||||
indentBegins=len(clean) - len(clean.lstrip())
|
indentBegins=thisLineIndent
|
||||||
)
|
))
|
||||||
active = active.more
|
|
||||||
return active
|
return active
|
||||||
else:
|
else:
|
||||||
rawstrip = line.strip()
|
rawstrip = line.strip()
|
||||||
if rawstrip:
|
if rawstrip:
|
||||||
self.words.append(rawstrip)
|
self.words.append(rawstrip)
|
||||||
if len(list(self.pointTracker.filterWords(self.words))):
|
if len(list(self.pointTracker.filterWords(self.words))):
|
||||||
return self.nextRegular()
|
return self.connect(self.genRegular(originalIndent=thisLineIndent))
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
@ -253,15 +293,11 @@ class RegularParagraph(object):
|
||||||
self.otherIndent)
|
self.otherIndent)
|
||||||
|
|
||||||
|
|
||||||
def genRegular(self):
|
def genRegular(self, originalIndent=0):
|
||||||
return RegularParagraph(pointTracker=self.pointTracker,
|
return RegularParagraph(pointTracker=self.pointTracker,
|
||||||
fixedIndent=self.nextIndent(),
|
fixedIndent=self.nextIndent(),
|
||||||
followIndent=self.nextIndent())
|
followIndent=self.nextIndent(),
|
||||||
|
originalIndent=originalIndent)
|
||||||
|
|
||||||
def nextRegular(self):
|
|
||||||
self.more = self.genRegular()
|
|
||||||
return self.more
|
|
||||||
|
|
||||||
|
|
||||||
def nextIndent(self):
|
def nextIndent(self):
|
||||||
|
@ -345,9 +381,37 @@ class PreFormattedParagraph(object):
|
||||||
self.indentBegins = indentBegins
|
self.indentBegins = indentBegins
|
||||||
self.fixedIndent = fixedIndent
|
self.fixedIndent = fixedIndent
|
||||||
self.more = None
|
self.more = None
|
||||||
|
self.prev = None
|
||||||
self.pointTracker = pointTracker
|
self.pointTracker = pointTracker
|
||||||
|
|
||||||
|
|
||||||
|
def islist(self):
|
||||||
|
"""
|
||||||
|
It's not a list.
|
||||||
|
"""
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def connect(self, more):
|
||||||
|
self.more = more
|
||||||
|
more.prev = self
|
||||||
|
return more
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def originalIndent(self):
|
||||||
|
return self.indentBegins
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def words(self):
|
||||||
|
"""
|
||||||
|
Used by wrapper below to see if there are any words in a given
|
||||||
|
paragraph and whether it should be skipped.
|
||||||
|
"""
|
||||||
|
return bool(self.lines)
|
||||||
|
|
||||||
|
|
||||||
def matchesTag(self, other):
|
def matchesTag(self, other):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -357,7 +421,7 @@ class PreFormattedParagraph(object):
|
||||||
|
|
||||||
if actualLine.strip():
|
if actualLine.strip():
|
||||||
if len(actualLine) - len(actualLine.lstrip()) <= self.indentBegins:
|
if len(actualLine) - len(actualLine.lstrip()) <= self.indentBegins:
|
||||||
next = self.more = self.before.genRegular()
|
next = self.connect(self.before.genRegular())
|
||||||
return next.add(line)
|
return next.add(line)
|
||||||
self.lines.append(line.rstrip())
|
self.lines.append(line.rstrip())
|
||||||
else:
|
else:
|
||||||
|
@ -372,9 +436,9 @@ class PreFormattedParagraph(object):
|
||||||
self.lines.pop()
|
self.lines.pop()
|
||||||
if not self.lines:
|
if not self.lines:
|
||||||
return
|
return
|
||||||
cleanLines = map(self.pointTracker.peek, self.lines)
|
cleanLines = list(map(self.pointTracker.peek, self.lines))
|
||||||
commonLeadingIndent = min([len(x) - len(x.lstrip()) for x in cleanLines
|
commonLeadingIndent = min([len(x) - len(x.lstrip()) for x in cleanLines
|
||||||
if x.strip()])
|
if x.strip()] or [0])
|
||||||
newLines = []
|
newLines = []
|
||||||
for actualLine, line in zip(cleanLines, self.lines):
|
for actualLine, line in zip(cleanLines, self.lines):
|
||||||
if actualLine != line and line[:commonLeadingIndent].strip():
|
if actualLine != line and line[:commonLeadingIndent].strip():
|
||||||
|
@ -511,13 +575,13 @@ def wrapPythonDocstring(docstring, output, indentation=" ",
|
||||||
# output.write("{}".format(initialBlank))
|
# output.write("{}".format(initialBlank))
|
||||||
for paragraph in start.all():
|
for paragraph in start.all():
|
||||||
if initialBlank:
|
if initialBlank:
|
||||||
if not paragraph.matchesTag(prevp):
|
if paragraph.words and not paragraph.matchesTag(prevp):
|
||||||
output.write("\n")
|
output.write("\n")
|
||||||
prevp = paragraph
|
prevp = paragraph
|
||||||
paragraph.wrap(output, indentation, width, initialBlank, singleSpace)
|
paragraph.wrap(output, indentation, width, initialBlank, singleSpace)
|
||||||
initialBlank = True
|
initialBlank = True
|
||||||
output.write(indentation)
|
output.write(indentation)
|
||||||
return pt.outPoints[0]
|
return pt.outPoints[0] if pt.outPoints else 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ single space is used."
|
||||||
(shell-command-on-region
|
(shell-command-on-region
|
||||||
string-start string-end
|
string-start string-end
|
||||||
(format
|
(format
|
||||||
(concat "python2 %s --offset %s --indent %s --width %s"
|
(concat "python3 %s --offset %s --indent %s --width %s"
|
||||||
(unless python-docstring-sentence-end-double-space
|
(unless python-docstring-sentence-end-double-space
|
||||||
" --single-space"))
|
" --single-space"))
|
||||||
(shell-quote-argument python-docstring-script)
|
(shell-quote-argument python-docstring-script)
|
||||||
|
|
Loading…
Add table
Reference in a new issue