Skip to content

Commit

Permalink
Drop default values
Browse files Browse the repository at this point in the history
  • Loading branch information
macurovc committed Oct 6, 2021
1 parent 1d5e471 commit 07b8703
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions insert-docstring-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
'("first" "second" "third")))
(should (equal (insert-docstring--get-python-arguments-names-from-string
" first: Type\n,\n second:\n Map[some, other]") '("first" "second")))
(should (equal (insert-docstring--get-python-arguments-names-from-string
" first: Type = 1,\n second: Map[some, other] = 2") '("first" "second")))
(should (equal (insert-docstring--get-python-arguments-names-from-string
" first = 1,\n second = 2") '("first" "second")))
)
)

Expand Down
32 changes: 24 additions & 8 deletions insert-docstring.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"Tabulation width in Python files"
:group 'insert-docstring
)

(defcustom insert-docstring--default-python-indentation
(make-string insert-docstring--python-tab-width ? )
"Python indentation string"
Expand All @@ -41,6 +42,7 @@
"Regex to find the indentation of a function"
:group 'insert-docstring
)

(defcustom insert-docstring--python-function-name-regex
(rx "def" (+ (or blank "\n"))
(group (+ (not whitespace)))
Expand All @@ -54,12 +56,19 @@
"Regex to find the string of arguments of a function"
:group 'insert-docstring
)

(defcustom insert-docstring--python-function-end-regex
(rx ")" (* (not (any ":"))) ":")
"Regex to find the end of a function"
:group 'insert-docstring
)

(defcustom insert-docstring--blank-or-newline-regex
(rx (+ (or blank "\n")))
"Regex to find blanks and newlines (used for trimming)"
:group 'insert-docstring
)


(cl-defstruct insert-docstring--argument-data
"Data associated to a function argument."
Expand Down Expand Up @@ -116,16 +125,23 @@
"Parse the argument names contained in ARGUMENTS-STRING and
return them in a list."
(if (string-equal "" arguments-string) nil
(cl-remove-if
(mapcar
(lambda (string)
(string-match-p (rx (or "[" "]")) string)
"Remove default value if any and trim"
(car (split-string string "=" t insert-docstring--blank-or-newline-regex))
)
(mapcar (lambda (single-argument-string)
(car (split-string single-argument-string ":" t
(rx (+ (or blank "\n")))))
)
(split-string arguments-string ",")
)
(cl-remove-if
(lambda (string)
"Match type data leftovers"
(string-match-p (rx (or "[" "]")) string)
)
(mapcar (lambda (single-argument-string)
"Drop type data"
(car (split-string single-argument-string ":"))
)
(split-string arguments-string ",")
)
)
)
)
)
Expand Down

0 comments on commit 07b8703

Please sign in to comment.