|
2 | 2 | // Import the module and reference it with the alias vscode in your code below
|
3 | 3 | import * as vscode from "vscode";
|
4 | 4 | import getDesignToken from "antd-token-previewer/es/utils/getDesignToken";
|
| 5 | +import rgbHex from "rgb-hex"; |
| 6 | +import { getMarkdownString } from "./utils"; |
5 | 7 |
|
6 | 8 | // this method is called when your extension is activated
|
7 | 9 | // your extension is activated the very first time the command is executed
|
8 | 10 | export function activate(context: vscode.ExtensionContext) {
|
9 | 11 | // Use the console to output diagnostic information (console.log) and errors (console.error)
|
10 | 12 | // This line of code will only be executed once when your extension is activated
|
11 |
| - console.log( |
12 |
| - 'Congratulations, your extension "antd-design-token" is now active!' |
| 13 | + const fullToken = getDesignToken(); |
| 14 | + |
| 15 | + // vscode hover widget only support color style in <span></span> |
| 16 | + // https://github.com/microsoft/vscode/blob/6d2920473c6f13759c978dd89104c4270a83422d/src/vs/base/browser/markdownRenderer.ts#L296 |
| 17 | + vscode.languages.registerHoverProvider( |
| 18 | + [ |
| 19 | + "javascript", |
| 20 | + "javascriptreact", |
| 21 | + "typescript", |
| 22 | + "typescriptreact", |
| 23 | + "vue", |
| 24 | + "vue-html", |
| 25 | + "html", |
| 26 | + ], |
| 27 | + { |
| 28 | + provideHover(document, position) { |
| 29 | + const range = document.getWordRangeAtPosition(position); |
| 30 | + const word = document.getText(range); |
| 31 | + |
| 32 | + if (fullToken.hasOwnProperty(word as string)) { |
| 33 | + const value = String(fullToken[word as keyof typeof fullToken]); |
| 34 | + const colorSpan = getMarkdownString(value); |
| 35 | + |
| 36 | + const markDownString = new vscode.MarkdownString( |
| 37 | + `<h3>antd design token: ${word}</h3>${colorSpan}<code>${value}</code><br></br>` |
| 38 | + ); |
| 39 | + markDownString.supportHtml = true; |
| 40 | + markDownString.isTrusted = true; |
| 41 | + |
| 42 | + return new vscode.Hover(markDownString); |
| 43 | + } |
| 44 | + }, |
| 45 | + } |
| 46 | + ); |
| 47 | + |
| 48 | + // Add antd token value tips on typing |
| 49 | + // Note: 11 is a `value` kind of completion items. |
| 50 | + // Based on the kind an icon is chosen by the editor. |
| 51 | + const items: any[] | undefined = []; |
| 52 | + |
| 53 | + for (let key in fullToken) { |
| 54 | + const value = fullToken[key as keyof typeof fullToken]; |
| 55 | + const item = new vscode.CompletionItem(`${key}: ${value}`, 11); |
| 56 | + item.insertText = key; |
| 57 | + item.filterText = key; |
| 58 | + item.sortText = key; |
| 59 | + |
| 60 | + const colorSpan = getMarkdownString(value); |
| 61 | + const markDownString = new vscode.MarkdownString( |
| 62 | + `<h3>antd design token: ${key}</h3>${colorSpan}<code>${value}</code><br></br>` |
| 63 | + ); |
| 64 | + |
| 65 | + markDownString.supportHtml = true; |
| 66 | + markDownString.isTrusted = true; |
| 67 | + item.documentation = markDownString; |
| 68 | + |
| 69 | + items.push(item); |
| 70 | + } |
| 71 | + |
| 72 | + vscode.languages.registerCompletionItemProvider( |
| 73 | + [ |
| 74 | + "javascript", |
| 75 | + "javascriptreact", |
| 76 | + "typescript", |
| 77 | + "typescriptreact", |
| 78 | + "vue", |
| 79 | + "vue-html", |
| 80 | + "html", |
| 81 | + ], |
| 82 | + { |
| 83 | + provideCompletionItems(document): any { |
| 84 | + return new vscode.CompletionList(items, false); |
| 85 | + }, |
| 86 | + } |
13 | 87 | );
|
14 |
| - console.log(getDesignToken()); |
15 |
| - |
16 |
| - vscode.languages.registerHoverProvider("javascript", { |
17 |
| - provideHover(document, position, token) { |
18 |
| - console.log("=========="); |
19 |
| - console.log("document", document); |
20 |
| - console.log("position", position); |
21 |
| - console.log("token", token); |
22 |
| - return new vscode.Hover("I am a hover!"); |
23 |
| - }, |
24 |
| - }); |
25 | 88 |
|
26 | 89 | // The command has been defined in the package.json file
|
27 | 90 | // Now provide the implementation of the command with registerCommand
|
|
0 commit comments