Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chbk
Copy link

@chbk chbk commented Aug 5, 2020

Description of the Change

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

Benefits

  • Many new scopes added to make the grammar explicit and exhaustive.
  • Notable improvements on punctuation and types.
  • Highlighting to be consistent with other languages.

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
Copy link
Author

chbk commented Aug 5, 2020

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
Copy link
Author

chbk commented Aug 5, 2020

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
Copy link

jeff-hykin commented Aug 6, 2020

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
Copy link

jeff-hykin commented Aug 6, 2020

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

@chbk
Copy link
Author

chbk commented Aug 6, 2020

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
Copy link

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.

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

Successfully merging this pull request may close these issues.

2 participants