@scalar/highlight 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +26 -8
  3. package/dist/all.d.ts.map +1 -1
  4. package/dist/all.js +2 -0
  5. package/dist/compat/hljs.d.ts.map +1 -1
  6. package/dist/compat/hljs.js +1 -0
  7. package/dist/compat/languages.d.ts.map +1 -1
  8. package/dist/compat/languages.js +1 -4
  9. package/dist/core/compile.d.ts.map +1 -1
  10. package/dist/core/compile.js +50 -5
  11. package/dist/core/registry.d.ts.map +1 -1
  12. package/dist/core/registry.js +11 -4
  13. package/dist/core/scopes.d.ts +3 -1
  14. package/dist/core/scopes.d.ts.map +1 -1
  15. package/dist/core/scopes.js +3 -1
  16. package/dist/core/tokenize.d.ts.map +1 -1
  17. package/dist/core/tokenize.js +5 -1
  18. package/dist/core/types.d.ts +3 -2
  19. package/dist/core/types.d.ts.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/langs/cpp.d.ts.map +1 -1
  24. package/dist/langs/cpp.js +8 -0
  25. package/dist/langs/csharp.d.ts.map +1 -1
  26. package/dist/langs/csharp.js +16 -11
  27. package/dist/langs/css.d.ts.map +1 -1
  28. package/dist/langs/css.js +27 -2
  29. package/dist/langs/curl.d.ts +4 -0
  30. package/dist/langs/curl.d.ts.map +1 -0
  31. package/dist/langs/curl.js +60 -0
  32. package/dist/langs/diff.d.ts +2 -1
  33. package/dist/langs/diff.d.ts.map +1 -1
  34. package/dist/langs/diff.js +19 -7
  35. package/dist/langs/go.d.ts.map +1 -1
  36. package/dist/langs/go.js +23 -0
  37. package/dist/langs/haskell.d.ts.map +1 -1
  38. package/dist/langs/haskell.js +33 -0
  39. package/dist/langs/java.d.ts.map +1 -1
  40. package/dist/langs/java.js +17 -10
  41. package/dist/langs/javascript.d.ts.map +1 -1
  42. package/dist/langs/javascript.js +149 -4
  43. package/dist/langs/kotlin.d.ts.map +1 -1
  44. package/dist/langs/kotlin.js +8 -1
  45. package/dist/langs/nginx.d.ts.map +1 -1
  46. package/dist/langs/nginx.js +26 -1
  47. package/dist/langs/powershell.d.ts.map +1 -1
  48. package/dist/langs/powershell.js +4 -1
  49. package/dist/langs/python.d.ts.map +1 -1
  50. package/dist/langs/python.js +9 -2
  51. package/dist/langs/ruby.d.ts.map +1 -1
  52. package/dist/langs/ruby.js +8 -7
  53. package/dist/langs/rust.d.ts.map +1 -1
  54. package/dist/langs/rust.js +15 -1
  55. package/dist/langs/swift.d.ts.map +1 -1
  56. package/dist/langs/swift.js +27 -1
  57. package/dist/lazy.d.ts +1 -0
  58. package/dist/lazy.d.ts.map +1 -1
  59. package/dist/lazy.js +1 -1
  60. package/dist/style.css +1 -0
  61. package/package.json +6 -6
@@ -0,0 +1,60 @@
1
+ /**
2
+ * cURL command lines.
3
+ *
4
+ * A curl invocation is a shell command, but reading one is nothing like
5
+ * reading a shell script: what matters is the request method and which flag
6
+ * carries which value, not control flow. `@scalar/code-highlight` shipped a
7
+ * bespoke curl grammar for that reason and `code.css` styles
8
+ * `.hljs.language-curl` to match it, so this mirrors that scoping rather than
9
+ * deferring to bash — which leaves `curl` and the method word unscoped.
10
+ */
11
+ /** The methods `--request` and `-X` accept. Matched case-insensitively. */
12
+ const METHODS = 'get|post|put|patch|delete|head|options|trace|connect';
13
+ const curl = {
14
+ name: 'curl',
15
+ states: {
16
+ root: {
17
+ // curl itself does not care about the case of a method, and neither did
18
+ // the grammar this replaces.
19
+ ignoreCase: true,
20
+ rules: [
21
+ { match: '\\bcurl\\b', scope: 'keyword' },
22
+ // The method is the most useful token on the line, so it carries its
23
+ // own scope instead of blending into the flag that introduces it.
24
+ {
25
+ match: `(^|\\s)(--request|-X)(\\s+)(${METHODS})\\b`,
26
+ scope: [null, 'constant.builtin', null, 'symbol'],
27
+ },
28
+ // Anchored to a boundary so the hyphens inside a URL or a header name
29
+ // are not mistaken for flags.
30
+ { match: '(^|\\s)(--[A-Za-z][\\w-]*)', scope: [null, 'constant.builtin'] },
31
+ { match: '(^|\\s)(-[A-Za-z])(?![\\w-])', scope: [null, 'constant.builtin'] },
32
+ // Ahead of the opening-quote rule: an escaped quote is content, not a
33
+ // string boundary.
34
+ { match: '\\\\"', scope: 'string' },
35
+ { match: '"', scope: 'string', push: 'string-double' },
36
+ { match: "'", scope: 'string', push: 'string-single' },
37
+ { match: '\\b\\d+(?:\\.\\d+)?\\b', scope: 'number' },
38
+ ],
39
+ },
40
+ 'string-double': {
41
+ default: 'string',
42
+ rules: [
43
+ { match: '\\\\.', scope: 'string.escape' },
44
+ // `$(...)` runs a command, so it reads as a value rather than text.
45
+ { match: '\\$\\(', scope: 'variable', push: 'subshell' },
46
+ { match: '"', scope: 'string', pop: true },
47
+ ],
48
+ },
49
+ subshell: {
50
+ default: 'variable',
51
+ rules: [{ match: '\\)', scope: 'variable', pop: true }],
52
+ },
53
+ // Single quotes are literal in the shell, so nothing expands inside them.
54
+ 'string-single': {
55
+ default: 'string',
56
+ rules: [{ match: "'", scope: 'string', pop: true }],
57
+ },
58
+ },
59
+ };
60
+ export default curl;
@@ -1,6 +1,7 @@
1
1
  import type { Grammar } from '../core/types.js';
2
2
  /**
3
- * Unified diffs and `git diff` output.
3
+ * Unified diffs and `git diff` output, plus the older context (`diff -c`) and
4
+ * normal (`diff`) formats that still turn up in READMEs and mailing lists.
4
5
  *
5
6
  * Whole lines are scoped, which lets a theme give added and removed lines a
6
7
  * background tint rather than only recoloring the leading sign.
@@ -1 +1 @@
1
- {"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../src/langs/diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OAkBX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../src/langs/diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;GAMG;AACH,QAAA,MAAM,IAAI,EAAE,OA+BX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Unified diffs and `git diff` output.
2
+ * Unified diffs and `git diff` output, plus the older context (`diff -c`) and
3
+ * normal (`diff`) formats that still turn up in READMEs and mailing lists.
3
4
  *
4
5
  * Whole lines are scoped, which lets a theme give added and removed lines a
5
6
  * background tint rather than only recoloring the leading sign.
@@ -10,15 +11,26 @@ const diff = {
10
11
  states: {
11
12
  root: {
12
13
  rules: [
14
+ // Few alternatives on purpose: diffs are large and simple, so each
15
+ // extra one is a measurable share of the time spent on them.
16
+ //
17
+ // Metadata: git's extended headers, svn's `Index:` and the `====` rule
18
+ // under it, `diff -r` notes, `#` lines in a patch being edited by
19
+ // `git add -p`, and ``.
13
20
  {
14
- match: '^(?:diff|index|new file|deleted file|old mode|new mode|similarity index|rename) [^\\n]*',
21
+ match: '^(?:(?:diff|index|new file|deleted file|old mode|new mode|similarity index|rename|Index:|Only in|Binary files) |={3,}|#|\\\\)[^\\n]*',
15
22
  scope: 'comment',
16
23
  },
17
- { match: '^(?:---|\\+\\+\\+)[^\\n]*', scope: 'heading' },
18
- { match: '^@@[^\\n]*', scope: 'keyword' },
19
- { match: '^\\+[^\\n]*', scope: 'diff.plus' },
20
- { match: '^-[^\\n]*', scope: 'diff.minus' },
21
- { match: '^\\\\[^\\n]*', scope: 'comment' },
24
+ // Hunk markers: unified `@@`, context `***************`, `*** 1,5 ****`
25
+ // and `--- 1,6 ----`, and a normal-format command such as `12,14c12`.
26
+ // Ahead of the file headers, which also start with `***` and `---`.
27
+ {
28
+ match: '^(?:@@|\\*{15}|\\*{3} \\d+(?:,\\d+)? \\*{4}|-{3} \\d+(?:,\\d+)? -{4}|\\d+(?:,\\d+)?[acd]\\d+(?:,\\d+)?$)[^\\n]*',
29
+ scope: 'keyword',
30
+ },
31
+ { match: '^(?:---|\\+\\+\\+|\\*\\*\\*)[^\\n]*', scope: 'heading' },
32
+ { match: '^[+>][^\\n]*', scope: 'diff.plus' },
33
+ { match: '^[-<][^\\n]*', scope: 'diff.minus' },
22
34
  ],
23
35
  },
24
36
  },
@@ -1 +1 @@
1
- {"version":3,"file":"go.d.ts","sourceRoot":"","sources":["../../src/langs/go.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C,UAAU;AACV,QAAA,MAAM,EAAE,EAAE,OAsGT,CAAA;AAED,eAAe,EAAE,CAAA"}
1
+ {"version":3,"file":"go.d.ts","sourceRoot":"","sources":["../../src/langs/go.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C,UAAU;AACV,QAAA,MAAM,EAAE,EAAE,OA8HT,CAAA;AAED,eAAe,EAAE,CAAA"}
package/dist/langs/go.js CHANGED
@@ -32,6 +32,10 @@ const go = {
32
32
  match: '(package)(\\s+)([A-Za-z_]\\w*)',
33
33
  scope: ['keyword.declaration', null, 'namespace'],
34
34
  },
35
+ {
36
+ match: '\\b(var)(\\s+)([A-Za-z_]\\w*)',
37
+ scope: ['keyword.declaration', null, 'variable'],
38
+ },
35
39
  {
36
40
  match: '\\b(?:func|var|const|type|struct|interface|map|chan|package)\\b',
37
41
  scope: 'keyword.declaration',
@@ -47,6 +51,21 @@ const go = {
47
51
  match: '\\b(?:bool|byte|complex64|complex128|error|float32|float64|int|int8|int16|int32|int64|rune|string|uint|uint8|uint16|uint32|uint64|uintptr|any)\\b',
48
52
  scope: 'type.builtin',
49
53
  },
54
+ // A package-qualified type, `http.Request`, where a type goes: after a
55
+ // name (`w http.ResponseWriter`, `var c http.Client`), a pointer star
56
+ // that is not a multiplication (`*http.Request`, but not
57
+ // `2*time.Second`), a closing `]` or `)` (`[]http.Header`,
58
+ // `func() http.Handler`), a type assertion's `.(`, or in front of a
59
+ // composite literal's `{`. A keyword in front makes it a value instead:
60
+ // `return http.StatusOK`, `case http.MethodGet:`.
61
+ //
62
+ // The context is checked last, looking back from the end of the match,
63
+ // so the rule fails on the `.` test at nearly every identifier before
64
+ // it ever looks behind.
65
+ {
66
+ match: '\\b([a-z]\\w*)(\\.)([A-Z]\\w*)\\b(?![ \\t]*\\()(?:(?=\\{)|(?<=(?:(?<![\\w)\\]])\\*|\\]|\\.\\(|\\)[ \\t]+|\\b(?!(?:return|case|go|defer|if|else|for|range|switch|select|goto|break|continue)\\b)[A-Za-z_]\\w*[ \\t]+)[a-z]\\w*\\.[A-Z]\\w*))',
67
+ scope: [null, 'punctuation', 'type'],
68
+ },
50
69
  {
51
70
  match: '(\\.)([A-Za-z_]\\w*)(?=\\s*\\()',
52
71
  scope: ['punctuation', 'function.method'],
@@ -55,6 +74,10 @@ const go = {
55
74
  match: '(\\.)([A-Za-z_]\\w*)',
56
75
  scope: ['punctuation', 'variable.member'],
57
76
  },
77
+ // The names a short declaration binds: `res, err := …`, `for i, v :=`.
78
+ // Each name looks ahead over the rest of the list to the `:=`; the list
79
+ // is capped at eight names so a long argument list stays linear.
80
+ { match: '\\b[A-Za-z_]\\w*\\b(?=(?:\\s*,\\s*[A-Za-z_]\\w*){0,7}\\s*:=)', scope: 'variable' },
58
81
  { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
59
82
  { match: '\\b[A-Z][A-Za-z0-9_]*\\b', scope: 'type' },
60
83
  { match: '\\b[a-z_]\\w*(?=\\s*\\()', scope: 'function.call' },
@@ -1 +1 @@
1
- {"version":3,"file":"haskell.d.ts","sourceRoot":"","sources":["../../src/langs/haskell.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAwF5C,QAAA,MAAM,OAAO,EAAE,OA2Td,CAAA;AAED,eAAe,OAAO,CAAA"}
1
+ {"version":3,"file":"haskell.d.ts","sourceRoot":"","sources":["../../src/langs/haskell.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAwF5C,QAAA,MAAM,OAAO,EAAE,OA6Vd,CAAA;AAED,eAAe,OAAO,CAAA"}
@@ -96,6 +96,7 @@ const haskell = {
96
96
  {
97
97
  match: `${START}(import)([ \\t]+)(?:(qualified)([ \\t]+))?(${MODULE})`,
98
98
  scope: ['keyword.import', null, 'keyword', null, 'namespace'],
99
+ push: 'import-tail',
99
100
  },
100
101
  // `as` is a keyword only in an import, and `as` is a common name for a
101
102
  // list elsewhere — requiring a module name after it is what keeps
@@ -296,6 +297,38 @@ const haskell = {
296
297
  { include: 'operators' },
297
298
  ],
298
299
  },
300
+ /**
301
+ * What follows the module name of an import: `qualified`, `as M`, `hiding`
302
+ * and the list of names. Only import syntax keeps this state open, so the
303
+ * next declaration can start at any indentation, including on the same line.
304
+ */
305
+ 'import-tail': {
306
+ rules: [
307
+ { match: '\\s+' },
308
+ { include: 'comments' },
309
+ {
310
+ match: `${START}(as)([ \\t]+)(${MODULE})`,
311
+ scope: ['keyword', null, 'namespace'],
312
+ },
313
+ { match: `${START}(?:hiding|qualified)${END}`, scope: 'keyword' },
314
+ { match: '\\(', scope: 'punctuation.bracket', set: 'import-names' },
315
+ { match: '(?=\\S)', pop: true },
316
+ ],
317
+ },
318
+ /** `(unfoldr, Map, Maybe(..), (<|>))`: functions, types and operators. */
319
+ 'import-names': {
320
+ rules: [
321
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
322
+ { match: '\\(', scope: 'punctuation.bracket', push: 'import-names' },
323
+ { include: 'comments' },
324
+ { match: `${START}(?:type|pattern)${END}`, scope: 'keyword' },
325
+ { match: UID, scope: 'type' },
326
+ { match: LID, scope: 'function' },
327
+ { match: ',', scope: 'punctuation.delimiter' },
328
+ { match: '\\.\\.', scope: 'punctuation' },
329
+ { match: `[${SYM}]+`, scope: 'operator' },
330
+ ],
331
+ },
299
332
  /** A module export list: types and names, ending at the header's `where`. */
300
333
  exports: {
301
334
  rules: [
@@ -1 +1 @@
1
- {"version":3,"file":"java.d.ts","sourceRoot":"","sources":["../../src/langs/java.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA0C5C,QAAA,MAAM,IAAI,EAAE,OA6JX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"java.d.ts","sourceRoot":"","sources":["../../src/langs/java.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA0C5C,QAAA,MAAM,IAAI,EAAE,OAoKX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -100,18 +100,25 @@ const java = {
100
100
  },
101
101
  // A declaration is a name that follows a type, which in Java always
102
102
  // ends in an identifier character, a `>` or a `]`: `int run(`,
103
- // `Money priceOf(`, `List<T> items(`, `String[] main(`. The exclusions
104
- // are the tokens that end the same way but introduce a *call*: `->` for
105
- // a lambda body, and the keywords that can sit directly before one.
103
+ // `Money priceOf(`, `List<T> items(`, `String[] main(`. Followed by
104
+ // `(` it names a method; followed by `=`, `;` or a for-each `:` it
105
+ // names a variable: `Request request = …`, `int count;`,
106
+ // `for (Item item :`. One rule with two endings, because a second rule
107
+ // with the same lookbehinds costs a pass at every position.
106
108
  //
107
- // The reading it gets wrong: a `>` comparison against a call looks
108
- // exactly like a generic return type, so `if (a[i] > f(x))` and
109
- // `if (list.size() > max(a, b))` scope the call as a definition. A
110
- // subscript is safe — `if (xs[i]) run();` has a `)` before the space,
111
- // which the lookbehind class does not accept.
109
+ // The exclusions are the tokens that end the same way but come before
110
+ // a call or a bare value: `->` for a lambda body, the keywords that can
111
+ // sit directly before one (`return total;`, `case READY:`), and a `>`
112
+ // with a space before it, which is a comparison rather than the end of
113
+ // `List<String>`, so `if (a > f(x))` and `t = a > b;` stay as they are.
114
+ //
115
+ // The reading it still gets wrong: a comparison spaced on one side only,
116
+ // `if (a> f(x))`, scopes the call as a definition. A subscript is safe —
117
+ // `if (xs[i]) run();` has a `)` before the space, which the lookbehind
118
+ // class does not accept.
112
119
  {
113
- match: `(?<=[\\w$>\\]])(?<!->)(?<!\\b(?:return|new|throw|else|yield|assert|do))([ \\t]+)(${ID})(?=[ \\t]*\\()`,
114
- scope: [null, 'function'],
120
+ match: `(?<=[\\w$>\\]])(?<![ \\t]>)(?<!->)(?<!\\b(?:return|new|throw|else|yield|assert|do|case|break|continue))([ \\t]+)(?:(${ID})(?=[ \\t]*\\()|(${ID})(?=[ \\t]*(?:=(?!=)|;|:(?!:))))`,
121
+ scope: [null, 'function', 'variable'],
115
122
  },
116
123
  // SCREAMING_CASE reads as a constant — which also covers enum members —
117
124
  // and CapWords as a type. Between them they cover classes, interfaces
@@ -1 +1 @@
1
- {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/langs/javascript.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAkF5C,QAAA,MAAM,UAAU,EAAE,OAkSjB,CAAA;AAED,eAAe,UAAU,CAAA"}
1
+ {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/langs/javascript.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA0F5C,QAAA,MAAM,UAAU,EAAE,OA6ajB,CAAA;AAED,eAAe,UAAU,CAAA"}
@@ -59,12 +59,19 @@ const NOT_TS_AFTER = '(?!>[ \\t]*\\()(?!\\s+(?:extends\\b|=))(?!>(?![ \\t]*$)(?!
59
59
  const NOT_KEY = '(?!\\??:)';
60
60
  /** A JSX tag name: a component, a member expression, or a lowercase element. */
61
61
  const TAG = '[A-Z][\\w.]*|[a-z][\\w-]*(?:[.:][\\w-]+)*';
62
+ /**
63
+ * What follows the `<` of a type argument list that leads into a call:
64
+ * `number>(` in `useState<number>(0)`. Stops at an assignment or an unmatched
65
+ * bracket; balanced function and object types are allowed, but the scan cannot
66
+ * escape into a function body.
67
+ */
68
+ const GENERIC_CALL_REST = '(?:[^;=(){}\\n]|=>|\\([^(){}\\n]{0,128}\\)|\\{[^{}()\\n]{0,128}\\}){1,512}>[ \\t]*\\(';
62
69
  /**
63
70
  * What follows a name that is assigned a function: `= () =>`, `= async x =>`,
64
- * `= function`. The parameter list cannot nest parentheses, which covers real
65
- * code and keeps the scan bounded.
71
+ * `= function`, and the generic `= <T,>(x: T) =>`. The parameter list cannot
72
+ * nest parentheses, which covers real code and keeps the scan bounded.
66
73
  */
67
- const ASSIGNS_FUNCTION = `[ \\t]*=[ \\t]*(?:async[ \\t]*)?(?:function\\b|\\([^()]{0,512}\\)[ \\t]*(?::[^=;\\n]{0,256})?=>|${ID}[ \\t]*=>)`;
74
+ const ASSIGNS_FUNCTION = `[ \\t]*=[ \\t]*(?:async[ \\t]*)?(?:function\\b|(?:<[^<>()=]{0,128}>[ \\t]*)?\\([^()]{0,512}\\)[ \\t]*(?::[^=;\\n]{0,256})?=>|${ID}[ \\t]*=>)`;
68
75
  /** Body of a regex literal: no unescaped `/`, character classes may hold one. */
69
76
  const REGEX = '/(?![/*])(?:[^/\\\\\\n\\[]|\\\\.|\\[(?:[^\\]\\\\\\n]|\\\\.)*\\])+/[dgimsuvy]*';
70
77
  const javascript = {
@@ -139,6 +146,22 @@ const javascript = {
139
146
  // rule here: a rule that has to look back at `const` runs at every
140
147
  // identifier in the file, while the state only runs after the keyword.
141
148
  { match: '\\b(?:const|let|var)\\b', scope: 'keyword.declaration', push: 'declared-name' },
149
+ // A parameter list reads its names as parameters: after `function` and
150
+ // its optional name, or before the `=>` of an arrow function. The arrow
151
+ // lookahead cannot see past nested parentheses, so a list holding a
152
+ // call in a default value or a function type keeps reading as plain
153
+ // code. Quotes and slashes also force that fallback: a string, comment
154
+ // or regex can contain `) =>` without introducing an arrow function.
155
+ {
156
+ match: `(\\bfunction\\b)([ \\t]*\\*?[ \\t]*)(?:(${ID})([ \\t]*))?(\\()`,
157
+ scope: ['keyword.declaration', null, 'function', null, 'punctuation.bracket'],
158
+ push: 'params',
159
+ },
160
+ {
161
+ match: '\\((?=[^()"\'`/]{0,512}\\)[ \\t]*(?::[^=;\\n"\'`/]{0,256})?=>)',
162
+ scope: 'punctuation.bracket',
163
+ push: 'params',
164
+ },
142
165
  {
143
166
  match: `\\b(?:function|class|interface|enum|namespace|module|declare|abstract|implements|extends|constructor|static|readonly|override|public|private|protected|async|get(?!\\()|set(?!\\()|type(?!\\())\\b${NOT_KEY}`,
144
167
  scope: 'keyword.declaration',
@@ -147,6 +170,8 @@ const javascript = {
147
170
  match: `\\b(?:import|export|from(?!\\()|require)\\b${NOT_KEY}`,
148
171
  scope: 'keyword.import',
149
172
  },
173
+ // Import attributes: `import data from './x.json' with { type: 'json' }`.
174
+ { match: '\\bwith(?=[ \\t]*\\{)', scope: 'keyword.import' },
150
175
  {
151
176
  match: `\\b(?:typeof|instanceof|in|of(?!\\()|new|delete(?!\\()|void|satisfies|keyof|infer|asserts|as)\\b${NOT_KEY}`,
152
177
  scope: 'keyword.operator',
@@ -177,7 +202,12 @@ const javascript = {
177
202
  // Requiring no space before the colon keeps `cond ? a : b` out.
178
203
  { match: `\\b${ID}(?=\\??:)`, scope: 'property' },
179
204
  { match: `\\b${ID}(?=\\s*=>)`, scope: 'variable.parameter' },
180
- { match: `\\b${ID}(?=[ \\t]*[(<]|\`)`, scope: 'function.call' },
205
+ { match: `\\b${ID}(?=[ \\t]*\\(|\`)`, scope: 'function.call' },
206
+ // A generic call such as `useState<number>(0)`. The argument list has to
207
+ // close and be followed by `(`, so a comparison like `count <= max` is
208
+ // not mistaken for a call. The body mirrors the generic-call scan in
209
+ // `nesting`, which bounds it so a long line cannot make it quadratic.
210
+ { match: `\\b${ID}(?=[ \\t]*<${GENERIC_CALL_REST})`, scope: 'function.call' },
181
211
  {
182
212
  // Bare `=` goes last so `=>`, `==`, `===` and the compound forms all
183
213
  // get first refusal. Without it plain assignment matched nothing.
@@ -200,6 +230,121 @@ const javascript = {
200
230
  { match: '(?=\\S)', pop: true },
201
231
  ],
202
232
  },
233
+ // ---- parameter lists -----------------------------------------------------
234
+ /** After the `(` of a parameter list, up to the `)` that closes it. */
235
+ params: {
236
+ rules: [{ match: '\\)', scope: 'punctuation.bracket', pop: true }, { include: 'param-pattern' }],
237
+ },
238
+ /**
239
+ * What a parameter can be: a name, a destructuring pattern, a rest
240
+ * element, and after it a TypeScript annotation or a default value.
241
+ */
242
+ 'param-pattern': {
243
+ rules: [
244
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
245
+ { match: '//[^\\n]*', scope: 'comment' },
246
+ { match: ':', scope: 'punctuation.delimiter', push: 'param-type' },
247
+ // Leave the operator for expression rules that need it to recognize a regex.
248
+ { match: '(?==(?!>))', push: 'param-default' },
249
+ { match: '\\{', scope: 'punctuation.bracket', push: 'param-object' },
250
+ { match: '\\[', scope: 'punctuation.bracket', push: 'param-array' },
251
+ { match: '\\.{3}|\\?', scope: 'operator' },
252
+ { match: '\\bthis\\b', scope: 'variable.builtin' },
253
+ // A constructor's parameter properties: `constructor(private x: X)`.
254
+ {
255
+ match: '\\b(?:public|private|protected|readonly|override)\\b(?=[ \\t]+[\\w$\\[{])',
256
+ scope: 'keyword.declaration',
257
+ },
258
+ { match: `@${ID}`, scope: 'decorator' },
259
+ { match: ID, scope: 'variable.parameter' },
260
+ { match: ',', scope: 'punctuation.delimiter' },
261
+ ],
262
+ },
263
+ /**
264
+ * `{ a, b: renamed, c = 1, ...rest }`: keys are not bindings. A computed
265
+ * key uses expression rules, unlike the bindings inside an array pattern.
266
+ */
267
+ 'param-object': {
268
+ rules: [
269
+ { match: '\\}', scope: 'punctuation.bracket', pop: true },
270
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
271
+ // Inside an object pattern a colon renames a key, never annotates a type.
272
+ { match: ':', scope: 'punctuation.delimiter' },
273
+ { include: 'numbers' },
274
+ // Quoted keys rename a binding; their colon does not introduce a type.
275
+ {
276
+ match: `("(?:[^"\\\\\\r\\n]|\\\\.)*"|'(?:[^'\\\\\\r\\n]|\\\\.)*')([ \\t]*)(:)`,
277
+ scope: ['string', null, 'punctuation.delimiter'],
278
+ },
279
+ {
280
+ match: `(${ID})([ \\t]*)(:)`,
281
+ scope: ['property', null, 'punctuation.delimiter'],
282
+ },
283
+ { include: 'param-pattern' },
284
+ ],
285
+ },
286
+ /** Array destructuring binds its elements, including nested patterns. */
287
+ 'param-array': {
288
+ rules: [{ match: '\\]', scope: 'punctuation.bracket', pop: true }, { include: 'param-pattern' }],
289
+ },
290
+ /**
291
+ * A parameter's type, up to the `,`, `)` or `=` that ends it. Brackets
292
+ * nest so the commas in `Map<K, V>` and `{ a: A, b: B }` stay inside.
293
+ */
294
+ 'param-type': {
295
+ rules: [
296
+ { match: '(?=[,)}\\]]|=(?!>))', pop: true },
297
+ { match: '<', scope: 'punctuation.bracket', push: 'type-angle' },
298
+ { include: 'nesting' },
299
+ { include: 'expression' },
300
+ ],
301
+ },
302
+ 'type-angle': {
303
+ rules: [
304
+ { match: '=>', scope: 'operator' },
305
+ { match: '>', scope: 'punctuation.bracket', pop: true },
306
+ { match: '<', scope: 'punctuation.bracket', push: 'type-angle' },
307
+ { include: 'nesting' },
308
+ { include: 'expression' },
309
+ ],
310
+ },
311
+ /** A default value, up to the `,` or closing bracket that ends it. */
312
+ 'param-default': {
313
+ rules: [{ match: '(?=[,)}\\]])', pop: true }, { include: 'nesting' }, { include: 'expression' }],
314
+ },
315
+ /** Bracket depth, so a comma inside a nested call or literal stays nested. */
316
+ nesting: {
317
+ rules: [
318
+ // Consume the regex with its opener, then track the bracket around it.
319
+ { match: `(\\()(\\s*)(${REGEX})`, scope: ['punctuation.bracket', null, 'regexp'], push: 'paren' },
320
+ { match: `(\\[)(\\s*)(${REGEX})`, scope: ['punctuation.bracket', null, 'regexp'], push: 'bracket' },
321
+ // Generic calls nest commas too. Require a following call and stop at an
322
+ // assignment or an unmatched closing parenthesis or brace. Balanced function/object
323
+ // types are allowed, but the scan cannot escape into the function body.
324
+ {
325
+ match: `<(?=${GENERIC_CALL_REST})`,
326
+ scope: 'punctuation.bracket',
327
+ push: 'type-angle',
328
+ },
329
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
330
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
331
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
332
+ ],
333
+ },
334
+ paren: {
335
+ rules: [
336
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
337
+ { include: 'nesting' },
338
+ { include: 'expression' },
339
+ ],
340
+ },
341
+ bracket: {
342
+ rules: [
343
+ { match: '\\]', scope: 'punctuation.bracket', pop: true },
344
+ { include: 'nesting' },
345
+ { include: 'expression' },
346
+ ],
347
+ },
203
348
  numbers: {
204
349
  rules: [
205
350
  { match: '\\b0[xX](?:_?[0-9a-fA-F])+n?\\b', scope: 'number' },
@@ -1 +1 @@
1
- {"version":3,"file":"kotlin.d.ts","sourceRoot":"","sources":["../../src/langs/kotlin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiC5C,QAAA,MAAM,MAAM,EAAE,OAiNb,CAAA;AAED,eAAe,MAAM,CAAA"}
1
+ {"version":3,"file":"kotlin.d.ts","sourceRoot":"","sources":["../../src/langs/kotlin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiC5C,QAAA,MAAM,MAAM,EAAE,OAwNb,CAAA;AAED,eAAe,MAAM,CAAA"}
@@ -87,9 +87,16 @@ const kotlin = {
87
87
  scope: 'keyword.control',
88
88
  },
89
89
  {
90
- match: '\\b(?:fun|val|var|class|interface|object|typealias|const(?:ructor)?|init|companion|enum|data|sealed|annotation|inner|open|override|abstract|final|private|protected|public|internal|lateinit|(?:no|cross)?inline|reified|vararg|suspend|operator|infix|actual|expect|by|where|get|set|out)\\b',
90
+ match: '\\b(?:fun|val|var|class|interface|object|typealias|const(?:ructor)?|init|companion|enum|data|sealed|annotation|inner|open|override|abstract|final|private|protected|public|internal|lateinit|(?:no|cross)?inline|reified|vararg|suspend|operator|infix|actual|expect|by|where|get|set|out|tailrec|external)\\b',
91
91
  scope: 'keyword.declaration',
92
92
  },
93
+ // `value` is a modifier only in front of `class`; anywhere else it is
94
+ // one of the most common names there is.
95
+ { match: '\\bvalue(?=\\s+class\\b)', scope: 'keyword.declaration' },
96
+ {
97
+ match: '\\b(package|import)(\\s+)([A-Za-z_]\\w*(?:\\.[A-Za-z_]\\w*)*(?:\\.\\*)?)',
98
+ scope: ['keyword.import', null, 'namespace'],
99
+ },
93
100
  { match: '\\b(?:package|import)\\b', scope: 'keyword.import' },
94
101
  // `as?` cannot use a trailing `\b`: the boundary after `?` needs a word
95
102
  // character next, and a cast is followed by a space.
@@ -1 +1 @@
1
- {"version":3,"file":"nginx.d.ts","sourceRoot":"","sources":["../../src/langs/nginx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6E5C,QAAA,MAAM,KAAK,EAAE,OA4GZ,CAAA;AAED,eAAe,KAAK,CAAA"}
1
+ {"version":3,"file":"nginx.d.ts","sourceRoot":"","sources":["../../src/langs/nginx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6E5C,QAAA,MAAM,KAAK,EAAE,OAuIZ,CAAA;AAED,eAAe,KAAK,CAAA"}
@@ -82,7 +82,14 @@ const nginx = {
82
82
  scope: ['property'],
83
83
  push: 'regex',
84
84
  },
85
- { match: `${STATEMENT}(${NAME})${OPENS_BLOCK}`, scope: ['keyword'] },
85
+ // A map defines its second argument; the first is an expression that
86
+ // keeps ordinary variable and string highlighting.
87
+ { match: `${STATEMENT}(map)\\b${OPENS_BLOCK}`, scope: ['keyword'], push: 'map-arguments' },
88
+ // An upstream defines its first argument, used by `proxy_pass http://name`.
89
+ {
90
+ match: `${STATEMENT}(?:(upstream)([ \\t]+)([$\\w.-]+)|(${NAME}))${OPENS_BLOCK}`,
91
+ scope: ['keyword', null, 'type', 'keyword'],
92
+ },
86
93
  { match: `${STATEMENT}(${NAME})`, scope: ['property'] },
87
94
  // Quotes get a state because nginx allows a value to run across lines
88
95
  // and expands `$variables` inside single quotes as well as double.
@@ -97,6 +104,16 @@ const nginx = {
97
104
  // `-F` of a header name like `X-Forwarded-For` out of this.
98
105
  { match: '\\B-!?[fdex]\\b', scope: 'keyword.operator' },
99
106
  { match: '\\b(?:on|off)\\b', scope: 'boolean' },
107
+ // The few argument values nginx defines itself — log levels, event
108
+ // methods, the default log format — and the flags that end a rewrite.
109
+ // Bounded on both sides by anything that can continue a path, so the
110
+ // `error` of `logs/error.log` stays part of the path. One rule for
111
+ // both, since every alternative costs on files this regular, and the
112
+ // check on what comes before runs last, once a word has matched.
113
+ {
114
+ match: '\\b(?:(debug|info|notice|warn|error|crit|alert|emerg|main|combined|epoll|kqueue|eventport|devpoll)(?![\\w/.-])|(last|break|redirect|permanent)(?=[ \\t]*;))(?<![/.$-][a-z]*)',
115
+ scope: ['constant.builtin', 'keyword'],
116
+ },
100
117
  // Paths and URLs are most of what a config actually says, so they are
101
118
  // worth a colour. The scheme is capped rather than open-ended: an
102
119
  // unbounded scan for `://` would be retried at every dotted segment of
@@ -120,6 +137,14 @@ const nginx = {
120
137
  block: {
121
138
  rules: [{ match: '\\}', scope: 'punctuation.bracket', pop: true }, { include: 'root' }],
122
139
  },
140
+ /** Scope the map destination without consuming or recoloring its source. */
141
+ 'map-arguments': {
142
+ rules: [
143
+ { match: '\\$\\w+(?=[ \\t]*\\{)', scope: 'type', pop: true },
144
+ { match: '(?=[;{}\\n])', pop: true },
145
+ { include: 'root' },
146
+ ],
147
+ },
123
148
  variables: {
124
149
  rules: [
125
150
  { match: '\\$\\{[A-Za-z_]\\w*\\}', scope: 'variable' },
@@ -1 +1 @@
1
- {"version":3,"file":"powershell.d.ts","sourceRoot":"","sources":["../../src/langs/powershell.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA8D5C,QAAA,MAAM,UAAU,EAAE,OAoRjB,CAAA;AAED,eAAe,UAAU,CAAA"}
1
+ {"version":3,"file":"powershell.d.ts","sourceRoot":"","sources":["../../src/langs/powershell.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA8D5C,QAAA,MAAM,UAAU,EAAE,OAuRjB,CAAA;AAED,eAAe,UAAU,CAAA"}
@@ -163,7 +163,10 @@ const powershell = {
163
163
  // to be a lookbehind rather than a consumed separator, because the
164
164
  // merged regex picks the leftmost match before it picks the
165
165
  // higher-priority rule — eating the space would make `-eq` a parameter.
166
- { match: '(?<![\\w)\\]}\'"])-[a-z]\\w*', scope: 'variable.parameter' },
166
+ // Scoped as a constant, as Bash scopes a flag, so `-Uri` and `--request`
167
+ // look alike; the parameter scope is kept for the names a script
168
+ // declares in `param(…)`.
169
+ { match: '(?<![\\w)\\]}\'"])-[a-z]\\w*', scope: 'constant' },
167
170
  { match: `\\b${ID}(?=\\()`, scope: 'function.call' },
168
171
  // ---- numbers -------------------------------------------------------
169
172
  {
@@ -1 +1 @@
1
- {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../src/langs/python.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6B5C,QAAA,MAAM,MAAM,EAAE,OAwWb,CAAA;AAED,eAAe,MAAM,CAAA"}
1
+ {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../src/langs/python.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6B5C,QAAA,MAAM,MAAM,EAAE,OA+Wb,CAAA;AAED,eAAe,MAAM,CAAA"}
@@ -63,9 +63,16 @@ const python = {
63
63
  },
64
64
  { match: '\\b(?:import|from)\\b', scope: 'keyword.import' },
65
65
  // `match`/`case` are soft keywords: only treat them as control flow in
66
- // the statement shape that introduces a block.
66
+ // the statement shape that introduces a block. A `case` may put its
67
+ // body on the same line (`case 200: return r`). Exclude an assignment
68
+ // (plain, annotated or augmented) immediately after `case`, but allow
69
+ // `=` in class patterns and guards.
67
70
  {
68
- match: '^([ \\t]*)(match|case)\\b(?=[^\\n]*:[ \\t]*(?:#[^\\n]*)?$)',
71
+ match: '^([ \\t]*)(match)\\b(?=[^\\n]*:[ \\t]*(?:#[^\\n]*)?$)',
72
+ scope: [null, 'keyword.control'],
73
+ },
74
+ {
75
+ match: '^([ \\t]*)(case)\\b(?![ \\t]*(?::|(?:\\*\\*|//|<<|>>|[-+*/%@&|^])?=))(?=[ \\t]+[^\\n]*:)',
69
76
  scope: [null, 'keyword.control'],
70
77
  },
71
78
  ],
@@ -1 +1 @@
1
- {"version":3,"file":"ruby.d.ts","sourceRoot":"","sources":["../../src/langs/ruby.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6D5C,QAAA,MAAM,IAAI,EAAE,OAkVX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"ruby.d.ts","sourceRoot":"","sources":["../../src/langs/ruby.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA6D5C,QAAA,MAAM,IAAI,EAAE,OAmVX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -192,24 +192,25 @@ const ruby = {
192
192
  scope: 'variable.special',
193
193
  },
194
194
  {
195
- match: '\\b(?:puts|pp|print|raise|fail|abort|loop|lambda|proc|format|sprintf|gets|exit|freeze|attr_accessor|attr_reader|attr_writer|define_method|block_given\\?)(?![\\w?!])',
195
+ match: '\\b(?:puts|pp|print|printf|putc|warn|raise|fail|abort|loop|lambda|proc|format|sprintf|gets|readlines?|sleep|rand|srand|exit|at_exit|freeze|attr_accessor|attr_reader|attr_writer|define_method|block_given\\?)(?![\\w?!])',
196
196
  scope: 'function.builtin',
197
197
  },
198
+ // `p x` prints, but `p` is also a common variable name, so it counts
199
+ // only when an argument follows it.
200
+ { match: '\\bp(?=\\(|[ \\t]+[\\w"\':\\[@$])', scope: 'function.builtin' },
198
201
  // A method with a block is still a call, so `.each do |x|` and
199
202
  // `.map { }` count alongside `.push(x)`.
200
203
  {
201
204
  match: `(&?\\.)([ \\t]*)(${ID}[?!]?)(?=[ \\t]*(?:\\(|\\{|do\\b))`,
202
205
  scope: ['punctuation', null, 'function.method'],
203
206
  },
204
- // A `?` or `!` suffix only ever ends a method name.
207
+ // Ruby has no public fields: `.length`, `.to_s` and `.name = x` all
208
+ // call a method, parentheses or not. A `?` or `!` suffix only ever ends
209
+ // a method name.
205
210
  {
206
- match: `(&?\\.)([ \\t]*)(${ID}[?!])`,
211
+ match: `(&?\\.)([ \\t]*)(${ID}[?!]?)`,
207
212
  scope: ['punctuation', null, 'function.method'],
208
213
  },
209
- {
210
- match: `(&?\\.)([ \\t]*)(${ID})`,
211
- scope: ['punctuation', null, 'variable.member'],
212
- },
213
214
  { match: '\\b[A-Z]\\w*(?=::)', scope: 'namespace' },
214
215
  // SCREAMING_CASE reads as a constant, CapWords as a type — between them
215
216
  // that covers module constants, classes and the exception hierarchy
@@ -1 +1 @@
1
- {"version":3,"file":"rust.d.ts","sourceRoot":"","sources":["../../src/langs/rust.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OA+HX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"rust.d.ts","sourceRoot":"","sources":["../../src/langs/rust.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OA6IX,CAAA;AAED,eAAe,IAAI,CAAA"}