@@ -4,7 +4,7 @@ import "source-map-support/register";
44import * as VError from "verror" ;
55import {
66 commands , CompletionItem , CompletionItemKind , Disposable ,
7- ExtensionContext , languages , Position , Range , TextDocument , Uri , window ,
7+ ExtensionContext , Hover , languages , Location , Position , Range , TextDocument , Uri , window ,
88 workspace ,
99} from "vscode" ;
1010import CssClassDefinition from "./common/css-class-definition" ;
@@ -148,6 +148,11 @@ const registerCompletionProvider = (
148148
149149 completionItem . filterText = completionClassName ;
150150 completionItem . insertText = completionClassName ;
151+ completionItem . range = document . getWordRangeAtPosition ( position , / [ - \w , @ \\ : \[ \] ] + / ) ;
152+
153+ if ( definition . comments && definition . comments . length !== 0 ) {
154+ completionItem . detail = definition . comments ! [ 0 ] . split ( / \r ? \n / , 2 ) [ 0 ] ;
155+ }
151156
152157 return completionItem ;
153158 } ) ;
@@ -165,6 +170,72 @@ const registerCompletionProvider = (
165170 } ,
166171} , ...completionTriggerChars ) ;
167172
173+ const registerDefinitionProvider = ( languageSelector : string , classMatchRegex : RegExp ) => languages . registerDefinitionProvider ( languageSelector , {
174+ provideDefinition ( document , position , _token ) {
175+ // Check if the cursor is on a class attribute and retrieve all the css rules in this class attribute
176+ {
177+ const start : Position = new Position ( position . line , 0 ) ;
178+ const range : Range = new Range ( start , position ) ;
179+ const text : string = document . getText ( range ) ;
180+
181+ const rawClasses : RegExpMatchArray | null = text . match ( classMatchRegex ) ;
182+ if ( ! rawClasses || rawClasses . length === 1 ) {
183+ return ;
184+ }
185+ }
186+
187+ const range : Range | undefined = document . getWordRangeAtPosition ( position , / [ - \w , @ \\ : \[ \] ] + / ) ;
188+ if ( range == null ) {
189+ return ;
190+ }
191+
192+ const word : string = document . getText ( range ) ;
193+
194+ const definition = uniqueDefinitions . find ( ( definition ) => {
195+ return definition . className === word ;
196+ } ) ;
197+ if ( definition == null || ! definition . location ) {
198+ return ;
199+ }
200+
201+ return definition . location as Location ;
202+ } ,
203+ } )
204+
205+ const registerHoverProvider = ( languageSelector : string , classMatchRegex : RegExp ) => languages . registerHoverProvider ( languageSelector , {
206+ provideHover ( document , position , _token ) {
207+ {
208+ const start : Position = new Position ( position . line , 0 ) ;
209+ const range : Range = new Range ( start , position ) ;
210+ const text : string = document . getText ( range ) ;
211+
212+ const rawClasses : RegExpMatchArray | null = text . match ( classMatchRegex ) ;
213+ if ( ! rawClasses || rawClasses . length === 1 ) {
214+ return ;
215+ }
216+ }
217+
218+ const range : Range | undefined = document . getWordRangeAtPosition ( position , / [ - \w , @ \\ : \[ \] ] + / ) ;
219+ if ( range == null ) {
220+ return ;
221+ }
222+
223+ const word : string = document . getText ( range ) ;
224+
225+ // Creates a collection of CompletionItem based on the classes already cached
226+ const definition = uniqueDefinitions . find ( ( definition ) => {
227+ return definition . className === word
228+ } ) ;
229+ if ( definition == null ) {
230+ return ;
231+ }
232+
233+ if ( definition . comments != null ) {
234+ return new Hover ( `**.\`${ word } \`**\n${ definition . comments . join ( "\n\n" ) } ` , range )
235+ }
236+ } ,
237+ } )
238+
168239const registerHTMLProviders = ( disposables : Disposable [ ] ) =>
169240 workspace . getConfiguration ( )
170241 ?. get < string [ ] > ( Configuration . HTMLLanguages )
@@ -186,8 +257,10 @@ const registerJavaScriptProviders = (disposables: Disposable[]) =>
186257 workspace . getConfiguration ( )
187258 . get < string [ ] > ( Configuration . JavaScriptLanguages )
188259 ?. forEach ( ( extension ) => {
189- disposables . push ( registerCompletionProvider ( extension , / c l a s s N a m e = (?: { ? " | { ? ' ) ( [ \w - @ : \/ ] * $ ) / ) ) ;
260+ disposables . push ( registerCompletionProvider ( extension , / c l a s s N a m e = (?: { ? " | { ? ' | { ? ` ) ( [ \w - @ : \/ ] * $ ) / ) ) ;
190261 disposables . push ( registerCompletionProvider ( extension , / c l a s s = (?: { ? " | { ? ' ) ( [ \w - @ : \/ ] * $ ) / ) ) ;
262+ disposables . push ( registerDefinitionProvider ( extension , / c l a s s (?: N a m e ) ? = [ " | ' ] ( [ \w - ] * $ ) / ) ) ;
263+ disposables . push ( registerHoverProvider ( extension , / c l a s s (?: N a m e ) ? = [ " | ' ] ( [ \w - ] * $ ) / ) ) ;
191264 } ) ;
192265
193266function registerEmmetProviders ( disposables : Disposable [ ] ) {
0 commit comments