Skip to content

Commit

Permalink
Support regex literals
Browse files Browse the repository at this point in the history
  • Loading branch information
taku0 committed Jul 9, 2022
1 parent 975e256 commit 9b8a926
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 37 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ var x = foo

Both are syntactically correct code. We cannot handle this case properly. This is also a future work.

Other example is regex literals and custom operators. The following example is valid Swift code with regex literals and custom operators.

```swift
let x = /^/ /^/ /^/
```

We parse them as regex literals rather than custom operators for now.


## Hacking

To build the package locally, run `make package`.
Expand Down
12 changes: 8 additions & 4 deletions swift-mode-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,15 @@ declaration and its offset is `swift-mode:basic-offset'."
(swift-mode:indentation (point) 0)))))

(defun swift-mode:calculate-indent-of-multiline-string ()
"Return the indentation of the current line inside a multiline string."
"Return the indentation of the current line inside a multiline string.
Also used for regexes."
(back-to-indentation)
(let ((string-beginning-position
(save-excursion (swift-mode:beginning-of-string))))
(if (looking-at "\"\"\"")
(if (and (looking-at "\\(\"\"\"\\|/\\)#*")
(equal (get-text-property (1- (match-end 0)) 'syntax-table)
(string-to-syntax "|")))
;; The last line.
(progn
(goto-char string-beginning-position)
Expand All @@ -240,13 +244,13 @@ declaration and its offset is `swift-mode:basic-offset'."
(swift-mode:goto-non-interpolated-expression-bol)
(back-to-indentation)
(if (<= (point) string-beginning-position)
;; The cursor was on the 2nd line of the comment, so aligns with
;; The cursor was on the 2nd line of the string, so aligns with
;; that line with offset.
(progn
(goto-char string-beginning-position)
(swift-mode:calculate-indent-of-expression
swift-mode:multiline-statement-offset))
;; The cursor was on the 3rd or following lines of the comment, so
;; The cursor was on the 3rd or following lines of the string, so
;; aligns with a non-empty preceding line.
(if (and (bolp) (eolp))
;; The cursor is on an empty line, so seeks a non-empty-line.
Expand Down
Loading

0 comments on commit 9b8a926

Please sign in to comment.