File tree Expand file tree Collapse file tree 6 files changed +99
-10
lines changed
css-variables-language-server/src Expand file tree Collapse file tree 6 files changed +99
-10
lines changed Original file line number Diff line number Diff line change @@ -199,10 +199,6 @@ connection.onCompletion(
199199 sortText : 'z' ,
200200 } ;
201201
202- if ( isFunctionCall ) {
203- completion . detail = varSymbol . value ;
204- }
205-
206202 items . push ( completion ) ;
207203 } ) ;
208204
@@ -318,6 +314,37 @@ connection.onDefinition((params) => {
318314 return null ;
319315} ) ;
320316
317+ // search a css variable by name or value;
318+ connection . onRequest ( 'search' , async ( params ) : Promise < CompletionItem [ ] > => {
319+ if ( ! params || typeof params !== 'string' ) {
320+ return [ ] ;
321+ }
322+ const items : CompletionItem [ ] = [ ] ;
323+ cssVariableManager . getAll ( ) . forEach ( ( variable ) => {
324+ const varSymbol = variable . symbol ;
325+ const { name, value } = varSymbol ;
326+
327+ // either of name and value can compare to params
328+ if ( ! name . includes ( params ) && ! value . startsWith ( params ) ) {
329+ return ;
330+ }
331+ const insertText = `var(${ varSymbol . name } )` ;
332+ const completion : CompletionItem = {
333+ label : name ,
334+ detail : value ,
335+ documentation : value ,
336+ insertText,
337+ kind : isColor ( value )
338+ ? CompletionItemKind . Color
339+ : CompletionItemKind . Variable ,
340+ sortText : 'z' ,
341+ } ;
342+ items . push ( completion ) ;
343+ } ) ;
344+
345+ return items ;
346+ } ) ;
347+
321348// Make the text document manager listen on the connection
322349// for open, change and close text document events
323350documents . listen ( connection ) ;
Original file line number Diff line number Diff line change 3232 },
3333 "main" : " ./dist/index.js" ,
3434 "contributes" : {
35+ "commands" : [
36+ {
37+ "command" : " vscode-css-variables.search" ,
38+ "title" : " Search for css variable"
39+ }
40+ ],
3541 "configuration" : {
3642 "title" : " CSS Variables" ,
3743 "properties" : {
141147 "publisherId" : " 411219bd-68b8-4df2-9a3b-1ac214fcfd05" ,
142148 "isPreReleaseVersion" : false
143149 }
144- }
150+ }
Original file line number Diff line number Diff line change 1+ import { commands , ExtensionContext , window } from "vscode" ;
2+ import { LanguageClient } from "vscode-languageclient/node" ;
3+ import search from "./commands/search" ;
4+
5+ export const commandsMap = {
6+ "vscode-css-variables.search" : search ,
7+ } ;
8+
9+ export default function registerCommands (
10+ context : ExtensionContext ,
11+ client : LanguageClient
12+ ) {
13+ Object . entries ( commandsMap ) . forEach ( ( [ name , command ] ) => {
14+ context . subscriptions . push (
15+ commands . registerCommand ( name , ( ) => command ( client ) )
16+ ) ;
17+ } ) ;
18+ }
Original file line number Diff line number Diff line change 1+ import { window } from "vscode" ;
2+ import { LanguageClient , CompletionItem } from "vscode-languageclient/node" ;
3+
4+ export default async function search ( client : LanguageClient ) {
5+ const editor = window . activeTextEditor ;
6+
7+ // 获取当前选择的文本
8+ const selectedText = editor ?. document . getText ( editor . selection ) ?. trim ( ) ?? "" ;
9+ // 将颜色值或简单文本作为搜索依据
10+ const defaultInput = / ^ ( # [ 0 - 9 A - F a - f ] { 3 , 6 } ) $ | ^ [ \w - ] + $ / . test ( selectedText )
11+ ? selectedText
12+ : "" ;
13+ const input = await window . showInputBox ( {
14+ value : defaultInput ,
15+ prompt : "Enter the CSS variable name or value to search" ,
16+ } ) ;
17+ if ( ! input ) {
18+ return ;
19+ }
20+
21+ // 向 language server 发送请求,获取选项数据
22+ const options : CompletionItem [ ] = await client . sendRequest ( "search" , input ) ;
23+
24+ // 如果没有查询到数据,就返回
25+ if ( options . length === 0 ) {
26+ window . showInformationMessage ( `No css variable found for '${ input } '` ) ;
27+ return ;
28+ }
29+
30+ // 弹出命令选项,将选项数据提供给用户选择
31+ const selectedOption = await window . showQuickPick ( options ) ;
32+ if ( selectedOption && editor ) {
33+ editor . edit ( ( edit ) => {
34+ edit . replace ( editor . selection , selectedOption . insertText ) ;
35+ } ) ;
36+ }
37+ }
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import {
1212 ServerOptions ,
1313 TransportKind ,
1414} from 'vscode-languageclient/node' ;
15+ import registerCommands from './commands' ;
1516
1617let client : LanguageClient ;
1718
@@ -76,6 +77,8 @@ export function activate(context: ExtensionContext) {
7677
7778 // Start the client. This will also launch the server
7879 client . start ( ) ;
80+
81+ registerCommands ( context , client ) ;
7982}
8083
8184export function deactivate ( ) : Thenable < void > | undefined {
You can’t perform that action at this time.
0 commit comments