Highlight issues in Swift language

15 views
Skip to first unread message

Alessandro Antonello

unread,
May 27, 2020, 3:39:49 PM5/27/20
to Vim mailing list
Hi, everybody.

There has been a few months now that I'm struggling to highlight parameters
labels of Swift functions. Labels on Swift functions are awkward. If it is
not defined the parameter name should be used as label. Labels are used when the
function is called and became part of functions signature.

When I define a function like this:

~~~~
func someFunction(first: Int, second: String) -> Void { }
~~~~


I should call it using parameters names as labels:

~~~~
someFunction(first: 0, second: "string")
~~~~


When I define a function with labels, they should be used in function
call. I also can mix labeled parameters with unlabeled ones:

~~~~
func someFunction(firstParameter first: Int, second: String) -> Void { }
~~~~


In this case the function should be called as:

~~~~
someFunction(firstParameter: 0, second: "string")
~~~~


I can also free the caller on type labels by using "_" (underscore character)
as the parameter label in the function declaration:

~~~~
func someFunction(_ first: Int, second: String) -> Void { }
~~~~


Would be called as:

~~~~
someFunction(0, second: "string")
~~~~


What I was pursuing is highlight parameter labels, when they are present.
Otherwise, highlight parameter names. That I did successfully with the
following syntax groups:

~~~~
syn region  swiftCall      transparent matchgroup=Delimiter start=/(/ end=/)/

syn match   swiftArgLabel  contained /\<\(\_[,(]\s*\)\@<=\k\+\ze\s*:/ containedin=swiftCall
syn match   swiftArgLabel  contained /\<\(_\|\k\+\)\ze\(\s\+\k\+:\)/ containedin=swiftCall
~~~~


~~~~
func someFunction(first: Int, theLabel second: String) { }
                  ^^^^^       ^^^^^^^^
                  These are highlighted successfully.

someFunction(first: 0, theLabel: "text")
             ^^^^^     ^^^^^^^^
             In the function call it also works.
~~~~


The problem arose when the function definition or call spans multiple lines.

~~~~
func someFunctions(first: Int,
                   ^^^^^        <- This is not highlighted.
                   theLabel second: String)
                   ^^^^^^^^     <- Highlighted while the "close paren" is at the end of the line.

func someFunctions(first: Int,
                   ^^^^^        <- This is not highlighted.
                   theLabel second: String
                   ^^^^^^^^     <- Not highlighted becouse "close paren" is in the next line.
                   )
~~~~


There is more, if I have a 'swiftCall' region inside another 'swiftCall', the
highlight works. Like this, in a function call:

~~~~
someFunction(first: getSomeIntNumber(),
             ^^^^^         <- Now the highlight works.
             theLabel: "some string"
             ^^^^^^^^      <- Astonishing, here works too.
             )
~~~~


I wonder if someone can enlighten me on why the highlights doesn't work in all
cases.

Thanks in advance,

Alessandro Antonello
Reply all
Reply to author
Forward
0 new messages