@@ -145,6 +145,23 @@ pub(crate) fn text_edit(line_index: &LineIndex, indel: Indel) -> lsp_types::Text
145145 lsp_types:: TextEdit { range, new_text }
146146}
147147
148+ pub ( crate ) fn completion_text_edit (
149+ line_index : & LineIndex ,
150+ insert_replace_support : Option < lsp_types:: Position > ,
151+ indel : Indel ,
152+ ) -> lsp_types:: CompletionTextEdit {
153+ let text_edit = text_edit ( line_index, indel) ;
154+ match insert_replace_support {
155+ Some ( cursor_pos) => lsp_types:: InsertReplaceEdit {
156+ new_text : text_edit. new_text ,
157+ insert : lsp_types:: Range { start : text_edit. range . start , end : cursor_pos } ,
158+ replace : text_edit. range ,
159+ }
160+ . into ( ) ,
161+ None => text_edit. into ( ) ,
162+ }
163+ }
164+
148165pub ( crate ) fn snippet_text_edit (
149166 line_index : & LineIndex ,
150167 is_snippet : bool ,
@@ -179,6 +196,7 @@ pub(crate) fn snippet_text_edit_vec(
179196}
180197
181198pub ( crate ) fn completion_item (
199+ insert_replace_support : Option < lsp_types:: Position > ,
182200 line_index : & LineIndex ,
183201 item : CompletionItem ,
184202) -> Vec < lsp_types:: CompletionItem > {
@@ -190,15 +208,15 @@ pub(crate) fn completion_item(
190208 for indel in item. text_edit ( ) . iter ( ) {
191209 if indel. delete . contains_range ( source_range) {
192210 text_edit = Some ( if indel. delete == source_range {
193- self :: text_edit ( line_index, indel. clone ( ) )
211+ self :: completion_text_edit ( line_index, insert_replace_support , indel. clone ( ) )
194212 } else {
195213 assert ! ( source_range. end( ) == indel. delete. end( ) ) ;
196214 let range1 = TextRange :: new ( indel. delete . start ( ) , source_range. start ( ) ) ;
197215 let range2 = source_range;
198216 let indel1 = Indel :: replace ( range1, String :: new ( ) ) ;
199217 let indel2 = Indel :: replace ( range2, indel. insert . clone ( ) ) ;
200218 additional_text_edits. push ( self :: text_edit ( line_index, indel1) ) ;
201- self :: text_edit ( line_index, indel2)
219+ self :: completion_text_edit ( line_index, insert_replace_support , indel2)
202220 } )
203221 } else {
204222 assert ! ( source_range. intersect( indel. delete) . is_none( ) ) ;
@@ -213,7 +231,7 @@ pub(crate) fn completion_item(
213231 detail : item. detail ( ) . map ( |it| it. to_string ( ) ) ,
214232 filter_text : Some ( item. lookup ( ) . to_string ( ) ) ,
215233 kind : item. kind ( ) . map ( completion_item_kind) ,
216- text_edit : Some ( text_edit. into ( ) ) ,
234+ text_edit : Some ( text_edit) ,
217235 additional_text_edits : Some ( additional_text_edits) ,
218236 documentation : item. documentation ( ) . map ( documentation) ,
219237 deprecated : Some ( item. deprecated ( ) ) ,
@@ -1135,7 +1153,7 @@ mod tests {
11351153 . unwrap ( )
11361154 . into_iter ( )
11371155 . filter ( |c| c. label ( ) . ends_with ( "arg" ) )
1138- . map ( |c| completion_item ( & line_index, c) )
1156+ . map ( |c| completion_item ( None , & line_index, c) )
11391157 . flat_map ( |comps| comps. into_iter ( ) . map ( |c| ( c. label , c. sort_text ) ) )
11401158 . collect ( ) ;
11411159 expect_test:: expect![ [ r#"
0 commit comments