[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/kotlin-mode 5898586ac4 146/162: Refactor indentation logic
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/kotlin-mode 5898586ac4 146/162: Refactor indentation logic |
Date: |
Sat, 29 Jan 2022 08:25:31 -0500 (EST) |
branch: elpa/kotlin-mode
commit 5898586ac4f159128d3c1d7f788012062b378c84
Author: taku0 <mxxouy6x3m_github@tatapa.org>
Commit: taku0 <mxxouy6x3m_github@tatapa.org>
Refactor indentation logic
Add comments.
Clean Lisp code.
---
kotlin-mode.el | 166 +++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 121 insertions(+), 45 deletions(-)
diff --git a/kotlin-mode.el b/kotlin-mode.el
index a0ce272048..81d5ada482 100644
--- a/kotlin-mode.el
+++ b/kotlin-mode.el
@@ -392,10 +392,15 @@
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."
(save-excursion
(while (and (<= (oref counter count) 0) (not (bolp)))
(backward-char)
@@ -403,39 +408,110 @@
(cl-incf (oref counter count)))
((looking-at "\\s)")
(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))
@@ -476,33 +552,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))
- [nongnu] elpa/kotlin-mode b689626987 070/162: handle } else { lines for indentation, (continued)
- [nongnu] elpa/kotlin-mode b689626987 070/162: handle } else { lines for indentation, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 37b93116c9 085/162: A couple of corrections to the README., ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode f42a0745dd 089/162: Ignores blank lines when finding indent level, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 6a579f6549 092/162: Require cc-cmds to load the definition of c-indent-line-or-region, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode b2085f5315 100/162: Put key-binding to README, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 666187a086 110/162: Merge pull request #33 from mikegolod/issue-32, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 3b9b51801a 111/162: auto-enable kt mode for kotlin scripts (e.g. for build.gradle.kts), ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode c7f53d4137 124/162: Save point and mark when sending a block to the interpreter, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 002b5dfc21 136/162: Use an older baseline emacs version in travis, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 0786e24d70 142/162: Merge pull request #44 from taku0/propertise-multiline-strings, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 5898586ac4 146/162: Refactor indentation logic,
ELPA Syncer <=
- [nongnu] elpa/kotlin-mode 193fcc6e16 003/162: Add kotlin-mode.el, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode ec34e8627d 002/162: Cask: Refactor - Sort depends-on, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 59e8dc97dd 004/162: kotlin-mode: Define kotlin-mode, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 2c9598c4e6 005/162: kotlin-mode: Define a hook after entering kotlin-mode, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode b23bbf758e 010/162: kotlin-mode: Use kotlin-mode-syntax-table, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 81a49e95e6 011/162: kotlin-mode: Add kotlin-mode-font-keywords, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode ea74bb577a 016/162: kotlin-mode: Define function declaration keywords, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 20dca19dba 009/162: kotlin-mode: syntax-table - Define strings, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 0c7ca08d99 018/162: kotlin-mode: Define statement keywords, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode d801f5c5cf 020/162: kotlin-mode: Define keywords, ELPA Syncer, 2022/01/29