From 47fa073feaa3326f1bdacda7d5fd45b2795afb51 Mon Sep 17 00:00:00 2001 From: Louis de Charsonville Date: Sun, 23 Apr 2023 19:13:31 +0200 Subject: [PATCH] split param-string by escaping brackets to allow for type containing brackets --- docstr-util.el | 34 ++++++++++++++++++++++++++++++++++ docstr-writers.el | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docstr-util.el b/docstr-util.el index 0fffd12..9e2046a 100644 --- a/docstr-util.el +++ b/docstr-util.el @@ -134,6 +134,40 @@ or `suffix'." (suffix (string-suffix-p regexp str ignore-case)) (t (string-match-p regexp str)))) + +(defun docstr--split-string-escape-brackets (string separator) + "This code splits a STRING into multiple parts, based on a given SEPARATOR. +It also accounts for inner brackets, such that strings within the same brackets +will be considered as one string. + +Example usage: + +(split-string-by-separator \"This is [a test] string\" \" \") + +=> (\"This\" \"is\" \"[a test]\" \"string\")" + (let ((str-list '()) + (start 0) + (end 0) + (in-brackets 0) + ) + (dotimes (i (length string)) + (let ((char (aref string i))) + (cond + ((equal (char-to-string char) separator) + (unless (> in-brackets 0) + (setq end i) + (push (substring string start end) str-list) + (setq start (+ i 1)))) + ((equal char ?\[) + (setq in-brackets (+ in-brackets 1)) + ) + ((equal char ?\]) + (setq in-brackets (- in-brackets 1)) + )))) + (push (substring string start) str-list) + (nreverse str-list))) + + ;; ;; (@* "Insertion" ) ;; diff --git a/docstr-writers.el b/docstr-writers.el index a1dab66..d4de863 100644 --- a/docstr-writers.el +++ b/docstr-writers.el @@ -31,6 +31,7 @@ (declare-function docstr-form-param "ext:docstr.el") (declare-function docstr-form-return "ext:docstr.el") +(declare-function docstr--split-string-escape-brackets "docstr-util") (defvar docstr-default-typename) (defvar docstr-desc-param) @@ -185,7 +186,7 @@ the last word only." (ignore-errors (docstr-writers--analyze-param-string search-string))) (when (stringp param-string) - (setq param-lst (split-string param-string ","))) + (setq param-lst (docstr--split-string-escape-brackets param-string ","))) (when (docstr-writers--param-empty-p param-lst) (setq param-lst '()))