From 07b87037233a13a1e93ce8a3697629a689002fee Mon Sep 17 00:00:00 2001 From: Marco Vocialta Date: Wed, 6 Oct 2021 15:58:16 +0200 Subject: [PATCH] Drop default values --- insert-docstring-tests.el | 4 ++++ insert-docstring.el | 32 ++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/insert-docstring-tests.el b/insert-docstring-tests.el index cda0a68..4c14df9 100644 --- a/insert-docstring-tests.el +++ b/insert-docstring-tests.el @@ -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"))) ) ) diff --git a/insert-docstring.el b/insert-docstring.el index 7c4972a..e18b0e9 100644 --- a/insert-docstring.el +++ b/insert-docstring.el @@ -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" @@ -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))) @@ -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." @@ -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 ",") + ) + ) ) ) )