@@ -12,10 +12,11 @@ import {
12
12
isValue ,
13
13
} from '../utils/stylus'
14
14
15
- // Examples matched: "color #fff", "color: #fff;", "color:#fff", " background #aaa"
16
- // Examples not matched: "#fff" at line start, "#id", selectors like "a #abc", "#bbb #ccc" (second #)
15
+ // Match Stylus property values containing hex colors
16
+ // Examples matched: "color #fff", "color: #fff", "background #aaa", "border-color:#333"
17
+ // Examples not matched: "#fff" at line start, selectors like "a #abc", "#bbb #ccc"
17
18
const stylusColorRegex =
18
- / (?< ! ^ ) (?< ! ^ \s * ) (?< ! # [ 0 - 9 a - f A - F ] { 0 , 6 } \s ) (?< = : | ) # ( [ 0 - 9 a - f A - F ] { 3 } | [ 0 - 9 a - f A - F ] { 6 } ) (? ! [ 0 - 9 a - f A - F ] ) / gm
19
+ / ^ ( \s * ) ( [ a - z A - Z ] [ \w - ] * ) \s * : ? \s + ( . * ? # ( [ 0 - 9 a - f A - F ] { 3 } | [ 0 - 9 a - f A - F ] { 6 } ) (? ! [ 0 - 9 a - f A - F ] ) . * ? ) $ / gm
19
20
20
21
export function create ( ) : LanguageServicePlugin {
21
22
const cssBuiltinData = CSS . getDefaultCSSDataProvider ( )
@@ -176,12 +177,23 @@ function parseStylusColors(document: TextDocument): CSS.ColorInformation[] {
176
177
const text = document . getText ( )
177
178
let match : RegExpExecArray | null
178
179
while ( ( match = stylusColorRegex . exec ( text ) ) ) {
179
- const start = document . positionAt ( match . index )
180
- const end = document . positionAt ( match . index + match [ 0 ] . length )
181
- const hex = match [ 1 ]
182
- let r = 0 ,
183
- g = 0 ,
184
- b = 0
180
+ const fullMatch = match [ 0 ]
181
+ const valueWithColor = match [ 3 ] // The part containing the hex color
182
+ const hex = match [ 4 ] // The captured hex digits
183
+
184
+ // Find the position of the # in the value part
185
+ const colorIndex = valueWithColor . indexOf ( '#' )
186
+ if ( colorIndex === - 1 ) continue
187
+
188
+ // Calculate the absolute position in the document
189
+ const lineStartIndex = match . index
190
+ const colorStartIndex =
191
+ lineStartIndex + fullMatch . indexOf ( valueWithColor ) + colorIndex
192
+ const colorEndIndex = colorStartIndex + 1 + hex . length // +1 for the #
193
+ const start = document . positionAt ( colorStartIndex )
194
+ const end = document . positionAt ( colorEndIndex )
195
+ let [ r , g , b ] = [ 0 , 0 , 0 ]
196
+
185
197
if ( hex . length === 3 ) {
186
198
r = parseInt ( hex [ 0 ] + hex [ 0 ] , 16 )
187
199
g = parseInt ( hex [ 1 ] + hex [ 1 ] , 16 )
0 commit comments