Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@

(attribute attribute: (identifier) @property)
(type (identifier) @type)
(splat_type (identifier) @type)
(class_definition
superclasses: (argument_list
(subscript subscript: (list_splat (identifier) @type))))
(member_type (identifier) @type)
(generic_type (identifier) @type)
(default_type_parameter name: (identifier) @type)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/highlights/lazyImports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lazy import json
lazy from json import dumps
lazy = 1
import lazy
14 changes: 14 additions & 0 deletions test/fixtures/highlights/typeParameterDefaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class C[T: list[dict[str, int]] = list[dict[str, int]]]: pass
def f[*Ts = *tuple[int, str], **P = [str]](): pass
type A[T: (int, str) = list[int]] = T
class D[T: list[int] | None = list[str] | None]: pass
class E[T: int | str = typing.List[int]]: pass
class F[T = typing.Dict[str, typing.List[int | str]]]: pass
class G[T = (list[int] | None), **P = [typing.List[str] | None]]: pass
class H[T = int | str | None, *Ts = *typing.Tuple[int, str]]: pass
class I[*Ts = *(typing.Tuple[int] | tuple[str])]: pass
class J[T: typing.Protocol = typing.List[int]]: pass
class K[T = tuple[*tuple[int]]]: pass
class L[T = match[int]]: pass
class M[T = lazy.List[int]]: pass
class N[T = pkg.types.Value, U = other.models.Box[Leaf]]: pass
11 changes: 11 additions & 0 deletions test/fixtures/highlights/variadicTypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class C[*Ts]: pass
def f[**P](): pass
def g[*Vs](*args: *Vs): pass
type A[*Us] = tuple[*Us]
class B(Generic[*Ts]): pass
class D(typing.Generic[*Us]): pass
Ts = 1
P = 2
values = [*Vs]
lookup = table[*keys]
register(Generic[*idx])
25 changes: 0 additions & 25 deletions test/helpers/renderHighlights.ts

This file was deleted.

47 changes: 23 additions & 24 deletions test/unit/lazyImports.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { expect, test } from 'vitest';
import { expect } from 'vitest';
import { testCommand } from './run.js';

test('highlights lazy only as an import modifier', () => {
const root = path.resolve(import.meta.dirname, '../..');
const directory = fs.mkdtempSync(path.join(root, '.tmp/lazy-highlights-'));
const input = path.join(directory, 'imports.py');
try {
fs.writeFileSync(input, 'lazy import json\nlazy from json import dumps\nlazy = 1\nimport lazy\n');
const html = execFileSync(
path.join(root, 'script/tree-sitter'),
['highlight', '-p', root, '-H', '--layout', 'fragment', '--style', 'minimal', input],
{
cwd: root,
encoding: 'utf8',
env: { ...process.env, TREE_SITTER_LIBDIR: path.join(root, '.tmp/tree-sitter-lib') },
}
);
const classes = [...html.matchAll(/<span class='([^']+)'>lazy<\/span>/g)].map(([, capture]) => capture);
expect(classes).toEqual(['keyword', 'keyword', 'variable', 'variable']);
} finally {
fs.rmSync(directory, { recursive: true, force: true });
testCommand(
'highlights lazy only as an import modifier',
[
'script/tree-sitter',
'highlight',
'-p',
'.',
'-H',
'--layout',
'fragment',
'--style',
'minimal',
'test/fixtures/highlights/lazyImports.py',
],
170_000,
{
check: (html) => {
const classes = [...html.matchAll(/<span class='([^']+)'>lazy<\/span>/g)].map(([, capture]) => capture);
expect(classes).toEqual(['keyword', 'keyword', 'variable', 'variable']);
},
}
}, 180_000);
);
56 changes: 28 additions & 28 deletions test/unit/typeParameterHighlights.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { Language, Parser, Query } from '@willbooster/web-tree-sitter';
import { expect, test } from 'vitest';
import { renderHighlights } from '../helpers/renderHighlights.js';
import { testCommand } from './run.js';

test('highlights defaulted type parameter names, bounds, and direct defaults as types', async () => {
await Parser.init();
Expand Down Expand Up @@ -34,31 +34,31 @@ test('highlights defaulted type parameter names, bounds, and direct defaults as
}
});

test('renders nested default and bound type names with the type highlight', () => {
const html = renderHighlights(
[
'class C[T: list[dict[str, int]] = list[dict[str, int]]]: pass',
'def f[*Ts = *tuple[int, str], **P = [str]](): pass',
'type A[T: (int, str) = list[int]] = T',
'class D[T: list[int] | None = list[str] | None]: pass',
'class E[T: int | str = typing.List[int]]: pass',
'class F[T = typing.Dict[str, typing.List[int | str]]]: pass',
'class G[T = (list[int] | None), **P = [typing.List[str] | None]]: pass',
'class H[T = int | str | None, *Ts = *typing.Tuple[int, str]]: pass',
'class I[*Ts = *(typing.Tuple[int] | tuple[str])]: pass',
'class J[T: typing.Protocol = typing.List[int]]: pass',
'class K[T = tuple[*tuple[int]]]: pass',
'class L[T = match[int]]: pass',
'class M[T = lazy.List[int]]: pass',
'class N[T = pkg.types.Value, U = other.models.Box[Leaf]]: pass',
].join('\n')
);
expect(html.match(/<span class='type'>(?:int|str)<\/span>/g)).toHaveLength(30);
expect(html).not.toMatch(/<span class='variable'>(?:int|str)<\/span>/);
expect(html).toContain("<span class='type'>Protocol</span>");
expect(html).toContain("<span class='type'>List</span>");
for (const name of ['typing', 'lazy', 'pkg', 'types', 'Value', 'other', 'models', 'Box', 'Leaf']) {
expect(html).toContain(`<span class='type'>${name}</span>`);
expect(html).not.toContain(`<span class='variable'>${name}</span>`);
testCommand(
'renders nested default and bound type names with the type highlight',
[
'script/tree-sitter',
'highlight',
'-p',
'.',
'-H',
'--layout',
'fragment',
'--style',
'minimal',
'test/fixtures/highlights/typeParameterDefaults.py',
],
170_000,
{
check: (html) => {
expect(html.match(/<span class='type'>(?:int|str)<\/span>/g)).toHaveLength(30);
expect(html).not.toMatch(/<span class='variable'>(?:int|str)<\/span>/);
expect(html).toContain("<span class='type'>Protocol</span>");
expect(html).toContain("<span class='type'>List</span>");
for (const name of ['typing', 'lazy', 'pkg', 'types', 'Value', 'other', 'models', 'Box', 'Leaf']) {
expect(html).toContain(`<span class='type'>${name}</span>`);
expect(html).not.toContain(`<span class='variable'>${name}</span>`);
}
},
}
}, 180_000);
);
38 changes: 38 additions & 0 deletions test/unit/variadicTypeHighlights.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from 'vitest';
import { testCommand } from './run.js';

testCommand(
'renders variadic type parameter names as types',
[
'script/tree-sitter',
'highlight',
'-p',
'.',
'-H',
'--layout',
'fragment',
'--style',
'minimal',
'test/fixtures/highlights/variadicTypes.py',
],
170_000,
{
check: (html) => {
const highlights = [...html.matchAll(/<span class='([^']+)'>(Ts|P|Us|Vs|keys|idx)<\/span>/g)].map(
([, kind, name]) => [name, kind]
);
expect(highlights.slice(0, 8)).toEqual([
['Ts', 'type'],
['P', 'type'],
['Vs', 'type'],
['Vs', 'type'],
['Us', 'type'],
['Us', 'type'],
['Ts', 'type'],
['Us', 'type'],
]);
expect(highlights.slice(8).map(([name]) => name)).toEqual(['Ts', 'P', 'Vs', 'keys', 'idx']);
expect(highlights.slice(8).map(([, kind]) => kind)).not.toContain('type');
},
}
);
Loading