66
77import * as path from 'path' ;
88
9- import { languages , window , commands , ExtensionContext , TextDocument , ColorInformation , ColorPresentation , Color , TextEdit as CodeTextEdit } from 'vscode' ;
9+ import { languages , window , commands , ExtensionContext , TextDocument , ColorInformation , ColorPresentation , Color } from 'vscode' ;
1010import { LanguageClient , LanguageClientOptions , ServerOptions , TransportKind , TextEdit } from 'vscode-languageclient' ;
1111
12- import { ConfigurationFeature } from 'vscode-languageclient/lib/proposed' ;
13- import { DocumentColorRequest } from 'vscode-languageserver-protocol/lib/protocol.colorProvider.proposed' ;
12+ import { ConfigurationFeature } from 'vscode-languageclient/lib/configuration. proposed' ;
13+ import { DocumentColorRequest , DocumentColorParams , ColorPresentationRequest , ColorPresentationParams } from 'vscode-languageserver-protocol/lib/protocol.colorProvider.proposed' ;
1414
1515import * as nls from 'vscode-nls' ;
16- import * as convert from 'color-convert' ;
1716let localize = nls . loadMessageBundle ( ) ;
1817
1918// this method is called when vs code is activated
@@ -52,16 +51,13 @@ export function activate(context: ExtensionContext) {
5251 // client can be deactivated on extension deactivation
5352 context . subscriptions . push ( disposable ) ;
5453
55- var _toTwoDigitHex = function ( n : number ) : string {
56- const r = n . toString ( 16 ) ;
57- return r . length !== 2 ? '0' + r : r ;
58- } ;
59-
6054 client . onReady ( ) . then ( _ => {
6155 // register color provider
6256 context . subscriptions . push ( languages . registerColorProvider ( documentSelector , {
6357 provideDocumentColors ( document : TextDocument ) : Thenable < ColorInformation [ ] > {
64- let params = client . code2ProtocolConverter . asDocumentSymbolParams ( document ) ;
58+ let params : DocumentColorParams = {
59+ textDocument : client . code2ProtocolConverter . asTextDocumentIdentifier ( document )
60+ } ;
6561 return client . sendRequest ( DocumentColorRequest . type , params ) . then ( symbols => {
6662 return symbols . map ( symbol => {
6763 let range = client . protocol2CodeConverter . asRange ( symbol . range ) ;
@@ -71,34 +67,18 @@ export function activate(context: ExtensionContext) {
7167 } ) ;
7268 } ,
7369 provideColorPresentations ( document : TextDocument , colorInfo : ColorInformation ) : ColorPresentation [ ] | Thenable < ColorPresentation [ ] > {
74- let result : ColorPresentation [ ] = [ ] ;
75- let color = colorInfo . color ;
76- let label ;
77- if ( color . alpha === 1 ) {
78- label = `rgb(${ Math . round ( color . red * 255 ) } , ${ Math . round ( color . green * 255 ) } , ${ Math . round ( color . blue * 255 ) } )` ;
79- } else {
80- label = `rgba(${ Math . round ( color . red * 255 ) } , ${ Math . round ( color . green * 255 ) } , ${ Math . round ( color . blue * 255 ) } , ${ color . alpha } )` ;
81- }
82-
83- result . push ( { label : label , textEdit : new CodeTextEdit ( colorInfo . range , label ) } ) ;
84-
85- if ( color . alpha === 1 ) {
86- label = `#${ _toTwoDigitHex ( Math . round ( color . red * 255 ) ) } ${ _toTwoDigitHex ( Math . round ( color . green * 255 ) ) } ${ _toTwoDigitHex ( Math . round ( color . blue * 255 ) ) } ` ;
87- } else {
88- label = `#${ _toTwoDigitHex ( Math . round ( color . red * 255 ) ) } ${ _toTwoDigitHex ( Math . round ( color . green * 255 ) ) } ${ _toTwoDigitHex ( Math . round ( color . blue * 255 ) ) } ${ _toTwoDigitHex ( Math . round ( color . alpha * 255 ) ) } ` ;
89- }
90-
91- result . push ( { label : label , textEdit : new CodeTextEdit ( colorInfo . range , label ) } ) ;
92-
93- const hsl = convert . rgb . hsl ( Math . round ( color . red * 255 ) , Math . round ( color . green * 255 ) , Math . round ( color . blue * 255 ) ) ;
94- if ( color . alpha === 1 ) {
95- label = `hsl(${ hsl [ 0 ] } , ${ hsl [ 1 ] } %, ${ hsl [ 2 ] } %)` ;
96- } else {
97- label = `hsla(${ hsl [ 0 ] } , ${ hsl [ 1 ] } %, ${ hsl [ 2 ] } %, ${ color . alpha } )` ;
98- }
99-
100- result . push ( { label : label , textEdit : new CodeTextEdit ( colorInfo . range , label ) } ) ;
101- return result ;
70+ let params : ColorPresentationParams = {
71+ textDocument : client . code2ProtocolConverter . asTextDocumentIdentifier ( document ) ,
72+ colorInfo : { range : client . code2ProtocolConverter . asRange ( colorInfo . range ) , color : colorInfo . color }
73+ } ;
74+ return client . sendRequest ( ColorPresentationRequest . type , params ) . then ( presentations => {
75+ return presentations . map ( p => {
76+ let presentation = new ColorPresentation ( p . label ) ;
77+ presentation . textEdit = p . textEdit && client . protocol2CodeConverter . asTextEdit ( p . textEdit ) ;
78+ presentation . additionalTextEdits = p . additionalTextEdits && client . protocol2CodeConverter . asTextEdits ( p . additionalTextEdits ) ;
79+ return presentation ;
80+ } ) ;
81+ } ) ;
10282 }
10383 } ) ) ;
10484 } ) ;
0 commit comments