mirror of
https://github.com/vale981/python-docstring-mode
synced 2025-03-05 09:31:43 -05:00
Add support for using single space between sentences
python-docstring.el will respect the value of sentence-end-double-space.
This commit is contained in:
parent
a07bad8498
commit
a4397b0148
3 changed files with 27 additions and 7 deletions
|
@ -195,7 +195,7 @@ class RegularParagraph(object):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
def wrap(self, output, indentation, width, initialBlank):
|
def wrap(self, output, indentation, width, initialBlank, singleSpace):
|
||||||
maxWidthThisLine = width
|
maxWidthThisLine = width
|
||||||
if not self.words:
|
if not self.words:
|
||||||
return
|
return
|
||||||
|
@ -213,7 +213,7 @@ class RegularParagraph(object):
|
||||||
normalPrevWord = self.pointTracker.peek(prevWord)
|
normalPrevWord = self.pointTracker.peek(prevWord)
|
||||||
if num == 1 and startslist(normalPrevWord):
|
if num == 1 and startslist(normalPrevWord):
|
||||||
spaces = 1
|
spaces = 1
|
||||||
elif isSentenceEnd(normalPrevWord):
|
elif isSentenceEnd(normalPrevWord) and singleSpace:
|
||||||
spaces = 2
|
spaces = 2
|
||||||
else:
|
else:
|
||||||
spaces = 1
|
spaces = 1
|
||||||
|
@ -389,7 +389,7 @@ class PreFormattedParagraph(object):
|
||||||
self.lines = newLines
|
self.lines = newLines
|
||||||
|
|
||||||
|
|
||||||
def wrap(self, output, indentation, width, initialBlank):
|
def wrap(self, output, indentation, width, initialBlank, singleSpace):
|
||||||
# OK, now we know about all the lines we're going to know about.
|
# OK, now we know about all the lines we're going to know about.
|
||||||
self.fixIndentation()
|
self.fixIndentation()
|
||||||
for line in self.lines:
|
for line in self.lines:
|
||||||
|
@ -470,7 +470,8 @@ class PointTracker(object):
|
||||||
|
|
||||||
|
|
||||||
def wrapPythonDocstring(docstring, output, indentation=" ",
|
def wrapPythonDocstring(docstring, output, indentation=" ",
|
||||||
width=79, point=0, initialBlank=True):
|
width=79, point=0, initialBlank=True,
|
||||||
|
singleSpace=False):
|
||||||
"""
|
"""
|
||||||
Wrap a given Python docstring.
|
Wrap a given Python docstring.
|
||||||
|
|
||||||
|
@ -494,6 +495,9 @@ def wrapPythonDocstring(docstring, output, indentation=" ",
|
||||||
return value of this function) to reposition the cursor at the relative
|
return value of this function) to reposition the cursor at the relative
|
||||||
position which the user will expect.
|
position which the user will expect.
|
||||||
|
|
||||||
|
@param singleSpace: If true, use a single space between sentences instead
|
||||||
|
of two.
|
||||||
|
|
||||||
@return: The new location of the cursor.
|
@return: The new location of the cursor.
|
||||||
"""
|
"""
|
||||||
# TODO: multiple points; usable, for example, for start and end of a
|
# TODO: multiple points; usable, for example, for start and end of a
|
||||||
|
@ -510,7 +514,7 @@ def wrapPythonDocstring(docstring, output, indentation=" ",
|
||||||
if not paragraph.matchesTag(prevp):
|
if not paragraph.matchesTag(prevp):
|
||||||
output.write("\n")
|
output.write("\n")
|
||||||
prevp = paragraph
|
prevp = paragraph
|
||||||
paragraph.wrap(output, indentation, width, initialBlank)
|
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]
|
||||||
|
@ -548,7 +552,8 @@ def main(argv, indata):
|
||||||
parser.add_argument("--indent", type = int)
|
parser.add_argument("--indent", type = int)
|
||||||
parser.add_argument("--width", type = int, default = 79)
|
parser.add_argument("--width", type = int, default = 79)
|
||||||
parser.add_argument("--linewise", action='store_true')
|
parser.add_argument("--linewise", action='store_true')
|
||||||
namespace = parser.parse_args()
|
parser.add_argument("--single-space", action='store_false')
|
||||||
|
namespace = parser.parse_args(argv[1:])
|
||||||
|
|
||||||
io = StringIO()
|
io = StringIO()
|
||||||
inlines = indata.split("\n")
|
inlines = indata.split("\n")
|
||||||
|
@ -569,6 +574,7 @@ def main(argv, indata):
|
||||||
width=width,
|
width=width,
|
||||||
point=point,
|
point=point,
|
||||||
initialBlank=initialBlank,
|
initialBlank=initialBlank,
|
||||||
|
singleSpace=namespace.single_space
|
||||||
)
|
)
|
||||||
prefix = StringIO()
|
prefix = StringIO()
|
||||||
if namespace.offset is not None:
|
if namespace.offset is not None:
|
||||||
|
|
|
@ -75,7 +75,9 @@
|
||||||
(shell-command-on-region
|
(shell-command-on-region
|
||||||
string-start string-end
|
string-start string-end
|
||||||
(format
|
(format
|
||||||
"python2 %s --offset %s --indent %s --width %s"
|
(concat "python2 %s --offset %s --indent %s --width %s"
|
||||||
|
(unless sentence-end-double-space
|
||||||
|
" --single-space"))
|
||||||
(shell-quote-argument python-docstring-script)
|
(shell-quote-argument python-docstring-script)
|
||||||
orig-offset
|
orig-offset
|
||||||
indent-count
|
indent-count
|
||||||
|
|
|
@ -78,6 +78,18 @@ class FunctionalTests(unittest.TestCase):
|
||||||
main(["test"], ds)
|
main(["test"], ds)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_single_space_used_when_specified(self):
|
||||||
|
"""
|
||||||
|
When called with --single-space, only a single space is inserted at
|
||||||
|
the end of sentences.
|
||||||
|
"""
|
||||||
|
ds = """
|
||||||
|
Sentence number one. Sentence number two.
|
||||||
|
"""
|
||||||
|
self.assertEqual(
|
||||||
|
ds,
|
||||||
|
main(["test", "--single-space"], ds))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue