From 6c0455b5e2e105a2ac405549883bf979c3487b90 Mon Sep 17 00:00:00 2001 From: Carlo Sciolla Date: Thu, 16 Aug 2018 11:32:44 +0200 Subject: [PATCH] Make indentation work --- plantuml-mode.el | 41 +++++++++++++++---------- test/plantuml-indentation-test.el | 50 +++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 21 deletions(-) diff --git a/plantuml-mode.el b/plantuml-mode.el index a1c5cf8..edc94f6 100644 --- a/plantuml-mode.el +++ b/plantuml-mode.el @@ -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 diff --git a/test/plantuml-indentation-test.el b/test/plantuml-indentation-test.el index 541dfd9..c8bc723 100644 --- a/test/plantuml-indentation-test.el +++ b/test/plantuml-indentation-test.el @@ -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 }")