atom / language-c

C support in Atom
Other
118 stars 152 forks source link

Implement naming conventions in C and C++ grammars #351

Open chbk opened 3 years ago

chbk commented 3 years ago

Description of the Change

This is a rewrite of the Tree-sitter grammar to implement naming conventions for syntax scopes.

Benefits

Possible Drawbacks

Some new scopes to be added to themes. The changes aim to facilitate theme development, filling the template is enough to ensure coherent highlighting across languages, instead of painfully creating styling rules for every language separately.

Applicable Issues

Related Pull Requests

chbk commented 3 years ago

Preview of the changes in C with Atom's default syntax themes:

Without naming conventions
(current Tree-sitter grammar)
With naming conventions in
theme and Tree-sitter grammar
Solarized Dark
solarized-dark-c-0
Solarized Dark
solarized-dark-c-1
One Dark
one-dark-c-0
One Dark
one-dark-c-1
Base16 Tomorrow Dark
base16-dark-c-0
Base16 Tomorrow Dark
base16-dark-c-1
Atom Dark
atom-dark-c-0
Atom Dark
atom-dark-c-1
Solarized Light
solarized-light-c-0
Solarized Light
solarized-light-c-1
One Light
one-light-c-0
One Light
one-light-c-1
Base16 Tomorrow Light
base16-light-c-0
Base16 Tomorrow Light
base16-light-c-1
Atom Light
atom-light-c-0
Atom Light
atom-light-c-1

Code snippet:

#include <Dagobah>
#define ONE 1

/* There is no try */

typedef struct {
  double rise;
  char fall[sizeof(int)];
} Rock;

void move(Rock *pebble) {
  static int force += 327;
  pebble->levitate++;
}

int main() {

  const int R2D = 2, C3P = 0;

  Starfighter redfive;
  redfive.target = "reactor";

  int *trooper = &clone;
  *trooper << 1;

  goto deathstar;

  char wookie[1][5] = {
    {'h', 'a', 'i', 'r', 'y'}
  };

  deathstar:

  if (padawan != NULL) {
    say("Size matters not.");
  }
}
chbk commented 3 years ago

Preview of the changes in C++ with Atom's default syntax themes:

Without naming conventions
(current Tree-sitter grammar)
With naming conventions in
theme and Tree-sitter grammar
Solarized Dark
solarized-dark-cpp-0
Solarized Dark
solarized-dark-cpp-1
One Dark
one-dark-cpp-0
One Dark
one-dark-cpp-1
Base16 Tomorrow Dark
base16-dark-cpp-0
Base16 Tomorrow Dark
base16-dark-cpp-1
Atom Dark
atom-dark-cpp-0
Atom Dark
atom-dark-cpp-1
Solarized Light
solarized-light-cpp-0
Solarized Light
solarized-light-cpp-1
One Light
one-light-cpp-0
One Light
one-light-cpp-1
Base16 Tomorrow Light
base16-light-cpp-0
Base16 Tomorrow Light
base16-light-cpp-1
Atom Light
atom-light-cpp-0
Atom Light
atom-light-cpp-1

Code snippet:

using namespace hillvalley;
#include <plutonium>

// We don't need roads

class DeLorean: public Car {
  private:
    int speed;
  public:
    std::string plate;
    template<typename T>
    void drive(T mph, ...) {
      this->speed = mph;
    }
};

struct Travel {
  int year, month, day;
};

int main() {

  auto gigawatts = 1.21;
  double *power = nullptr;
  power = &gigawatts;

  if (*power >= 1.21) {
    say("Great", "Scott");
  }

  static_cast<int>(*power);

  DeLorean car;
  car.plate = "OUTATIME";
  car.drive(88);

  auto add = [](int i = 0) {
    return ++i;
  };

  Travel to {1955, 11, 5};

  const char *purple[2] = {
    "Calvin", "Klein"
  };
}
jeff-hykin commented 3 years ago

I'm really glad to see some standardization, textmate scopes are an absolute mess (everywhere not just atom). I've been wanting them standardized for years.

would you want to help create a standard guide for Atom Textmate scopes for anyone creating a grammar? It would probably be helpful for theme makers updating their themes as well.

jeff-hykin commented 3 years ago

Also, Are the screenshots here including the other PR (the changes to the default themes)?

chbk commented 3 years ago

Yeah standardization is really needed to resolve ambiguities and missing scopes. Creating grammars and themes should be easy. You can check out this PR for a guide on scope conventions. It's still a work in progress but pretty stable now. The template is also a good starting point for creating/updating themes.

Regarding the screenshots, on the right are previews with all the PRs. On the left is the current state of Atom.

jeff-hykin commented 3 years ago

Haven't looked at the whole thing yet but,

name in entity.name

Adding name to entity is redundant as the latter is exclusively used to scope named elements.

Trailing scopes identifying the language are redundant as this information is provided by the root scope, e.g. source.java.

These are wonderful, exactly the same kind of changes I've been wanting to make. I'm looking forward to taking a deeper look into your proposed changes like having variable underneath entity.