Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split param-string by escaping brackets to allow for type containing brackets #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docstr-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
;;
Expand Down
3 changes: 2 additions & 1 deletion docstr-writers.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 '()))

Expand Down