1
1
// The module 'vscode' contains the VS Code extensibility API
2
2
import * as vscode from "vscode" ;
3
- import getDesignToken from "antd-token-previewer/es/utils/getDesignToken " ;
4
- import { genMarkdownString } from "./utils " ;
3
+ import setupChangeEvent from "./decorator " ;
4
+ import setupAntdToken from "./hover " ;
5
5
6
6
// this method is called when your extension is activated
7
7
// your extension is activated the very first time the command is executed
8
8
let isActive = true ;
9
- let disposeHover : vscode . Disposable ;
10
- let disposeTyping : vscode . Disposable ;
11
9
12
10
export function activate ( context : vscode . ExtensionContext ) {
13
11
// This line of code will only be executed once when your extension is activated
14
- setUpAntdToken ( ) ;
12
+ let disposeHover : vscode . Disposable ;
13
+ let disposeTyping : vscode . Disposable ;
14
+ [ disposeHover , disposeTyping ] = setupAntdToken ( ) ;
15
+ setupChangeEvent ( ) ;
15
16
16
17
vscode . commands . registerCommand ( "antd-design-token.toggle" , ( ) => {
17
18
isActive = ! isActive ;
18
19
19
20
if ( isActive ) {
20
- setUpAntdToken ( ) ;
21
+ [ disposeHover , disposeTyping ] = setupAntdToken ( ) ;
21
22
vscode . window . showInformationMessage ( "antd design token is active now." ) ;
22
23
} else {
23
24
disposeHover . dispose ( ) ;
@@ -29,89 +30,5 @@ export function activate(context: vscode.ExtensionContext) {
29
30
} ) ;
30
31
}
31
32
32
- function setUpAntdToken ( ) {
33
- const fullToken = getDesignToken ( ) ;
34
-
35
- if ( ! fullToken ) {
36
- throw new Error ( "Get fullToken failed." ) ;
37
- }
38
-
39
- // HOVER
40
- disposeHover = vscode . languages . registerHoverProvider (
41
- [
42
- "javascript" ,
43
- "javascriptreact" ,
44
- "typescript" ,
45
- "typescriptreact" ,
46
- "vue" ,
47
- "vue-html" ,
48
- "html" ,
49
- ] ,
50
- {
51
- provideHover ( document , position ) {
52
- const range = document . getWordRangeAtPosition ( position ) ;
53
- const word = document . getText ( range ) ;
54
-
55
- if ( fullToken . hasOwnProperty ( word as string ) ) {
56
- const value = String ( fullToken [ word as keyof typeof fullToken ] ) ;
57
- const colorSpan = genMarkdownString ( value ) ;
58
-
59
- const markDownString = new vscode . MarkdownString (
60
- `<h3>antd design token: ${ word } </h3>${ colorSpan } <code>${ value } </code><br></br>`
61
- ) ;
62
- markDownString . supportHtml = true ;
63
- markDownString . isTrusted = true ;
64
-
65
- return new vscode . Hover ( markDownString ) ;
66
- }
67
- } ,
68
- }
69
- ) ;
70
-
71
- // TYPING
72
- // Add antd token value tips on typing
73
- // Note: 11 is a `value` kind of completion items.
74
- // Based on the kind an icon is chosen by the editor.
75
- const items : any [ ] | undefined = [ ] ;
76
-
77
- for ( let key in fullToken ) {
78
- const value = String ( fullToken [ key as keyof typeof fullToken ] ) ;
79
- const item = new vscode . CompletionItem ( `antd-${ key } : ${ value } ` , 11 ) ;
80
- item . insertText = key . includes ( "-" ) ? `['${ key } ']` : key ;
81
-
82
- const sortValue = value . padStart ( 5 , "0" ) ;
83
- item . sortText = `a-${ sortValue } -${ key } ` ;
84
-
85
- const colorSpan = genMarkdownString ( value ) ;
86
- let documentContent : vscode . MarkdownString | string = "" ;
87
-
88
- documentContent = new vscode . MarkdownString (
89
- `<h4>antd design token: ${ key } </h4>${ colorSpan } <code>${ value } </code><br></br>`
90
- ) ;
91
- documentContent . supportHtml = true ;
92
-
93
- item . documentation = documentContent ;
94
-
95
- items . push ( item ) ;
96
- }
97
-
98
- disposeTyping = vscode . languages . registerCompletionItemProvider (
99
- [
100
- "javascript" ,
101
- "javascriptreact" ,
102
- "typescript" ,
103
- "typescriptreact" ,
104
- "vue" ,
105
- "vue-html" ,
106
- "html" ,
107
- ] ,
108
- {
109
- provideCompletionItems ( document ) : any {
110
- return new vscode . CompletionList ( items , false ) ;
111
- } ,
112
- }
113
- ) ;
114
- }
115
-
116
33
// this method is called when your extension is deactivated
117
34
export function deactivate ( ) { }
0 commit comments