Skip to content

Commit 7a9cf18

Browse files
committed
Fix beginning/end-of-sentence
Add tests for beginning/end-of-sentence
1 parent e81ab53 commit 7a9cf18

File tree

5 files changed

+262
-96
lines changed

5 files changed

+262
-96
lines changed

swift-mode-beginning-of-defun.el

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ Intended for internal use."
218218
(let ((chunk (swift-mode:chunk-after)))
219219
(when chunk
220220
(goto-char (swift-mode:chunk:start chunk))))
221+
(when (and (eq (char-syntax (or (char-after) ?.)) ?w)
222+
(eq (char-syntax (or (char-before) ?.)) ?w))
223+
(swift-mode:forward-token))
221224
(let ((pos (point))
222225
(previous-token (save-excursion
223226
(forward-comment (- (point)))
@@ -289,7 +292,7 @@ Statements include comments on the same line.
289292
Intended for internal use."
290293
(let ((pos (point)))
291294
(swift-mode:beginning-of-statement)
292-
(when (eq pos (point))
295+
(when (<= pos (point))
293296
(swift-mode:backward-token-or-list)
294297
(swift-mode:beginning-of-statement))))
295298

@@ -336,6 +339,9 @@ Intended for internal use."
336339
(let ((chunk (swift-mode:chunk-after)))
337340
(when chunk
338341
(goto-char (swift-mode:chunk:start chunk))))
342+
(when (and (eq (char-syntax (or (char-after) ?.)) ?w)
343+
(eq (char-syntax (or (char-before) ?.)) ?w))
344+
(swift-mode:backward-token))
339345
(forward-comment (point-max))
340346
(let ((pos (point))
341347
token)
@@ -808,6 +814,17 @@ In comments or strings, skip a sentence. Otherwise, skip a stateement."
808814
"Skip forward a sentence in a comment.
809815
810816
IS-SINGLE-LINE should be non-nil when called inside a single-line comment."
817+
(when (and (not is-single-line)
818+
(eq (char-before) ?/)
819+
(eq (char-after) ?*))
820+
(forward-char))
821+
(when (and is-single-line
822+
(< (point) (save-excursion
823+
(forward-line 0)
824+
(if (looking-at "\\s *//+")
825+
(match-end 0)
826+
(point)))))
827+
(goto-char (match-end 0)))
811828
(let ((current-buffer (current-buffer))
812829
(pos (point))
813830
(comment-block-end-position
@@ -824,7 +841,8 @@ IS-SINGLE-LINE should be non-nil when called inside a single-line comment."
824841
(if is-single-line
825842
(while (re-search-forward "^[ \t]*/+[ \t]*" nil t)
826843
(replace-match ""))
827-
(when (looking-at "\\*+")
844+
(when (and (not (looking-at "\\*+/"))
845+
(looking-at "\\*+"))
828846
(replace-match ""))))
829847
;; Forwards sentence.
830848
(let ((old-position (point)))
@@ -848,7 +866,14 @@ IS-SINGLE-LINE should be non-nil when called inside a single-line comment."
848866
(forward-line -1)
849867
(setq line-count (1+ line-count)))))
850868
(forward-line line-count)
851-
(goto-char (- (line-end-position) offset-from-line-end))
869+
(goto-char (- (if (and (not is-single-line)
870+
(eq (line-end-position)
871+
(save-excursion
872+
(goto-char comment-block-end-position)
873+
(line-end-position))))
874+
comment-block-end-position
875+
(line-end-position))
876+
offset-from-line-end))
852877
(or (/= (point) pos)
853878
(progn
854879
(goto-char comment-block-end-position)
@@ -901,7 +926,7 @@ IS-SINGLE-LINE should be non-nil when called inside a single-line comment."
901926
(or (< (point) pos)
902927
(progn
903928
(goto-char comment-block-beginning-position)
904-
(swift-mode:backward-sentence-inside-code nil)))))
929+
(swift-mode:backward-sentence-inside-code t)))))
905930

906931
(defmacro swift-mode:with-temp-comment-buffer (&rest body)
907932
"Eval BODY inside a temporary buffer keeping sentence related variables."
@@ -1098,10 +1123,7 @@ the end of a sentence, keep the position."
10981123
(progn (swift-mode:end-of-statement) t)
10991124
(let ((pos (point)))
11001125
(swift-mode:forward-statement)
1101-
(or (not (eobp))
1102-
(progn
1103-
(forward-comment (- (point)))
1104-
(< pos (point))))))))
1126+
(< pos (point))))))
11051127

11061128
(defun swift-mode:backward-sentence-inside-code
11071129
(&optional keep-position-if-at-beginning-of-sentence)
@@ -1118,10 +1140,7 @@ already at the beginning of a sentence, keep the position."
11181140
(progn (swift-mode:beginning-of-statement) t)
11191141
(let ((pos (point)))
11201142
(swift-mode:backward-statement)
1121-
(or (not (bobp))
1122-
(progn
1123-
(forward-comment (point-max))
1124-
(< (point) pos)))))))
1143+
(< (point) pos)))))
11251144

11261145
(defun swift-mode:kill-sentence (&optional arg)
11271146
"Kill from the point to the end of sentences.

swift-mode-lexer.el

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,23 +1331,30 @@ If this line ends with a single-line comment, goto just before the comment."
13311331
If the cursor is outside of strings and comments, return nil.
13321332
13331333
If PARSER-STATE is given, it is used instead of (syntax-ppss)."
1334-
(when (or (null parser-state) (number-or-marker-p parser-state))
1335-
(setq parser-state (save-excursion (syntax-ppss parser-state))))
1336-
(cond
1337-
((eq (nth 3 parser-state) t)
1338-
(swift-mode:chunk 'multiline-string (nth 8 parser-state)))
1339-
((nth 3 parser-state)
1340-
(swift-mode:chunk 'single-line-string (nth 8 parser-state)))
1341-
((eq (nth 4 parser-state) t)
1342-
(swift-mode:chunk 'single-line-comment (nth 8 parser-state)))
1343-
((nth 4 parser-state)
1344-
(swift-mode:chunk 'multiline-comment (nth 8 parser-state)))
1345-
((and (eq (char-before) ?/) (eq (char-after) ?/))
1346-
(swift-mode:chunk 'single-line-comment (1- (point))))
1347-
((and (eq (char-before) ?/) (eq (char-after) ?*))
1348-
(swift-mode:chunk 'multiline-comment (1- (point))))
1349-
(t
1350-
nil)))
1334+
(save-excursion
1335+
(when (number-or-marker-p parser-state)
1336+
(goto-char parser-state))
1337+
(when (or (null parser-state) (number-or-marker-p parser-state))
1338+
(setq parser-state (save-excursion (syntax-ppss parser-state))))
1339+
(cond
1340+
((nth 3 parser-state)
1341+
;; Syntax category "|" is attached to both single-line and multiline
1342+
;; string delimiters. So (nth 3 parser-state) may be t even for
1343+
;; single-line string delimiters.
1344+
(if (save-excursion (goto-char (nth 8 parser-state))
1345+
(looking-at "\"\"\""))
1346+
(swift-mode:chunk 'multiline-string (nth 8 parser-state))
1347+
(swift-mode:chunk 'single-line-string (nth 8 parser-state))))
1348+
((eq (nth 4 parser-state) t)
1349+
(swift-mode:chunk 'single-line-comment (nth 8 parser-state)))
1350+
((nth 4 parser-state)
1351+
(swift-mode:chunk 'multiline-comment (nth 8 parser-state)))
1352+
((and (eq (char-before) ?/) (eq (char-after) ?/))
1353+
(swift-mode:chunk 'single-line-comment (1- (point))))
1354+
((and (eq (char-before) ?/) (eq (char-after) ?*))
1355+
(swift-mode:chunk 'multiline-comment (1- (point))))
1356+
(t
1357+
nil))))
13511358

13521359
(provide 'swift-mode-lexer)
13531360

0 commit comments

Comments
 (0)