Say I have PHP array like this:
$arr = ['one', 'two', 'three']; and I want to convert it to
$arr = [ 'one', 'two', 'three' ];
Is there any ready to use soluions?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
Don't know about PHP. You can try to search for PHP code formatters and see if it has such function.
Here is a simple solution using regular expression. It works for your particular example at least.
(defun your-php-array-multiline (b e) "Turn oneline php array to multiline." (interactive "*r") (insert (replace-regexp-in-string (rx (in "[,]")) (lambda (s) (pcase (match-string 0 s) ("[" "[\n") ("," ",\n") ("]" "\n]"))) (delete-and-extract-region b e))) (indent-region b (point)))
In case you want to convert the multiline array back:
(defun your-php-array-oneline (b e) "Turn multiline php array to oneline." (interactive "*r") (insert (replace-regexp-in-string "\n\s*" "" (delete-and-extract-region b e))))
and toggle between oneline and multiline
(defun your-php-array-toggle (b e) "Toggle php array between oneline and multiline." (interactive "*r") (if (string-match-p "\n" (buffer-substring b e)) (your-php-array-oneline b e) (your-php-array-multiline b e)))