Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: gleam: add highlighting for .gleam #3374

Closed
wants to merge 2 commits into from

Conversation

jthile
Copy link

@jthile jthile commented Jul 7, 2024

- symbol: "(\\.\\.|\\.|,|:)"
- symbol.brackets: "({|}|\\(|\\)|\\[|\\]|<<|>>)"
- symbol.operator: "(^\\s+\\|>|<>|<-|->|\\+\\.|\\-\\.|/\\.|\\*\\.|%\\.|\\+|\\-|/|\\*|%|=)"
- constant.number: "\\b(0[bBoOxX][a-fA-F0-9][a-fA-F0-9_\\.]*|[0-9][0-9_\\.]*)\\b"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made more precise: binary, octal, and hexadecimal literals can't have . in them, and Gleam also supports scientific notation for floats (https://tour.gleam.run/basics/number-formats/). Trailing underscore is not valid.

Suggested change
- constant.number: "\\b(0[bBoOxX][a-fA-F0-9][a-fA-F0-9_\\.]*|[0-9][0-9_\\.]*)\\b"
- constant.number: "\\b[0-9](_?[0-9]+)*(\\.([0-9](_?[0-9]+)*)?(e-?[0-9]+)?)?\\b"
- constant.number: "\\b0([bB][01](_?[01]+)*|[oO][0-7](_?[0-7]+)*|[xX][0-9A-Fa-f](_?[0-9A-Fa-f]+)*)\\b"

Comment on lines +17 to +21
# bit arrays
- special:
start: "<<"
end: ">>"
rules: []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add highlighting for the bit array options under rules here (https://tour.gleam.run/data-types/bit-arrays/)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually thought it might be clearer as is, making them look similar to strings, due to their special syntax. I took an initial stab, but a bug in the regex helps make a comparison. Here, I have no idea what makes :size(16) not match :(size|unit|bits|bytes|float|int|big|little|native|utf8|utf16|utf32|utf8_codepoint|utf16_codepoint|utf32_codepoint|signed|unsigned)(\\([0-9]+\\))?, but I actually think having the entire bit array highlighted similar to a string makes it read easier...

image

- symbol.brackets: "({|}|\\(|\\)|\\[|\\]|<<|>>)"
- symbol.operator: "(^\\s+\\|>|<>|<-|->|\\+\\.|\\-\\.|/\\.|\\*\\.|%\\.|\\+|\\-|/|\\*|%|=)"
- constant.number: "\\b(0[bBoOxX][a-fA-F0-9][a-fA-F0-9_\\.]*|[0-9][0-9_\\.]*)\\b"
- constant.bool: "\\s(<|>|<\\.|>\\.|<=|>=|<=\\.|>=\\.|==|!=|==\\.|&&|\\|\\||\\|)\\s"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are operators, not constants. There's also some overlap with symbol.operator defined above. I don't think the whitespace before and after is required because this seems to work:

io.debug(True&&False)

Copy link
Author

@jthile jthile Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, ideally there would be an symbol.operator.bool option to support the highlighting logic below, which I find helpful. I understand if it's too idiosyncratic for a default, though. Will fix the whitespace issue.

image
image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to remove the whitespace without clashing with the other operators... I'm afraid it's a bit brittle, but it does well with real code, at least using my color scheme.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, ideally there would be an symbol.operator.bool option to support the highlighting logic below, which I find helpful. I understand if it's too idiosyncratic for a default, though.

The way the highlighting works if you have symbol.operator.bool it will first look for symbol.operator.bool, then symbol.operator, and finally symbol in the current colorscheme. So that should just work, although you would need to use a customized colorscheme to see the difference.

I wasn't able to remove the whitespace without clashing with the other operators... I'm afraid it's a bit brittle, but it does well with real code, at least using my color scheme.

You can usually work around the clashes by reordering the definitions (if I recall correctly the later ones take precedence). Although it's not much of an issue in practice because almost all colorschemes use the same color for the two different kinds of operators anyway.


rules:
- identifier: "\\b[a-z][a-z0-9_]*\\b"
- ignore: "(\\b(_[a-z][a-z0-9_]*)\\b|\\b(auto|delegate|derive|echo|else|implement|macro|test)\\b)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these reserved but unimplemented keywords? I can't find a reference for them online.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the ignore line is meant to cover ignored (preceded by _) and reserved words. I found the latter here, under "Language additions", and also noticed a few existing Gleam syntax definitions included them.

@jthile jthile closed this Jul 10, 2024
@jthile jthile deleted the jthile-gleam branch July 10, 2024 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants