Skip to content

Commit e692903

Browse files
committed
feat: support hover hint
1 parent 6964fb4 commit e692903

File tree

5 files changed

+118
-25
lines changed

5 files changed

+118
-25
lines changed

.prettierrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"singleQuote": false,
3+
"printWidth": 80,
4+
"tabWidth": 2,
5+
"useTabs": false,
6+
"semi": true,
7+
"trailingComma": "es5",
8+
"bracketSpacing": true
9+
}

.vscode/settings.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Place your settings in this file to overwrite default and user settings.
22
{
3-
"files.exclude": {
4-
"out": false, // set this to true to hide the "out" folder with the compiled JS files
5-
"dist": false // set this to true to hide the "dist" folder with the compiled JS files
6-
},
7-
"search.exclude": {
8-
"out": true, // set this to false to include "out" folder in search results
9-
"dist": true // set this to false to include "dist" folder in search results
10-
},
11-
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
12-
"typescript.tsc.autoDetect": "off"
13-
}
3+
"files.exclude": {
4+
"out": false, // set this to true to hide the "out" folder with the compiled JS files
5+
"dist": false // set this to true to hide the "dist" folder with the compiled JS files
6+
},
7+
"search.exclude": {
8+
"out": true, // set this to false to include "out" folder in search results
9+
"dist": true // set this to false to include "dist" folder in search results
10+
},
11+
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
12+
"typescript.tsc.autoDetect": "off",
13+
"editor.hover.delay": 600
14+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"webpack-cli": "^4.9.2"
5151
},
5252
"dependencies": {
53-
"antd-token-previewer": "^1.0.0-alpha.19"
53+
"antd-token-previewer": "^1.0.0-alpha.19",
54+
"rgb-hex": "^4.0.0"
5455
}
5556
}

src/extension.ts

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,89 @@
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from "vscode";
44
import getDesignToken from "antd-token-previewer/es/utils/getDesignToken";
5+
import rgbHex from "rgb-hex";
6+
import { getMarkdownString } from "./utils";
57

68
// this method is called when your extension is activated
79
// your extension is activated the very first time the command is executed
810
export function activate(context: vscode.ExtensionContext) {
911
// Use the console to output diagnostic information (console.log) and errors (console.error)
1012
// 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+
}
1387
);
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-
});
2588

2689
// The command has been defined in the package.json file
2790
// Now provide the implementation of the command with registerCommand

src/utils/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import rgbHex from "rgb-hex";
2+
3+
/**
4+
* Get the MarkdownString for vscode.MarkdownString function
5+
* Note: should convert rgb color to hex
6+
* @param value string
7+
* @returns MarkdownString | string
8+
*/
9+
export function getMarkdownString(value: string) {
10+
let hexColor = "";
11+
if (/#[0-9a-fA-F]+/.test(value)) {
12+
hexColor = value;
13+
} else if (value.startsWith("rgb")) {
14+
hexColor = `#${rgbHex(value)}`;
15+
}
16+
return hexColor
17+
? `<span style='background-color:${hexColor};'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;&nbsp;`
18+
: "";
19+
}

0 commit comments

Comments
 (0)