[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/swift-mode a042764 374/496: Fix indentation of comments
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/swift-mode a042764 374/496: Fix indentation of comments |
Date: |
Sun, 29 Aug 2021 11:34:10 -0400 (EDT) |
branch: elpa/swift-mode
commit a04276484e1d3b74f4d66f997646e5bc420d02c3
Author: taku0 <mxxouy6x3m_github@tatapa.org>
Commit: taku0 <mxxouy6x3m_github@tatapa.org>
Fix indentation of comments
---
swift-mode-indent.el | 149 +++++++++++++++++++++++++++++------------
test/swift-files/comment.swift | 2 +-
2 files changed, 106 insertions(+), 45 deletions(-)
diff --git a/swift-mode-indent.el b/swift-mode-indent.el
index 9a8194e..7d196cf 100644
--- a/swift-mode-indent.el
+++ b/swift-mode-indent.el
@@ -172,6 +172,9 @@ declaration and its offset is `swift-mode:basic-offset'."
((eq (nth 3 parser-state) t)
(swift-mode:calculate-indent-of-multiline-string))
+ ((looking-at "//")
+ (swift-mode:calculate-indent-of-single-line-comment))
+
(t
(swift-mode:calculate-indent-of-code)))))
@@ -238,6 +241,23 @@ declaration and its offset is `swift-mode:basic-offset'."
(setq string-beginning-position (nth 8 (syntax-ppss)))))
(forward-line 0)))
+(defun swift-mode:calculate-indent-of-single-line-comment ()
+ "Return the indentation of the current line inside a single-line comment."
+ (cond
+ ((save-excursion
+ (forward-line 0)
+ (bobp))
+ (swift-mode:indentation (point-min) 0))
+ ((save-excursion
+ (forward-line -1)
+ (skip-syntax-forward " ")
+ (looking-at "//"))
+ (forward-line -1)
+ (skip-syntax-forward " ")
+ (swift-mode:indentation (point) 0))
+ (t
+ (swift-mode:calculate-indent-of-code))))
+
(defun swift-mode:calculate-indent-of-code ()
"Return the indentation of the current line outside multiline comments."
(back-to-indentation)
@@ -1525,53 +1545,93 @@ multiline comment, close the previous comment and start
new one if
See `indent-new-comment-line' for SOFT."
(interactive)
(let* ((chunk (swift-mode:chunk-after))
- (comment-beginning-position (swift-mode:chunk:start chunk))
- (space-after-asterisk
- (if swift-mode:insert-space-after-asterisk-in-comment " " ""))
- (default-line-prefix
- (if swift-mode:prepend-asterisk-to-comment-line
- (concat "*" space-after-asterisk)
- "")))
- (delete-horizontal-space)
+ (comment-beginning-position (swift-mode:chunk:start chunk)))
(if soft (insert-and-inherit ?\n) (newline 1))
(delete-horizontal-space)
- (when (swift-mode:chunk:comment-p chunk)
+ (cond
+ ((not (swift-mode:chunk:comment-p chunk))
+ (indent-according-to-mode))
+
+ ((swift-mode:chunk:single-line-comment-p chunk)
(insert-before-markers-and-inherit
- (cond
- ((swift-mode:chunk:single-line-comment-p chunk)
- (save-excursion
- (goto-char comment-beginning-position)
- (looking-at "/+\\(\\s *\\)")
- (match-string-no-properties 0)))
+ (save-excursion
+ (goto-char comment-beginning-position)
+ (looking-at "/+\\s *")
+ (match-string-no-properties 0)))
+ (indent-according-to-mode))
- (comment-multi-line
- (save-excursion
- (forward-line 0)
- (forward-char -1)
- (forward-line 0)
- (if (<= (point) comment-beginning-position)
- ;; The cursor was on the 2nd line of the comment.
- ;; Uses default prefix.
- default-line-prefix
- ;; The cursor was on the 3nd or following lines of
- ;; the comment.
- ;; Use the prefix of the previous line.
- (back-to-indentation)
- (if (and swift-mode:prepend-asterisk-to-comment-line
- (looking-at "\\*+\\s *"))
- (match-string-no-properties 0)
- ""))))
-
- (t
- (backward-char)
- (insert-before-markers-and-inherit " */")
- (forward-char)
- (save-excursion
- (goto-char comment-beginning-position)
- (looking-at "/\\*+\\s *")
- (match-string-no-properties 0))))))
- (indent-according-to-mode)
+ ((not comment-multi-line)
+ (insert-before-markers-and-inherit
+ (save-excursion
+ (goto-char comment-beginning-position)
+ (looking-at "/\\*+\\s *")
+ (match-string-no-properties 0)))
+ ;; Cleans up and closes the previous line.
+ (save-excursion
+ (forward-line 0)
+ (backward-char)
+ (delete-horizontal-space)
+ (insert-before-markers-and-inherit " */"))
+ (indent-according-to-mode))
+
+ ((save-excursion
+ (forward-line -1)
+ (<= (point) comment-beginning-position))
+ ;; The cursor was on the 2nd line of the comment.
+ ;; Uses default prefix.
+ (when swift-mode:prepend-asterisk-to-comment-line
+ (insert-before-markers-and-inherit "*")
+ (when swift-mode:insert-space-after-asterisk-in-comment
+ (insert-before-markers-and-inherit " ")))
+ (indent-according-to-mode)
+ (insert-before-markers-and-inherit
+ (save-excursion
+ (goto-char comment-beginning-position)
+ (forward-char 1)
+ (looking-at "\\**\\(\\s *\\)")
+ (let ((prefix (match-string-no-properties 0)))
+ (if (= (length (match-string-no-properties 1)) 0)
+ ""
+ (substring
+ (replace-regexp-in-string "\\*" " " prefix)
+ (if swift-mode:prepend-asterisk-to-comment-line
+ (if swift-mode:insert-space-after-asterisk-in-comment 2 1)
+ 0)
+ (length prefix)))))))
+
+ ;; The cursor was on the 3nd or following lines of
+ ;; the comment.
+ ;; Uses the prefix of the previous line.
+
+ ((and
+ swift-mode:prepend-asterisk-to-comment-line
+ (save-excursion
+ (forward-line -1)
+ (looking-at "\\s *\\(\\*+\\s *\\)")))
+ ;; The previous line has a prefix. Uses it.
+ (insert-before-markers-and-inherit (match-string-no-properties 1))
+ (indent-according-to-mode))
+
+ ((save-excursion
+ (forward-line -1)
+ (looking-at "$"))
+ ;; The previous line is empty. Uses the default indentation.
+ (indent-according-to-mode))
+
+ (t
+ ;; Uses the prefix of the previous line.
+ (insert-before-markers-and-inherit
+ (save-excursion
+ (forward-line -1)
+ (looking-at "\\s *")
+ (match-string-no-properties 0)))))
+
+ ;; Cleans up the previous line.
+ (save-excursion
+ (forward-line 0)
+ (backward-char)
+ (delete-horizontal-space))
;; Closes incomplete multiline comment.
(when (and swift-mode:auto-close-multiline-comment
@@ -1579,8 +1639,9 @@ See `indent-new-comment-line' for SOFT."
(swift-mode:incomplete-comment-p))
(save-excursion
(end-of-line)
- (if soft (insert-and-inherit ?\n) (newline 1))
- (insert-before-markers-and-inherit "*/")
+ (when comment-multi-line
+ (if soft (insert-and-inherit ?\n) (newline 1)))
+ (insert-and-inherit "*/")
(indent-according-to-mode)))))
(defun swift-mode:post-self-insert ()
diff --git a/test/swift-files/comment.swift b/test/swift-files/comment.swift
index 0ae6905..0942643 100644
--- a/test/swift-files/comment.swift
+++ b/test/swift-files/comment.swift
@@ -19,7 +19,7 @@
// aaa
// bbb
// ccc // swift-mode:test:keep-indent
- // ddd // swift-mode:test:known-bug
+ // ddd
/* // swift-mode:test:known-bug
* aa
* aa // swift-mode:test:keep-indent
- [nongnu] elpa/swift-mode 99ff376 362/496: Improve functions related to defuns, (continued)
- [nongnu] elpa/swift-mode 99ff376 362/496: Improve functions related to defuns, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode e3cb72b 360/496: Fix `swift-mode:forward-sexp` for `up-list` and `down-list`, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 50394e4 357/496: Bump version to 2.4.0, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 82ee822 361/496: Fix code styles, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 5df00bc 367/496: Fix typo, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 1167fec 369/496: Fix comment, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode e58e4e0 368/496: Tweak test, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 089853c 384/496: Fix typo "identifer" to "identifier", ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode e81ab53 381/496: Fix comments, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 8d4a5a5 373/496: Add kill/mark/narrow-to-sentence, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode a042764 374/496: Fix indentation of comments,
ELPA Syncer <=
- [nongnu] elpa/swift-mode 8c45f69 383/496: Bump version to 4.0.1, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode ed03fde 377/496: Refactor tests, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 1970f1d 380/496: Fix `beginning-of-defun' inside class methods, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 4b2c628 372/496: Add forward/backward sentence, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode c67b950 388/496: Fixes which-function-mode hang when running swift-mode:current-defun-name in a non swift-mode buffer., ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 9809fb6 391/496: Add more comprehensive support for highlighting built-ins, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 544c265 397/496: Move macro before its use, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 3751335 398/496: Tweak and checkdoc compliance, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 4c5a1d9 401/496: Add support for running on device via ios-deploy, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 7ea1d4e 404/496: Bump version to 5.0.0, ELPA Syncer, 2021/08/29