@scalar/highlight 0.3.1 → 0.5.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 (78) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +52 -13
  3. package/dist/all.d.ts.map +1 -1
  4. package/dist/all.js +2 -0
  5. package/dist/compat/languages.d.ts.map +1 -1
  6. package/dist/compat/languages.js +2 -4
  7. package/dist/compat/syntax-highlight.d.ts.map +1 -1
  8. package/dist/compat/syntax-highlight.js +101 -27
  9. package/dist/core/compile.d.ts +9 -1
  10. package/dist/core/compile.d.ts.map +1 -1
  11. package/dist/core/compile.js +168 -9
  12. package/dist/core/index.d.ts +1 -0
  13. package/dist/core/index.d.ts.map +1 -1
  14. package/dist/core/index.js +1 -0
  15. package/dist/core/tokenize.d.ts.map +1 -1
  16. package/dist/core/tokenize.js +147 -7
  17. package/dist/core/types.d.ts +76 -1
  18. package/dist/core/types.d.ts.map +1 -1
  19. package/dist/core/types.js +5 -1
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -0
  23. package/dist/langs/bash.d.ts.map +1 -1
  24. package/dist/langs/bash.js +60 -13
  25. package/dist/langs/cpp.d.ts.map +1 -1
  26. package/dist/langs/cpp.js +8 -0
  27. package/dist/langs/csharp.d.ts.map +1 -1
  28. package/dist/langs/csharp.js +11 -5
  29. package/dist/langs/css.d.ts.map +1 -1
  30. package/dist/langs/css.js +111 -9
  31. package/dist/langs/diff.d.ts +2 -1
  32. package/dist/langs/diff.d.ts.map +1 -1
  33. package/dist/langs/diff.js +19 -7
  34. package/dist/langs/go.d.ts.map +1 -1
  35. package/dist/langs/go.js +22 -1
  36. package/dist/langs/haskell.d.ts.map +1 -1
  37. package/dist/langs/haskell.js +33 -0
  38. package/dist/langs/html.d.ts +0 -10
  39. package/dist/langs/html.d.ts.map +1 -1
  40. package/dist/langs/html.js +63 -24
  41. package/dist/langs/http.d.ts.map +1 -1
  42. package/dist/langs/http.js +25 -9
  43. package/dist/langs/javascript.d.ts.map +1 -1
  44. package/dist/langs/javascript.js +279 -30
  45. package/dist/langs/json.d.ts.map +1 -1
  46. package/dist/langs/json.js +7 -1
  47. package/dist/langs/julia.d.ts +4 -0
  48. package/dist/langs/julia.d.ts.map +1 -0
  49. package/dist/langs/julia.js +181 -0
  50. package/dist/langs/kotlin.d.ts.map +1 -1
  51. package/dist/langs/kotlin.js +8 -1
  52. package/dist/langs/markdown.d.ts +9 -3
  53. package/dist/langs/markdown.d.ts.map +1 -1
  54. package/dist/langs/markdown.js +127 -6
  55. package/dist/langs/nginx.d.ts.map +1 -1
  56. package/dist/langs/nginx.js +26 -1
  57. package/dist/langs/ocaml.d.ts.map +1 -1
  58. package/dist/langs/ocaml.js +16 -7
  59. package/dist/langs/php.d.ts.map +1 -1
  60. package/dist/langs/php.js +69 -19
  61. package/dist/langs/python.d.ts.map +1 -1
  62. package/dist/langs/python.js +9 -2
  63. package/dist/langs/r.js +9 -9
  64. package/dist/langs/ruby.d.ts.map +1 -1
  65. package/dist/langs/ruby.js +8 -7
  66. package/dist/langs/rust.d.ts.map +1 -1
  67. package/dist/langs/rust.js +27 -11
  68. package/dist/langs/sql.d.ts.map +1 -1
  69. package/dist/langs/sql.js +9 -3
  70. package/dist/langs/swift.d.ts.map +1 -1
  71. package/dist/langs/swift.js +27 -1
  72. package/dist/langs/yaml.d.ts +0 -7
  73. package/dist/langs/yaml.d.ts.map +1 -1
  74. package/dist/langs/yaml.js +157 -28
  75. package/dist/lazy.d.ts +1 -0
  76. package/dist/lazy.d.ts.map +1 -1
  77. package/dist/lazy.js +2 -0
  78. package/package.json +2 -2
@@ -7,6 +7,12 @@
7
7
  * the way the shell does.
8
8
  */
9
9
  const VAR = '\\$(?:[A-Za-z_]\\w*|[0-9!@#?*$-])';
10
+ /**
11
+ * Where a command name can stand: at the start of a line that does not
12
+ * continue the one before it, after a prompt, after a pipe, list operator or
13
+ * subshell opener, and after the keywords and wrappers that run a command.
14
+ */
15
+ const BEFORE_COMMAND = '(?:^(?<!\\\\\\r?\\n)(?:[ \\t]*\\$(?=[ \\t]))?|[|&;(`{]|\\$\\(|\\b(?:then|do|else|elif|sudo|time|exec|xargs|nohup|env)\\b|!)';
10
16
  const bash = {
11
17
  name: 'bash',
12
18
  aliases: ['sh', 'shell', 'zsh', 'console'],
@@ -16,31 +22,58 @@ const bash = {
16
22
  { match: '^#!.*', scope: 'comment' },
17
23
  { match: '(?:^|[ \\t])#[^\\n]*', scope: 'comment' },
18
24
  { include: 'expansion' },
25
+ // An escaped character outside quotes is literal. It has to come
26
+ // before the quote rules: `'it'\\''s'` is how a single quote is written
27
+ // inside a single-quoted string — close, escaped quote, reopen — and
28
+ // reading that `\\'` as the start of a string inverts the rest of the
29
+ // line. snippetz writes it for every curl body with an apostrophe.
30
+ // A backslash before a newline is a line continuation, left unscoped.
31
+ { match: '\\\\[^\\r\\n]', scope: 'string.escape' },
19
32
  { match: '"', scope: 'string', push: 'string-double' },
20
33
  { match: "'", scope: 'string', push: 'string-single' },
21
34
  { match: "\\$'", scope: 'string', push: 'string-ansi' },
22
35
  { match: '`', scope: 'string', push: 'backtick' },
23
36
  // Heredocs: the body is scoped as a string up to a line holding only
24
- // the terminator. The terminator name is not tracked (that needs a
25
- // backreference), so an inner line equal to another terminator ends it.
37
+ // the remembered terminator. A quoted marker (`<<'EOF'`) turns
38
+ // expansion off, so `$HOME` in its body is text.
26
39
  {
27
- match: '(<<-?)(\\s*)([\'"]?)([A-Za-z_]\\w*)([\'"]?)',
40
+ match: '(<<-?)([ \\t]*)([\'"])([A-Za-z_]\\w*)([\'"])',
28
41
  scope: ['operator', null, 'string', 'string.special', 'string'],
42
+ push: 'heredoc-literal',
43
+ remember: 4,
44
+ },
45
+ {
46
+ match: '(<<-?)([ \\t]*)([A-Za-z_]\\w*)',
47
+ scope: ['operator', null, 'string.special'],
29
48
  push: 'heredoc',
49
+ remember: 3,
30
50
  },
31
51
  {
32
52
  match: '\\b(?:if|then|else|elif|fi|for|while|until|do|done|case|esac|select|break|continue|return|exit)\\b',
33
53
  scope: 'keyword.control',
34
54
  },
55
+ // The name a declaration sets gets a state of its own, so the argument
56
+ // rule below cannot read `API_KEY=…` in `export API_KEY=…` as a string.
35
57
  {
36
- match: '\\b(?:function|local|declare|typeset|readonly|export|alias|unset)\\b',
58
+ match: '\\b(?:local|declare|typeset|readonly|export)\\b',
37
59
  scope: 'keyword.declaration',
60
+ push: 'declared-name',
38
61
  },
62
+ { match: '\\b(?:function|alias|unset)\\b', scope: 'keyword.declaration' },
39
63
  { match: '\\b(?:in|then)\\b', scope: 'keyword.operator' },
40
64
  {
41
65
  match: '\\b(?:echo|printf|read|cd|pwd|source|eval|exec|test|trap|set|shift|wait|kill|jobs|getopts|command|type|hash|umask|ulimit)\\b',
42
66
  scope: 'function.builtin',
43
67
  },
68
+ // The command itself: `curl`, `./build.sh`, `/usr/bin/env`. Keywords
69
+ // and builtins start at the same position and come first, so they keep
70
+ // their own scopes. The lookbehind sits after the first character so
71
+ // the rule still starts with a literal class, which keeps the engine's
72
+ // first-character scan.
73
+ {
74
+ match: `[A-Za-z_./~](?<=${BEFORE_COMMAND}[ \\t]*[A-Za-z_./~])[\\w./~+-]{0,128}(?=[ \\t]|$|[;|&)\`])`,
75
+ scope: 'function.call',
76
+ },
44
77
  {
45
78
  match: '^([ \\t]*)([A-Za-z_]\\w*)(\\s*)(\\(\\))',
46
79
  scope: [null, 'function', null, 'punctuation.bracket'],
@@ -49,12 +82,24 @@ const bash = {
49
82
  // Flags are a large share of a shell line and benefit from their own color.
50
83
  { match: '(?:^|\\s)(--?[A-Za-z][\\w-]*)', scope: ['constant'] },
51
84
  { match: '\\b\\d+\\b', scope: 'number' },
85
+ // An unquoted argument is a string to the shell, and themes colour it
86
+ // like one. Command names, keywords and flags start at the same place
87
+ // and come first.
88
+ { match: '[^\\s$\'"`\\\\|&;()<>{}\\[\\]#=-][^\\s$\'"`\\\\|&;()<>{}]{0,256}', scope: 'string' },
52
89
  // `/` is left out: at the top level it is almost always a path
53
90
  // separator, and coloring every path segment boundary is noise.
54
91
  { match: '&&|\\|\\||[|&;]|[<>]{1,2}|[-+*%!=]=?|=~', scope: 'operator' },
55
92
  { match: '[()\\[\\]{}]', scope: 'punctuation.bracket' },
56
93
  ],
57
94
  },
95
+ /** After `export`, `local` and the like: flags, then the one name it sets. */
96
+ 'declared-name': {
97
+ rules: [
98
+ { match: '(?:^|\\s)(--?[A-Za-z][\\w-]*)', scope: ['constant'] },
99
+ { match: '[A-Za-z_]\\w*', scope: 'variable', pop: true },
100
+ { match: '(?=[^ \\t])', pop: true },
101
+ ],
102
+ },
58
103
  expansion: {
59
104
  rules: [
60
105
  { match: '\\$\\{', scope: 'variable', push: 'brace-expansion' },
@@ -84,7 +129,7 @@ const bash = {
84
129
  'string-double': {
85
130
  default: 'string',
86
131
  rules: [
87
- { match: '\\\\[\\\\$`"\\n]', scope: 'string.escape' },
132
+ { match: '\\\\(?:[\\\\$`"\\n]|\\r\\n)', scope: 'string.escape' },
88
133
  { include: 'expansion' },
89
134
  { match: '`', scope: 'string', push: 'backtick' },
90
135
  { match: '"', scope: 'string', pop: true },
@@ -109,14 +154,16 @@ const bash = {
109
154
  },
110
155
  heredoc: {
111
156
  default: 'string',
112
- rules: [
113
- {
114
- match: '^[ \\t]*[A-Za-z_]\\w*[ \\t]*$',
115
- scope: 'string.special',
116
- pop: true,
117
- },
118
- { include: 'expansion' },
119
- ],
157
+ rules: [{ include: 'heredoc-end' }, { include: 'expansion' }],
158
+ },
159
+ 'heredoc-literal': {
160
+ default: 'string',
161
+ rules: [{ include: 'heredoc-end' }],
162
+ },
163
+ 'heredoc-end': {
164
+ // `<<-` lets the terminator be indented with tabs; allowing any indent
165
+ // costs nothing and also covers bodies pasted with spaces.
166
+ rules: [{ match: '^[ \\t]*{{remembered}}[ \\t]*$', scope: 'string.special', pop: true }],
120
167
  },
121
168
  },
122
169
  };
@@ -1 +1 @@
1
- {"version":3,"file":"cpp.d.ts","sourceRoot":"","sources":["../../src/langs/cpp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAgG5C,QAAA,MAAM,GAAG,EAAE,OAwUV,CAAA;AAED,eAAe,GAAG,CAAA"}
1
+ {"version":3,"file":"cpp.d.ts","sourceRoot":"","sources":["../../src/langs/cpp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAgG5C,QAAA,MAAM,GAAG,EAAE,OAgVV,CAAA;AAED,eAAe,GAAG,CAAA"}
package/dist/langs/cpp.js CHANGED
@@ -263,6 +263,14 @@ const cpp = {
263
263
  // Before the constant and type rules, so a SCREAMING_CASE macro used as
264
264
  // `CLAMP(x, 0, 9)` reads as the call it expands to.
265
265
  { match: `\\b${ID}(?=[ \\t]*\\()`, scope: 'function.call' },
266
+ // A call with explicit template arguments: `any_cast<int>(x)`,
267
+ // `get<std::vector<int>>(t)`. One level of nesting covers real code and
268
+ // keeps the scan bounded; `std::vector<int> v(3)` has a name between
269
+ // the `>` and the `(`, so it stays a declaration.
270
+ {
271
+ match: `\\b${ID}(?=<[^<>;(){}\\n]{0,80}(?:<[^<>;(){}\\n]{0,80}>[^<>;(){}\\n]{0,40})?>[ \\t]*\\()`,
272
+ scope: 'function.call',
273
+ },
266
274
  // SCREAMING_CASE is a macro or an enumerator; `_t` and CapWords are the
267
275
  // two ways a C++ codebase spells a type.
268
276
  { match: '\\b[A-Z][A-Z0-9_]+\\b', scope: 'constant' },
@@ -1 +1 @@
1
- {"version":3,"file":"csharp.d.ts","sourceRoot":"","sources":["../../src/langs/csharp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAuD5C,QAAA,MAAM,MAAM,EAAE,OA4Rb,CAAA;AAED,eAAe,MAAM,CAAA"}
1
+ {"version":3,"file":"csharp.d.ts","sourceRoot":"","sources":["../../src/langs/csharp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAuD5C,QAAA,MAAM,MAAM,EAAE,OAkSb,CAAA;AAED,eAAe,MAAM,CAAA"}
@@ -192,12 +192,18 @@ const csharp = {
192
192
  // rather than as part of it. Longest opener first: `"""` before `"`.
193
193
  strings: {
194
194
  rules: [
195
+ // A raw literal opens with three or more quotes and closes with the
196
+ // same number, so it can hold a shorter run: `""""` wraps text that
197
+ // contains `"""`. The opening run is remembered for the close.
195
198
  {
196
- match: '(\\$)(""")',
199
+ // The lookbehind keeps the `$` run from being retried at each of its
200
+ // characters, which is O(n²) on a long line of `$`.
201
+ match: '(\\$(?<!\\$\\$)\\$*)("{3,})',
197
202
  scope: ['string.special', 'string'],
198
203
  push: 'raw-interpolated',
204
+ remember: 2,
199
205
  },
200
- { match: '"""', scope: 'string', push: 'raw' },
206
+ { match: '("{3,})', scope: 'string', push: 'raw', remember: 1 },
201
207
  {
202
208
  match: '(\\$@|@\\$)(")',
203
209
  scope: ['string.special', 'string'],
@@ -261,15 +267,15 @@ const csharp = {
261
267
  { match: '"', scope: 'string', pop: true },
262
268
  ],
263
269
  },
264
- // Raw literals have no escapes at all: the closing `"""` is the only thing
270
+ // Raw literals have no escapes at all: the closing run of quotes is the only thing
265
271
  // that ends them, which is what lets them hold quotes verbatim.
266
272
  raw: {
267
273
  default: 'string',
268
- rules: [{ match: '"""', scope: 'string', pop: true }],
274
+ rules: [{ match: '{{remembered}}', scope: 'string', pop: true }],
269
275
  },
270
276
  'raw-interpolated': {
271
277
  default: 'string',
272
- rules: [{ include: 'holes' }, { match: '"""', scope: 'string', pop: true }],
278
+ rules: [{ include: 'holes' }, { match: '{{remembered}}', scope: 'string', pop: true }],
273
279
  },
274
280
  /** The `{…}` holes shared by every interpolated flavour. */
275
281
  holes: {
@@ -1 +1 @@
1
- {"version":3,"file":"css.d.ts","sourceRoot":"","sources":["../../src/langs/css.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAW5C,QAAA,MAAM,GAAG,EAAE,OA0FV,CAAA;AAED,eAAe,GAAG,CAAA"}
1
+ {"version":3,"file":"css.d.ts","sourceRoot":"","sources":["../../src/langs/css.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAc5C,QAAA,MAAM,GAAG,EAAE,OAkMV,CAAA;AAED,eAAe,GAAG,CAAA"}
package/dist/langs/css.js CHANGED
@@ -6,6 +6,8 @@
6
6
  * as a property and `color` inside a selector cannot be confused.
7
7
  */
8
8
  const IDENT = '-{0,2}[A-Za-z_\\u0080-\\uFFFF][\\w-\\u0080-\\uFFFF]*';
9
+ /** At-rules followed by a condition rather than by a selector or a name. */
10
+ const AT_CONDITION = '@(?:media|supports|container|import)\\b';
9
11
  const css = {
10
12
  name: 'css',
11
13
  aliases: ['scss', 'less'],
@@ -14,6 +16,10 @@ const css = {
14
16
  rules: [
15
17
  { include: 'comments' },
16
18
  { include: 'strings' },
19
+ { match: AT_CONDITION, scope: 'keyword', push: 'condition' },
20
+ // An SCSS `$name:` or Less `@name:` definition takes a value, not a
21
+ // selector, so `red` in `$brand: red` is a color rather than a tag.
22
+ { include: 'variable-definition' },
17
23
  { match: '@[A-Za-z-]+', scope: 'keyword' },
18
24
  { match: `\\$${IDENT}`, scope: 'variable' },
19
25
  { match: '\\{', scope: 'punctuation.bracket', push: 'declarations' },
@@ -35,6 +41,8 @@ const css = {
35
41
  // A nested rule (SCSS, or a media query body) gets its own scope.
36
42
  { match: '\\{', scope: 'punctuation.bracket', push: 'declarations' },
37
43
  { match: ';', scope: 'punctuation.delimiter' },
44
+ { match: AT_CONDITION, scope: 'keyword', push: 'condition' },
45
+ { include: 'variable-definition' },
38
46
  { match: '@[A-Za-z-]+', scope: 'keyword' },
39
47
  {
40
48
  match: `(${IDENT})(\\s*)(:)`,
@@ -61,32 +69,126 @@ const css = {
61
69
  match: '#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\\b',
62
70
  scope: 'number',
63
71
  },
72
+ // A custom property reads the same where it is used as where it is
73
+ // defined, so `var(--brand)` and `--brand: …` share the property color.
64
74
  {
65
75
  match: 'var\\(\\s*(--[\\w-]+)',
66
- scope: ['variable'],
76
+ scope: ['property'],
67
77
  rest: 'function.builtin',
68
78
  },
69
- { match: '--[\\w-]+', scope: 'variable' },
79
+ { match: '--[\\w-]+', scope: 'property' },
80
+ // SCSS `$brand` and Less `@brand` used in a value.
81
+ { match: `[$@]${IDENT}`, scope: 'variable' },
82
+ // An unquoted `url(…)` is a string up to the `)`. Without its own state
83
+ // the `//` in `url(https://…)` reads as an SCSS line comment and takes
84
+ // the rest of the declaration with it.
85
+ { include: 'value-tokens' },
86
+ { match: '\\b[a-zA-Z-]+\\b', scope: 'constant' },
87
+ { match: '[(),/]', scope: 'punctuation' },
88
+ { match: ';', scope: 'punctuation.delimiter', pop: true },
89
+ ],
90
+ },
91
+ /**
92
+ * The condition of `@media`, `@supports`, `@container` or `@import`, up to
93
+ * the `{` or `;` that ends it: `screen and (min-width: 600px)`. Media
94
+ * features read as properties and their values as values, and the words
95
+ * between them as keywords.
96
+ */
97
+ condition: {
98
+ rules: [
99
+ { match: '(?=[{;}])', pop: true },
100
+ { include: 'comments' },
101
+ { include: 'strings' },
102
+ { match: '\\b(?:and|or|not|only)\\b', scope: 'keyword' },
103
+ {
104
+ match: `(${IDENT})(\\s*)(:)`,
105
+ scope: ['property', null, 'punctuation.delimiter'],
106
+ },
107
+ { include: 'value-tokens' },
108
+ { match: `\\$${IDENT}`, scope: 'variable' },
109
+ // A whole identifier, underscores included, so the feature rule above
110
+ // is not retried — and failed for want of a colon — at every position
111
+ // of a long name, which is O(n) each.
112
+ { match: IDENT, scope: 'constant' },
113
+ { match: '[(),/<>=]', scope: 'punctuation' },
114
+ ],
115
+ },
116
+ 'variable-definition': {
117
+ rules: [
118
+ // CSS permits `@page:first` without whitespace before the pseudo-class.
119
+ { match: '@page(?=:(?:first|left|right|blank)\\b)', scope: 'keyword' },
120
+ {
121
+ match: `(\\$${IDENT})(\\s*)(:)`,
122
+ scope: ['variable', null, 'punctuation.delimiter'],
123
+ push: 'value',
124
+ },
125
+ // No space before the colon for Less, so `@page :first` stays an at-rule.
126
+ {
127
+ match: `(@${IDENT})(:)`,
128
+ scope: ['variable', 'punctuation.delimiter'],
129
+ push: 'value',
130
+ },
131
+ ],
132
+ },
133
+ 'value-tokens': {
134
+ rules: [
135
+ {
136
+ match: '\\b(url)(\\()(?=[ \\t]*[^\\s"\'])',
137
+ scope: ['function.builtin', 'punctuation'],
138
+ push: 'url',
139
+ },
70
140
  { match: `\\b(${IDENT})(?=\\()`, scope: 'function.call' },
71
141
  {
72
142
  match: '(-?\\d*\\.?\\d+)((?:e[-+]?\\d+)?(?:%|[a-zA-Z]+)?)',
73
143
  scope: ['number', 'unit'],
74
144
  },
75
- { match: '\\b[a-zA-Z-]+\\b', scope: 'constant' },
76
- { match: '[(),/]', scope: 'punctuation' },
77
- { match: ';', scope: 'punctuation.delimiter', pop: true },
145
+ ],
146
+ },
147
+ url: {
148
+ default: 'string',
149
+ rules: [
150
+ { match: '\\\\.', scope: 'string.escape' },
151
+ { match: '\\)', scope: 'punctuation', pop: true },
152
+ { match: '(?=[;}\\n])', pop: true },
78
153
  ],
79
154
  },
80
155
  comments: {
81
156
  rules: [
82
- { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
83
- { match: '//[^\\n]*', scope: 'comment' },
157
+ // A state, not a lazy `[\\s\\S]*?` scan: that form runs to EOF and
158
+ // fails once per opener, which is O(n²) on unterminated `/*` runs.
159
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
160
+ // SCSS and Less line comments. Not after a `:`, which is a URL scheme.
161
+ { match: '(?<!:)//[^\\n]*', scope: 'comment' },
84
162
  ],
85
163
  },
164
+ 'block-comment': {
165
+ default: 'comment',
166
+ rules: [{ match: '\\*/', scope: 'comment', pop: true }],
167
+ },
168
+ // States rather than one pattern per literal: a pattern that alternates
169
+ // escapes with other characters rescans to the end of the line for every
170
+ // unterminated quote, which is O(n²) on a line of `\"`.
86
171
  strings: {
87
172
  rules: [
88
- { match: '"(?:[^"\\\\\\n]|\\\\.)*"', scope: 'string' },
89
- { match: "'(?:[^'\\\\\\n]|\\\\.)*'", scope: 'string' },
173
+ { match: '"', scope: 'string', push: 'string-double' },
174
+ { match: "'", scope: 'string', push: 'string-single' },
175
+ ],
176
+ },
177
+ 'string-double': {
178
+ default: 'string',
179
+ rules: [
180
+ // A backslash before a line break continues the string; `\\r\\n` is one.
181
+ { match: '\\\\(?:\\r\\n|[\\s\\S])', scope: 'string' },
182
+ { match: '"', scope: 'string', pop: true },
183
+ { match: '$', pop: true },
184
+ ],
185
+ },
186
+ 'string-single': {
187
+ default: 'string',
188
+ rules: [
189
+ { match: '\\\\(?:\\r\\n|[\\s\\S])', scope: 'string' },
190
+ { match: "'", scope: 'string', pop: true },
191
+ { match: '$', pop: true },
90
192
  ],
91
193
  },
92
194
  },
@@ -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,OAgGT,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,OAsHT,CAAA;AAED,eAAe,EAAE,CAAA"}
package/dist/langs/go.js CHANGED
@@ -6,7 +6,9 @@ const go = {
6
6
  root: {
7
7
  rules: [
8
8
  { match: '//[^\\n]*', scope: 'comment' },
9
- { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
9
+ // A state, not a lazy `[\\s\\S]*?` scan: that form runs to EOF and fails
10
+ // once per opener, which is O(n²) on a run of unterminated `/*`.
11
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
10
12
  { match: '`[^`]*`', scope: 'string' },
11
13
  { match: '"', scope: 'string', push: 'string' },
12
14
  {
@@ -45,6 +47,21 @@ const go = {
45
47
  match: '\\b(?:bool|byte|complex64|complex128|error|float32|float64|int|int8|int16|int32|int64|rune|string|uint|uint8|uint16|uint32|uint64|uintptr|any)\\b',
46
48
  scope: 'type.builtin',
47
49
  },
50
+ // A package-qualified type, `http.Request`, where a type goes: after a
51
+ // name (`w http.ResponseWriter`, `var c http.Client`), a pointer star
52
+ // that is not a multiplication (`*http.Request`, but not
53
+ // `2*time.Second`), a closing `]` or `)` (`[]http.Header`,
54
+ // `func() http.Handler`), a type assertion's `.(`, or in front of a
55
+ // composite literal's `{`. A keyword in front makes it a value instead:
56
+ // `return http.StatusOK`, `case http.MethodGet:`.
57
+ //
58
+ // The context is checked last, looking back from the end of the match,
59
+ // so the rule fails on the `.` test at nearly every identifier before
60
+ // it ever looks behind.
61
+ {
62
+ 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*))',
63
+ scope: [null, 'punctuation', 'type'],
64
+ },
48
65
  {
49
66
  match: '(\\.)([A-Za-z_]\\w*)(?=\\s*\\()',
50
67
  scope: ['punctuation', 'function.method'],
@@ -85,6 +102,10 @@ const go = {
85
102
  { match: '$', pop: true },
86
103
  ],
87
104
  },
105
+ 'block-comment': {
106
+ default: 'comment',
107
+ rules: [{ match: '\\*/', scope: 'comment', pop: true }],
108
+ },
88
109
  },
89
110
  };
90
111
  export default go;
@@ -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,14 +1,4 @@
1
1
  import type { Grammar } from '../core/types.js';
2
- /**
3
- * HTML, XML and SVG.
4
- *
5
- * Attribute names, attribute values and the tag name each get their own scope,
6
- * and character entities are pulled out of the text so `&amp;` reads as markup
7
- * rather than as content.
8
- *
9
- * `<script>` and `<style>` bodies are left unstyled: highlighting them means
10
- * running another grammar, which is a follow-up (see README).
11
- */
12
2
  declare const html: Grammar;
13
3
  export default html;
14
4
  //# sourceMappingURL=html.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../../src/langs/html.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;;;;GASG;AACH,QAAA,MAAM,IAAI,EAAE,OA+DX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../../src/langs/html.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAgB5C,QAAA,MAAM,IAAI,EAAE,OAsGX,CAAA;AAED,eAAe,IAAI,CAAA"}