Wednesday, May 27, 2020

Highlight issues in Swift language

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

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CA%2BJN8R%2BwNEFhnRVkofCemdWhRtHUHcthOrVHr5rm7yXhzo3a%2BQ%40mail.gmail.com.

No comments: