33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import * as fs from 'fs' ;
76import * as nls from 'vscode-nls' ;
87const localize = nls . loadMessageBundle ( ) ;
98
@@ -13,13 +12,17 @@ import {
1312 DocumentSemanticTokensProvider , DocumentRangeSemanticTokensProvider , SemanticTokens , window , commands
1413} from 'vscode' ;
1514import {
16- LanguageClient , LanguageClientOptions , ServerOptions , TransportKind , RequestType , TextDocumentPositionParams , DocumentRangeFormattingParams ,
17- DocumentRangeFormattingRequest , ProvideCompletionItemsSignature , TextDocumentIdentifier , RequestType0 , Range as LspRange
15+ LanguageClientOptions , RequestType , TextDocumentPositionParams , DocumentRangeFormattingParams ,
16+ DocumentRangeFormattingRequest , ProvideCompletionItemsSignature , TextDocumentIdentifier , RequestType0 , Range as LspRange , NotificationType , CommonLanguageClient
1817} from 'vscode-languageclient' ;
1918import { EMPTY_ELEMENTS } from './htmlEmptyTagsShared' ;
2019import { activateTagClosing } from './tagClosing' ;
21- import TelemetryReporter from 'vscode-extension-telemetry' ;
22- import { getCustomDataPathsInAllWorkspaces , getCustomDataPathsFromAllExtensions } from './customData' ;
20+ import { RequestService } from './requests' ;
21+ import { getCustomDataSource } from './customData' ;
22+
23+ namespace CustomDataChangedNotification {
24+ export const type : NotificationType < string [ ] > = new NotificationType ( 'html/customDataChanged' ) ;
25+ }
2326
2427namespace TagCloseRequest {
2528 export const type : RequestType < TextDocumentPositionParams , string , any , any > = new RequestType ( 'html/tag' ) ;
@@ -46,44 +49,33 @@ namespace SettingIds {
4649
4750}
4851
49- interface IPackageInfo {
50- name : string ;
51- version : string ;
52- aiKey : string ;
53- main : string ;
52+ export interface TelemetryReporter {
53+ sendTelemetryEvent ( eventName : string , properties ?: {
54+ [ key : string ] : string ;
55+ } , measurements ?: {
56+ [ key : string ] : number ;
57+ } ) : void ;
5458}
5559
56- let telemetryReporter : TelemetryReporter | null ;
57-
58-
59- export function activate ( context : ExtensionContext ) {
60- let toDispose = context . subscriptions ;
60+ export type LanguageClientConstructor = ( name : string , description : string , clientOptions : LanguageClientOptions ) => CommonLanguageClient ;
6161
62- let clientPackageJSON = getPackageInfo ( context ) ;
63- telemetryReporter = new TelemetryReporter ( clientPackageJSON . name , clientPackageJSON . version , clientPackageJSON . aiKey ) ;
62+ export interface Runtime {
63+ TextDecoder : { new ( encoding ?: string ) : { decode ( buffer : ArrayBuffer ) : string ; } } ;
64+ fs ?: RequestService ;
65+ telemetry ?: TelemetryReporter ;
66+ }
6467
65- const serverMain = `./server/${ clientPackageJSON . main . indexOf ( '/dist/' ) !== - 1 ? 'dist' : 'out' } /htmlServerMain` ;
66- const serverModule = context . asAbsolutePath ( serverMain ) ;
68+ export function startClient ( context : ExtensionContext , newLanguageClient : LanguageClientConstructor , runtime : Runtime ) {
6769
68- // The debug options for the server
69- let debugOptions = { execArgv : [ '--nolazy' , '--inspect=6045' ] } ;
70+ let toDispose = context . subscriptions ;
7071
71- // If the extension is launch in debug mode the debug server options are use
72- // Otherwise the run options are used
73- let serverOptions : ServerOptions = {
74- run : { module : serverModule , transport : TransportKind . ipc } ,
75- debug : { module : serverModule , transport : TransportKind . ipc , options : debugOptions }
76- } ;
7772
7873 let documentSelector = [ 'html' , 'handlebars' ] ;
7974 let embeddedLanguages = { css : true , javascript : true } ;
8075
8176 let rangeFormatting : Disposable | undefined = undefined ;
8277
83- let dataPaths = [
84- ...getCustomDataPathsInAllWorkspaces ( workspace . workspaceFolders ) ,
85- ...getCustomDataPathsFromAllExtensions ( )
86- ] ;
78+ const customDataSource = getCustomDataSource ( context . subscriptions ) ;
8779
8880 // Options to control the language client
8981 let clientOptions : LanguageClientOptions = {
@@ -93,7 +85,7 @@ export function activate(context: ExtensionContext) {
9385 } ,
9486 initializationOptions : {
9587 embeddedLanguages,
96- dataPaths ,
88+ handledSchemas : [ 'file' ] ,
9789 provideFormatter : false , // tell the server to not provide formatting capability and ignore the `html.format.enable` setting.
9890 } ,
9991 middleware : {
@@ -123,12 +115,18 @@ export function activate(context: ExtensionContext) {
123115 } ;
124116
125117 // Create the language client and start the client.
126- let client = new LanguageClient ( 'html' , localize ( 'htmlserver.name' , 'HTML Language Server' ) , serverOptions , clientOptions ) ;
118+ let client = newLanguageClient ( 'html' , localize ( 'htmlserver.name' , 'HTML Language Server' ) , clientOptions ) ;
127119 client . registerProposedFeatures ( ) ;
128120
129121 let disposable = client . start ( ) ;
130122 toDispose . push ( disposable ) ;
131123 client . onReady ( ) . then ( ( ) => {
124+
125+ client . sendNotification ( CustomDataChangedNotification . type , customDataSource . uris ) ;
126+ customDataSource . onDidChange ( ( ) => {
127+ client . sendNotification ( CustomDataChangedNotification . type , customDataSource . uris ) ;
128+ } ) ;
129+
132130 let tagRequestor = ( document : TextDocument , position : Position ) => {
133131 let param = client . code2ProtocolConverter . asTextDocumentPositionParams ( document , position ) ;
134132 return client . sendRequest ( TagCloseRequest . type , param ) ;
@@ -137,9 +135,7 @@ export function activate(context: ExtensionContext) {
137135 toDispose . push ( disposable ) ;
138136
139137 disposable = client . onTelemetry ( e => {
140- if ( telemetryReporter ) {
141- telemetryReporter . sendTelemetryEvent ( e . key , e . data ) ;
142- }
138+ runtime . telemetry ?. sendTelemetryEvent ( e . key , e . data ) ;
143139 } ) ;
144140 toDispose . push ( disposable ) ;
145141
@@ -201,7 +197,7 @@ export function activate(context: ExtensionContext) {
201197 return client . sendRequest ( DocumentRangeFormattingRequest . type , params , token ) . then (
202198 client . protocol2CodeConverter . asTextEdits ,
203199 ( error ) => {
204- client . logFailedRequest ( DocumentRangeFormattingRequest . type , error ) ;
200+ client . handleFailedRequest ( DocumentRangeFormattingRequest . type , error , [ ] ) ;
205201 return Promise . resolve ( [ ] ) ;
206202 }
207203 ) ;
@@ -319,17 +315,3 @@ export function activate(context: ExtensionContext) {
319315
320316 toDispose . push ( ) ;
321317}
322-
323- function getPackageInfo ( context : ExtensionContext ) : IPackageInfo {
324- const location = context . asAbsolutePath ( './package.json' ) ;
325- try {
326- return JSON . parse ( fs . readFileSync ( location ) . toString ( ) ) ;
327- } catch ( e ) {
328- console . log ( `Problems reading ${ location } : ${ e } ` ) ;
329- return { name : '' , version : '' , aiKey : '' , main : '' } ;
330- }
331- }
332-
333- export function deactivate ( ) : Promise < any > {
334- return telemetryReporter ? telemetryReporter . dispose ( ) : Promise . resolve ( null ) ;
335- }
0 commit comments