@@ -8,6 +8,7 @@ import * as path from 'path';
88import * as vscode from 'vscode' ;
99import { Middleware } from 'vscode-languageclient' ;
1010import * as util from '../common' ;
11+ import { logAndReturn } from '../Utility/Async/returns' ;
1112import { Client } from './client' ;
1213import { clients } from './extension' ;
1314import { shouldChangeFromCToCpp } from './utils' ;
@@ -18,14 +19,8 @@ export const ServerCancelled: number = -32802;
1819let anyFileOpened : boolean = false ;
1920
2021export function createProtocolFilter ( ) : Middleware {
21- // Disabling lint for invoke handlers
22- const invoke1 = ( a : any , next : ( a : any ) => any ) : any => clients . ActiveClient . enqueue ( ( ) => next ( a ) ) ;
23- const invoke2 = ( a : any , b : any , next : ( a : any , b : any ) => any ) : any => clients . ActiveClient . enqueue ( ( ) => next ( a , b ) ) ;
24- const invoke3 = ( a : any , b : any , c : any , next : ( a : any , b : any , c : any ) => any ) : any => clients . ActiveClient . enqueue ( ( ) => next ( a , b , c ) ) ;
25- const invoke4 = ( a : any , b : any , c : any , d : any , next : ( a : any , b : any , c : any , d : any ) => any ) : any => clients . ActiveClient . enqueue ( ( ) => next ( a , b , c , d ) ) ;
26-
2722 return {
28- didOpen : async ( document , sendMessage ) => clients . ActiveClient . enqueue ( async ( ) => {
23+ didOpen : async ( document , sendMessage ) => {
2924 if ( ! util . isCpp ( document ) ) {
3025 return ;
3126 }
@@ -41,62 +36,41 @@ export function createProtocolFilter(): Middleware {
4136 const mappingString : string = baseFileName + "@" + document . fileName ;
4237 client . addFileAssociations ( mappingString , "cpp" ) ;
4338 client . sendDidChangeSettings ( ) ;
44- document = await vscode . languages . setTextDocumentLanguage ( document , "cpp" ) ;
39+ // This will cause the file to be closed and reopened.
40+ void vscode . languages . setTextDocumentLanguage ( document , "cpp" ) ;
41+ return ;
4542 }
4643 // client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set.
4744 client . onDidOpenTextDocument ( document ) ;
4845 client . takeOwnership ( document ) ;
49- await sendMessage ( document ) ;
50-
51- // For a file already open when we activate, sometimes we don't get any notifications about visible
52- // or active text editors, visible ranges, or text selection. As a workaround, we trigger
53- // onDidChangeVisibleTextEditors here, only for the first file opened.
54- if ( ! anyFileOpened ) {
55- anyFileOpened = true ;
56- const cppEditors : vscode . TextEditor [ ] = vscode . window . visibleTextEditors . filter ( e => util . isCpp ( e . document ) ) ;
57- await client . onDidChangeVisibleTextEditors ( cppEditors ) ;
58- }
46+ void sendMessage ( document ) . then ( ( ) => {
47+ // For a file already open when we activate, sometimes we don't get any notifications about visible
48+ // or active text editors, visible ranges, or text selection. As a workaround, we trigger
49+ // onDidChangeVisibleTextEditors here, only for the first file opened.
50+ if ( ! anyFileOpened ) {
51+ anyFileOpened = true ;
52+ const cppEditors : vscode . TextEditor [ ] = vscode . window . visibleTextEditors . filter ( e => util . isCpp ( e . document ) ) ;
53+ client . onDidChangeVisibleTextEditors ( cppEditors ) . catch ( logAndReturn . undefined ) ;
54+ }
55+ } ) ;
5956 }
6057 }
61- } ) ,
62- didChange : invoke1 ,
63- willSave : invoke1 ,
58+ } ,
6459 willSaveWaitUntil : async ( event , sendMessage ) => {
65- // await clients.ActiveClient.ready;
66- // Don't use awaitUntilLanguageClientReady.
67- // Otherwise, the message can be delayed too long.
6860 const me : Client = clients . getClientFor ( event . document . uri ) ;
6961 if ( me . TrackedDocuments . has ( event . document . uri . toString ( ) ) ) {
7062 return sendMessage ( event ) ;
7163 }
7264 return [ ] ;
7365 } ,
74- didSave : invoke1 ,
75- didClose : async ( document , sendMessage ) => clients . ActiveClient . enqueue ( async ( ) => {
66+ didClose : async ( document , sendMessage ) => {
7667 const me : Client = clients . getClientFor ( document . uri ) ;
7768 const uriString : string = document . uri . toString ( ) ;
7869 if ( me . TrackedDocuments . has ( uriString ) ) {
7970 me . onDidCloseTextDocument ( document ) ;
8071 me . TrackedDocuments . delete ( uriString ) ;
81- await sendMessage ( document ) ;
82- }
83- } ) ,
84- provideCompletionItem : invoke4 ,
85- resolveCompletionItem : invoke2 ,
86- provideHover : async ( document , position , token , next : ( document : any , position : any , token : any ) => any ) => clients . ActiveClient . enqueue ( async ( ) => {
87- const me : Client = clients . getClientFor ( document . uri ) ;
88- if ( me . TrackedDocuments . has ( document . uri . toString ( ) ) ) {
89- return next ( document , position , token ) ;
72+ void sendMessage ( document ) ;
9073 }
91- return null ;
92- } ) ,
93- provideSignatureHelp : invoke4 ,
94- provideDefinition : invoke3 ,
95- provideReferences : invoke4 ,
96- provideDocumentHighlights : invoke3 ,
97- provideDeclaration : invoke3 ,
98- workspace : {
99- didChangeConfiguration : invoke1
10074 }
10175 } ;
10276}
0 commit comments