@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License").
55You may not use this file except in compliance with the License.
66A copy of the License is located at
77
8- http://www.apache.org/licenses/LICENSE-2.0
8+ http://www.apache.org/licenses/LICENSE-2.0
99
1010or in the "license" file accompanying this file. This file is distributed
1111on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
@@ -17,12 +17,13 @@ permissions and limitations under the License.
1717import * as path from 'path' ;
1818import * as fs from 'fs' ;
1919import { workspace , ExtensionContext , ConfigurationTarget , window , WebviewPanel , Uri , commands , ViewColumn , window as VsCodeWindow } from 'vscode' ;
20- import { LanguageClient , LanguageClientOptions , ServerOptions , TransportKind } from 'vscode-languageclient' ;
20+ import { LanguageClient , LanguageClientOptions , ServerOptions , TransportKind } from 'vscode-languageclient/node ' ;
2121import { registerYamlSchemaSupport } from './yaml-support/yaml-schema' ;
2222
2323let previews : { [ index : string ] : WebviewPanel } = { } ;
24+ let languageClient : LanguageClient ;
2425
25- export function activate ( context : ExtensionContext ) {
26+ export async function activate ( context : ExtensionContext ) {
2627
2728 // The server is implemented in node
2829 let serverModule = context . asAbsolutePath ( path . join ( 'server' , 'out' , 'server.js' ) ) ;
@@ -93,45 +94,40 @@ export function activate(context: ExtensionContext) {
9394 }
9495
9596 // Create the language client and start the client.
96- let languageClient = new LanguageClient ( 'cfnLint' , 'CloudFormation linter Language Server' , serverOptions , clientOptions ) ;
97- let clientDisposable = languageClient . start ( ) ;
97+ languageClient = new LanguageClient ( 'cfnLint' , 'CloudFormation linter Language Server' , serverOptions , clientOptions ) ;
98+ await languageClient . start ( ) ;
9899
99- languageClient . onReady ( ) . then ( ( ) => {
100- languageClient . onNotification ( 'cfn/busy' , ( ) => {
101- window . showInformationMessage ( "Linter is already running. Please try again." ) ;
102- } ) ;
103- languageClient . onNotification ( 'cfn/previewIsAvailable' , ( uri ) => {
104- reloadSidePreview ( uri , languageClient ) ;
105- } ) ;
106- languageClient . onNotification ( 'cfn/isPreviewable' , ( value ) => {
107- commands . executeCommand ( 'setContext' , 'isPreviewable' , value ) ;
108- } ) ;
109- languageClient . onNotification ( 'cfn/fileclosed' , ( uri ) => {
110- // if the user closed the template itself, we close the preview
111- if ( previews [ uri ] ) {
112- previews [ uri ] . dispose ( ) ;
113- }
114- } ) ;
115-
116- let previewDisposable = commands . registerCommand ( 'extension.sidePreview' , ( ) => {
100+ languageClient . onNotification ( 'cfn/busy' , ( ) => {
101+ window . showInformationMessage ( "Linter is already running. Please try again." ) ;
102+ } ) ;
103+ languageClient . onNotification ( 'cfn/previewIsAvailable' , ( uri ) => {
104+ reloadSidePreview ( uri , languageClient ) ;
105+ } ) ;
106+ languageClient . onNotification ( 'cfn/isPreviewable' , ( value ) => {
107+ commands . executeCommand ( 'setContext' , 'isPreviewable' , value ) ;
108+ } ) ;
109+ languageClient . onNotification ( 'cfn/fileclosed' , ( uri ) => {
110+ // if the user closed the template itself, we close the preview
111+ if ( previews [ uri ] ) {
112+ previews [ uri ] . dispose ( ) ;
113+ }
114+ } ) ;
117115
118- if ( window . activeTextEditor . document ) {
119- let uri = Uri . file ( window . activeTextEditor . document . fileName ) . toString ( ) ;
116+ let previewDisposable = commands . registerCommand ( 'extension.sidePreview' , ( ) => {
120117
121- languageClient . sendNotification ( 'cfn/requestPreview' , uri ) ;
122- }
118+ if ( window . activeTextEditor . document ) {
119+ let uri = Uri . file ( window . activeTextEditor . document . fileName ) . toString ( ) ;
123120
124- } ) ;
121+ languageClient . sendNotification ( 'cfn/requestPreview' , uri ) ;
122+ }
125123
126- context . subscriptions . push ( previewDisposable ) ;
127124 } ) ;
128125
129- // Push the disposable to the context's subscriptions so that the
130- // client can be deactivated on extension deactivation
131- context . subscriptions . push ( clientDisposable ) ;
126+ context . subscriptions . push ( previewDisposable ) ;
127+
132128}
133129
134- function reloadSidePreview ( file :string , languageClient :LanguageClient ) {
130+ function reloadSidePreview ( file : string , languageClient : LanguageClient ) {
135131 let uri = Uri . parse ( file ) ;
136132 let stringifiedUri = uri . toString ( ) ;
137133 let dotFile = uri . fsPath + ".dot" ;
@@ -145,7 +141,7 @@ function reloadSidePreview(file:string, languageClient:LanguageClient) {
145141 if ( ! previews [ stringifiedUri ] ) {
146142 previews [ stringifiedUri ] = VsCodeWindow . createWebviewPanel (
147143 'cfnLintPreview' , // Identifies the type of the webview. Used internally
148- 'Template: ' + dotFile . slice ( 0 , - 4 ) , // Title of the panel displayed to the user
144+ 'Template: ' + dotFile . slice ( 0 , - 4 ) , // Title of the panel displayed to the user
149145 ViewColumn . Two , // Editor column to show the new webview panel in.
150146 {
151147 enableScripts : true ,
@@ -163,7 +159,7 @@ function reloadSidePreview(file:string, languageClient:LanguageClient) {
163159 panel . webview . html = getPreviewContent ( content ) ;
164160}
165161
166- function getPreviewContent ( content : String ) : string {
162+ function getPreviewContent ( content : String ) : string {
167163
168164 let multilineString = "`" + content + "`" ;
169165 // FIXME is there a better way of converting from dot to svg that is not using cdn urls?
@@ -207,4 +203,11 @@ export async function yamlLangaugeServerValidation(): Promise<void> {
207203 workspace . getConfiguration ( ) . update ( 'yaml.validate' , false , ConfigurationTarget . Global ) ;
208204 }
209205 }
206+ }
207+
208+ export function deactivate ( ) : Thenable < void > | undefined {
209+ if ( ! languageClient ) {
210+ return undefined ;
211+ }
212+ return languageClient . stop ( ) ;
210213}
0 commit comments