Skip to content

Commit c659c45

Browse files
committed
Highlight anchor point for debugging.
1 parent ea77cf9 commit c659c45

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

swift-mode-indent.el

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
(require 'swift-mode-lexer)
3636

37+
;;; Customizations
38+
3739
;;;###autoload
3840
(defcustom swift-mode:basic-offset 4
3941
"Amount of indentation for block contents."
@@ -83,6 +85,17 @@
8385
:group 'swift
8486
:safe 'booleanp)
8587

88+
;;;###autoload
89+
(defcustom swift-mode:highlight-anchor nil
90+
"Highlight anchor point for indentation if non-nil.
91+
92+
Intended for debugging."
93+
:type 'boolean
94+
:group 'swift
95+
:safe 'booleanp)
96+
97+
;;; Constants and variables
98+
8699
(defconst swift-mode:statement-parent-tokens
87100
'(implicit-\; \; case-: { \( \[ anonymous-function-parameter-in)
88101
"Parent tokens for statements.")
@@ -92,16 +105,47 @@
92105
'(\, < supertype-: "where" "if" "guard" "while" "for" "catch"))
93106
"Parent tokens for expressions.")
94107

108+
(defvar-local swift-mode:anchor-overlay nil)
109+
(defvar-local swift-mode:anchor-overlay-timer nil)
110+
111+
;;; Indentation struct
112+
113+
(defun swift-mode:indentation (point offset)
114+
"Construct and return a indentation.
115+
116+
POINT is the position of the anchor point, such as the start of the previous
117+
line or the start of the class declaration.
118+
OFFSET is the offset from the anchor point. For example, when indenting the
119+
first line of a class body, its anchor point is the start of the class
120+
declaration and its offset is `swift-mode:basic-offset'."
121+
(list point offset))
122+
123+
(defun swift-mode:indentation:point (indentation)
124+
"Return the point of INDENTATION."
125+
(nth 0 indentation))
126+
127+
(defun swift-mode:indentation:offset (indentation)
128+
"Return the offset of INDENTATION."
129+
(nth 1 indentation))
130+
131+
;;; Indentation logics
132+
95133
(defun swift-mode:indent-line ()
96134
"Indent the current line."
97-
(let ((indent (save-excursion (swift-mode:calculate-indent)))
98-
(current-indent
99-
(save-excursion (back-to-indentation) (current-column))))
135+
(let* ((indentation (save-excursion (swift-mode:calculate-indent)))
136+
(indentation-column
137+
(save-excursion
138+
(goto-char (swift-mode:indentation:point indentation))
139+
(+ (current-column) (swift-mode:indentation:offset indentation))))
140+
(current-indent
141+
(save-excursion (back-to-indentation) (current-column))))
100142
(if (<= (current-column) current-indent)
101143
;; The cursor is on the left margin. Moving to the new indent.
102-
(indent-line-to indent)
144+
(indent-line-to indentation-column)
103145
;; Keeps current relative position.
104-
(save-excursion (indent-line-to indent)))))
146+
(save-excursion (indent-line-to indentation-column)))
147+
(when swift-mode:highlight-anchor
148+
(swift-mode:highlight-anchor indentation))))
105149

106150
(defun swift-mode:calculate-indent ()
107151
"Return the indentation of the current line."
@@ -128,13 +172,13 @@
128172
(progn
129173
(goto-char comment-beginning-position)
130174
(forward-char)
131-
(current-column))
175+
(swift-mode:indentation (point) 0))
132176
;; The cursor was on the 3rd or following lines of the comment, so aligns
133177
;; with a non-empty preceding line.
134178
(if (eolp)
135179
;; The cursor is on an empty line, so seeks a non-empty-line.
136180
(swift-mode:calculate-indent-of-multiline-comment)
137-
(current-column)))))
181+
(swift-mode:indentation (point) 0)))))
138182

139183
(defun swift-mode:calculate-indent-of-code ()
140184
"Return the indentation of the current line outside multiline comments."
@@ -150,7 +194,7 @@
150194
(cond
151195
;; Beginning of the buffer
152196
((eq previous-type 'outside-of-buffer)
153-
0)
197+
(swift-mode:indentation (point-min) 0))
154198

155199
;; Before } on the same line
156200
((and next-is-on-same-line (eq next-type '}))
@@ -837,7 +881,7 @@ This is also known as Utrecht-style in the Haskell community."
837881
;; Aligns with the end of the parent.
838882
(goto-char (swift-mode:token:end parent))
839883
(backward-char)
840-
(current-column))))
884+
(swift-mode:indentation (point) 0))))
841885

842886
(defun swift-mode:calculate-indent-after-comma ()
843887
"Return indentation after comma.
@@ -1139,13 +1183,13 @@ is the symbol `any', it matches all tokens."
11391183
(when (< (point) parent-end)
11401184
(goto-char parent-end))
11411185
(swift-mode:skip-whitespaces)
1142-
(+ (or offset 0) (current-column))))
1186+
(swift-mode:indentation (point) (or offset 0))))
11431187

11441188
(defun swift-mode:align-with-current-line (&optional offset)
11451189
"Return indentation of the current line with OFFSET."
11461190
(swift-mode:goto-non-comment-bol)
11471191
(swift-mode:skip-whitespaces)
1148-
(+ (or offset 0) (current-column)))
1192+
(swift-mode:indentation (point) (or offset 0)))
11491193

11501194
(defun swift-mode:backward-token-or-list ()
11511195
"Move point to the start position of the previous token or list.
@@ -1464,6 +1508,26 @@ See `indent-new-comment-line' for SOFT."
14641508
(delete-horizontal-space)
14651509
(forward-char))))
14661510

1511+
(defun swift-mode:highlight-anchor (indentation)
1512+
"Highlight the anchor point of the INDENTATION."
1513+
(move-overlay
1514+
swift-mode:anchor-overlay
1515+
(swift-mode:indentation:point indentation)
1516+
(1+ (swift-mode:indentation:point indentation)))
1517+
1518+
(overlay-put swift-mode:anchor-overlay 'face 'highlight)
1519+
1520+
(when swift-mode:anchor-overlay-timer
1521+
(cancel-timer swift-mode:anchor-overlay-timer))
1522+
1523+
(setq swift-mode:anchor-overlay-timer
1524+
(run-at-time
1525+
"1 sec"
1526+
nil
1527+
(lambda ()
1528+
(delete-overlay swift-mode:anchor-overlay)
1529+
(setq swift-mode:anchor-overlay-timer nil)))))
1530+
14671531
(provide 'swift-mode-indent)
14681532

14691533
;;; swift-mode-indent.el ends here

swift-mode.el

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ See `forward-sexp for ARG."
150150
(setq-local imenu-generic-expression swift-mode:imenu-generic-expression)
151151

152152
(setq-local beginning-of-defun-function #'swift-mode:beginning-of-defun)
153-
(setq-local end-of-defun-function #'swift-mode:end-of-defun))
153+
(setq-local end-of-defun-function #'swift-mode:end-of-defun)
154+
155+
(setq-local swift-mode:anchor-overlay
156+
(make-overlay (point-min) (point-min) nil t))
157+
158+
(delete-overlay swift-mode:anchor-overlay))
154159

155160
;;;###autoload (add-to-list 'auto-mode-alist '("\\.swift\\'" . swift-mode))
156161

0 commit comments

Comments
 (0)