Skip to content

Commit 15209ea

Browse files
authored
Definition Context for Hover Info (#197)
* return definition context for hover info * update E2E test * return definition context of value declaration * update E2E test * add definition preview description in README * add changelog
1 parent 5a8adb7 commit 15209ea

14 files changed

Lines changed: 695 additions & 20 deletions

File tree

.changeset/big-badgers-roll.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@css-modules-kit/ts-plugin': minor
3+
'@css-modules-kit/core': minor
4+
---
5+
6+
feat: support "Definition Preview for Hover"

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ https://github.com/user-attachments/assets/df1e2feb-2a1a-4bf5-ae70-1cac36d90409
3939

4040
</details>
4141

42+
<details>
43+
<summary>Definition Preview by Hover</summary>
44+
45+
You can preview the definition with <kbd>Command</kbd> + <kbd>Hover</kbd> on macOS and VS Code (key bindings may vary depending on your OS and editor).
46+
47+
https://github.com/user-attachments/assets/8d42acb8-2822-4fe6-89ce-8472c7065b8b
48+
49+
</details>
50+
4251
<details>
4352
<summary>Automatically update import statements when moving `*.module.css`</summary>
4453

packages/core/src/parser/at-value-parser.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ describe('parseAtValue', () => {
2424
[
2525
{
2626
"atValue": {
27+
"declarationLoc": {
28+
"end": {
29+
"column": 19,
30+
"line": 1,
31+
"offset": 18,
32+
},
33+
"start": {
34+
"column": 1,
35+
"line": 1,
36+
"offset": 0,
37+
},
38+
},
2739
"loc": {
2840
"end": {
2941
"column": 13,
@@ -43,6 +55,18 @@ describe('parseAtValue', () => {
4355
},
4456
{
4557
"atValue": {
58+
"declarationLoc": {
59+
"end": {
60+
"column": 25,
61+
"line": 2,
62+
"offset": 44,
63+
},
64+
"start": {
65+
"column": 1,
66+
"line": 2,
67+
"offset": 20,
68+
},
69+
},
4670
"loc": {
4771
"end": {
4872
"column": 20,
@@ -62,6 +86,18 @@ describe('parseAtValue', () => {
6286
},
6387
{
6488
"atValue": {
89+
"declarationLoc": {
90+
"end": {
91+
"column": 14,
92+
"line": 3,
93+
"offset": 59,
94+
},
95+
"start": {
96+
"column": 1,
97+
"line": 3,
98+
"offset": 46,
99+
},
100+
},
65101
"loc": {
66102
"end": {
67103
"column": 13,
@@ -81,6 +117,18 @@ describe('parseAtValue', () => {
81117
},
82118
{
83119
"atValue": {
120+
"declarationLoc": {
121+
"end": {
122+
"column": 29,
123+
"line": 4,
124+
"offset": 89,
125+
},
126+
"start": {
127+
"column": 1,
128+
"line": 4,
129+
"offset": 61,
130+
},
131+
},
84132
"loc": {
85133
"end": {
86134
"column": 15,
@@ -100,6 +148,18 @@ describe('parseAtValue', () => {
100148
},
101149
{
102150
"atValue": {
151+
"declarationLoc": {
152+
"end": {
153+
"column": 35,
154+
"line": 5,
155+
"offset": 125,
156+
},
157+
"start": {
158+
"column": 1,
159+
"line": 5,
160+
"offset": 91,
161+
},
162+
},
103163
"loc": {
104164
"end": {
105165
"column": 15,
@@ -255,6 +315,18 @@ describe('parseAtValue', () => {
255315
},
256316
{
257317
"atValue": {
318+
"declarationLoc": {
319+
"end": {
320+
"column": 28,
321+
"line": 9,
322+
"offset": 268,
323+
},
324+
"start": {
325+
"column": 2,
326+
"line": 9,
327+
"offset": 242,
328+
},
329+
},
258330
"loc": {
259331
"end": {
260332
"column": 20,

packages/core/src/parser/at-value-parser.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ interface ValueDeclaration {
66
name: string;
77
// value: string; // unused
88
loc: Location;
9+
/**
10+
* NOTE: The `declarationLoc` for value declaration does not include the trailing semicolon.
11+
* @example `@value white: #fff` has `declarationLoc` as `{ start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 19, offset: 18 } }`.
12+
*/
13+
declarationLoc: Location;
914
}
1015

1116
interface ValueImportDeclaration {
@@ -149,7 +154,15 @@ export function parseAtValue(atValue: AtRule): ParseAtValueResult {
149154
column: start.column + name.length,
150155
offset: start.offset + name.length,
151156
};
152-
const parsedAtValue = { type: 'valueDeclaration', name, loc: { start, end } } as const;
157+
const parsedAtValue: ValueDeclaration = {
158+
type: 'valueDeclaration',
159+
name,
160+
loc: { start, end },
161+
declarationLoc: {
162+
start: atValue.source!.start!,
163+
end: atValue.positionBy({ index: atValue.toString().length }),
164+
},
165+
} as const;
153166
return { atValue: parsedAtValue, diagnostics };
154167
}
155168
diagnostics.push({

0 commit comments

Comments
 (0)