PrismJS / prism

Lightweight, robust, elegant syntax highlighting.
https://prismjs.com
MIT License
12.13k stars 1.28k forks source link

Swift enum with associated values incorrect highlighting #2023

Open d-srd opened 4 years ago

d-srd commented 4 years ago

Information

Does the problem still occur in the latest version of Prism? You can check using the test page or get the latest version at the download page.

Yes, the problem still occurs in the latest version of Prism.

Description An enum with associated values has its cases incorrectly interpreted as a function token, which is a problem when an enum has a case with one associated value and an empty case.

Incorrect:

Incorrectly rendered Swift enum

Correct:

Correctly rendered Swift enum

All of the cases should have the same color.

Code snippet

The code being highlighted incorrectly. ``` public enum Event { case next(Element) case error(Error) case completed } ```
RunDevelopment commented 4 years ago

While not 100% semantically correct, you could argue that they behave like function in that they can be invoked:

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}

var productBarcode = Barcode.upc(8, 85909, 51226, 3)

GitHub's highlighting even highlights simple values like functions: (Well, not directly functions but pl-c1, if that tells you anything.)

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}
switch productBarcode {
case .upc(let numberSystem, let manufacturer, let product, let check):
    print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
case .qrCode(let productCode):
    print("QR code: \(productCode).")
}
enum Planet {
    case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}

But whether we highlight them all as functions of not, it will be really hard to implement this with Prism's regex-based tokenizer. So maybe it's the best solution to just leave it as is, even if it is a bit inconsistent?