1
- use crate :: { adapters:: get_cursor_position, session:: Session } ;
1
+ use crate :: {
2
+ adapters:: { self , get_cursor_position} ,
3
+ diagnostics:: LspError ,
4
+ session:: Session ,
5
+ } ;
2
6
use anyhow:: Result ;
3
7
use pgt_workspace:: { WorkspaceError , features:: completions:: GetCompletionsParams } ;
4
- use tower_lsp:: lsp_types:: { self , CompletionItem , CompletionItemLabelDetails } ;
8
+ use tower_lsp:: lsp_types:: { self , CompletionItem , CompletionItemLabelDetails , TextEdit } ;
5
9
6
10
#[ tracing:: instrument( level = "debug" , skip( session) , err) ]
7
11
pub fn get_completions (
8
12
session : & Session ,
9
13
params : lsp_types:: CompletionParams ,
10
- ) -> Result < lsp_types:: CompletionResponse > {
14
+ ) -> Result < lsp_types:: CompletionResponse , LspError > {
11
15
let url = params. text_document_position . text_document . uri ;
12
16
let path = session. file_path ( & url) ?;
13
17
18
+ let doc = session. document ( & url) ?;
19
+ let encoding = adapters:: negotiated_encoding ( session. client_capabilities ( ) . unwrap ( ) ) ;
20
+
14
21
let completion_result = match session. workspace . get_completions ( GetCompletionsParams {
15
22
path,
16
23
position : get_cursor_position ( session, & url, params. text_document_position . position ) ?,
@@ -36,6 +43,12 @@ pub fn get_completions(
36
43
} ) ,
37
44
preselect : Some ( i. preselected ) ,
38
45
sort_text : Some ( i. sort_text ) ,
46
+ text_edit : i. completion_text . map ( |c| {
47
+ lsp_types:: CompletionTextEdit :: Edit ( TextEdit {
48
+ new_text : c. text ,
49
+ range : adapters:: to_lsp:: range ( & doc. line_index , c. range , encoding) . unwrap ( ) ,
50
+ } )
51
+ } ) ,
39
52
kind : Some ( to_lsp_types_completion_item_kind ( i. kind ) ) ,
40
53
..CompletionItem :: default ( )
41
54
} )
0 commit comments