More accurate token group for the right side of the scoped expression #2

Merged
neochief merged 1 commit from main into main 2021-09-24 15:06:23 +02:00
neochief commented 2021-09-24 14:42:25 +02:00 (Migrated from github.com)

This is a suggestion to add a designated token group for the right side of the ScopedExpression.

The goal is to be able to distinguish between the left and right sides of a scoped expression when describing the styleTags rules. This is needed to colorize properties and method calls in both objects and class uniformly. For example, take this code:

$anObject->property
Class::property

If I want to colorize the "property", I may use the following rules in styleTags (https://github.com/codemirror/lang-php/blob/main/src/php.ts#L48):

styleTags({
  ...
  "MemberExpression/Name": t.propertyName,
  "ScopedExpression/Name": t.propertyName,
  "CallExpression/MemberExpression/Name": t.function(t.propertyName),
  "CallExpression/ScopedExpression/Name": t.function(t.propertyName),
  ...

The side effect of this solution is that the Name may be in both the left and right parts of the expression. And while in 99% of cases of MemberExpression this can be mitigated by styling VariableName (that is very likely inside the left part), there's no workaround for ScopedExpression. Most of ScopedExpressions are just ScopedExpression(Name, Name).

The proposed solution changes that to ScopedExpression(Name, ClassMemberName(Name)). This way you can describe styleTags as:

styleTags({
  ...
  "MemberExpression/Name": t.propertyName,
  "ScopedExpression/ClassMemberName/Name": t.propertyName,
  "CallExpression/MemberExpression/Name": t.function(t.propertyName),
  "CallExpression/ScopedExpression/ClassMemberName/Name": t.function(t.propertyName),
  ...

...and reach the desired effect.

If you think there's a better way, please suggest it and I'll send the updated code.

This is a suggestion to add a designated token group for the right side of the ScopedExpression. The goal is to be able to distinguish between the left and right sides of a scoped expression when describing the `styleTags` rules. This is needed to colorize properties and method calls in both objects and class uniformly. For example, take this code: ``` $anObject->property Class::property ``` If I want to colorize the "property", I may use the following rules in `styleTags` (https://github.com/codemirror/lang-php/blob/main/src/php.ts#L48): ``` styleTags({ ... "MemberExpression/Name": t.propertyName, "ScopedExpression/Name": t.propertyName, "CallExpression/MemberExpression/Name": t.function(t.propertyName), "CallExpression/ScopedExpression/Name": t.function(t.propertyName), ... ``` The side effect of this solution is that the `Name` may be in both the left and right parts of the expression. And while in 99% of cases of `MemberExpression` this can be mitigated by styling VariableName (that is very likely inside the left part), there's no workaround for ScopedExpression. Most of ScopedExpressions are just `ScopedExpression(Name, Name)`. The proposed solution changes that to `ScopedExpression(Name, ClassMemberName(Name))`. This way you can describe `styleTags` as: ``` styleTags({ ... "MemberExpression/Name": t.propertyName, "ScopedExpression/ClassMemberName/Name": t.propertyName, "CallExpression/MemberExpression/Name": t.function(t.propertyName), "CallExpression/ScopedExpression/ClassMemberName/Name": t.function(t.propertyName), ... ``` ...and reach the desired effect. If you think there's a better way, please suggest it and I'll send the updated code.
marijnh commented 2021-09-24 15:06:31 +02:00 (Migrated from github.com)

Seems like a good idea, thanks.

Seems like a good idea, thanks.
neochief commented 2021-09-24 15:16:49 +02:00 (Migrated from github.com)

Do you want me to send a pull request to update the lang-php styleTags accordingly?

Do you want me to send a pull request to update the lang-php `styleTags` accordingly?
marijnh commented 2021-09-24 15:42:58 +02:00 (Migrated from github.com)

That'd be wonderful, yes.

That'd be wonderful, yes.
neochief commented 2021-09-24 17:12:33 +02:00 (Migrated from github.com)
There you go: https://github.com/codemirror/lang-php/pull/1
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lezer/php!2
No description provided.