Make indentation work

This commit is contained in:
Carlo Sciolla 2018-08-16 11:32:44 +02:00
parent 5c8a95850b
commit 6c0455b5e2
No known key found for this signature in database
GPG Key ID: BA5D71E6F3C580C1
2 changed files with 70 additions and 21 deletions

View File

@ -396,32 +396,41 @@ Uses prefix (as PREFIX) to choose where to display it:
;; indentation
(defun plantuml-current-block-indentation ()
(defun plantuml-current-block-depth ()
"Trace the current block indentation level by recursively looking back line by line."
(save-excursion
(let ((relative-depth 0))
(while (>= relative-depth 0)
(forward-line -1)
(beginning-of-line)
(forward-line -1)
(setq bob-visited? nil)
(while (and (>= relative-depth 0)
(not bob-visited?))
(if (bobp)
(setq relative-depth -2)) ;end fast
(setq bob-visited? t))
(if (looking-at plantuml-indent-regexp-end)
(setq relative-depth (1+ relative-depth)))
(setq relative-depth (1- relative-depth)))
(if (looking-at plantuml-indent-regexp-start)
(setq relative-depth (1- relative-depth))))
(setq relative-depth (1+ relative-depth)))
(forward-line -1))
(if (= -2 relative-depth)
(if (<= relative-depth 0)
0
(+ tab-width (current-indentation))))))
relative-depth))))
(defun plantuml-indent-line ()
"Indent the current line to its desired indentation level."
(interactive)
(save-excursion
(beginning-of-line)
(if (bobp)
(indent-line-to 0)
(let ((offset (plantuml-current-block-indentation)))
(when (looking-at plantuml-indent-regexp-end)
(setq offset (max (- offset tab-width) 0)))
(indent-line-to offset)))))
(let ((original-indentation (current-indentation)))
(save-excursion
(beginning-of-line)
(if (bobp)
(indent-line-to 0)
(let ((depth (plantuml-current-block-depth)))
(when (looking-at plantuml-indent-regexp-end)
(setq depth (max (1- depth) 0)))
(indent-line-to (* tab-width depth)))))
(forward-char (- (current-indentation)
original-indentation))))
;;;###autoload

View File

@ -10,6 +10,44 @@
;;; Code:
(defun add-text-and-position-cursor (txt)
"Add TXT into the buffer, move cursor to the position of the marker | and delete the marker."
(insert txt)
(goto-char (point-min))
(search-forward "|")
(delete-char -1))
(defun assert-block-depth (expected txt)
"Assert the EXPECTED indentation level for the given TXT."
(with-temp-buffer
(add-text-and-position-cursor txt)
(let ((actual (plantuml-current-block-depth)))
(should (equal expected actual)))))
(ert-deftest test-plantuml-current-block-depth ()
(setq-local plantuml-jar-path plantuml-test-jar-path)
(plantuml-init-once)
(assert-block-depth 0 "
A |-> B")
(assert-block-depth 0 "
pac|kage Foo {
A -> B
}")
(assert-block-depth 1 "
package APackage {
|A -> B
}")
(assert-block-depth 1 "
alt choice 1
|A -> B
end
"))
;; This is taken from https://github.com/clojure-emacs/clojure-mode/blob/master/test/clojure-mode-indentation-test.el
(defmacro check-indentation (description before after &optional var-bindings)
"Declare an ert test for indentation behaviour.
@ -36,11 +74,12 @@ values of customisable variables."
(setq-local plantuml-jar-path plantuml-test-jar-path)
(plantuml-init-once)
(insert ,before)
(goto-char (point-min))
(search-forward "|")
(delete-char -1)
(add-text-and-position-cursor ,before)
(plantuml-mode)
;; use 2 spaces instead of one tab for indentation
(setq-local indent-tabs-mode nil)
(setq-local tab-width 2)
(indent-according-to-mode)
(should (equal expected-state (buffer-string)))
@ -53,7 +92,8 @@ values of customisable variables."
(check-indentation package-block
"package APackage {
|A -> B
}" "package APackage {
}"
"package APackage {
|A -> B
}")