Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
Enhancements to docstring formatting when filling paragraphs. * progm…
Browse files Browse the repository at this point in the history
…odes/python.el (python-fill-docstring-style): Rename from python-fill-string-style. Added new style. (python-fill-string): Use new style. Better checks for docstrings.
  • Loading branch information
fgallina authored and Fabián Ezequiel Gallina committed Oct 14, 2012
1 parent 6be48b5 commit 341b2f5
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions python.el
Expand Up @@ -2290,15 +2290,15 @@ fill parens."
:type 'symbol
:group 'python)

(defcustom python-fill-string-style 'pep-257
(defcustom python-fill-docstring-style 'pep-257
"Style used to fill docstrings.
This affects `python-fill-string' behavior with regards to
triple quotes positioning.
Possible values are DJANGO, PEP-257, PEP-257-NN, SYMMETRIC and
NIL. A value of NIL won't care about quotes position, will do
what `fill-paragraph' does, any other value may result in one of
the following docstring styles:
Possible values are DJANGO, ONETWO, PEP-257, PEP-257-NN,
SYMMETRIC, and NIL. A value of NIL won't care about quotes
position and will treat docstrings a normal string, any other
value may result in one of the following docstring styles:
DJANGO:
Expand All @@ -2312,6 +2312,17 @@ DJANGO:
If processing fails throw ProcessingError.
\"\"\"
ONETWO:
\"\"\"Process foo, return bar.\"\"\"
\"\"\"
Process foo, return bar.
If processing fails throw ProcessingError.
\"\"\"
PEP-257:
\"\"\"Process foo, return bar.\"\"\"
Expand Down Expand Up @@ -2340,9 +2351,16 @@ SYMMETRIC:
If processing fails throw ProcessingError.
\"\"\""
:type 'symbol
:type '(choice
(const :tag "Don't format docstrings" nil)
(const :tag "Django's coding standards style." django)
(const :tag "One newline and start and Two at end style." onetwo)
(const :tag "PEP-257 with 2 newlines at end of string." pep-257)
(const :tag "PEP-257 with 1 newline at end of string." pep-257-nn)
(const :tag "Symmetric style." symmetric))
:group 'python
:safe (lambda (val) (memq val '(django pep-257 pep-257-nn symmetric nil))))
:safe (lambda (val)
(memq val '(django onetwo pep-257 pep-257-nn symmetric nil))))

(defun python-fill-paragraph-function (&optional justify)
"`fill-paragraph-function' handling multi-line strings and possibly comments.
Expand Down Expand Up @@ -2403,28 +2421,28 @@ JUSTIFY should be used (if applicable) as in `fill-paragraph'."
;; Docstring styles may vary for oneliners and multi-liners.
(> (count-matches "\n" str-start-pos str-end-pos) 0))
(delimiters-style
(case python-fill-string-style
(case python-fill-docstring-style
;; delimiters-style is a cons cell with the form
;; (START-NEWLINES . END-NEWLINES). When any of the sexps
;; is NIL means to not add any newlines for start or end
;; of docstring. See `python-fill-string-style' for a
;; of docstring. See `python-fill-docstring-style' for a
;; graphic idea of each style.
(django (cons 1 1))
(onetwo (and multi-line-p (cons 1 2)))
(pep-257 (and multi-line-p (cons nil 2)))
(pep-257-nn (and multi-line-p (cons nil 1)))
(django (cons 1 1))
(symmetric (and multi-line-p (cons 1 1)))))
(docstring-p (save-excursion
;; Consider docstrings those strings which
;; start on a line by themselves.
(goto-char str-start-pos)
(skip-chars-backward (rx whitespace))
(= (point) (line-beginning-position))))
(python-nav-beginning-of-statement)
(and (= (point) str-start-pos))))
(fill-paragraph-function))
(save-restriction
(narrow-to-region str-start-pos str-end-pos)
(fill-paragraph justify))
(save-excursion
(when (and docstring-p python-fill-string-style)
(when (and docstring-p python-fill-docstring-style)
;; Add the number of newlines indicated by the selected style
;; at the start of the docstring.
(goto-char (+ str-start-pos num-quotes))
Expand Down

0 comments on commit 341b2f5

Please sign in to comment.