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

nix: major cleanup #4140

Merged
merged 11 commits into from
Oct 31, 2024
Merged

nix: major cleanup #4140

merged 11 commits into from
Oct 31, 2024

Conversation

h7x4
Copy link
Contributor

@h7x4 h7x4 commented Oct 13, 2024

Changes

This PR contains an overall cleanup of the nix language. It fixes some bugs with string handling, adds a bunch of new keywords that are now part of the language, and adds a bunch of extra support for different language constructs.

  • enh(nix) update keywords
  • enh(nix) support paths
  • enh(nix) support lookup paths
  • enh(nix) support operators
  • enh(nix) support URLs
  • enh(nix) support REPL keywords
  • enh(nix) support markdown comments
  • enh(nix) support basic function params
  • enh(nix) better parsing of attrsets
  • fix(nix) remove add builtin
  • fix(nix) mark or as builtin instead of literal
  • fix(nix) handle ''' string escapes
  • fix(nix) handle backslash string escapes
  • fix(nix) don't mix escapes for " and '' strings

Checklist

  • Added markup tests, or they don't apply here because...
  • Updated the changelog at CHANGES.md

@h7x4 h7x4 changed the title nix: update keywords nix: major cleanup Oct 13, 2024
Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +1.86 KB

View Changes
file base pr diff
es/core.min.js 8.18 KB 8.18 KB -1 B
es/highlight.min.js 8.18 KB 8.18 KB -1 B
es/languages/nix.min.js 523 B 1.45 KB +931 B
highlight.min.js 8.22 KB 8.22 KB -1 B
languages/nix.min.js 528 B 1.46 KB +928 B

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +1.82 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.44 KB +914 B
highlight.min.js 8.22 KB 8.22 KB -1 B
languages/nix.min.js 528 B 1.44 KB +909 B

src/languages/nix.js Outdated Show resolved Hide resolved
src/languages/nix.js Outdated Show resolved Hide resolved
src/languages/nix.js Outdated Show resolved Hide resolved
Comment on lines 285 to 289
match: /nix-repl> /
},
{
className: 'meta',
match: /:[a-z]+/,
Copy link
Member

Choose a reason for hiding this comment

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

Can we pin to the beginning of line here perhaps with ^... not sure, just a suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have pinned nix-repl> above with a ^. :[a-z]+ is meant to highlight commands used in the REPL after the prompt. There isn't anything else that can come after a colon in the nix language for now, so I'm not sure how much better we could make this one.

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +1.8 KB

View Changes
file base pr diff
es/core.min.js 8.18 KB 8.18 KB -3 B
es/highlight.min.js 8.18 KB 8.18 KB -3 B
es/languages/nix.min.js 523 B 1.43 KB +907 B
highlight.min.js 8.22 KB 8.22 KB -3 B
languages/nix.min.js 528 B 1.43 KB +902 B

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

2 files changed

Total change +1.97 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.51 KB +989 B
languages/nix.min.js 528 B 1.51 KB +984 B

@h7x4
Copy link
Contributor Author

h7x4 commented Oct 14, 2024

I have rewritten the test to test more language features, catching a few bugs in the process. Also added support for basic function parameters.

There are some more advanced function parameters as well (you can find an example at the top of the test), but I've not been able to properly parse those without breaking parser backtracking.

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +1.99 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.52 KB +999 B
highlight.min.js 8.22 KB 8.22 KB +1 B
languages/nix.min.js 528 B 1.52 KB +994 B

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

2 files changed

Total change +2.03 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.54 KB +1.01 KB
languages/nix.min.js 528 B 1.54 KB +1.01 KB

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

2 files changed

Total change +2.03 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.54 KB +1.02 KB
languages/nix.min.js 528 B 1.54 KB +1.01 KB

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +2.04 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.54 KB +1.02 KB
highlight.min.js 8.22 KB 8.22 KB +1 B
languages/nix.min.js 528 B 1.54 KB +1.01 KB

src/languages/nix.js Outdated Show resolved Hide resolved
src/languages/nix.js Outdated Show resolved Hide resolved
Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +2.27 KB

View Changes
file base pr diff
es/core.min.js 8.18 KB 8.18 KB +1 B
es/highlight.min.js 8.18 KB 8.18 KB +1 B
es/languages/nix.min.js 523 B 1.66 KB +1.14 KB
highlight.min.js 8.22 KB 8.22 KB +1 B
languages/nix.min.js 528 B 1.66 KB +1.13 KB

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +2.27 KB

View Changes
file base pr diff
es/core.min.js 8.18 KB 8.18 KB +1 B
es/highlight.min.js 8.18 KB 8.18 KB +1 B
es/languages/nix.min.js 523 B 1.66 KB +1.14 KB
highlight.min.js 8.22 KB 8.22 KB +1 B
languages/nix.min.js 528 B 1.66 KB +1.13 KB

@h7x4
Copy link
Contributor Author

h7x4 commented Oct 18, 2024

@joshgoebel I have gotten rid of all the lookbehinds by using beforeMatch and handling - on its own with various cases. There are still some edgecases not covered (in particular with identifiers like abc-123-def), but I think it's better than nothing :)

@h7x4 h7x4 requested a review from joshgoebel October 18, 2024 16:43

const URL = {
scope: 'link',
match: /https?:\/\/[A-Za-z0-9\-._~:/?#[\]@!$&'()*+,=]+/,
Copy link
Member

Choose a reason for hiding this comment

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

Does nix support ftp, ssh, or other protocols?

Copy link
Contributor Author

@h7x4 h7x4 Oct 20, 2024

Choose a reason for hiding this comment

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

Nix actually supports the entirety of rfc2396 (see last paragraph of https://nix.dev/manual/nix/2.24/language/syntax#string-literal), but I wasn't able to make direct use of the regex provided in the rfc because it kept picking up all sorts of false positives. The URLs are just shorthands for a string with the same content.

The feature is quite discouraged these days, and with no instances left that I'm aware of in nixpkgs (the largest and most important repository for the ecosystem). So I used a slightly suboptimal regex, hoping to catch some basic instances.

I could add a few more common protocols, but I'm not entirely sure where to stop.

Copy link
Member

Choose a reason for hiding this comment

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

If it's deprecated/discouraged perhaps lets drop it altogether so people don't complain it's not working in their own special use cases... What are they doing now instead, quoted strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed the commits related to it. It will pick instances up like a literal path, but maybe it's better to actively not support it (at least for time being, until someone comes up with a better regex). Quoted strings is the recommended replacement, yes.

Nix 2.24.9
Type :? for help.
nix-repl> https://nixos.org == "https://nixos.org"
true

Copy link

Choose a reason for hiding this comment

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

I think this is more than OK, as there are linters that will automatically fix it and both active lsp implementation warn and provide code actions for fixes:

image

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +2.27 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.66 KB +1.14 KB
highlight.min.js 8.22 KB 8.22 KB +1 B
languages/nix.min.js 528 B 1.66 KB +1.13 KB

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

3 files changed

Total change +2.18 KB

View Changes
file base pr diff
es/languages/nix.min.js 523 B 1.61 KB +1.09 KB
highlight.min.js 8.22 KB 8.22 KB +1 B
languages/nix.min.js 528 B 1.61 KB +1.08 KB

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +2.18 KB

View Changes
file base pr diff
es/core.min.js 8.18 KB 8.18 KB +2 B
es/highlight.min.js 8.18 KB 8.18 KB +2 B
es/languages/nix.min.js 523 B 1.61 KB +1.09 KB
highlight.min.js 8.22 KB 8.22 KB +2 B
languages/nix.min.js 528 B 1.61 KB +1.08 KB

- Add a bunch of new keywords
- List all `builtins` explicitly
- Sort alphabetically, to make it easier to spot missing builtins
- Remove `add`, not a real builtin
- Move `or` to `keywords`, this is not a `literal`
- The different string variants have different modes of escapes.
  Split `STRING.contains` into their different variants to reflect this.
- Add escape logic for `'''`
- Add escape logic for backslash escaped characters.
Also ensure comments are handled before any other expressions
Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +2.18 KB

View Changes
file base pr diff
es/core.min.js 8.18 KB 8.18 KB +1 B
es/highlight.min.js 8.18 KB 8.18 KB +1 B
es/languages/nix.min.js 523 B 1.61 KB +1.09 KB
highlight.min.js 8.22 KB 8.22 KB +1 B
languages/nix.min.js 528 B 1.61 KB +1.08 KB

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +2.17 KB

View Changes
file base pr diff
es/core.min.js 8.18 KB 8.18 KB -1 B
es/highlight.min.js 8.18 KB 8.18 KB -1 B
es/languages/nix.min.js 523 B 1.61 KB +1.09 KB
highlight.min.js 8.22 KB 8.22 KB -1 B
languages/nix.min.js 528 B 1.61 KB +1.08 KB

@joshgoebel joshgoebel merged commit 347c462 into highlightjs:main Oct 31, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
almost-there Getting close ot merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants