@scalar/highlight 0.4.0 → 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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @scalar/highlight
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#10668](https://github.com/scalar/scalar/pull/10668): feat: close more gaps with TextMate grammars, and highlight faster
8
+ - Engine: only the wrapper group around each rule captures in a state's merged regex. Past 63 capture groups V8 allocates on every match, which cost large grammars such as C, C++, C#, PHP, Ruby and Python a third of their throughput or more.
9
+ - CSS: `var(--brand)` has the same scope as the `--brand:` it refers to. SCSS `$name` and Less `@name` read whole where they are used, and `$brand: red` at the top level reads as a value rather than a selector.
10
+ - JavaScript/TypeScript: the names in a parameter list read as parameters, destructuring and annotations included; `function f()` names a definition; `with { type: 'json' }` on an import is a keyword.
11
+ - Swift: `#/…/#` and `/…/` regex literals, and the `nonisolated`, `isolated`, `distributed`, `borrowing`, `consuming`, `sending` and `macro` modifiers.
12
+ - Diff: context (`diff -c`) and normal (`diff`) formats, svn `Index:` headers, and `#` comment lines.
13
+ - Ruby: a method called without parentheses, `list.length`, reads as a call, and `p`, `printf`, `warn` and friends as builtins.
14
+ - Go: a package-qualified type, `w http.ResponseWriter` or `*testing.T`, reads as a type where a type goes.
15
+ - nginx: log levels, event methods, rewrite flags, and the names of `upstream` and `map` blocks.
16
+ - Haskell: the names in an import list. Kotlin: `package` and `import` paths, and `tailrec`, `external` and `value class`. C++: calls with explicit template arguments, `any_cast<int>(x)`.
17
+ - Rust: `.await` is a keyword, `self` the receiver and `Self` a type, and `r#type` stays a name. Python: `case 200: return x` with its body on the same line is a keyword. TypeScript: `const id = <T,>(x: T) => x` names a function.
18
+
3
19
  ## 0.4.0
4
20
 
5
21
  ### Minor Changes
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Scalar's code highlighter.
4
4
 
5
- - **Small.** 3.3 KB core, ~1.7 KB for Python, gzipped. Nothing you do not import.
5
+ - **Small.** 3.5 KB core, ~1.7 KB for Python, gzipped. Nothing you do not import.
6
6
  - **No theme system.** Colors come from the `--scalar-*` variables `@scalar/themes` already defines, so a code block follows the app's theme and switches with `.light-mode` / `.dark-mode` — without being re-highlighted.
7
7
  - **Zero runtime dependencies.**
8
8
 
@@ -43,7 +43,7 @@ highlight(source, 'rust');
43
43
  ```
44
44
 
45
45
  Every entry in the loader is a static `import()`, so bundlers split each language into its
46
- own chunk. The initial chunk with the loader and no languages is 4.9 KB.
46
+ own chunk. The initial chunk with the loader and no languages is 5.0 KB.
47
47
 
48
48
  ### Entry points
49
49
 
@@ -83,12 +83,12 @@ re-exporting it, so tree-shaking runs the way it does in a real app.
83
83
 
84
84
  | Bundle | Gzipped |
85
85
  | --- | ---: |
86
- | `@scalar/highlight` core | 3.32 KB |
87
- | …+ Python | 5.25 KB |
88
- | …lazy loader, initial chunk | 4.88 KB |
86
+ | `@scalar/highlight` core | 3.50 KB |
87
+ | …+ Python | 5.44 KB |
88
+ | …lazy loader, initial chunk | 5.05 KB |
89
89
  | …`style.css` | 1.26 KB |
90
90
 
91
- Per language, on top of the core: `diff` 0.14 KB · `json` 0.29 KB · `html` 0.53 KB · `nginx` 0.54 KB · `http` 0.67 KB · `ini` 0.70 KB · `graphql` 0.72 KB · `css` 0.73 KB · `go` 0.80 KB · `yaml` 0.82 KB · `rust` 0.85 KB · `makefile` 0.90 KB · `markdown` 0.91 KB · `dockerfile` 0.94 KB · `bash` 1.03 KB · `sql` 1.10 KB · `java` 1.15 KB · `dart` 1.17 KB · `lua` 1.19 KB · `r` 1.28 KB · `swift` 1.29 KB · `c` 1.31 KB · `clojure` 1.31 KB · `kotlin` 1.33 KB · `julia` 1.36 KB · `scala` 1.36 KB · `powershell` 1.37 KB · `elixir` 1.38 KB · `haskell` 1.41 KB · `matlab` 1.43 KB · `objectivec` 1.43 KB · `ocaml` 1.44 KB · `csharp` 1.52 KB · `mojo` 1.54 KB · `fsharp` 1.68 KB · `python` 1.69 KB · `ruby` 1.70 KB · `cpp` 1.75 KB · `javascript` 1.78 KB · `perl` 1.86 KB · `php` 1.91 KB.
91
+ Per language, on top of the core: `diff` 0.21 KB · `json` 0.29 KB · `html` 0.53 KB · `http` 0.67 KB · `nginx` 0.69 KB · `ini` 0.70 KB · `graphql` 0.73 KB · `css` 0.79 KB · `yaml` 0.82 KB · `rust` 0.88 KB · `go` 0.90 KB · `makefile` 0.90 KB · `markdown` 0.91 KB · `dockerfile` 0.95 KB · `bash` 1.03 KB · `sql` 1.11 KB · `java` 1.15 KB · `dart` 1.17 KB · `lua` 1.19 KB · `r` 1.28 KB · `c` 1.31 KB · `clojure` 1.32 KB · `scala` 1.36 KB · `julia` 1.37 KB · `elixir` 1.38 KB · `kotlin` 1.38 KB · `powershell` 1.38 KB · `matlab` 1.43 KB · `objectivec` 1.43 KB · `swift` 1.44 KB · `ocaml` 1.45 KB · `haskell` 1.50 KB · `csharp` 1.52 KB · `mojo` 1.54 KB · `fsharp` 1.68 KB · `python` 1.71 KB · `ruby` 1.74 KB · `cpp` 1.78 KB · `perl` 1.87 KB · `php` 2.01 KB · `javascript` 2.11 KB.
92
92
 
93
93
  Reproduce with `pnpm size`.
94
94
 
@@ -297,7 +297,7 @@ For the same three languages `ScalarCodeBlock` shows most often:
297
297
  | | gzipped |
298
298
  | --- | ---: |
299
299
  | `@scalar/code-highlight` | 127.58 KB |
300
- | `@scalar/highlight/compat` | 7.18 KB |
300
+ | `@scalar/highlight/compat` | 7.67 KB |
301
301
 
302
302
  ## Trade-offs
303
303
 
@@ -1 +1 @@
1
- {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/core/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAgB,aAAa,EAAE,OAAO,EAAmB,MAAM,SAAS,CAAA;AAuOrG;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,aAAa,EAAE,OAAO,MAAM,KAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAA;CAmBxG,CAAA;AAED,eAAO,MAAM,OAAO,GAAI,SAAS,OAAO,KAAG,eAmC1C,CAAA"}
1
+ {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/core/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAgB,aAAa,EAAE,OAAO,EAAmB,MAAM,SAAS,CAAA;AA+QrG;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,aAAa,EAAE,OAAO,MAAM,KAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAA;CAmBxG,CAAA;AAED,eAAO,MAAM,OAAO,GAAI,SAAS,OAAO,KAAG,eAmC1C,CAAA"}
@@ -8,7 +8,8 @@ import { REMEMBERED } from './types.js';
8
8
  * is skipped inside the regex engine rather than by a JS loop.
9
9
  *
10
10
  * Each rule is wrapped in its own group so that, after a match, we can tell
11
- * which alternative won by finding the first defined wrapper group.
11
+ * which alternative won by finding the first defined wrapper group. That
12
+ * wrapper is the only group that captures; see `uncapture`.
12
13
  */
13
14
  const BACKREF = /\\(?:[1-9]|k<)/;
14
15
  /** Counts capture groups in a pattern, ignoring escapes and character classes. */
@@ -47,6 +48,50 @@ const countGroups = (src) => {
47
48
  }
48
49
  return count;
49
50
  };
51
+ /**
52
+ * Turns every capture group in a pattern into a non-capturing one.
53
+ *
54
+ * Only the wrapper group around each rule has to capture in the merged regex,
55
+ * to say which rule won. A rule's own groups are read back from its sticky
56
+ * `own` pattern, so in the merged regex they would only cost: V8 keeps the
57
+ * match offsets of up to 63 groups in a preallocated buffer and allocates a
58
+ * fresh one on every `exec` past that, which is a fifth of the throughput of
59
+ * a large state such as JavaScript's.
60
+ */
61
+ const uncapture = (src) => {
62
+ let out = '';
63
+ let inClass = false;
64
+ for (let i = 0; i < src.length; i++) {
65
+ const c = src[i];
66
+ if (c === '\\') {
67
+ out += c + (src[i + 1] ?? '');
68
+ i++;
69
+ }
70
+ else if (inClass) {
71
+ inClass = c !== ']';
72
+ out += c;
73
+ }
74
+ else if (c === '[') {
75
+ inClass = true;
76
+ out += c;
77
+ }
78
+ else if (c !== '(') {
79
+ out += c;
80
+ }
81
+ else if (src[i + 1] !== '?') {
82
+ out += '(?:';
83
+ }
84
+ else if (src[i + 2] === '<' && src[i + 3] !== '=' && src[i + 3] !== '!' && src.includes('>', i)) {
85
+ // `(?<name>` — drop the name along with the capture.
86
+ i = src.indexOf('>', i);
87
+ out += '(?:';
88
+ }
89
+ else {
90
+ out += c;
91
+ }
92
+ }
93
+ return out;
94
+ };
50
95
  const patternSource = (match) => {
51
96
  return typeof match === 'string' ? match : match.source;
52
97
  };
@@ -163,7 +208,7 @@ const compileState = (grammar, name) => {
163
208
  captures,
164
209
  // Sticky, so it can be re-run at the position the merged regex chose.
165
210
  // A pattern that depends on the remembered text is built per value.
166
- own: captures && !usesMemory ? new RegExp(src, `ymd${caseFlag}`) : null,
211
+ own: (captures || rule.remember) && !usesMemory ? new RegExp(src, `ymd${caseFlag}`) : null,
167
212
  rest: rule.rest ?? null,
168
213
  group,
169
214
  push: rule.push ?? null,
@@ -172,9 +217,9 @@ const compileState = (grammar, name) => {
172
217
  remember: rule.remember ?? 0,
173
218
  rememberIndent: rule.rememberAs === 'indent',
174
219
  });
175
- ownSources.push(captures && usesMemory ? src : null);
176
- sources.push(`(${src})`);
177
- group += 1 + inner;
220
+ ownSources.push((captures || rule.remember) && usesMemory ? src : null);
221
+ sources.push(`(${uncapture(src)})`);
222
+ group += 1;
178
223
  }
179
224
  // `m` so grammars can anchor to lines. Deliberately no `d`: offsets are
180
225
  // resolved per rule instead, which keeps the scan free of that cost.
@@ -1 +1 @@
1
- {"version":3,"file":"tokenize.d.ts","sourceRoot":"","sources":["../../src/core/tokenize.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAA+B,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAGxF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,MAAM,IAAI,KAAG,IA4G7E,CAAA;AAgLD;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,KAAG,KAAK,EAM7E,CAAA"}
1
+ {"version":3,"file":"tokenize.d.ts","sourceRoot":"","sources":["../../src/core/tokenize.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAA+B,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAGxF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,MAAM,IAAI,KAAG,IAgH7E,CAAA;AAgLD;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,KAAG,KAAK,EAM7E,CAAA"}
@@ -73,7 +73,11 @@ export const tokenize = (code, grammar, emit) => {
73
73
  // the state that entered it unless the rule remembers something new.
74
74
  let remembered = memory[memory.length - 1];
75
75
  if (rule.remember > 0) {
76
- const text = m[rule.group + rule.remember] ?? '';
76
+ // The merged regex captures only which rule won, so the group comes from
77
+ // the rule's own pattern, re-run where the merged one matched.
78
+ const own = dyn?.own[index] ?? rule.own;
79
+ own.lastIndex = start;
80
+ const text = own.exec(code)?.[rule.remember] ?? '';
77
81
  remembered = rule.rememberIndent ? ' '.repeat(text.length) : text;
78
82
  }
79
83
  if (rule.pop > 0) {
@@ -122,7 +122,8 @@ export type CompiledRule = {
122
122
  captures: (ScopeName | null)[] | null;
123
123
  /**
124
124
  * This rule alone, sticky and with `hasIndices`, used to locate its capture
125
- * groups after the merged regex has already picked it.
125
+ * groups — and to read the one it remembers — after the merged regex has
126
+ * already picked it. The merged regex does not capture them at all.
126
127
  *
127
128
  * Asking the merged regex for indices would make every match in the state
128
129
  * pay to have offsets computed for every group of every rule; re-running one
@@ -135,7 +136,7 @@ export type CompiledRule = {
135
136
  push: string | null;
136
137
  set: string | null;
137
138
  pop: number;
138
- /** Capture group to remember, as an offset from `group`; `0` for none. */
139
+ /** Capture group of `own` to remember, 1-based; `0` for none. */
139
140
  remember: number;
140
141
  rememberIndent: boolean;
141
142
  };
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,4DAA4D;AAC5D,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B,MAAM,MAAM,IAAI,GAAG;IACjB;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;IAEtB;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,SAAS,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,EAAE,CAAA;IAExC,kEAAkE;IAClE,IAAI,CAAC,EAAE,SAAS,CAAA;IAEhB,mCAAmC;IACnC,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,gEAAgE;IAChE,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,uCAAuC;IACvC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAEnB;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IAEjB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,QAAQ,CAAA;CAC/B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,UAAU,mBAAmB,CAAA;AAE1C,iFAAiF;AACjF,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,OAAO,CAAA;AAEtC,MAAM,MAAM,KAAK,GAAG;IAClB,2EAA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,MAAM,GAAG,IAAI,CAAC,EAAE,CAAA;SAAE,CAAC,CAAA;KAC3D,GAAG,IAAI,CAAA;IACR,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,GAAG,IAAI,CAAA;CAC3E,CAAA;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IACrC,IAAI,EAAE,aAAa,CAAA;CACpB,CAAA"}
@@ -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":"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,OAwKV,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
@@ -17,6 +17,9 @@ const css = {
17
17
  { include: 'comments' },
18
18
  { include: 'strings' },
19
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' },
20
23
  { match: '@[A-Za-z-]+', scope: 'keyword' },
21
24
  { match: `\\$${IDENT}`, scope: 'variable' },
22
25
  { match: '\\{', scope: 'punctuation.bracket', push: 'declarations' },
@@ -39,6 +42,7 @@ const css = {
39
42
  { match: '\\{', scope: 'punctuation.bracket', push: 'declarations' },
40
43
  { match: ';', scope: 'punctuation.delimiter' },
41
44
  { match: AT_CONDITION, scope: 'keyword', push: 'condition' },
45
+ { include: 'variable-definition' },
42
46
  { match: '@[A-Za-z-]+', scope: 'keyword' },
43
47
  {
44
48
  match: `(${IDENT})(\\s*)(:)`,
@@ -65,12 +69,16 @@ const css = {
65
69
  match: '#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\\b',
66
70
  scope: 'number',
67
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.
68
74
  {
69
75
  match: 'var\\(\\s*(--[\\w-]+)',
70
- scope: ['variable'],
76
+ scope: ['property'],
71
77
  rest: 'function.builtin',
72
78
  },
73
- { match: '--[\\w-]+', scope: 'variable' },
79
+ { match: '--[\\w-]+', scope: 'property' },
80
+ // SCSS `$brand` and Less `@brand` used in a value.
81
+ { match: `[$@]${IDENT}`, scope: 'variable' },
74
82
  // An unquoted `url(…)` is a string up to the `)`. Without its own state
75
83
  // the `//` in `url(https://…)` reads as an SCSS line comment and takes
76
84
  // the rest of the declaration with it.
@@ -105,6 +113,23 @@ const css = {
105
113
  { match: '[(),/<>=]', scope: 'punctuation' },
106
114
  ],
107
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
+ },
108
133
  'value-tokens': {
109
134
  rules: [
110
135
  {
@@ -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,OAsHT,CAAA;AAED,eAAe,EAAE,CAAA"}
package/dist/langs/go.js CHANGED
@@ -47,6 +47,21 @@ const go = {
47
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',
48
48
  scope: 'type.builtin',
49
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
+ },
50
65
  {
51
66
  match: '(\\.)([A-Za-z_]\\w*)(?=\\s*\\()',
52
67
  scope: ['punctuation', 'function.method'],
@@ -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":"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;AAkF5C,QAAA,MAAM,UAAU,EAAE,OAwajB,CAAA;AAED,eAAe,UAAU,CAAA"}
@@ -61,10 +61,10 @@ const NOT_KEY = '(?!\\??:)';
61
61
  const TAG = '[A-Z][\\w.]*|[a-z][\\w-]*(?:[.:][\\w-]+)*';
62
62
  /**
63
63
  * 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.
64
+ * `= function`, and the generic `= <T,>(x: T) =>`. The parameter list cannot
65
+ * nest parentheses, which covers real code and keeps the scan bounded.
66
66
  */
67
- const ASSIGNS_FUNCTION = `[ \\t]*=[ \\t]*(?:async[ \\t]*)?(?:function\\b|\\([^()]{0,512}\\)[ \\t]*(?::[^=;\\n]{0,256})?=>|${ID}[ \\t]*=>)`;
67
+ const ASSIGNS_FUNCTION = `[ \\t]*=[ \\t]*(?:async[ \\t]*)?(?:function\\b|(?:<[^<>()=]{0,128}>[ \\t]*)?\\([^()]{0,512}\\)[ \\t]*(?::[^=;\\n]{0,256})?=>|${ID}[ \\t]*=>)`;
68
68
  /** Body of a regex literal: no unescaped `/`, character classes may hold one. */
69
69
  const REGEX = '/(?![/*])(?:[^/\\\\\\n\\[]|\\\\.|\\[(?:[^\\]\\\\\\n]|\\\\.)*\\])+/[dgimsuvy]*';
70
70
  const javascript = {
@@ -139,6 +139,22 @@ const javascript = {
139
139
  // rule here: a rule that has to look back at `const` runs at every
140
140
  // identifier in the file, while the state only runs after the keyword.
141
141
  { match: '\\b(?:const|let|var)\\b', scope: 'keyword.declaration', push: 'declared-name' },
142
+ // A parameter list reads its names as parameters: after `function` and
143
+ // its optional name, or before the `=>` of an arrow function. The arrow
144
+ // lookahead cannot see past nested parentheses, so a list holding a
145
+ // call in a default value or a function type keeps reading as plain
146
+ // code. Quotes and slashes also force that fallback: a string, comment
147
+ // or regex can contain `) =>` without introducing an arrow function.
148
+ {
149
+ match: `(\\bfunction\\b)([ \\t]*\\*?[ \\t]*)(?:(${ID})([ \\t]*))?(\\()`,
150
+ scope: ['keyword.declaration', null, 'function', null, 'punctuation.bracket'],
151
+ push: 'params',
152
+ },
153
+ {
154
+ match: '\\((?=[^()"\'`/]{0,512}\\)[ \\t]*(?::[^=;\\n"\'`/]{0,256})?=>)',
155
+ scope: 'punctuation.bracket',
156
+ push: 'params',
157
+ },
142
158
  {
143
159
  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
160
  scope: 'keyword.declaration',
@@ -147,6 +163,8 @@ const javascript = {
147
163
  match: `\\b(?:import|export|from(?!\\()|require)\\b${NOT_KEY}`,
148
164
  scope: 'keyword.import',
149
165
  },
166
+ // Import attributes: `import data from './x.json' with { type: 'json' }`.
167
+ { match: '\\bwith(?=[ \\t]*\\{)', scope: 'keyword.import' },
150
168
  {
151
169
  match: `\\b(?:typeof|instanceof|in|of(?!\\()|new|delete(?!\\()|void|satisfies|keyof|infer|asserts|as)\\b${NOT_KEY}`,
152
170
  scope: 'keyword.operator',
@@ -200,6 +218,121 @@ const javascript = {
200
218
  { match: '(?=\\S)', pop: true },
201
219
  ],
202
220
  },
221
+ // ---- parameter lists -----------------------------------------------------
222
+ /** After the `(` of a parameter list, up to the `)` that closes it. */
223
+ params: {
224
+ rules: [{ match: '\\)', scope: 'punctuation.bracket', pop: true }, { include: 'param-pattern' }],
225
+ },
226
+ /**
227
+ * What a parameter can be: a name, a destructuring pattern, a rest
228
+ * element, and after it a TypeScript annotation or a default value.
229
+ */
230
+ 'param-pattern': {
231
+ rules: [
232
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
233
+ { match: '//[^\\n]*', scope: 'comment' },
234
+ { match: ':', scope: 'punctuation.delimiter', push: 'param-type' },
235
+ // Leave the operator for expression rules that need it to recognize a regex.
236
+ { match: '(?==(?!>))', push: 'param-default' },
237
+ { match: '\\{', scope: 'punctuation.bracket', push: 'param-object' },
238
+ { match: '\\[', scope: 'punctuation.bracket', push: 'param-array' },
239
+ { match: '\\.{3}|\\?', scope: 'operator' },
240
+ { match: '\\bthis\\b', scope: 'variable.builtin' },
241
+ // A constructor's parameter properties: `constructor(private x: X)`.
242
+ {
243
+ match: '\\b(?:public|private|protected|readonly|override)\\b(?=[ \\t]+[\\w$\\[{])',
244
+ scope: 'keyword.declaration',
245
+ },
246
+ { match: `@${ID}`, scope: 'decorator' },
247
+ { match: ID, scope: 'variable.parameter' },
248
+ { match: ',', scope: 'punctuation.delimiter' },
249
+ ],
250
+ },
251
+ /**
252
+ * `{ a, b: renamed, c = 1, ...rest }`: keys are not bindings. A computed
253
+ * key uses expression rules, unlike the bindings inside an array pattern.
254
+ */
255
+ 'param-object': {
256
+ rules: [
257
+ { match: '\\}', scope: 'punctuation.bracket', pop: true },
258
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
259
+ // Inside an object pattern a colon renames a key, never annotates a type.
260
+ { match: ':', scope: 'punctuation.delimiter' },
261
+ { include: 'numbers' },
262
+ // Quoted keys rename a binding; their colon does not introduce a type.
263
+ {
264
+ match: `("(?:[^"\\\\\\r\\n]|\\\\.)*"|'(?:[^'\\\\\\r\\n]|\\\\.)*')([ \\t]*)(:)`,
265
+ scope: ['string', null, 'punctuation.delimiter'],
266
+ },
267
+ {
268
+ match: `(${ID})([ \\t]*)(:)`,
269
+ scope: ['property', null, 'punctuation.delimiter'],
270
+ },
271
+ { include: 'param-pattern' },
272
+ ],
273
+ },
274
+ /** Array destructuring binds its elements, including nested patterns. */
275
+ 'param-array': {
276
+ rules: [{ match: '\\]', scope: 'punctuation.bracket', pop: true }, { include: 'param-pattern' }],
277
+ },
278
+ /**
279
+ * A parameter's type, up to the `,`, `)` or `=` that ends it. Brackets
280
+ * nest so the commas in `Map<K, V>` and `{ a: A, b: B }` stay inside.
281
+ */
282
+ 'param-type': {
283
+ rules: [
284
+ { match: '(?=[,)}\\]]|=(?!>))', pop: true },
285
+ { match: '<', scope: 'punctuation.bracket', push: 'type-angle' },
286
+ { include: 'nesting' },
287
+ { include: 'expression' },
288
+ ],
289
+ },
290
+ 'type-angle': {
291
+ rules: [
292
+ { match: '=>', scope: 'operator' },
293
+ { match: '>', scope: 'punctuation.bracket', pop: true },
294
+ { match: '<', scope: 'punctuation.bracket', push: 'type-angle' },
295
+ { include: 'nesting' },
296
+ { include: 'expression' },
297
+ ],
298
+ },
299
+ /** A default value, up to the `,` or closing bracket that ends it. */
300
+ 'param-default': {
301
+ rules: [{ match: '(?=[,)}\\]])', pop: true }, { include: 'nesting' }, { include: 'expression' }],
302
+ },
303
+ /** Bracket depth, so a comma inside a nested call or literal stays nested. */
304
+ nesting: {
305
+ rules: [
306
+ // Consume the regex with its opener, then track the bracket around it.
307
+ { match: `(\\()(\\s*)(${REGEX})`, scope: ['punctuation.bracket', null, 'regexp'], push: 'paren' },
308
+ { match: `(\\[)(\\s*)(${REGEX})`, scope: ['punctuation.bracket', null, 'regexp'], push: 'bracket' },
309
+ // Generic calls nest commas too. Require a following call and stop at an
310
+ // assignment or an unmatched closing parenthesis or brace. Balanced function/object
311
+ // types are allowed, but the scan cannot escape into the function body.
312
+ {
313
+ match: '<(?=(?:[^;=(){}\\n]|=>|\\([^(){}\\n]{0,128}\\)|\\{[^{}()\\n]{0,128}\\}){1,512}>[ \\t]*\\()',
314
+ scope: 'punctuation.bracket',
315
+ push: 'type-angle',
316
+ },
317
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
318
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
319
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
320
+ ],
321
+ },
322
+ paren: {
323
+ rules: [
324
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
325
+ { include: 'nesting' },
326
+ { include: 'expression' },
327
+ ],
328
+ },
329
+ bracket: {
330
+ rules: [
331
+ { match: '\\]', scope: 'punctuation.bracket', pop: true },
332
+ { include: 'nesting' },
333
+ { include: 'expression' },
334
+ ],
335
+ },
203
336
  numbers: {
204
337
  rules: [
205
338
  { 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":"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,OAsIX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -51,7 +51,12 @@ const rust = {
51
51
  match: '\\b(?:fn|let|const|static|struct|enum|trait|impl|type|mod|union|async|move|dyn|pub|crate|unsafe|extern|ref|mut|where)\\b',
52
52
  scope: 'keyword.declaration',
53
53
  },
54
- { match: '\\b(?:use|as|super|self|Self)\\b', scope: 'keyword.import' },
54
+ // `r#type` is an identifier spelled like a keyword; claiming it here,
55
+ // where it starts, keeps the keyword rules from matching its tail.
56
+ { match: 'r#[A-Za-z_]\\w*' },
57
+ { match: '\\b(?:use|as|super)\\b', scope: 'keyword.import' },
58
+ { match: '\\bself\\b', scope: 'variable.builtin' },
59
+ { match: '\\bSelf\\b', scope: 'type' },
55
60
  { match: '\\b(?:in|box)\\b', scope: 'keyword.operator' },
56
61
  { match: '\\b(?:true|false)\\b', scope: 'boolean' },
57
62
  { match: '\\b(?:None|Some|Ok|Err)\\b', scope: 'constant.builtin' },
@@ -66,6 +71,8 @@ const rust = {
66
71
  match: '\\b([A-Za-z_]\\w*)(!)',
67
72
  scope: ['function.builtin', 'function.builtin'],
68
73
  },
74
+ // `.await` is a keyword in field position, not a member.
75
+ { match: '(\\.)(await)\\b', scope: ['punctuation', 'keyword.control'] },
69
76
  {
70
77
  match: '(\\.)([A-Za-z_]\\w*)(?=\\s*\\()',
71
78
  scope: ['punctuation', 'function.method'],
@@ -1 +1 @@
1
- {"version":3,"file":"swift.d.ts","sourceRoot":"","sources":["../../src/langs/swift.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,QAAA,MAAM,KAAK,EAAE,OAwPZ,CAAA;AAED,eAAe,KAAK,CAAA"}
1
+ {"version":3,"file":"swift.d.ts","sourceRoot":"","sources":["../../src/langs/swift.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,QAAA,MAAM,KAAK,EAAE,OAmRZ,CAAA;AAED,eAAe,KAAK,CAAA"}
@@ -35,6 +35,7 @@ const swift = {
35
35
  rules: [
36
36
  { include: 'comments' },
37
37
  { include: 'strings' },
38
+ { include: 'regex' },
38
39
  // `@escaping`, `@MainActor`, `@available(iOS 15, *)`. The argument list
39
40
  // is deliberately left as ordinary code so `iOS 15` still reads as one.
40
41
  { match: '@[A-Za-z_]\\w*', scope: 'decorator' },
@@ -72,7 +73,7 @@ const swift = {
72
73
  scope: 'keyword.control',
73
74
  },
74
75
  {
75
- match: '\\b(?:func|var|let|class|struct|enum|protocol|extension|actor|init|deinit|subscript|typealias|associatedtype|operator|precedencegroup|static|final|lazy|weak|unowned|mutating|nonmutating|override|open|public|internal|fileprivate|private|package|required|convenience|indirect|inout|dynamic|async|throws|rethrows|some|any|get|set|willSet|didSet)\\b',
76
+ match: '\\b(?:func|var|let|class|struct|enum|protocol|extension|actor|init|deinit|subscript|typealias|associatedtype|operator|precedencegroup|static|final|lazy|weak|unowned|mutating|nonmutating|override|open|public|internal|fileprivate|private|package|required|convenience|indirect|inout|dynamic|async|throws|rethrows|some|any|get|set|willSet|didSet|nonisolated|isolated|distributed|borrowing|consuming|sending|macro)\\b',
76
77
  scope: 'keyword.declaration',
77
78
  },
78
79
  { match: '\\b(?:is|as|in)\\b', scope: 'keyword.operator' },
@@ -176,6 +177,31 @@ const swift = {
176
177
  { match: '"', scope: 'string', push: 'string' },
177
178
  ],
178
179
  },
180
+ // ---- regex literals ------------------------------------------------------
181
+ regex: {
182
+ rules: [
183
+ // `#/…/#` can span lines and holds a bare `/`; only the `/` followed by
184
+ // as many `#` as opened it closes the literal.
185
+ // Only try at the start of a hash run; retrying each suffix is quadratic
186
+ // when a pasted or incomplete run has no opening slash.
187
+ { match: '(?<!#)(#+)/', scope: 'regexp', push: 'regex-extended', remember: 1 },
188
+ // A bare `/…/` is a regex only where an expression starts, and never
189
+ // with a space just inside either slash, which is how Swift itself
190
+ // tells it apart from division. The lookbehind sits after the `/` so the
191
+ // rule still starts with a literal character.
192
+ {
193
+ match: '/(?<=(?:[=(,:\\[!&|?{]|\\breturn|\\bcase)[ \\t]*/)(?![\\s/*])(?:[^/\\\\\\n]|\\\\.)+(?<!\\s)/',
194
+ scope: 'regexp',
195
+ },
196
+ ],
197
+ },
198
+ 'regex-extended': {
199
+ default: 'regexp',
200
+ rules: [
201
+ { match: '\\\\.', scope: 'regexp' },
202
+ { match: '/{{remembered}}', scope: 'regexp', pop: true },
203
+ ],
204
+ },
179
205
  /** Escape and interpolation rules shared by both ordinary string states. */
180
206
  'string-body': {
181
207
  rules: [
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "code",
17
17
  "performance"
18
18
  ],
19
- "version": "0.4.0",
19
+ "version": "0.5.0",
20
20
  "engines": {
21
21
  "node": ">=22"
22
22
  },