@@ -108,9 +108,7 @@ export function activateColorDecorations(decoratorProvider: (uri: string) => The
108108 let decorations = [ ] ;
109109 for ( let i = 0 ; i < ranges . length && decorations . length < MAX_DECORATORS ; i ++ ) {
110110 let range = ranges [ i ] ;
111- let text = document . getText ( range ) ;
112- let value = < string > JSON . parse ( text ) ;
113- let c = Color . fromHex ( value ) ;
111+ let c = parseColorFromRange ( document , range ) ;
114112 if ( c ) {
115113 decorations . push ( < DecorationOptions > {
116114 range : range ,
@@ -144,9 +142,7 @@ export class ColorProvider implements DocumentColorProvider {
144142 const ranges = await this . decoratorProvider ( document . uri . toString ( ) ) ;
145143 const result = [ ] ;
146144 for ( let range of ranges ) {
147- let text = document . getText ( range ) ;
148- let value = < string > JSON . parse ( text ) ;
149- let color = Color . fromHex ( value ) ;
145+ let color = parseColorFromRange ( document , range ) ;
150146 if ( color ) {
151147 let r = new Range ( range . start . line , range . start . character + 1 , range . end . line , range . end . character - 1 ) ;
152148 result . push ( new ColorRange ( r , color , [ ColorFormat_HEX ] ) ) ;
@@ -155,3 +151,16 @@ export class ColorProvider implements DocumentColorProvider {
155151 return result ;
156152 }
157153}
154+
155+ function parseColorFromRange ( document : TextDocument , range : Range ) {
156+ let text = document . getText ( range ) ;
157+ try {
158+ let value = < string > JSON . parse ( text ) ;
159+ if ( typeof value === 'string' ) {
160+ return Color . fromHex ( value ) ;
161+ }
162+ } catch ( e ) {
163+ // ignore JSON parse error
164+ }
165+ return null ;
166+ }
0 commit comments