Skip to content

Commit

Permalink
Add support for using single space between sentences
Browse files Browse the repository at this point in the history
python-docstring.el will respect the value of sentence-end-double-space.
  • Loading branch information
selmanj committed Feb 15, 2017
1 parent a07bad8 commit a4397b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
18 changes: 12 additions & 6 deletions docstring_wrap.py
Expand Up @@ -195,7 +195,7 @@ def add(self, line):
return self


def wrap(self, output, indentation, width, initialBlank):
def wrap(self, output, indentation, width, initialBlank, singleSpace):
maxWidthThisLine = width
if not self.words:
return
Expand All @@ -213,7 +213,7 @@ def wrap(self, output, indentation, width, initialBlank):
normalPrevWord = self.pointTracker.peek(prevWord)
if num == 1 and startslist(normalPrevWord):
spaces = 1
elif isSentenceEnd(normalPrevWord):
elif isSentenceEnd(normalPrevWord) and singleSpace:
spaces = 2
else:
spaces = 1
Expand Down Expand Up @@ -389,7 +389,7 @@ def fixIndentation(self):
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.
self.fixIndentation()
for line in self.lines:
Expand Down Expand Up @@ -470,7 +470,8 @@ def scan(self, text, offset):


def wrapPythonDocstring(docstring, output, indentation=" ",
width=79, point=0, initialBlank=True):
width=79, point=0, initialBlank=True,
singleSpace=False):
"""
Wrap a given Python docstring.
Expand All @@ -494,6 +495,9 @@ def wrapPythonDocstring(docstring, output, indentation=" ",
return value of this function) to reposition the cursor at the relative
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.
"""
# TODO: multiple points; usable, for example, for start and end of a
Expand All @@ -510,7 +514,7 @@ def wrapPythonDocstring(docstring, output, indentation=" ",
if not paragraph.matchesTag(prevp):
output.write("\n")
prevp = paragraph
paragraph.wrap(output, indentation, width, initialBlank)
paragraph.wrap(output, indentation, width, initialBlank, singleSpace)
initialBlank = True
output.write(indentation)
return pt.outPoints[0]
Expand Down Expand Up @@ -548,7 +552,8 @@ def main(argv, indata):
parser.add_argument("--indent", type = int)
parser.add_argument("--width", type = int, default = 79)
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()
inlines = indata.split("\n")
Expand All @@ -569,6 +574,7 @@ def main(argv, indata):
width=width,
point=point,
initialBlank=initialBlank,
singleSpace=namespace.single_space
)
prefix = StringIO()
if namespace.offset is not None:
Expand Down
4 changes: 3 additions & 1 deletion python-docstring.el
Expand Up @@ -75,7 +75,9 @@
(shell-command-on-region
string-start string-end
(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)
orig-offset
indent-count
Expand Down
12 changes: 12 additions & 0 deletions test_docstring_wrap.py
Expand Up @@ -78,6 +78,18 @@ def test_single_line_too_wide(self):
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__':
unittest.main()

0 comments on commit a4397b0

Please sign in to comment.