@@ -14,10 +14,11 @@ import postcss, {type Root as ASTRoot} from 'postcss'
1414// import {properties, stories} from '@primer/primitives/dist/js/intellisense'
1515import camelCase from 'lodash.camelcase'
1616import { isColor } from './utils/is-color'
17- import { getSuggestions } from './suggestions'
17+ import { getSuggestions , getSuggestionsLikeVariable , type SuggestionWithSortText } from './suggestions'
1818import { getCssVariable } from './utils/get-css-variable'
1919import { getVariableInfo } from './utils/get-variable-info'
2020import { getDocumentation } from './documentation'
21+ import { getCurrentWord } from './utils/get-current-word'
2122
2223// Create a connection for the server, using Node's IPC as a transport.
2324// Also include all preview / proposed LSP features.
@@ -67,12 +68,24 @@ connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] =
6768 let property : string = ast . nodes [ 0 ] . type === 'decl' ? camelCase ( ast . nodes [ 0 ] . prop ) : undefined
6869 if ( ! property ) return [ ]
6970
71+ const value : string =
72+ ast . nodes [ 0 ] . type === 'decl' ? ast . nodes [ 0 ] . value . replace ( ');' , '' ) . replace ( '\n' , '' ) . trim ( ) : undefined
73+
74+ const suggestedVariablesWithSortText : SuggestionWithSortText [ ] = [ ]
75+
76+ // if the cursor is at the css variable, get getSuggestionsLikeVariable instead of getSuggestions(property)
77+ const offset = doc . offsetAt ( params . position )
78+ const currentWord = getCurrentWord ( doc , offset )
79+ if ( currentWord . includes ( '--' ) && currentWord . length > 2 /* checking it's not just -- */ ) {
80+ const variableSuggestions = getSuggestionsLikeVariable ( currentWord . replace ( '--' , '' ) )
81+ suggestedVariablesWithSortText . push ( ...variableSuggestions )
82+ }
83+
7084 // TODO: for shorthands, property might be the second property like borderColor or paddingInline
7185 // we can be smarter about this
7286 if ( property === 'padding' ) {
7387 try {
7488 // padding: block inline
75- const value = currentLine . split ( ':' ) [ 1 ] . trim ( )
7689 const [ blockValue ] = value . split ( ' ' )
7790 const blockValuePositionEnd = currentLine . indexOf ( blockValue ) + blockValue . length
7891
@@ -87,7 +100,6 @@ connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] =
87100 } else if ( property === 'border' ) {
88101 try {
89102 // border: width style color
90- const value = currentLine . split ( ':' ) [ 1 ] . trim ( )
91103 const [ borderWidth ] = value . split ( ' ' )
92104 const borderWidthPositionEnd = currentLine . indexOf ( borderWidth ) + borderWidth . length
93105
@@ -101,7 +113,7 @@ connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] =
101113 }
102114 }
103115
104- const suggestedVariablesWithSortText = getSuggestions ( property )
116+ suggestedVariablesWithSortText . push ( ... getSuggestions ( property ) )
105117
106118 const items = suggestedVariablesWithSortText . map ( variable => {
107119 const documentation = getDocumentation ( variable . name )
@@ -114,8 +126,8 @@ connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] =
114126 typeof variable . value === 'string' && isColor ( variable . value )
115127 ? CompletionItemKind . Color
116128 : variable . kind === 'functional'
117- ? CompletionItemKind . Field
118- : CompletionItemKind . Constructor ,
129+ ? CompletionItemKind . Field
130+ : CompletionItemKind . Constructor ,
119131 sortText : variable . sortText ,
120132 // this is slightly silly because what about multiple variables in one line
121133 // like shorthands or fallbacks
0 commit comments