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

Customize if then else template #99

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
25 changes: 25 additions & 0 deletions elisp/shm-customizations.el
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,31 @@ when option is nil.
:group 'shm
:type 'list)

(defcustom shm-indent-if-offset 3
"Amount of indentation for 'true' and 'false' branches of the if expression.

E.g. if this is equal to `0' then 'if' snippet will be expanded as

if <condition>
then <true branch>
else <false branch>

If this is equal to `2' then 'if' snippet will be expanded as

if <condition>
then <true branch>
else <false branch>

If this is equal to `4' then 'if' snippet will be expanded as

if <condition>
then <true branch>
else <false branch>

etc."
:group 'shm
:type 'integer)


;; Provide

Expand Down
23 changes: 14 additions & 9 deletions elisp/shm-slot.el
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,26 @@ case {undefined} of
"Insert template

if {undefined}
then {undefined}
else {undefined}
<offset>then {undefined}
<offset>else {undefined}

or

if {undefined} then {undefined} else {undefined}

if inside parentheses."
(let ((start (save-excursion (forward-char -1)
(search-backward-regexp "[^a-zA-Z0-9_]")
(forward-char 1)
(point)))
(template (if (bound-and-true-p structured-haskell-repl-mode)
"if undefined then undefined else undefined"
"if undefined\n then undefined\n else undefined")))
(let* ((start (save-excursion (forward-char -1)
(search-backward-regexp "[^a-zA-Z0-9_]")
(forward-char 1)
(point)))
(offset (make-string
(if (bound-and-true-p structured-haskell-repl-mode)
3
(make-string shm-indent-if-offset ?\s))
?\s))
(template (format "if undefined\n%sthen undefined\n%selse undefined"
offset
offset)))
(shm-adjust-dependents (point) (- start (point)))
(delete-region start (point))
(shm-adjust-dependents (point) (length (car (last (split-string template "\n")))))
Expand Down
34 changes: 32 additions & 2 deletions elisp/shm-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ foo = do

(defvar shm-tests
(list
(list :name "inserting after '"
(list :name "inserting after '"
:start-buffer-content "x = [foo'
,()]
"
Expand Down Expand Up @@ -616,7 +616,37 @@ fn x = y + y
"
:kbd [?\M-x ?s ?h ?m ?/ ?g ?o ?t ?o ?- ?w ?h ?e ?r ?e return ?y]
:customizations
'((shm-indent-point-after-adding-where-clause t)))))
'((shm-indent-point-after-adding-where-clause . t)))
(list :name "if-then-else template width indent 0"
:start-buffer-content "fn :: a -> b
fn x = if
"
:start-cursor 23
:finish-cursor 24
:current-node-overlay 'nil
:end-buffer-content "fn :: a -> b
fn x = if undefined
then undefined
else undefined
"
:kbd " "
:customizations
'((shm-indent-if-offset . 0)))
(list :name "if-then-else template with indent 3"
:start-buffer-content "fn :: a -> b
fn x = if
"
:start-cursor 23
:finish-cursor 24
:current-node-overlay 'nil
:end-buffer-content "fn :: a -> b
fn x = if undefined
then undefined
else undefined
"
:kbd " "
:customizations
'((shm-indent-if-offset . 3)))))

(provide 'shm-tests)

Expand Down