github-linguist / linguist

Language Savant. If your repository's language is being reported incorrectly, send us a pull request!
MIT License
12.32k stars 4.27k forks source link

Carbon language support #7011

Closed bendaws closed 2 months ago

bendaws commented 2 months ago

Adds support for the Carbon language. Carbon is an experimental language developed by Google.

Checklist

Closes https://github.com/github-linguist/linguist/issues/6013

bendaws commented 2 months ago

A basic sample in /samples/Carbon, and fixed the multiple extensions problem (.carbon is the most accepted extension, so I excluded .cb)

lildude commented 2 months ago

Do you really want to add support without syntax highlighting? Normally the only reason people add support for a language is to get syntax highlighting on GitHub.

bendaws commented 2 months ago

There is no syntax highlighter for it avaliable, although I may try to add one in the future.

Alhadis commented 2 months ago

@btdw Usually when we add support for a language that doesn't have a syntax highlighting grammar (or one released under a permissive license), we sometimes use another language's grammar as a fallback that happens to produce "good enough" highlighting. For example, here's one of your Carbon samples highlighted using Rust:

(Click to toggle) ~~~rust package Shapes api; import Math; // Circle class Circle { var Radius: f32 = 1; const var Diameter: f32 = self.Radius * 2; const var Pi: f32 = Math.Pi; fn Area() -> self; fn Circumference() -> self; } fn Circle.Area() -> self { return Math.Pi * .Radius ^ 2 } fn Circle.Circumference() -> self { return 2 * Math.Pi * .Radius } // Rectangle class Rectangle { var Width: f32 = 3; var Height: f32 = 1; fn Area() -> self; } fn Rectangle.Area() -> self { return .Width * .Height; } // Square (Note: Provides same functions as "Rectangle" class.) class Square { var Width: f32 = 3; var Height: f32 = 1; fn Area() -> self; } fn Square.Area() -> self { return .Width * .Height; } // Triangle class Triangle { var Width: f32 = 3; var Height: f32 = 3; fn Area() -> self; } fn Triangle.Area() -> self { return (.Width * .Height) / 2; } // Hexagon class Hexagon { var Side: f32 = 5; fn Area() -> self; } fn Hexagon.Area() -> self { return ((3 * 1.732) / 2) * .Side ^ 2 } ~~~
bendaws commented 2 months ago

@btdw Usually when we add support for a language that doesn't have a syntax highlighting grammar (or one released under a permissive license), we sometimes use another language's grammar as a fallback that happens to produce "good enough" highlighting. For example, here's one of your Carbon samples highlighted using Rust:

I've experimented with a couple and found the syntax for V works the best:

package Shapes api;
import Math;

// Circle

class Circle {
    var Radius: f32 = 1;
    const var Diameter: f32 = self.Radius * 2;

    const var Pi: f32 = Math.Pi;

    fn Area() -> self;
    fn Circumference() -> self;
}

fn Circle.Area() -> self {
    return Math.Pi * .Radius ^ 2
}

fn Circle.Circumference() -> self {
    return 2 * Math.Pi * .Radius
}

// Rectangle

class Rectangle {
    var Width: f32 = 3;
    var Height: f32 = 1;

    fn Area() -> self;
}

fn Rectangle.Area() -> self {
    return .Width * .Height;
}

// Square (Note: Provides same functions as "Rectangle" class.)

class Square {
    var Width: f32 = 3;
    var Height: f32 = 1;

    fn Area() -> self;
}

fn Square.Area() -> self {
    return .Width * .Height;
}

// Triangle

class Triangle {
    var Width: f32 = 3;
    var Height: f32 = 3;

    fn Area() -> self;
}

fn Triangle.Area() -> self {
    return (.Width * .Height) / 2;
}

// Hexagon

class Hexagon {
    var Side: f32 = 5;

    fn Area() -> self;
}

fn Hexagon.Area() -> self {
    return ((3 * 1.732) / 2) * .Side ^ 2
}
bendaws commented 2 months ago

I've experimented with a couple and found the syntax for V works the best:

https://github.com/github-linguist/linguist/pull/7011/commits/9bdc91cf76fafacf81c515def9c4aac1214d7321 adds V syntax highlighting to Carbon files

bendaws commented 2 months ago

Thanks for the suggestion, it was committed

Alhadis commented 2 months ago

@btdw You'll need to fix the failed test reported here. It simply involves updating vendor/README.md to include Carbon in the list. Just add this to the file on line 75:

--- expected
+++ actual
@@ -83,6 +83,7 @@
 - **Cairo:** [xshitaka/atom-language-cairo](https://github.com/xshitaka/atom-language-cairo)
 - **CameLIGO:** [pewulfman/Ligo-grammar](https://github.com/pewulfman/Ligo-grammar)
 - **Cap'n Proto:** [textmate/capnproto.tmbundle](https://github.com/textmate/capnproto.tmbundle)
+- **Carbon:** [0x9ef/vscode-vlang](https://github.com/0x9ef/vscode-vlang)