@@ -8,6 +8,7 @@ import * as path from 'path';
8
8
import * as vscode from 'vscode' ;
9
9
import { Middleware } from 'vscode-languageclient' ;
10
10
import * as util from '../common' ;
11
+ import { logAndReturn } from '../Utility/Async/returns' ;
11
12
import { Client } from './client' ;
12
13
import { clients } from './extension' ;
13
14
import { shouldChangeFromCToCpp } from './utils' ;
@@ -18,14 +19,8 @@ export const ServerCancelled: number = -32802;
18
19
let anyFileOpened : boolean = false ;
19
20
20
21
export 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
-
27
22
return {
28
- didOpen : async ( document , sendMessage ) => clients . ActiveClient . enqueue ( async ( ) => {
23
+ didOpen : async ( document , sendMessage ) => {
29
24
if ( ! util . isCpp ( document ) ) {
30
25
return ;
31
26
}
@@ -41,62 +36,41 @@ export function createProtocolFilter(): Middleware {
41
36
const mappingString : string = baseFileName + "@" + document . fileName ;
42
37
client . addFileAssociations ( mappingString , "cpp" ) ;
43
38
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 ;
45
42
}
46
43
// client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set.
47
44
client . onDidOpenTextDocument ( document ) ;
48
45
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
+ } ) ;
59
56
}
60
57
}
61
- } ) ,
62
- didChange : invoke1 ,
63
- willSave : invoke1 ,
58
+ } ,
64
59
willSaveWaitUntil : async ( event , sendMessage ) => {
65
- // await clients.ActiveClient.ready;
66
- // Don't use awaitUntilLanguageClientReady.
67
- // Otherwise, the message can be delayed too long.
68
60
const me : Client = clients . getClientFor ( event . document . uri ) ;
69
61
if ( me . TrackedDocuments . has ( event . document . uri . toString ( ) ) ) {
70
62
return sendMessage ( event ) ;
71
63
}
72
64
return [ ] ;
73
65
} ,
74
- didSave : invoke1 ,
75
- didClose : async ( document , sendMessage ) => clients . ActiveClient . enqueue ( async ( ) => {
66
+ didClose : async ( document , sendMessage ) => {
76
67
const me : Client = clients . getClientFor ( document . uri ) ;
77
68
const uriString : string = document . uri . toString ( ) ;
78
69
if ( me . TrackedDocuments . has ( uriString ) ) {
79
70
me . onDidCloseTextDocument ( document ) ;
80
71
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 ) ;
90
73
}
91
- return null ;
92
- } ) ,
93
- provideSignatureHelp : invoke4 ,
94
- provideDefinition : invoke3 ,
95
- provideReferences : invoke4 ,
96
- provideDocumentHighlights : invoke3 ,
97
- provideDeclaration : invoke3 ,
98
- workspace : {
99
- didChangeConfiguration : invoke1
100
74
}
101
75
} ;
102
76
}
0 commit comments