branch: elpa/kotlin-mode
commit b86f9b4607ddb0f03a473bf43ef5d85c3e1121e6
Author: taku0 <[email protected]>
Commit: taku0 <[email protected]>
Fix indentation after comment containing close brackets
Example:
```kotlin
fun foo {
bar()
// }
bar() // indenting here
}
```
---
kotlin-mode.el | 19 +++++++++++++------
test/kotlin-mode-test.el | 35 ++++++++++++++++++++++++++++++++++-
2 files changed, 47 insertions(+), 7 deletions(-)
diff --git a/kotlin-mode.el b/kotlin-mode.el
index 81d5ada482..0c31e91dbd 100644
--- a/kotlin-mode.el
+++ b/kotlin-mode.el
@@ -401,13 +401,20 @@ the overall count exceeds zero. If the counter is zero
at the
beginning of the line, Mark the counter finished and set
indentation. If we hit a beginning of line but the counter is
negative, just return without marking finished."
+ (when (nth 4 (syntax-ppss))
+ ;; If the point is inside a comment, goto the beginning of the comment.
+ (goto-char (nth 8 (syntax-ppss))))
(save-excursion
- (while (and (<= (oref counter count) 0) (not (bolp)))
- (backward-char)
- (cond ((looking-at "\\s(")
- (cl-incf (oref counter count)))
- ((looking-at "\\s)")
- (cl-decf (oref counter count)))))
+ (let ((line-beginning-position (line-beginning-position)))
+ (while (and (<= (oref counter count) 0) (not (bolp)))
+ (forward-comment (- (point)))
+ (backward-char)
+ (when (< (point) line-beginning-position)
+ (goto-char line-beginning-position))
+ (cond ((eq (char-syntax (char-after)) ?\()
+ (cl-incf (oref counter count)))
+ ((eq (char-syntax (char-after)) ?\))
+ (cl-decf (oref counter count))))))
;; We are at the beginning of the line, or just before an
;; unmatching open bracket.
(cond
diff --git a/test/kotlin-mode-test.el b/test/kotlin-mode-test.el
index bb2af332e5..bbb39e504d 100644
--- a/test/kotlin-mode-test.el
+++ b/test/kotlin-mode-test.el
@@ -16,7 +16,6 @@ import bar.Bar as bBar
(setq-local kotlin-tab-width 4)
(kotlin-mode--indent-line)
-
(should (equal text (buffer-string)))
(forward-line)
@@ -84,6 +83,40 @@ return a + b
.map { it.toUpperCase() }
.forEach { print(it) }")))))
+(ert-deftest kotlin-mode--ignore-comment-test ()
+ (with-temp-buffer
+ (let ((text "fun foo {
+ bar()
+ // }
+ bar()
+}"))
+ (pop-to-buffer (current-buffer))
+ (insert text)
+ (goto-char (point-min))
+ (kotlin-mode)
+ (setq-local indent-tabs-mode nil)
+ (setq-local tab-width 4)
+ (setq-local kotlin-tab-width 4)
+
+ (kotlin-mode--indent-line)
+ (should (equal text (buffer-string)))
+
+ (forward-line)
+ (kotlin-mode--indent-line)
+ (should (equal text (buffer-string)))
+
+ (forward-line)
+ (kotlin-mode--indent-line)
+ (should (equal text (buffer-string)))
+
+ (forward-line)
+ (kotlin-mode--indent-line)
+ (should (equal text (buffer-string)))
+
+ (forward-line)
+ (kotlin-mode--indent-line)
+ (should (equal text (buffer-string))))))
+
(defun next-non-empty-line ()
"Moves to the next non-empty line"
(forward-line)