[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/kotlin-mode 9fa0d75997 119/162: Tidy up bracked-based inde
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/kotlin-mode 9fa0d75997 119/162: Tidy up bracked-based indentation into a class, and fix bugs |
Date: |
Sat, 29 Jan 2022 08:25:29 -0500 (EST) |
branch: elpa/kotlin-mode
commit 9fa0d7599702d30b8cff0d918e419dc93abe841d
Author: Martin Blake <martinblake@github.com>
Commit: Martin Blake <martinblake@github.com>
Tidy up bracked-based indentation into a class, and fix bugs
---
kotlin-mode.el | 186 +++++++++++++++++++++++++++++++++------------------------
test/sample.kt | 52 ++++++++++------
2 files changed, 143 insertions(+), 95 deletions(-)
diff --git a/kotlin-mode.el b/kotlin-mode.el
index 4edb79ca81..71b3724cc2 100644
--- a/kotlin-mode.el
+++ b/kotlin-mode.el
@@ -325,61 +325,109 @@
(defun kotlin-mode--line-begins (pattern)
"Return whether the current line begins with the given pattern"
- (looking-at (format "^[ \t]*%s" pattern)))
+ (save-excursion
+ (beginning-of-line)
+ (looking-at (format "^[ \t]*%s" pattern))))
(defun kotlin-mode--line-ends (pattern)
"Return whether the current line ends with the given pattern"
- (looking-at (format ".*%s[ \t]*$" pattern)))
+ (save-excursion
+ (beginning-of-line)
+ (looking-at (format ".*%s[ \t]*$" pattern))))
(defun kotlin-mode--line-contains (pattern)
"Return whether the current line contains the given pattern"
- (looking-at (format ".*%s.*" pattern)))
-
-(defun kotlin-mode--update-bracket-count (net-count)
- "Count the brackets on the current line, incrementing the count
- +1 for open-brackets, -1 for close-brackets.
- Return as soon as the overall count exceeds zero."
(save-excursion
- (end-of-line)
- (while (and (<= net-count 0) (not (bolp)))
- (backward-char)
- (cond ((looking-at "\\s\(")
- (incf net-count))
- ((looking-at "\\s\)")
- (decf net-count)))))
- net-count)
+ (beginning-of-line)
+ (looking-at (format ".*%s.*" pattern))))
+
+(defun kotlin-mode--line-continuation()
+ "Return whether this line continues a statement in the previous line"
+ (kotlin-mode--line-begins "[\.=:]"))
(defun kotlin-mode--base-indentation ()
"Return the indentation level of the current line based on brackets only,
i.e. ignoring 'continuation' indentation."
- (cond ((kotlin-mode--line-begins "\\.")
+ (cond ((kotlin-mode--line-continuation)
(- (current-indentation) kotlin-tab-width))
((kotlin-mode--in-comment-block)
(- (current-indentation) 1))
(t
(current-indentation))))
-(defun kotlin-mode--post-bracket-indent ()
- "Return the indentation at the first non-whitespace character
- following the last un-closed open-bracket on the line.
- If no non-whitespace character follows, return the default
- indentation level."
- (let ((net-count 0) position)
- (save-excursion
- (end-of-line)
- (while (and (<= net-count 0) (not (bolp)))
- (backward-char)
- (cond ((looking-at "\\s\(")
- (incf net-count))
- ((looking-at "\\s\)")
- (decf net-count))))
- (if (kotlin-mode--line-ends "\\s\(")
- (+ (kotlin-mode--base-indentation) kotlin-tab-width)
- (forward-char)
- (skip-syntax-forward "(")
- (skip-syntax-forward "-")
+(defclass kotlin-mode--bracket-counter ()
+ ((count :initarg :count
+ :initform 0
+ :type integer
+ :documentation "The net bracket count (+1 for open, -1 for close).")
+ (indent :initarg :indent
+ :initform 0
+ :type integer
+ :documentation "The indentation based on bracket layout.")
+ (finished :initarg :finished
+ :initform nil
+ :type boolean
+ :documentation "Whether the counting has finished."))
+ "A class for counting brackets to find the appropriate bracket-based indent.
+ The logic here involves keeping track of the net-bracket-count,
+ defined as the number of open-brackets minus the number of close-brackets.
+ We scroll backwards until the net-bracket-count is zero, and this point
+ determines the desired indentation level for the current line.")
+
+(cl-defmethod count-to-line-start ((counter kotlin-mode--bracket-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."
+ (save-excursion
+ (while (and (<= (oref counter count) 0) (not (bolp)))
+ (backward-char)
+ (cond ((looking-at "\\s\(")
+ (oset counter count (+ (oref counter count) 1)))
+ ((looking-at "\\s\)")
+ (oset counter count (- (oref counter count) 1)))))
+ (cond
+ ;; If the net-bracket-count is zero, use this indentation
+ ((= (oref counter count) 0)
+ (oset counter finished t)
+ (add-indent counter (kotlin-mode--base-indentation)))
+ ;; If we've now counted more open-brackets than close-brackets,
+ ;; use the indentation of the content immediately following the
+ ;; final open-bracket.
+ ((> (oref counter count) 0)
+ (oset counter finished t)
+ (forward-char)
+ (skip-syntax-forward "(")
+ (skip-syntax-forward "-")
+ (let (position)
(setq position (point))
- (- position (re-search-backward "^"))))))
+ (add-indent counter (- position (re-search-backward "^"))))))))
+
+(cl-defmethod count-leading-close-brackets
+ ((counter kotlin-mode--bracket-counter))
+ "Count any close-bracket at the start of the current line."
+ (subtract-count counter (skip-syntax-forward ")")))
+
+(cl-defmethod count-trailing-open-brackets
+ ((counter kotlin-mode--bracket-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)
+ (if (not (= (skip-syntax-backward "(") 0))
+ (add-indent counter kotlin-tab-width))))
+
+(cl-defmethod add-count ((counter kotlin-mode--bracket-counter) val)
+ (oset counter count (+ (oref counter count) val)))
+
+(cl-defmethod subtract-count ((counter kotlin-mode--bracket-counter) val)
+ (oset counter count (- (oref counter count) val)))
+
+(cl-defmethod add-indent ((counter kotlin-mode--bracket-counter) val)
+ (oset counter indent (+ (oref counter indent) val)))
+
+(cl-defmethod finished ((counter kotlin-mode--bracket-counter))
+ (oref counter finished))
+
(defun kotlin-mode--in-comment-block ()
"Return whether the cursor is within a standard comment block structure
@@ -389,7 +437,7 @@
*/ "
(save-excursion
(let ((in-comment-block nil)
- (keep-going (kotlin-mode--line-begins "\\*")))
+ (keep-going (not (kotlin-mode--line-ends "\\*\\*/"))))
(while keep-going
(kotlin-mode--prev-line)
(cond
@@ -399,52 +447,34 @@
(setq keep-going nil))
((kotlin-mode--line-begins "/\\*")
(setq keep-going nil)
- (setq in-comment-block t))
- ((not (kotlin-mode--line-begins "\\*"))
- (setq keep-going nil))))
+ (setq in-comment-block t))))
in-comment-block)))
(defun kotlin-mode--indent-line ()
- "Indent current line as kotlin code.
- The logic here involves keeping track of the net-bracket-count,
- defined as the number of open-brackets minus the number of close-brackets.
- We scroll backwards until the net-bracket-count is zero, and this point
- determines the desired indentation level for the current line."
+ "Indent current line as kotlin code."
(interactive)
(beginning-of-line)
(if (bobp)
- (progn
- (kotlin-mode--beginning-of-buffer-indent))
- (let ((not-indented t) cur-indent (net-bracket-count 0))
- ;; Count any close-bracket at the start of the current line
- (save-excursion
- (skip-syntax-forward "-")
- (decf net-bracket-count (skip-syntax-forward ")")))
- (save-excursion
- (while not-indented
- ;; Count-up the brackets in the previous line
- (kotlin-mode--prev-line)
- (setq net-bracket-count
- (kotlin-mode--update-bracket-count net-bracket-count))
-
- (cond
- ;; If the net-bracket-count is zero, use this indentation
- ((= net-bracket-count 0)
- (setq cur-indent (kotlin-mode--base-indentation))
- (setq not-indented nil))
- ;; If we've now counted more open-brackets than close-brackets,
- ;; use the indentation of the content immediately following the
- ;; final open-bracket.
- ;; But if the line *ends* with an open-bracket, apply the default
- ;; indentation.
- ((> net-bracket-count 0)
- (setq cur-indent (kotlin-mode--post-bracket-indent))
- (setq not-indented nil))
-
- ((bobp)
- (setq not-indented nil)))))
- (cond ((kotlin-mode--line-begins "\\.")
- ;; Add extra indentation if the line starts with a period
+ (kotlin-mode--beginning-of-buffer-indent)
+ (kotlin-mode--reset-indentation)
+ (let ((cur-indent 0))
+ ;; Find bracket-based indentation first
+ (let ((bracket-counter (kotlin-mode--bracket-counter)))
+ (save-excursion
+ (skip-syntax-forward "-")
+ (count-leading-close-brackets bracket-counter))
+ (save-excursion
+ (progn (kotlin-mode--prev-line) (end-of-line))
+ (count-trailing-open-brackets bracket-counter)
+ (count-to-line-start bracket-counter)
+ (while (and (not (finished bracket-counter)) (not (bobp)))
+ (progn (kotlin-mode--prev-line) (end-of-line))
+ (count-to-line-start bracket-counter)
+ )
+ (incf cur-indent (oref bracket-counter indent))))
+
+ (cond ((kotlin-mode--line-continuation)
+ ;; Add extra indentation if the line continues the previous one
(incf cur-indent kotlin-tab-width))
((kotlin-mode--in-comment-block)
;; Add one space of extra indentation if inside a comment block
diff --git a/test/sample.kt b/test/sample.kt
index e6e2802852..1aeafae5de 100644
--- a/test/sample.kt
+++ b/test/sample.kt
@@ -10,6 +10,11 @@ import bar.Bar as bBar
* a multiline comment
*/
+/****************************************************************
+ Multiline comment
+ without leading "*"
+****************************************************************/
+
fun sum(a: Int, b: Int): Int {
return a + b
}
@@ -21,7 +26,16 @@ fun printSum(a: Int, b: Int): Unit {
}
fun printSum(a: Int, b: Int) {
- print(a + b)
+ val veryLongResultVariableName: Int
+ = (a + b)
+ print(veryLongResultVariableName)
+}
+
+fun functionMultiLineArgs(first: Int,
+ second: Int,
+ third: Int,
+ fourth: Int) {
+ print("(${first}, ${second}, ${third}, ${fourth})")
}
val a: Int = 1
@@ -288,7 +302,9 @@ class Derived() : Base() {
override fun v() {}
}
-open class AnotherDerived() : Base() {
+open class AnotherDerived()
+ : Base() {
+
final override fun v() {}
}
@@ -564,14 +580,15 @@ enum class ProtocolState {
abstract fun signal(): ProtocolState
}
-window.addMouseListener(object : MouseAdapter() {
- override fun mouseClicked(e: MouseEvent) {
- // ...
- }
+window.addMouseListener(
+ object : MouseAdapter() {
+ override fun mouseClicked(e: MouseEvent) {
+ // ...
+ }
- override fun mouseEntered(e: MouseEvent) {
- // ...
- }
+ override fun mouseEntered(e: MouseEvent) {
+ // ...
+ }
})
val adHoc = object {
@@ -584,14 +601,15 @@ fun countClicks(window: JComponent) {
var clickCount = 0
var enterCount = 0
- window.addMouseListener(object : MouseAdapter() {
- override fun mouseClicked(e: MouseEvent) {
- clickCount++
- }
+ window.addMouseListener(
+ object : MouseAdapter() {
+ override fun mouseClicked(e: MouseEvent) {
+ clickCount++
+ }
- override fun mouseEntered(e: MouseEvent) {
- enterCount++
- }
+ override fun mouseEntered(e: MouseEvent) {
+ enterCount++
+ }
})
// ...
}
@@ -635,7 +653,7 @@ fun <T> asList(vararg ts: T): List<T> {
}
tailrec fun findFixPoint(x: Double = 1.0): Double
-= if (x == Math.cos(x)) x else findFixPoint(Math.cos(x))
+ = if (x == Math.cos(x)) x else findFixPoint(Math.cos(x))
fun <T> lock(lock: Lock, body: () -> T): T {
lock.lock()
- [nongnu] elpa/kotlin-mode 953bb91328 151/162: Merge pull request #50 from taku0/fix-comment-at-bob, (continued)
- [nongnu] elpa/kotlin-mode 953bb91328 151/162: Merge pull request #50 from taku0/fix-comment-at-bob, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode ab61099682 152/162: use 4 for defualt tab width, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode e5c6d845e6 093/162: Merge pull request #19 from gregghz/fixes-closing-bracket-indentation, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 72a2bf2347 096/162: Add a file to get Travis-CI to run the test suite., ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode b91bab57b6 099/162: Adding REPL support, credit to https://github.com/sarvex/kotlin-mode, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 1ed0c5589e 098/162: Merge pull request #22 from russel/master, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 8574583b30 105/162: Fix highlighting escaped identifiers in interpolations, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode eb3704e0e2 106/162: Set adaptive-fill-regexp to comment-start-skip, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 77d709ab54 107/162: Merge pull request #30 from CyberShadow/pull-20180624-142858, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode f08724b978 115/162: Improve handling of bracket-based indentation, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 9fa0d75997 119/162: Tidy up bracked-based indentation into a class, and fix bugs,
ELPA Syncer <=
- [nongnu] elpa/kotlin-mode 76921927de 117/162: Add a test for correct indentation of sample.kt, and get it to pass (with a few tweaks), ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 511e0a4ee3 121/162: Add in more kotlin keywords and indent get/set methods for properties, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 21ae667561 126/162: Add highlighted keywords, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode b2d5ff65fc 125/162: Highlight single quotes and back quotes, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode dd24461721 128/162: Add prefix to class methods, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 6aa6d56c0a 131/162: Merge pull request #42 from taku0/add_keywords, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 2f8a57ba64 138/162: Comment out failing emacs versions and link associated bugs, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 3062e14ac0 140/162: Merge pull request #47 from Emacs-Kotlin-Mode-Maintainers/emacs-version-test, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode d8d6461395 145/162: Fix warnings in tests, ELPA Syncer, 2022/01/29
- [nongnu] elpa/kotlin-mode 1f60b01659 148/162: Fix indentation of multiline comment at the beginning of buffer, ELPA Syncer, 2022/01/29