1
- import { Executable , LanguageClient , LanguageClientOptions , ServerOptions , Uri , workspace } from 'coc.nvim' ;
1
+ import { Executable , LanguageClient , LanguageClientOptions , ServerOptions , StaticFeature , Uri , workspace } from 'coc.nvim' ;
2
+ import { ClientCapabilities , CodeAction , CodeActionParams , CodeActionRequest , Command , InsertTextFormat , TextDocumentEdit } from 'vscode-languageserver-protocol' ;
3
+
4
+ class SnippetTextEditFeature implements StaticFeature {
5
+ fillClientCapabilities ( capabilities : ClientCapabilities ) : void {
6
+ const caps : any = capabilities . experimental ?? { } ;
7
+ caps . snippetTextEdit = true ;
8
+ capabilities . experimental = caps ;
9
+ }
10
+ initialize ( ) : void { }
11
+ }
12
+
13
+ function isSnippetEdit ( action : CodeAction ) : boolean {
14
+ const documentChanges = action . edit ?. documentChanges ?? [ ] ;
15
+ for ( const edit of documentChanges ) {
16
+ if ( TextDocumentEdit . is ( edit ) ) {
17
+ if ( edit . edits . some ( ( indel ) => ( indel as any ) . insertTextFormat === InsertTextFormat . Snippet ) ) {
18
+ return true ;
19
+ }
20
+ }
21
+ }
22
+ return false ;
23
+ }
2
24
3
25
export function createClient ( bin : string ) : LanguageClient {
4
26
let folder = '.' ;
@@ -24,6 +46,26 @@ export function createClient(bin: string): LanguageClient {
24
46
position . character = character - 1 ;
25
47
return help ;
26
48
} ,
49
+ provideCodeActions ( document , range , context , token ) {
50
+ const params : CodeActionParams = {
51
+ textDocument : { uri : document . uri } ,
52
+ range,
53
+ context,
54
+ } ;
55
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
56
+ return client . sendRequest ( CodeActionRequest . type , params , token ) . then ( ( values ) => {
57
+ if ( values === null ) return undefined ;
58
+ const result : ( CodeAction | Command ) [ ] = [ ] ;
59
+ for ( const item of values ) {
60
+ if ( CodeAction . is ( item ) && isSnippetEdit ( item ) ) {
61
+ item . command = Command . create ( '' , 'rust-analyzer.applySnippetWorkspaceEdit' , item . edit ) ;
62
+ item . edit = undefined ;
63
+ }
64
+ result . push ( item ) ;
65
+ }
66
+ return result ;
67
+ } ) ;
68
+ } ,
27
69
} ,
28
70
outputChannel,
29
71
} ;
@@ -51,5 +93,7 @@ export function createClient(bin: string): LanguageClient {
51
93
} ,
52
94
} ;
53
95
client . registerProposedFeatures ( ) ;
96
+ client . registerFeature ( new SnippetTextEditFeature ( ) ) ;
97
+
54
98
return client ;
55
99
}
0 commit comments