I want to modify .swiftlint.yml
to add some custom rules for enforcing braces on next line. This works for me ...
opening_braces:
name: "Opening Braces not on Next Line"
message: "Opening braces should be placed on the next line."
include: "*.swift"
regex: '\S[ \t]*\{'
severity: warning
However there are some cases where I want to allow braces on the same line, e.g. something like this:
override var cornerRadius: CGFloat
{
get { return layer.cornerRadius }
set { layer.cornerRadius = newValue }
}
How do I change my regexp to allow for same line for one-line getters/setters?
I suggest using
regex: '^(?![ \t]*[sg]et[ \t]+\{.*\}).*\S[ \t]*\{'
Or, its alternative with \h
matching horizontal whitespace:
regex: '^(?!\h*[sg]et\h+\{.*\}).*\S\h*\{'
See the regex demo (or this one).
Details
^
- start of string(?!\h*[sg]et\h+\{.*\})
- a location in string that should not be immediately followed with
\h*
- 0+ horizontal whitespaces[sg]et
- set
or get
\h+
- 1+ horizontal whitespaces\{.*\}
- {
, any 0+ chars, as many as possible, and }
.*
- any 0+ chars, as many as possible\S
- a non-whitespace char\h*
- 0+ horizontal whitespaces\{
- a {
char.