Skip to content

Commit 778e9e6

Browse files
committed
1 parent 6d7093f commit 778e9e6

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

swift-mode-lexer.el

+5
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ return non-nil."
508508
(if (memq (char-after) '(?/ ?*))
509509
(goto-char limit)
510510
(setq end-of-regex (point)))))))
511+
(when (and end-of-regex
512+
(memq (char-before (1- end-of-regex)) '(?\s ?\t))
513+
(not (swift-mode:escaped-p (- end-of-regex 2) 0)))
514+
;; Cannot ends with spaces or tabs unless escaped.
515+
(setq end-of-regex nil))
511516
(unless end-of-regex
512517
(goto-char pos))
513518
end-of-regex))

test/swift-files/indent/strings.swift

+40-24
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func f() {
169169
let x = /a/
170170

171171
// Slashes can be escaped.
172-
let x = /\/ /
172+
let x = /\/ +/
173173

174174
// Slashes must be escaped in character classes.
175175
let x = /[/ + "]/ + // "
@@ -217,18 +217,18 @@ func f() {
217217

218218
// Regexes without extended delimiters cannot be preceded by infix
219219
// operators without whitespaces.
220-
// `a`, infix operator `+/`, `b`, and infix operator `/`
221-
let x = a+/b /
220+
// `a`, infix operator `+/`, `b`, and infix operator `%/`
221+
let x = a+/b %/
222222
c()
223223

224224
// Regexes without extended delimiters can be preceded by infix operators
225225
// with whitespaces.
226-
// `a`, infix operator `+`, and regex /b /
227-
let x = a + /b /
226+
// `a`, infix operator `+`, and regex /b %/
227+
let x = a + /b %/
228228
c()
229229

230230
// Comments are whitespaces.
231-
let x = a/**/+/**//b /
231+
let x = a/**/+/**//b %/
232232
c()
233233

234234
// Regexes with extended delimiters can be preceded by infix operators
@@ -240,26 +240,42 @@ func f() {
240240
// Regexes without extended delimiters cannot start with spaces.
241241
let regex = Regex {
242242
digit
243-
/ [+-] /
243+
// infix operator `/`, and `a` with postfix operator `/'
244+
/ a/
245+
digit
246+
}
247+
// Regexes without extended delimiters cannot end with spaces.
248+
let regex = Regex {
249+
digit
250+
// prefix operator `/`, `a`, and infix operator `/'
251+
/a /
244252
digit
245253
}
246254
let regex = Regex {
247255
digit
248-
/[+-]/
256+
// regex /a/
257+
/a/
249258
digit
250259
}
251260

252261
// Initial space must be escaped.
253262
let regex = Regex {
254263
digit
255-
/\ [+-] /
264+
/\ a/
265+
digit
266+
}
267+
268+
// Final space must be escaped.
269+
let regex = Regex {
270+
digit
271+
/a\ /
256272
digit
257273
}
258274

259275
// Regexes with extended delimiters can start with spaces.
260276
let regex = Regex {
261277
digit
262-
#/ [+-] /#
278+
#/ a /#
263279
digit
264280
}
265281

@@ -291,39 +307,39 @@ func f() {
291307
c()
292308

293309
// Regexes can be preceded with prefix operators wihtout spaces.
294-
// prefix operator `+` and regex /a /.
295-
let x = +/a /
310+
// prefix operator `+` and regex /a %/.
311+
let x = +/a %/
296312
b()
297313

298314
// Regexes without extended delimiters cannot contain unmatching close
299315
// parentheses.
300-
array.reduce(1, /) { otherArray.reduce(1, /)
301-
array.reduce(1, /) }; otherArray.reduce(1, /)
316+
array.reduce(1,/) { otherArray.reduce(1,/)
317+
array.reduce(1,/) }; otherArray.reduce(1,/)
302318

303319
// Regexes without extended delimiters can contain matching close
304320
// parentheses.
305-
array.reduce(1, /(a) { otherArray.reduce(1, /)
306-
array.reduce(1, /(a) }; otherArray.reduce(1, /)
321+
array.reduce(1,/(a) { otherArray.reduce(1,/)
322+
array.reduce(1,/(a) }; otherArray.reduce(1,/)
307323

308324
// Regexes without extended delimiters can contain escaped close
309325
// parentheses.
310-
array.reduce(1, /\) { otherArray.reduce(1, /)
311-
array.reduce(1, /\) }; otherArray.reduce(1, /)
326+
array.reduce(1,/\) { otherArray.reduce(1,/)
327+
array.reduce(1,/\) }; otherArray.reduce(1,/)
312328

313329
// Character classes can contain closing parentheses.
314-
array.reduce(1, /[)] { otherArray.reduce(1, /)
315-
array.reduce(1, /[)] }; otherArray.reduce(1, /)
330+
array.reduce(1,/[)] { otherArray.reduce(1,/)
331+
array.reduce(1,/[)] }; otherArray.reduce(1,/)
316332

317333
// Regexes with extended delimiters can contain unmatching close
318334
// parentheses.
319-
array.reduce(1, #/) { otherArray.reduce(1, /#)
320-
array.reduce(1, #/) }; otherArray.reduce(1, /#)
335+
array.reduce(1,#/) { otherArray.reduce(1,/#)
336+
array.reduce(1,#/) }; otherArray.reduce(1,/#)
321337

322338

323339
// Regexes can contain unmatching close square brackets.
324-
let d = a[/] /
340+
let d = a[/] %/
325341
]
326-
let d = a[(/)] /
342+
let d = a[(/)] %/
327343
b()
328344

329345
// Comments have higher precedence.

0 commit comments

Comments
 (0)