|
1 | 1 | /* eslint-disable no-console */ |
2 | 2 |
|
| 3 | +import type { |
| 4 | + CSSModulesKitDocumentLinkRequest, |
| 5 | + CSSModulesKitDocumentLinkResponse, |
| 6 | + CSSModulesKitRenameInfoRequest, |
| 7 | + CSSModulesKitRenameInfoResponse, |
| 8 | + CSSModulesKitRenameRequest, |
| 9 | + CSSModulesKitRenameResponse, |
| 10 | +} from '@css-modules-kit/ts-plugin/type'; |
3 | 11 | import * as vscode from 'vscode'; |
4 | 12 |
|
5 | | -export function activate(_context: vscode.ExtensionContext) { |
| 13 | +export function activate(context: vscode.ExtensionContext) { |
6 | 14 | console.log('[css-modules-kit-vscode] Activated'); |
7 | 15 |
|
8 | 16 | // By default, `vscode.typescript-language-features` is not activated when a user opens *.css in VS Code. |
9 | 17 | // So, activate it manually. |
10 | 18 | const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features'); |
11 | | - if (tsExtension) { |
| 19 | + if (tsExtension && !tsExtension.isActive) { |
12 | 20 | console.log('[css-modules-kit-vscode] Activating `vscode.typescript-language-features`'); |
13 | 21 | tsExtension.activate(); |
14 | 22 | } |
| 23 | + |
| 24 | + context.subscriptions.push( |
| 25 | + // In VS Code, tsserver typically conflicts with the standard CSS Language Server, causing some language features to malfunction. |
| 26 | + // Therefore, an approach called "Request Forwarding to tsserver" is used to avoid this conflict. |
| 27 | + // See https://github.com/mizdra/css-modules-kit/pull/207 for more details. |
| 28 | + vscode.languages.registerRenameProvider( |
| 29 | + { scheme: 'file', language: 'css' }, |
| 30 | + { |
| 31 | + async provideRenameEdits(document, position, newName, _token) { |
| 32 | + const res = await vscode.commands.executeCommand<CSSModulesKitRenameResponse>( |
| 33 | + 'typescript.tsserverRequest', |
| 34 | + '_css-modules-kit:rename', |
| 35 | + { |
| 36 | + fileName: document.fileName, |
| 37 | + position: document.offsetAt(position), |
| 38 | + } satisfies CSSModulesKitRenameRequest['arguments'], |
| 39 | + ); |
| 40 | + // If the response does not contain any results, return undefined to fall back to standard CSS Language Server. |
| 41 | + if (!res.success || !res.body || !res.body.result || res.body.result.length === 0) return; |
| 42 | + const edit = new vscode.WorkspaceEdit(); |
| 43 | + for (const location of res.body.result) { |
| 44 | + // eslint-disable-next-line no-await-in-loop |
| 45 | + const document = await vscode.workspace.openTextDocument(location.fileName); |
| 46 | + const start = document.positionAt(location.textSpan.start); |
| 47 | + const end = document.positionAt(location.textSpan.start + location.textSpan.length); |
| 48 | + edit.replace(vscode.Uri.file(location.fileName), new vscode.Range(start, end), newName); |
| 49 | + } |
| 50 | + return edit; |
| 51 | + }, |
| 52 | + async prepareRename(document, position, _token) { |
| 53 | + const res = await vscode.commands.executeCommand<CSSModulesKitRenameInfoResponse>( |
| 54 | + 'typescript.tsserverRequest', |
| 55 | + '_css-modules-kit:renameInfo', |
| 56 | + { |
| 57 | + fileName: document.fileName, |
| 58 | + position: document.offsetAt(position), |
| 59 | + } satisfies CSSModulesKitRenameInfoRequest['arguments'], |
| 60 | + ); |
| 61 | + // If the response does not contain any results, return undefined to fall back to standard CSS Language Server. |
| 62 | + if (!res.success || !res.body || !res.body.result.canRename) return; |
| 63 | + return new vscode.Range( |
| 64 | + document.positionAt(res.body.result.triggerSpan.start), |
| 65 | + document.positionAt(res.body.result.triggerSpan.start + res.body.result.triggerSpan.length), |
| 66 | + ); |
| 67 | + }, |
| 68 | + }, |
| 69 | + ), |
| 70 | + vscode.languages.registerDocumentLinkProvider( |
| 71 | + { scheme: 'file', language: 'css' }, |
| 72 | + { |
| 73 | + async provideDocumentLinks(document, _token) { |
| 74 | + const res = await vscode.commands.executeCommand<CSSModulesKitDocumentLinkResponse>( |
| 75 | + 'typescript.tsserverRequest', |
| 76 | + '_css-modules-kit:documentLink', |
| 77 | + { |
| 78 | + fileName: document.fileName, |
| 79 | + } satisfies CSSModulesKitDocumentLinkRequest['arguments'], |
| 80 | + ); |
| 81 | + // If the response does not contain any results, return undefined to fall back to standard CSS Language Server. |
| 82 | + if (!res.success || !res.body || res.body.result.length === 0) return; |
| 83 | + return res.body.result.map((link) => { |
| 84 | + return new vscode.DocumentLink( |
| 85 | + new vscode.Range( |
| 86 | + document.positionAt(link.textSpan.start), |
| 87 | + document.positionAt(link.textSpan.start + link.textSpan.length), |
| 88 | + ), |
| 89 | + vscode.Uri.file(link.fileName), |
| 90 | + ); |
| 91 | + }); |
| 92 | + }, |
| 93 | + }, |
| 94 | + ), |
| 95 | + ); |
15 | 96 | } |
0 commit comments