Skip to content

Commit a042764

Browse files
committed
Fix indentation of comments
1 parent 8d4a5a5 commit a042764

File tree

2 files changed

+106
-45
lines changed

2 files changed

+106
-45
lines changed

swift-mode-indent.el

Lines changed: 105 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ declaration and its offset is `swift-mode:basic-offset'."
172172
((eq (nth 3 parser-state) t)
173173
(swift-mode:calculate-indent-of-multiline-string))
174174

175+
((looking-at "//")
176+
(swift-mode:calculate-indent-of-single-line-comment))
177+
175178
(t
176179
(swift-mode:calculate-indent-of-code)))))
177180

@@ -238,6 +241,23 @@ declaration and its offset is `swift-mode:basic-offset'."
238241
(setq string-beginning-position (nth 8 (syntax-ppss)))))
239242
(forward-line 0)))
240243

244+
(defun swift-mode:calculate-indent-of-single-line-comment ()
245+
"Return the indentation of the current line inside a single-line comment."
246+
(cond
247+
((save-excursion
248+
(forward-line 0)
249+
(bobp))
250+
(swift-mode:indentation (point-min) 0))
251+
((save-excursion
252+
(forward-line -1)
253+
(skip-syntax-forward " ")
254+
(looking-at "//"))
255+
(forward-line -1)
256+
(skip-syntax-forward " ")
257+
(swift-mode:indentation (point) 0))
258+
(t
259+
(swift-mode:calculate-indent-of-code))))
260+
241261
(defun swift-mode:calculate-indent-of-code ()
242262
"Return the indentation of the current line outside multiline comments."
243263
(back-to-indentation)
@@ -1525,62 +1545,103 @@ multiline comment, close the previous comment and start new one if
15251545
See `indent-new-comment-line' for SOFT."
15261546
(interactive)
15271547
(let* ((chunk (swift-mode:chunk-after))
1528-
(comment-beginning-position (swift-mode:chunk:start chunk))
1529-
(space-after-asterisk
1530-
(if swift-mode:insert-space-after-asterisk-in-comment " " ""))
1531-
(default-line-prefix
1532-
(if swift-mode:prepend-asterisk-to-comment-line
1533-
(concat "*" space-after-asterisk)
1534-
"")))
1535-
(delete-horizontal-space)
1548+
(comment-beginning-position (swift-mode:chunk:start chunk)))
15361549
(if soft (insert-and-inherit ?\n) (newline 1))
15371550
(delete-horizontal-space)
15381551

1539-
(when (swift-mode:chunk:comment-p chunk)
1552+
(cond
1553+
((not (swift-mode:chunk:comment-p chunk))
1554+
(indent-according-to-mode))
1555+
1556+
((swift-mode:chunk:single-line-comment-p chunk)
15401557
(insert-before-markers-and-inherit
1541-
(cond
1542-
((swift-mode:chunk:single-line-comment-p chunk)
1543-
(save-excursion
1544-
(goto-char comment-beginning-position)
1545-
(looking-at "/+\\(\\s *\\)")
1546-
(match-string-no-properties 0)))
1558+
(save-excursion
1559+
(goto-char comment-beginning-position)
1560+
(looking-at "/+\\s *")
1561+
(match-string-no-properties 0)))
1562+
(indent-according-to-mode))
15471563

1548-
(comment-multi-line
1549-
(save-excursion
1550-
(forward-line 0)
1551-
(forward-char -1)
1552-
(forward-line 0)
1553-
(if (<= (point) comment-beginning-position)
1554-
;; The cursor was on the 2nd line of the comment.
1555-
;; Uses default prefix.
1556-
default-line-prefix
1557-
;; The cursor was on the 3nd or following lines of
1558-
;; the comment.
1559-
;; Use the prefix of the previous line.
1560-
(back-to-indentation)
1561-
(if (and swift-mode:prepend-asterisk-to-comment-line
1562-
(looking-at "\\*+\\s *"))
1563-
(match-string-no-properties 0)
1564-
""))))
1565-
1566-
(t
1567-
(backward-char)
1568-
(insert-before-markers-and-inherit " */")
1569-
(forward-char)
1570-
(save-excursion
1571-
(goto-char comment-beginning-position)
1572-
(looking-at "/\\*+\\s *")
1573-
(match-string-no-properties 0))))))
1574-
(indent-according-to-mode)
1564+
((not comment-multi-line)
1565+
(insert-before-markers-and-inherit
1566+
(save-excursion
1567+
(goto-char comment-beginning-position)
1568+
(looking-at "/\\*+\\s *")
1569+
(match-string-no-properties 0)))
1570+
;; Cleans up and closes the previous line.
1571+
(save-excursion
1572+
(forward-line 0)
1573+
(backward-char)
1574+
(delete-horizontal-space)
1575+
(insert-before-markers-and-inherit " */"))
1576+
(indent-according-to-mode))
1577+
1578+
((save-excursion
1579+
(forward-line -1)
1580+
(<= (point) comment-beginning-position))
1581+
;; The cursor was on the 2nd line of the comment.
1582+
;; Uses default prefix.
1583+
(when swift-mode:prepend-asterisk-to-comment-line
1584+
(insert-before-markers-and-inherit "*")
1585+
(when swift-mode:insert-space-after-asterisk-in-comment
1586+
(insert-before-markers-and-inherit " ")))
1587+
(indent-according-to-mode)
1588+
(insert-before-markers-and-inherit
1589+
(save-excursion
1590+
(goto-char comment-beginning-position)
1591+
(forward-char 1)
1592+
(looking-at "\\**\\(\\s *\\)")
1593+
(let ((prefix (match-string-no-properties 0)))
1594+
(if (= (length (match-string-no-properties 1)) 0)
1595+
""
1596+
(substring
1597+
(replace-regexp-in-string "\\*" " " prefix)
1598+
(if swift-mode:prepend-asterisk-to-comment-line
1599+
(if swift-mode:insert-space-after-asterisk-in-comment 2 1)
1600+
0)
1601+
(length prefix)))))))
1602+
1603+
;; The cursor was on the 3nd or following lines of
1604+
;; the comment.
1605+
;; Uses the prefix of the previous line.
1606+
1607+
((and
1608+
swift-mode:prepend-asterisk-to-comment-line
1609+
(save-excursion
1610+
(forward-line -1)
1611+
(looking-at "\\s *\\(\\*+\\s *\\)")))
1612+
;; The previous line has a prefix. Uses it.
1613+
(insert-before-markers-and-inherit (match-string-no-properties 1))
1614+
(indent-according-to-mode))
1615+
1616+
((save-excursion
1617+
(forward-line -1)
1618+
(looking-at "$"))
1619+
;; The previous line is empty. Uses the default indentation.
1620+
(indent-according-to-mode))
1621+
1622+
(t
1623+
;; Uses the prefix of the previous line.
1624+
(insert-before-markers-and-inherit
1625+
(save-excursion
1626+
(forward-line -1)
1627+
(looking-at "\\s *")
1628+
(match-string-no-properties 0)))))
1629+
1630+
;; Cleans up the previous line.
1631+
(save-excursion
1632+
(forward-line 0)
1633+
(backward-char)
1634+
(delete-horizontal-space))
15751635

15761636
;; Closes incomplete multiline comment.
15771637
(when (and swift-mode:auto-close-multiline-comment
15781638
(swift-mode:chunk:multiline-comment-p chunk)
15791639
(swift-mode:incomplete-comment-p))
15801640
(save-excursion
15811641
(end-of-line)
1582-
(if soft (insert-and-inherit ?\n) (newline 1))
1583-
(insert-before-markers-and-inherit "*/")
1642+
(when comment-multi-line
1643+
(if soft (insert-and-inherit ?\n) (newline 1)))
1644+
(insert-and-inherit "*/")
15841645
(indent-according-to-mode)))))
15851646

15861647
(defun swift-mode:post-self-insert ()

test/swift-files/comment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// aaa
2020
// bbb
2121
// ccc // swift-mode:test:keep-indent
22-
// ddd // swift-mode:test:known-bug
22+
// ddd
2323
/* // swift-mode:test:known-bug
2424
* aa
2525
* aa // swift-mode:test:keep-indent

0 commit comments

Comments
 (0)