1
1
import path from 'path' ;
2
2
import { EOL } from 'os' ;
3
- import { Location , Position , Range } from 'vscode-languageserver-protocol' ;
3
+ import { Hover , Location , Position , Range } from 'vscode-languageserver-protocol' ;
4
4
import { TextDocument } from 'vscode-languageserver-textdocument' ;
5
5
import * as lsp from 'vscode-languageserver/node' ;
6
6
import {
7
7
CamelCaseValues ,
8
+ Classname ,
9
+ filePathToClassnameDict ,
8
10
findImportPath ,
9
11
genImportRegExp ,
10
12
getCurrentDirFromUri ,
11
13
getPosition ,
14
+ getTransformer ,
12
15
getWords ,
13
16
isImportLineMatch ,
17
+ stringiyClassname ,
14
18
} from './utils' ;
15
19
import { textDocuments } from './textDocuments' ;
16
20
@@ -34,6 +38,56 @@ export class CSSModulesDefinitionProvider {
34
38
return this . provideDefinition ( textdocument , params . position ) ;
35
39
} ;
36
40
41
+ hover = async ( params : lsp . HoverParams ) => {
42
+ const textdocument = textDocuments . get ( params . textDocument . uri ) ;
43
+ if ( textdocument === undefined ) {
44
+ return null ;
45
+ }
46
+
47
+ return this . provideHover ( textdocument , params . position ) ;
48
+ } ;
49
+
50
+ async provideHover (
51
+ textdocument : TextDocument ,
52
+ position : Position ,
53
+ ) : Promise < null | Hover > {
54
+ const fileContent = textdocument . getText ( ) ;
55
+ const lines = fileContent . split ( EOL ) ;
56
+ const currentLine = lines [ position . line ] ;
57
+
58
+ if ( typeof currentLine !== 'string' ) {
59
+ return null ;
60
+ }
61
+ const currentDir = getCurrentDirFromUri ( textdocument . uri ) ;
62
+
63
+ const words = getWords ( currentLine , position ) ;
64
+ if ( words === '' || words . indexOf ( '.' ) === - 1 ) {
65
+ return null ;
66
+ }
67
+
68
+ const [ obj , field ] = words . split ( '.' ) ;
69
+ const importPath = findImportPath ( fileContent , obj , currentDir ) ;
70
+ if ( importPath === '' ) {
71
+ return null ;
72
+ }
73
+
74
+ const dict = await filePathToClassnameDict (
75
+ importPath ,
76
+ getTransformer ( this . _camelCaseConfig ) ,
77
+ ) ;
78
+
79
+ const node : void | Classname = dict [ `.${ field } ` ] ;
80
+
81
+ if ( ! node ) return null ;
82
+
83
+ return {
84
+ contents : {
85
+ language : 'css' ,
86
+ value : stringiyClassname ( field , node . declarations ) ,
87
+ } ,
88
+ } ;
89
+ }
90
+
37
91
async provideDefinition (
38
92
textdocument : TextDocument ,
39
93
position : Position ,
0 commit comments