@@ -45,7 +45,7 @@ export const encryptionKey = crypto.randomBytes(32)
4545export async function startLanguageServer (
4646 extensionContext : vscode . ExtensionContext ,
4747 resourcePaths : AmazonQResourcePaths
48- ) {
48+ ) : Promise < LanguageClient > {
4949 const toDispose = extensionContext . subscriptions
5050
5151 const serverModule = resourcePaths . lsp
@@ -109,87 +109,83 @@ export async function startLanguageServer(
109109 const disposable = client . start ( )
110110 toDispose . push ( disposable )
111111
112- return client . onReady ( ) . then ( async ( ) => {
113- // Request handler for when the server wants to know about the clients auth connnection. Must be registered before the initial auth init call
114- client . onRequest < ConnectionMetadata , Error > ( auth2 . notificationTypes . getConnectionMetadata . method , ( ) => {
115- return {
116- sso : {
117- startUrl : AuthUtil . instance . connection ?. startUrl ,
118- } ,
119- }
120- } )
121-
122- client . onRequest < ShowDocumentResult , Error > ( ShowDocumentRequest . method , async ( params : ShowDocumentParams ) => {
123- try {
124- return { success : await openUrl ( vscode . Uri . parse ( params . uri ) , lspName ) }
125- } catch ( err : any ) {
126- getLogger ( ) . error ( `Failed to open document for LSP: ${ lspName } , error: %s` , err )
127- return { success : false }
112+ await client . onReady ( )
113+
114+ // Request handler for when the server wants to know about the clients auth connnection. Must be registered before the initial auth init call
115+ client . onRequest < ConnectionMetadata , Error > ( auth2 . notificationTypes . getConnectionMetadata . method , ( ) => {
116+ return {
117+ sso : {
118+ startUrl : AuthUtil . instance . connection ?. startUrl ,
119+ } ,
120+ }
121+ } )
122+
123+ client . onRequest < ShowDocumentResult , Error > ( ShowDocumentRequest . method , async ( params : ShowDocumentParams ) => {
124+ try {
125+ return { success : await openUrl ( vscode . Uri . parse ( params . uri ) , lspName ) }
126+ } catch ( err : any ) {
127+ getLogger ( ) . error ( `Failed to open document for LSP: ${ lspName } , error: %s` , err )
128+ return { success : false }
129+ }
130+ } )
131+
132+ client . onRequest < MessageActionItem | null , Error > (
133+ ShowMessageRequest . method ,
134+ async ( params : ShowMessageRequestParams ) => {
135+ const actions = params . actions ?. map ( ( a ) => a . title ) ?? [ ]
136+ const response = await vscode . window . showInformationMessage ( params . message , { modal : true } , ...actions )
137+ return params . actions ?. find ( ( a ) => a . title === response ) ?? ( undefined as unknown as null )
138+ }
139+ )
140+
141+ let promise : Promise < void > | undefined
142+ let resolver : ( ) => void = ( ) => { }
143+ client . onProgress ( GetSsoTokenProgressType , GetSsoTokenProgressToken , async ( partialResult : GetSsoTokenProgress ) => {
144+ const decryptedKey = await jose . compactDecrypt ( partialResult as unknown as string , encryptionKey )
145+ const val : GetSsoTokenProgress = JSON . parse ( decryptedKey . plaintext . toString ( ) )
146+
147+ if ( val . state === 'InProgress' ) {
148+ if ( promise ) {
149+ resolver ( )
128150 }
129- } )
130-
131- client . onRequest < MessageActionItem | null , Error > (
132- ShowMessageRequest . method ,
133- async ( params : ShowMessageRequestParams ) => {
134- const actions = params . actions ?. map ( ( a ) => a . title ) ?? [ ]
135- const response = await vscode . window . showInformationMessage ( params . message , { modal : true } , ...actions )
136- return params . actions ?. find ( ( a ) => a . title === response ) ?? ( undefined as unknown as null )
151+ promise = new Promise < void > ( ( resolve ) => {
152+ resolver = resolve
153+ } )
154+ } else {
155+ resolver ( )
156+ promise = undefined
157+ return
158+ }
159+
160+ void vscode . window . withProgress (
161+ {
162+ cancellable : true ,
163+ location : vscode . ProgressLocation . Notification ,
164+ title : val . message ,
165+ } ,
166+ async ( _ ) => {
167+ await promise
137168 }
138169 )
170+ } )
139171
140- let promise : Promise < void > | undefined
141- let resolver : ( ) => void = ( ) => { }
142- client . onProgress (
143- GetSsoTokenProgressType ,
144- GetSsoTokenProgressToken ,
145- async ( partialResult : GetSsoTokenProgress ) => {
146- const decryptedKey = await jose . compactDecrypt ( partialResult as unknown as string , encryptionKey )
147- const val : GetSsoTokenProgress = JSON . parse ( decryptedKey . plaintext . toString ( ) )
148-
149- if ( val . state === 'InProgress' ) {
150- if ( promise ) {
151- resolver ( )
152- }
153- promise = new Promise < void > ( ( resolve ) => {
154- resolver = resolve
155- } )
156- } else {
157- resolver ( )
158- promise = undefined
159- return
160- }
161-
162- void vscode . window . withProgress (
163- {
164- cancellable : true ,
165- location : vscode . ProgressLocation . Notification ,
166- title : val . message ,
167- } ,
168- async ( _ ) => {
169- await promise
170- }
171- )
172- }
172+ if ( Experiments . instance . get ( 'amazonqLSPInline' , false ) ) {
173+ const inlineManager = new InlineCompletionManager ( client )
174+ inlineManager . registerInlineCompletion ( )
175+ toDispose . push (
176+ inlineManager ,
177+ Commands . register ( { id : 'aws.amazonq.invokeInlineCompletion' , autoconnect : true } , async ( ) => {
178+ await vscode . commands . executeCommand ( 'editor.action.inlineSuggest.trigger' )
179+ } ) ,
180+ vscode . workspace . onDidCloseTextDocument ( async ( ) => {
181+ await vscode . commands . executeCommand ( 'aws.amazonq.rejectCodeSuggestion' )
182+ } )
173183 )
184+ }
174185
175- if ( Experiments . instance . get ( 'amazonqLSPInline' , false ) ) {
176- const inlineManager = new InlineCompletionManager ( client )
177- inlineManager . registerInlineCompletion ( )
178- toDispose . push (
179- inlineManager ,
180- Commands . register ( { id : 'aws.amazonq.invokeInlineCompletion' , autoconnect : true } , async ( ) => {
181- await vscode . commands . executeCommand ( 'editor.action.inlineSuggest.trigger' )
182- } ) ,
183- vscode . workspace . onDidCloseTextDocument ( async ( ) => {
184- await vscode . commands . executeCommand ( 'aws.amazonq.rejectCodeSuggestion' )
185- } )
186- )
187- }
186+ if ( Experiments . instance . get ( 'amazonqChatLSP' , false ) ) {
187+ activate ( client , encryptionKey , resourcePaths . ui )
188+ }
188189
189- if ( Experiments . instance . get ( 'amazonqChatLSP' , false ) ) {
190- activate ( client , encryptionKey , resourcePaths . ui )
191- AuthUtil . create ( new auth2 . LanguageClientAuth ( client , clientId , encryptionKey ) )
192- await AuthUtil . instance . restore ( )
193- }
194- } )
190+ return client
195191}
0 commit comments