Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing keywords for Swift #3021

Closed
7 tasks
AvdLee opened this issue Jul 31, 2021 · 12 comments
Closed
7 tasks

Add missing keywords for Swift #3021

AvdLee opened this issue Jul 31, 2021 · 12 comments

Comments

@AvdLee
Copy link
Contributor

AvdLee commented Jul 31, 2021

Information

  • Language: Swift
  • Plugins: 'none'

Description
Some of Swift's keywords are missing. This issue is here for myself to make a collection over the upcoming week, after which I'm happy to implement this myself. If you're reading this, feel free to open up a PR adding support for these keywords!

  • associatedtype
  • @autoclosure
  • @discardableResult
  • @dynamicMemberLookup
  • fileprivate
  • @resultBuilder
  • @unknown

Also, not sure if this code is currently highlighted wrong as well:

Available

Current:
image

Expected:
image

@RunDevelopment
Copy link
Member

RunDevelopment commented Jul 31, 2021

I'm happy to implement this myself.

Thank you very much!

Also, not sure if this code is currently highlighted wrong as well:

What does #available mean? Is # part of the function(?) name? I search for this but could only find stuff related to parameters.

@AvdLee
Copy link
Contributor Author

AvdLee commented Aug 2, 2021

What does #available mean? Is # part of the function(?) name? I search for this but could only find stuff related to parameters.

It's an availability API to make the compiler happy when using latest API's while also supporting older iOS/macOS versions.

It has the same syntax of functions, which I guess is the reason for its current highlighting.

In Swift, it's called a declaration attribute: https://docs.swift.org/swift-book/ReferenceManual/Attributes.html

@RunDevelopment
Copy link
Member

But attributes start with an @. This post explains the difference. Maybe we should highlight @available and #available differently?

Also, attributes seem to be very generic in Swift (devs can even define their own). It might be better to have a generic attribute pattern (e.g. /@\w+/) instead of the fixed list we currently have.

@AvdLee
Copy link
Contributor Author

AvdLee commented Aug 3, 2021

Also, attributes seem to be very generic in Swift (devs can even define their own). It might be better to have a generic attribute pattern (e.g. /@\w+/) instead of the fixed list we currently have.

That's actually a good point you raise. Property wrappers, for example, can lead to all kinds of @ statements. Though, the highlighting in that linked article about property wrappers does match what I see in Xcode:

image

It's hard for me to give a call on using a generic attribute in such that I don't know if it adds more than it breaks? If it even breaks anything, not sure either!

@RunDevelopment
Copy link
Member

My guess is that XCode knows that SampleFile is a struct and therefore highlights it as such.

We could do the same thing. Using the class-name detection from #3022, we could split attributes in two. All attributes starting with an upper-case letter are assumed to be property wrappers and will be highlighted as @ + class-name. All other attributes will be highlighted like the keyword-like attributes we have now (e.g. @propertyWrapper).

If it even breaks anything, not sure either!

Prism's languages are an implementation detail. We can change whatever we want (hopefully for the better).

@AvdLee
Copy link
Contributor Author

AvdLee commented Aug 3, 2021

Prism's languages are an implementation detail. We can change whatever we want (hopefully for the better).

That makes sense!

We could do the same thing. Using the class-name detection from #3022, we could split attributes in two. All attributes starting with an upper-case letter are assumed to be property wrappers and will be highlighted as @ + class-name. All other attributes will be highlighted like the keyword-like attributes we have now (e.g. @propertyWrapper).

That should do the trick, indeed. There are a few exceptions though, like @IBDesignable, @IBAction, @MainActor. Quite a few of the ones that are currently defined within Prism. Wouldn't we end up highlighting those as @ + class-name as well, effectively making a mistake?

@RunDevelopment
Copy link
Member

Hmmm, that's a problem. Would it even be possible to distinguish property wrappers from other attributes without compiling the program?

In that case, it might be best to be consistent to highlight all attributes in the same way. That's also what GitHub's highlighter does.

@propertyWrapper
@escaping
@MainActor
@SampleFile

@AvdLee
Copy link
Contributor Author

AvdLee commented Aug 9, 2021

In that case, it might be best to be consistent to highlight all attributes in the same way. That's also what GitHub's highlighter does.

That makes the most sense to me too, indeed!

@RunDevelopment
Copy link
Member

I think #3022 resolved all the tasks in this issue. Can we close this now @AvdLee?

@AvdLee
Copy link
Contributor Author

AvdLee commented Aug 9, 2021

In that case, it might be best to be consistent to highlight all attributes in the same way. That's also what GitHub's highlighter does.

Does #3022 also include this? I did not see a new commit since our discussion here. If so, yes! Otherwise, the PR should include an update for highlighting those keywords.

@RunDevelopment
Copy link
Member

Does #3022 also include this?

It does. I pushed the commit adding GitHub-like attribute highlighting before we talked about attributes here.

@AvdLee
Copy link
Contributor Author

AvdLee commented Aug 10, 2021

Perfect, thanks a lot @RunDevelopment. I'll make sure to test it out thoroughly on my blog and keep seeking for improvements over time 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants