emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/kotlin-mode 953bb91328 151/162: Merge pull request #50 fro


From: ELPA Syncer
Subject: [nongnu] elpa/kotlin-mode 953bb91328 151/162: Merge pull request #50 from taku0/fix-comment-at-bob
Date: Sat, 29 Jan 2022 08:25:31 -0500 (EST)

branch: elpa/kotlin-mode
commit 953bb913283af7227b20d2d5386392e6de46b215
Merge: 42fa1402a1 1f60b01659
Author: Gregg Hernandez <greggory.hz@gmail.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #50 from taku0/fix-comment-at-bob
    
    Fix indentation involving comment
---
 kotlin-mode.el           | 203 +++++++++++++++++++++++++++++++++--------------
 test/kotlin-mode-test.el |  64 ++++++++++++++-
 2 files changed, 208 insertions(+), 59 deletions(-)

diff --git a/kotlin-mode.el b/kotlin-mode.el
index a0ce272048..adf5cc5497 100644
--- a/kotlin-mode.el
+++ b/kotlin-mode.el
@@ -148,8 +148,9 @@
 
     ;; b-style comment
     (modify-syntax-entry ?/ ". 124b" st)
-    (modify-syntax-entry ?* ". 23" st)
+    (modify-syntax-entry ?* ". 23n" st)
     (modify-syntax-entry ?\n "> b" st)
+    (modify-syntax-entry ?\r "> b" st)
     st))
 
 
@@ -392,50 +393,133 @@
    determines the desired indentation level for the current line.")
 
 (defun kotlin-mode--count-to-line-start (counter)
-  "Count the brackets on the current line, starting from the cursor
-   position, and working backward, incrementing the count
-   +1 for open-brackets, -1 for close-brackets.
-   Return as soon as the overall count exceeds zero."
+  "Count the brackets on the current line, starting from the
+cursor position, and working backward, incrementing the count +1
+for open-brackets, -1 for close-brackets.
+
+Mark the COUNTER finished, set indentation, and return as soon as
+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
      ;; If the net-bracket-count is zero, use this indentation
      ((= (oref counter count) 0)
       (oset counter finished t)
       (if (oref counter use-base)
+          ;; Indenting a line that is neither close bracket nor the
+          ;; first element of a block or a list.  Found the previous
+          ;; line.  So align with the previous line, without effect of
+          ;; continued expression at the previous line.
           (kotlin-mode--add-indent counter (kotlin-mode--base-indentation))
+        ;; Indenting close bracket or the first element of a block or
+        ;; a list.  So align with this line, optionally with extra
+        ;; indentation.
         (kotlin-mode--add-indent counter (current-indentation))))
      ;; If we've now counted more open-brackets than close-brackets,
      ;; use the indentation of the content immediately following the
      ;; final open-bracket.
+     ;;
+     ;; Example:
+     ;;
+     ;; Suppose indenting "bar2()" in the following example:
+     ;;
+     ;; foo(  bar1(),
+     ;;       bar2())
+     ;;
+     ;; We are at just before the open bracket of "foo".  So skip the
+     ;; open bracket and spaces, then align "bar2()" with "bar1()".
      ((> (oref counter count) 0)
       (oset counter finished t)
       (forward-char)
       (skip-syntax-forward "(")
       (skip-syntax-forward "-")
-      (let (position)
-        (setq position (point))
-        (kotlin-mode--add-indent counter
-                                 (- position (re-search-backward "^"))))))))
+      (kotlin-mode--add-indent counter (current-column))))))
 
 (defun kotlin-mode--count-leading-close-brackets (counter)
-  "Count any close-bracket at the start of the current line."
-  (if (looking-at "\\s)")
-      (oset counter use-base nil))
-  (kotlin-mode--subtract-count counter (skip-syntax-forward ")")))
+  "Adjust COUNTER when indenting close brackets.
+
+This function should be called at the line being indented.
+
+Example:
+Suppose indenting the closing bracket of \"bar\" in the following example:
+
+fun foo() {
+    bar {
+      baz()
+    } // Indenting here
+}
+
+This function decrements the counter, so that
+`kotlin-mode--count-to-line-start' should not stop at the line
+\"baz()\", but goto the line \"bar {\", so that the close bracket
+aligned with \"bar {\"."
+
+  (save-excursion
+    (skip-syntax-forward "-")
+    (when (looking-at "\\s)")
+      (oset counter use-base nil)
+      (kotlin-mode--subtract-count counter (skip-syntax-forward ")")))))
 
 (defun kotlin-mode--count-trailing-open-brackets (counter)
-  "If the bracket count is at zero, and there are open-brackets at the end
-   of the line, do not count them, but add a single indentation level."
-  (if (= (oref counter count) 0)
-      (cond ((not (= (skip-syntax-backward "(") 0))
-             (kotlin-mode--add-indent counter kotlin-tab-width)
-             (oset counter use-base nil)))))
+  "Adjust COUNTER when indenting the first element of a block or list.
+
+This function should be called before calling
+`kotlin-mode--count-to-line-start', with the point at the end of
+the previous line of the line being indented.
+
+If the bracket count is at zero, and there are open-brackets at
+the end of the line, do not count them, but add a single
+indentation level. If bracket count is at zero, we are not
+indenting close brackets.
+
+Example:
+
+Suppose indenting \"baz()\" in the following example:
+
+fun foo() {
+    bar {
+        baz()
+    }
+}
+
+This function is called with the point at the end of the line
+\"bar {\". This function skips \"{\" backward and add indentation
+amount `kotlin-tab-width', say 4.  Then
+`kotlin-mode--count-to-line-start' seeks to the
+beginning-of-line.  So the final indentation is 8, that is the
+sum of indentation of bar and extra indentation.
+
+On the other hand, when indenting \"baz2()\" in the following
+line, keep cursor and indentation level as is because
+\"bar(baz1(),\" does not end with open brackets.  Then
+`kotlin-mode--count-to-line-start' stops at the close bracket of
+\"bar(\".  So \"baz2()\" is aligned with \"baz1()\".
+
+fun foo() {
+    bar(baz1(),
+        baz2())
+}"
+  (when (and (= (oref counter count) 0)
+             (not (= (skip-syntax-backward "(") 0)))
+    (kotlin-mode--add-indent counter kotlin-tab-width)
+    (oset counter use-base nil)))
 
 (defun kotlin-mode--add-count (counter val)
   (cl-incf (oref counter count) val))
@@ -454,20 +538,23 @@
    of the following format:
    /**
     * Description here
-    */ "
+    */"
   (save-excursion
     (let ((in-comment-block nil)
-          (keep-going (not (kotlin-mode--line-ends "\\*\\*/"))))
+          (keep-going (and
+                       (not (kotlin-mode--line-begins "\\*\\*+/"))
+                       (not (kotlin-mode--line-begins "/\\*"))
+                       (nth 4 (syntax-ppss)))))
       (while keep-going
         (kotlin-mode--prev-line)
         (cond
+         ((kotlin-mode--line-begins "/\\*")
+          (setq keep-going nil)
+          (setq in-comment-block t))
          ((bobp)
           (setq keep-going nil))
          ((kotlin-mode--line-contains "\\*/")
-          (setq keep-going nil))
-         ((kotlin-mode--line-begins "/\\*")
-          (setq keep-going nil)
-          (setq in-comment-block t))))
+          (setq keep-going nil))))
       in-comment-block)))
 
 (defun kotlin-mode--indent-line ()
@@ -476,33 +563,33 @@
   (beginning-of-line)
   (if (bobp)
       (kotlin-mode--beginning-of-buffer-indent)
-    (let ((cur-indent 0))
-      ;; Find bracket-based indentation first
-      (let ((bracket-counter (make-instance 'kotlin-mode--bracket-counter)))
-        (save-excursion
-          (skip-syntax-forward "-")
-          (kotlin-mode--count-leading-close-brackets bracket-counter))
-        (save-excursion
-          (progn (kotlin-mode--prev-line) (end-of-line))
-          (kotlin-mode--count-trailing-open-brackets bracket-counter)
-          (kotlin-mode--count-to-line-start bracket-counter)
-          (while (and (not (kotlin-mode--finished bracket-counter))
-                      (not (bobp)))
-            (progn (kotlin-mode--prev-line) (end-of-line))
-            (kotlin-mode--count-to-line-start bracket-counter))
-          (cl-incf cur-indent (oref bracket-counter indent))))
-
-      (cond ((kotlin-mode--line-continuation)
-             ;; Add extra indentation if the line continues the previous one
-             (cl-incf cur-indent kotlin-tab-width))
-            ((kotlin-mode--in-comment-block)
-             ;; Add one space of extra indentation if inside a comment block
-             (cl-incf cur-indent)))
-
-      (if cur-indent
-          (indent-line-to cur-indent)
-        (indent-line-to 0)))))
-
+    (let* ((bracket-counter (make-instance 'kotlin-mode--bracket-counter))
+           ;; Find bracket-based indentation first
+           (cur-indent
+            (progn
+              (kotlin-mode--count-leading-close-brackets bracket-counter)
+              (save-excursion
+                (kotlin-mode--prev-line)
+                (end-of-line)
+                (kotlin-mode--count-trailing-open-brackets bracket-counter)
+                (kotlin-mode--count-to-line-start bracket-counter)
+                (while (and (not (kotlin-mode--finished bracket-counter))
+                            (not (bobp)))
+                  (kotlin-mode--prev-line)
+                  (end-of-line)
+                  (kotlin-mode--count-to-line-start bracket-counter))
+                (oref bracket-counter indent)))))
+
+      (cond
+       ((kotlin-mode--line-continuation)
+        ;; Add extra indentation if the line continues the previous one
+        (cl-incf cur-indent kotlin-tab-width))
+
+       ((kotlin-mode--in-comment-block)
+        ;; Add one space of extra indentation if inside a comment block
+        (cl-incf cur-indent)))
+
+      (indent-line-to cur-indent))))
 
 (defun kotlin-mode--beginning-of-buffer-indent ()
   (indent-line-to 0))
diff --git a/test/kotlin-mode-test.el b/test/kotlin-mode-test.el
index bb2af332e5..fe96b6218a 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,69 @@ 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))))))
+
+(ert-deftest kotlin-mode--indent-comment-at-bob--test ()
+  (with-temp-buffer
+    (let ((text "/*
+ *
+ *
+ */"))
+      (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))))))
+
 (defun next-non-empty-line ()
   "Moves to the next non-empty line"
   (forward-line)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]